FileSoftNameSetting.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/Component/ComponentApplicationBus.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzFramework/StringFunc/StringFunc.h>
  11. #include <SceneAPI/SceneCore/Containers/Scene.h>
  12. #include <SceneAPI/SceneCore/DataTypes/IGraphObject.h>
  13. #include <SceneAPI/SceneCore/Utilities/PatternMatcher.h>
  14. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  15. #include <Config/SettingsObjects/FileSoftNameSetting.h>
  16. namespace AZ
  17. {
  18. namespace SceneProcessingConfig
  19. {
  20. FileSoftNameSetting::GraphType::GraphType()
  21. : m_cachedId(Uuid::CreateNull())
  22. {
  23. }
  24. FileSoftNameSetting::GraphType::GraphType(const AZStd::string& name)
  25. : m_name(name)
  26. , m_cachedId(Uuid::CreateNull())
  27. {
  28. }
  29. FileSoftNameSetting::GraphType::GraphType(AZStd::string&& name)
  30. : m_name(AZStd::move(name))
  31. , m_cachedId(Uuid::CreateNull())
  32. {
  33. }
  34. const AZStd::string& FileSoftNameSetting::GraphType::GetName() const
  35. {
  36. return m_name;
  37. }
  38. const Uuid& FileSoftNameSetting::GraphType::GetId() const
  39. {
  40. if (m_cachedId.IsNull())
  41. {
  42. SerializeContext* context = nullptr;
  43. ComponentApplicationBus::BroadcastResult(context, &ComponentApplicationBus::Events::GetSerializeContext);
  44. AZ_Assert(context, "Unable to find valid serialize context.");
  45. context->EnumerateDerived<SceneAPI::DataTypes::IGraphObject>(
  46. [this](const SerializeContext::ClassData* data, const Uuid& typeId) -> bool
  47. {
  48. AZ_UNUSED(typeId);
  49. if (AzFramework::StringFunc::Equal(data->m_name, m_name.c_str()))
  50. {
  51. m_cachedId = data->m_typeId;
  52. return false;
  53. }
  54. return true;
  55. });
  56. if (m_cachedId.IsNull())
  57. {
  58. AZ_TracePrintf(SceneAPI::Utilities::WarningWindow, "Unable to find '%s' in the serialize context.", m_name.c_str());
  59. }
  60. }
  61. return m_cachedId;
  62. }
  63. void FileSoftNameSetting::GraphType::Reflect(ReflectContext* context)
  64. {
  65. SerializeContext* serialize = azrtti_cast<SerializeContext*>(context);
  66. if (serialize)
  67. {
  68. serialize->Class<GraphType>()
  69. ->Version(1)
  70. ->Field("name", &GraphType::m_name);
  71. }
  72. }
  73. FileSoftNameSetting::GraphTypeContainer::GraphTypeContainer(std::initializer_list<GraphType> graphTypes)
  74. : m_types(graphTypes)
  75. {
  76. }
  77. AZStd::vector<FileSoftNameSetting::GraphType>& FileSoftNameSetting::GraphTypeContainer::GetGraphTypes()
  78. {
  79. return m_types;
  80. }
  81. const AZStd::vector<FileSoftNameSetting::GraphType>& FileSoftNameSetting::GraphTypeContainer::GetGraphTypes() const
  82. {
  83. return m_types;
  84. }
  85. void FileSoftNameSetting::GraphTypeContainer::Reflect(ReflectContext* context)
  86. {
  87. SerializeContext* serialize = azrtti_cast<SerializeContext*>(context);
  88. if (serialize)
  89. {
  90. serialize->Class<GraphTypeContainer>()
  91. ->Version(1)
  92. ->Field("types", &GraphTypeContainer::m_types);
  93. }
  94. }
  95. FileSoftNameSetting::FileSoftNameSetting(const char* pattern, SceneAPI::SceneCore::PatternMatcher::MatchApproach approach,
  96. const char* virtualType, bool inclusive, std::initializer_list<GraphType> graphTypes)
  97. : SoftNameSetting(pattern, approach, virtualType)
  98. , m_inclusiveList(inclusive)
  99. , m_graphTypes(graphTypes)
  100. , m_cachedScene(nullptr)
  101. {
  102. }
  103. bool FileSoftNameSetting::IsVirtualType(const SceneAPI::Containers::Scene& scene, SceneAPI::Containers::SceneGraph::NodeIndex node) const
  104. {
  105. bool nameMatch = false;
  106. if (m_cachedScene == &scene)
  107. {
  108. nameMatch = m_cachedNameMatch;
  109. }
  110. else
  111. {
  112. switch (m_pattern.GetMatchApproach())
  113. {
  114. case SceneAPI::SceneCore::PatternMatcher::MatchApproach::PreFix:
  115. nameMatch = m_pattern.MatchesPattern(scene.GetName());
  116. break;
  117. case SceneAPI::SceneCore::PatternMatcher::MatchApproach::PostFix:
  118. nameMatch = m_pattern.MatchesPattern(scene.GetName());
  119. break;
  120. case SceneAPI::SceneCore::PatternMatcher::MatchApproach::Regex:
  121. nameMatch = m_pattern.MatchesPattern(scene.GetSourceFilename());
  122. break;
  123. default:
  124. AZ_Assert(false, "Unknown option '%i' for pattern matcher.", m_pattern.GetMatchApproach());
  125. nameMatch = false;
  126. }
  127. m_cachedNameMatch = nameMatch;
  128. m_cachedScene = &scene;
  129. }
  130. if (nameMatch)
  131. {
  132. AZStd::shared_ptr<const SceneAPI::DataTypes::IGraphObject> object = scene.GetGraph().GetNodeContent(node);
  133. for (const GraphType& type : m_graphTypes.GetGraphTypes())
  134. {
  135. if (object->RTTI_IsTypeOf(type.GetId()))
  136. {
  137. return m_inclusiveList;
  138. }
  139. }
  140. return !m_inclusiveList;
  141. }
  142. else
  143. {
  144. return false;
  145. }
  146. }
  147. const AZ::Uuid FileSoftNameSetting::GetTypeId() const
  148. {
  149. return azrtti_typeid<FileSoftNameSetting>();
  150. }
  151. void FileSoftNameSetting::Reflect(ReflectContext* context)
  152. {
  153. GraphType::Reflect(context);
  154. GraphTypeContainer::Reflect(context);
  155. SerializeContext* serialize = azrtti_cast<SerializeContext*>(context);
  156. if (serialize)
  157. {
  158. serialize->Class<FileSoftNameSetting, SoftNameSetting>()
  159. ->Version(1)
  160. ->Field("graphTypes", &FileSoftNameSetting::m_graphTypes)
  161. ->Field("inclusiveList", &FileSoftNameSetting::m_inclusiveList);
  162. serialize->RegisterGenericType<AZStd::vector<AZStd::unique_ptr<FileSoftNameSetting>>>();
  163. EditContext* editContext = serialize->GetEditContext();
  164. if (editContext)
  165. {
  166. editContext->Class<FileSoftNameSetting>("File name setting", "Applies the pattern to the name of the scene file.")
  167. ->ClassElement(Edit::ClassElements::EditorData, "")
  168. ->Attribute(Edit::Attributes::AutoExpand, true)
  169. ->DataElement(AZ_CRC("GraphTypeSelector", 0x362ac245), &FileSoftNameSetting::m_graphTypes, "Graph type",
  170. "The graph types that are the soft name applies to.")
  171. ->Attribute(Edit::Attributes::AutoExpand, true)
  172. ->DataElement(Edit::UIHandlers::Default, &FileSoftNameSetting::m_inclusiveList, "Inclusive",
  173. "If true the types in the list will marked as the virtual type, otherwise any types that are NOT in the list.");
  174. }
  175. }
  176. }
  177. } // namespace SceneProcessingConfig
  178. } // namespace AZ