PrefabGroup.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <PrefabGroup/PrefabGroup.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <AzCore/JSON/error/error.h>
  13. #include <AzCore/JSON/error/en.h>
  14. #include <AzCore/Serialization/Json/JsonUtils.h>
  15. #include <AzCore/std/smart_ptr/make_shared.h>
  16. #include <AzCore/std/optional.h>
  17. #include <AzFramework/FileFunc/FileFunc.h>
  18. #include <SceneAPI/SceneCore/Containers/SceneManifest.h>
  19. namespace AZ::SceneAPI::SceneData
  20. {
  21. // PrefabGroup
  22. PrefabGroup::PrefabGroup()
  23. : m_id(Uuid::CreateNull())
  24. , m_name()
  25. {
  26. }
  27. const AZStd::string& PrefabGroup::GetName() const
  28. {
  29. return m_name;
  30. }
  31. void PrefabGroup::SetName(AZStd::string name)
  32. {
  33. m_name = AZStd::move(name);
  34. }
  35. const Uuid& PrefabGroup::GetId() const
  36. {
  37. return m_id;
  38. }
  39. void PrefabGroup::SetId(Uuid id)
  40. {
  41. m_id = AZStd::move(id);
  42. }
  43. Containers::RuleContainer& PrefabGroup::GetRuleContainer()
  44. {
  45. return m_rules;
  46. }
  47. const Containers::RuleContainer& PrefabGroup::GetRuleContainerConst() const
  48. {
  49. return m_rules;
  50. }
  51. DataTypes::ISceneNodeSelectionList& PrefabGroup::GetSceneNodeSelectionList()
  52. {
  53. return m_nodeSelectionList;
  54. }
  55. const DataTypes::ISceneNodeSelectionList& PrefabGroup::GetSceneNodeSelectionList() const
  56. {
  57. return m_nodeSelectionList;
  58. }
  59. void PrefabGroup::GetManifestObjectsToRemoveOnRemoved(
  60. AZStd::vector<const IManifestObject*>& toRemove, const AZ::SceneAPI::Containers::SceneManifest& manifest) const
  61. {
  62. for (size_t manifestIndex = 0; manifestIndex < manifest.GetEntryCount(); ++manifestIndex)
  63. {
  64. const AZStd::shared_ptr<const DataTypes::IManifestObject> manifestObjectAtIndex = manifest.GetValue(manifestIndex);
  65. if (manifestObjectAtIndex->RTTI_IsTypeOf(DataTypes::IGroup::TYPEINFO_Uuid()))
  66. {
  67. // Removing shared ownership is useful because this is about to be deleted.
  68. const DataTypes::IGroup* sceneNodeGroup = azrtti_cast<const DataTypes::IGroup*>(manifestObjectAtIndex.get());
  69. const Containers::RuleContainer& rules = sceneNodeGroup->GetRuleContainerConst();
  70. // Anything with the procedural mesh group rule was added automatically for this prefab group, so mark it for removal.
  71. if (rules.FindFirstByType<AZ::SceneAPI::SceneData::ProceduralMeshGroupRule>())
  72. {
  73. // Add it to the list to remove.
  74. toRemove.push_back(sceneNodeGroup);
  75. }
  76. }
  77. }
  78. }
  79. void PrefabGroup::SetPrefabDom(AzToolsFramework::Prefab::PrefabDom prefabDom)
  80. {
  81. m_prefabDomData = AZStd::make_shared<Prefab::PrefabDomData>();
  82. m_prefabDomData->CopyValue(prefabDom);
  83. }
  84. AzToolsFramework::Prefab::PrefabDomConstReference PrefabGroup::GetPrefabDomRef() const
  85. {
  86. if (m_prefabDomData)
  87. {
  88. return m_prefabDomData->GetValue();
  89. }
  90. return {};
  91. }
  92. void PrefabGroup::Reflect(ReflectContext* context)
  93. {
  94. SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context);
  95. if (serializeContext)
  96. {
  97. serializeContext->Class<DataTypes::IPrefabGroup, DataTypes::ISceneNodeGroup>()
  98. ->Version(1);
  99. serializeContext->Class<ProceduralMeshGroupRule, DataTypes::IRule>()
  100. ->Version(1);
  101. serializeContext->Class<PrefabGroup, DataTypes::IPrefabGroup>()
  102. ->Version(3) // added createProceduralPrefab
  103. ->Field("name", &PrefabGroup::m_name)
  104. ->Field("nodeSelectionList", &PrefabGroup::m_nodeSelectionList)
  105. ->Field("rules", &PrefabGroup::m_rules)
  106. ->Field("id", &PrefabGroup::m_id)
  107. ->Field("prefabDomData", &PrefabGroup::m_prefabDomData);
  108. AZ::EditContext* editContext = serializeContext->GetEditContext();
  109. const char* prefabTooltip = "The prefab group controls the generation of default procedural prefabs. "
  110. "This includes the generation of necessary mesh groups to construct the prefab. "
  111. "Removing this group will disable the default procedural prefab and remove the mesh groups used by that prefab. "
  112. "This group does not control the generation of non-default procedural prefabs, those must be disabled in the script that generates them.";
  113. if (editContext)
  114. {
  115. editContext->Class<PrefabGroup>("Prefab group", prefabTooltip)
  116. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  117. ->Attribute("AutoExpand", true)
  118. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "")
  119. ->Attribute(AZ::Edit::Attributes::Max, 1)
  120. ->Attribute(AZ::Edit::Attributes::CategoryStyle, "display divider")
  121. // There isn't a documentation page for default prefabs under the scene settings documentation category, yet.
  122. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://www.o3de.org/docs/user-guide/assets/scene-settings/")
  123. ->UIElement(AZ::Edit::UIHandlers::MultiLineEdit, "", prefabTooltip)
  124. ->Attribute(AZ::Edit::Attributes::ValueText, "The prefab group controls the generation of default procedural prefabs.")
  125. ->Attribute(AZ::Edit::Attributes::ReadOnly, true);
  126. }
  127. }
  128. BehaviorContext* behaviorContext = azrtti_cast<BehaviorContext*>(context);
  129. if (behaviorContext)
  130. {
  131. auto setPrefabDomData = [](PrefabGroup& self, const AZStd::string& json)
  132. {
  133. auto jsonOutcome = JsonSerializationUtils::ReadJsonString(json);
  134. if (jsonOutcome.IsSuccess())
  135. {
  136. self.SetPrefabDom(AZStd::move(jsonOutcome.GetValue()));
  137. return true;
  138. }
  139. AZ_Error("prefab", false, "Set PrefabDom failed (%s)", jsonOutcome.GetError().c_str());
  140. return false;
  141. };
  142. auto getPrefabDomData = [](const PrefabGroup& self) -> AZStd::string
  143. {
  144. if (self.GetPrefabDomRef().has_value() == false)
  145. {
  146. return {};
  147. }
  148. AZStd::string buffer;
  149. JsonSerializationUtils::WriteJsonString(self.GetPrefabDomRef().value(), buffer);
  150. return buffer;
  151. };
  152. behaviorContext->Class<PrefabGroup>()
  153. ->Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::All)
  154. ->Attribute(Script::Attributes::Scope, Script::Attributes::ScopeFlags::Common)
  155. ->Attribute(Script::Attributes::Module, "prefab")
  156. ->Property("name", BehaviorValueProperty(&PrefabGroup::m_name))
  157. ->Property("id", BehaviorValueProperty(&PrefabGroup::m_id))
  158. ->Property("prefabDomData", getPrefabDomData, setPrefabDomData);
  159. }
  160. }
  161. bool ProceduralMeshGroupRule::ModifyTooltip(AZStd::string& tooltip)
  162. {
  163. tooltip = AZStd::string::format("This group was generated by the procedural prefab. To remove this group, remove the procedural prefab. %.*s", AZ_STRING_ARG(tooltip));
  164. return true;
  165. }
  166. }