ManifestVectorHandler.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/EBus/EBus.h>
  9. #include <AzCore/Component/ComponentApplicationBus.h>
  10. #include <AzToolsFramework/Debug/TraceContext.h>
  11. #include <SceneAPI/SceneCore/DataTypes/Groups/IGroup.h>
  12. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  13. #include <SceneAPI/SceneUI/RowWidgets/ManifestVectorHandler.h>
  14. namespace AZ
  15. {
  16. namespace SceneAPI
  17. {
  18. namespace UI
  19. {
  20. AZ_CLASS_ALLOCATOR_IMPL((IManifestVectorHandler, AZ_CLASS), SystemAllocator);
  21. template<typename ManifestType> SerializeContext* IManifestVectorHandler<ManifestType>::s_serializeContext = nullptr;
  22. template<typename ManifestType> IManifestVectorHandler<ManifestType>* IManifestVectorHandler<ManifestType>::s_instance = nullptr;
  23. template<typename ManifestType>
  24. QWidget* IManifestVectorHandler<ManifestType>::CreateGUI(QWidget* parent)
  25. {
  26. if(IManifestVectorHandler::s_serializeContext)
  27. {
  28. ManifestVectorWidget* instance = aznew ManifestVectorWidget(IManifestVectorHandler::s_serializeContext, parent);
  29. connect(instance, &ManifestVectorWidget::valueChanged, this,
  30. [instance]()
  31. {
  32. EBUS_EVENT(AzToolsFramework::PropertyEditorGUIMessages::Bus, RequestWrite, instance);
  33. });
  34. return instance;
  35. }
  36. else
  37. {
  38. return nullptr;
  39. }
  40. }
  41. template<typename ManifestType>
  42. u32 IManifestVectorHandler<ManifestType>::GetHandlerName() const
  43. {
  44. return AZ_CRC_CE("ManifestVector");
  45. }
  46. template<typename ManifestType>
  47. bool IManifestVectorHandler<ManifestType>::AutoDelete() const
  48. {
  49. return false;
  50. }
  51. template<typename ManifestType>
  52. void IManifestVectorHandler<ManifestType>::ConsumeAttribute(ManifestVectorWidget* widget, u32 attrib,
  53. AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName)
  54. {
  55. AZ_TraceContext("Attribute name", debugName);
  56. if (attrib == AZ_CRC_CE("ObjectTypeName"))
  57. {
  58. AZStd::string name;
  59. if (attrValue->Read<AZStd::string>(name))
  60. {
  61. widget->SetCollectionTypeName(name);
  62. }
  63. }
  64. else if (attrib == AZ_CRC_CE("CollectionName"))
  65. {
  66. AZStd::string name;
  67. if (attrValue->Read<AZStd::string>(name))
  68. {
  69. widget->SetCollectionName(name);
  70. }
  71. }
  72. // Sets the number of entries the user can add through this widget. It doesn't limit
  73. // the amount of entries that can be stored.
  74. else if (attrib == AZ_CRC_CE("Cap"))
  75. {
  76. size_t cap;
  77. if (attrValue->Read<size_t>(cap))
  78. {
  79. widget->SetCapSize(cap);
  80. }
  81. }
  82. }
  83. template<typename ManifestType>
  84. void IManifestVectorHandler<ManifestType>::WriteGUIValuesIntoProperty(size_t /*index*/, ManifestVectorWidget* GUI,
  85. typename IManifestVectorHandler::property_t& instance, AzToolsFramework::InstanceDataNode* /*node*/)
  86. {
  87. instance.clear();
  88. AZStd::vector<AZStd::shared_ptr<DataTypes::IManifestObject> > manifestVector = GUI->GetManifestVector();
  89. for (auto& manifestObject : manifestVector)
  90. {
  91. instance.push_back(AZStd::static_pointer_cast<ManifestType>(manifestObject));
  92. }
  93. }
  94. template<typename ManifestType>
  95. bool IManifestVectorHandler<ManifestType>::ReadValuesIntoGUI(size_t /*index*/, ManifestVectorWidget* GUI, const typename IManifestVectorHandler::property_t& instance,
  96. AzToolsFramework::InstanceDataNode* node)
  97. {
  98. AzToolsFramework::InstanceDataNode* parentNode = node->GetParent();
  99. if (parentNode && parentNode->GetClassMetadata() && parentNode->GetClassMetadata()->m_azRtti)
  100. {
  101. AZ_TraceContext("Parent UUID", parentNode->GetClassMetadata()->m_azRtti->GetTypeId());
  102. if (parentNode->GetClassMetadata()->m_azRtti->IsTypeOf(DataTypes::IManifestObject::RTTI_Type()))
  103. {
  104. DataTypes::IManifestObject* owner = static_cast<DataTypes::IManifestObject*>(parentNode->FirstInstance());
  105. GUI->SetManifestVector(instance.begin(), instance.end(), owner);
  106. }
  107. else if (parentNode->GetClassMetadata()->m_azRtti->IsTypeOf(Containers::RuleContainer::RTTI_Type()))
  108. {
  109. AzToolsFramework::InstanceDataNode* manifestObject = parentNode->GetParent();
  110. if (manifestObject && manifestObject->GetClassMetadata()->m_azRtti->IsTypeOf(DataTypes::IManifestObject::RTTI_Type()))
  111. {
  112. DataTypes::IManifestObject* owner = static_cast<DataTypes::IManifestObject*>(manifestObject->FirstInstance());
  113. GUI->SetManifestVector(instance.begin(), instance.end(), owner);
  114. }
  115. else
  116. {
  117. AZ_TracePrintf(Utilities::WarningWindow, "RuleContainer requires a ManifestObject parent.");
  118. }
  119. }
  120. else
  121. {
  122. AZ_TracePrintf(Utilities::WarningWindow, "ManifestVectorWidget requires a ManifestObject parent.");
  123. }
  124. }
  125. else
  126. {
  127. AZ_TracePrintf(Utilities::WarningWindow, "ManifestVectorWidget requires valid parent with RTTI data specified");
  128. }
  129. return false;
  130. }
  131. template<typename ManifestType>
  132. void IManifestVectorHandler<ManifestType>::Register()
  133. {
  134. if (!IManifestVectorHandler::s_instance)
  135. {
  136. IManifestVectorHandler::s_instance = aznew IManifestVectorHandler();
  137. EBUS_EVENT(AzToolsFramework::PropertyTypeRegistrationMessages::Bus, RegisterPropertyType, IManifestVectorHandler::s_instance);
  138. EBUS_EVENT_RESULT(s_serializeContext, AZ::ComponentApplicationBus, GetSerializeContext);
  139. AZ_Assert(s_serializeContext, "Serialization context not available");
  140. }
  141. }
  142. template<typename ManifestType>
  143. void IManifestVectorHandler<ManifestType>::Unregister()
  144. {
  145. if (IManifestVectorHandler::s_instance)
  146. {
  147. EBUS_EVENT(AzToolsFramework::PropertyTypeRegistrationMessages::Bus, UnregisterPropertyType, IManifestVectorHandler::s_instance);
  148. delete IManifestVectorHandler::s_instance;
  149. IManifestVectorHandler::s_instance = nullptr;
  150. }
  151. }
  152. void ManifestVectorHandler::Register()
  153. {
  154. IManifestVectorHandler<DataTypes::IManifestObject>::Register();
  155. IManifestVectorHandler<DataTypes::IRule>::Register();
  156. }
  157. void ManifestVectorHandler::Unregister()
  158. {
  159. IManifestVectorHandler<DataTypes::IManifestObject>::Unregister();
  160. IManifestVectorHandler<DataTypes::IRule>::Unregister();
  161. }
  162. } // UI
  163. } // SceneAPI
  164. } // AZ