RuleContainer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/RTTI/ReflectContext.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  12. #include <SceneAPI/SceneCore/Containers/RuleContainer.h>
  13. namespace AZ
  14. {
  15. namespace SceneAPI
  16. {
  17. namespace Containers
  18. {
  19. size_t RuleContainer::GetRuleCount() const
  20. {
  21. return m_rules.size();
  22. }
  23. AZStd::shared_ptr<DataTypes::IRule> RuleContainer::GetRule(size_t index) const
  24. {
  25. AZ_Assert(index < m_rules.size(), "Cannot get rule. Index %i is out of range.", index);
  26. if (index >= m_rules.size())
  27. {
  28. return nullptr;
  29. }
  30. return m_rules[index];
  31. }
  32. void RuleContainer::AddRule(const AZStd::shared_ptr<DataTypes::IRule>& rule)
  33. {
  34. AZ_Assert(AZStd::find(m_rules.begin(), m_rules.end(), rule) == m_rules.end(), "Unable to add rule as it's already been added.");
  35. m_rules.push_back(rule);
  36. }
  37. void RuleContainer::AddRule(AZStd::shared_ptr<DataTypes::IRule>&& rule)
  38. {
  39. AZ_Assert(AZStd::find(m_rules.begin(), m_rules.end(), rule) == m_rules.end(), "Unable to add rule as it's already been added.");
  40. m_rules.push_back(rule);
  41. }
  42. void RuleContainer::InsertRule(const AZStd::shared_ptr<DataTypes::IRule>& rule, size_t position)
  43. {
  44. AZ_Assert(AZStd::find(m_rules.begin(), m_rules.end(), rule) == m_rules.end(), "Unable to insert rule as it has already been added.");
  45. m_rules.insert(m_rules.begin() + position, rule);
  46. }
  47. void RuleContainer::RemoveRule(size_t index)
  48. {
  49. if (index < m_rules.size())
  50. {
  51. m_rules.erase(m_rules.begin() + index);
  52. }
  53. }
  54. void RuleContainer::RemoveRule(const AZStd::shared_ptr<DataTypes::IRule>& rule)
  55. {
  56. auto it = AZStd::find(m_rules.begin(), m_rules.end(), rule);
  57. if (it != m_rules.end())
  58. {
  59. m_rules.erase(it);
  60. }
  61. }
  62. void RuleContainer::Reflect(ReflectContext* context)
  63. {
  64. SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context);
  65. if (!serializeContext)
  66. {
  67. return;
  68. }
  69. serializeContext->Class<RuleContainer>()
  70. ->Version(1)
  71. ->Field("rules", &RuleContainer::m_rules);
  72. EditContext* editContext = serializeContext->GetEditContext();
  73. if (editContext)
  74. {
  75. editContext->Class<RuleContainer>("Rule Container", "Description.")
  76. ->DataElement(AZ_CRC_CE("ManifestVector"), &RuleContainer::m_rules, "", "Add or remove entries to fine-tune source file processing.")
  77. ->Attribute(AZ::Edit::Attributes::ContainerCanBeModified, false)
  78. ->Attribute(AZ_CRC_CE("CollectionName"), "Modifiers")
  79. ->Attribute(AZ_CRC_CE("ObjectTypeName"), "Modifier")
  80. ->ElementAttribute(AZ::Edit::Attributes::Visibility, AZ_CRC_CE("PropertyVisibility_Hide"));
  81. }
  82. }
  83. // Previously, groups stored the vector of shared pointers of rules. We moved the vector of shared pointers of rules to the RuleContainer and
  84. // groups now have a RuleContainer as a member. This version converter converts from groups holding the vector
  85. bool RuleContainer::VectorToRuleContainerConverter(SerializeContext& context, SerializeContext::DataElementNode& classElement)
  86. {
  87. int elementIndex = classElement.FindElement(AZ_CRC_CE("rules"));
  88. if (elementIndex >= 0)
  89. {
  90. AZ::SerializeContext::DataElementNode& rulesElement = classElement.GetSubElement(elementIndex);
  91. // Clone the rule elements.
  92. AZStd::vector<AZ::SerializeContext::DataElementNode> rules;
  93. const int numSubElements = rulesElement.GetNumSubElements();
  94. rules.reserve(numSubElements);
  95. for (int i = 0; i < numSubElements; i++)
  96. {
  97. AZ::SerializeContext::DataElementNode& sharedPtrElement = rulesElement.GetSubElement(i);
  98. if (sharedPtrElement.GetNumSubElements() > 0)
  99. {
  100. AZ::SerializeContext::DataElementNode& ruleElement = sharedPtrElement.GetSubElement(0);
  101. rules.push_back(ruleElement);
  102. }
  103. }
  104. // Remove the original rule vector element.
  105. classElement.RemoveElement(elementIndex);
  106. // Add a new rule container element.
  107. const int ruleContainerIndex = classElement.AddElement<RuleContainer>(context, "rules");
  108. if (ruleContainerIndex >= 0)
  109. {
  110. AZ::SerializeContext::DataElementNode& ruleContainerElement = classElement.GetSubElement(ruleContainerIndex);
  111. // Create a rule vector element.
  112. const int rulesVectorIndex = ruleContainerElement.AddElement<AZStd::vector<AZStd::shared_ptr<DataTypes::IRule>>>(context, "rules");
  113. AZ::SerializeContext::DataElementNode& ruleVectorElement = ruleContainerElement.GetSubElement(rulesVectorIndex);
  114. // Add the copied rules to the rule vector element.
  115. for (SerializeContext::DataElementNode& rule : rules)
  116. {
  117. int valueIndex = ruleVectorElement.AddElement<AZStd::shared_ptr<DataTypes::IRule>>(context, "element");
  118. SerializeContext::DataElementNode& pointerNode = ruleVectorElement.GetSubElement(valueIndex);
  119. pointerNode.AddElement(rule);
  120. }
  121. }
  122. }
  123. return true;
  124. }
  125. } // Containers
  126. } // SceneAPI
  127. } // AZ