3
0

DynamicNode.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <AtomToolsFramework/Graph/DynamicNode/DynamicNode.h>
  9. #include <AtomToolsFramework/Graph/DynamicNode/DynamicNodeManager.h>
  10. #include <AtomToolsFramework/Graph/DynamicNode/DynamicNodeManagerRequestBus.h>
  11. #include <AtomToolsFramework/Util/Util.h>
  12. #include <AzCore/std/smart_ptr/make_shared.h>
  13. #include <GraphModel/Model/Graph.h>
  14. #include <GraphModel/Model/GraphContext.h>
  15. #include <GraphModel/Model/Slot.h>
  16. namespace AtomToolsFramework
  17. {
  18. void DynamicNode::Reflect(AZ::ReflectContext* context)
  19. {
  20. DynamicNodeSlotConfig::Reflect(context);
  21. DynamicNodeConfig::Reflect(context);
  22. DynamicNodeManager::Reflect(context);
  23. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  24. {
  25. serializeContext->Class<DynamicNode, GraphModel::Node>()
  26. ->Version(0)
  27. ->Field("toolId", &DynamicNode::m_toolId)
  28. ->Field("configId", &DynamicNode::m_configId)
  29. ;
  30. if (auto editContext = serializeContext->GetEditContext())
  31. {
  32. editContext->Class<DynamicNode>("DynamicNode", "")
  33. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  34. ->Attribute(AZ_CRC_CE("TitlePaletteOverride"), &DynamicNode::GetTitlePaletteName);
  35. }
  36. }
  37. }
  38. DynamicNode::DynamicNode(GraphModel::GraphPtr ownerGraph, const AZ::Crc32& toolId, const AZ::Uuid& configId)
  39. : GraphModel::Node(ownerGraph)
  40. , m_toolId(toolId)
  41. , m_configId(configId)
  42. {
  43. RegisterSlots();
  44. CreateSlotData();
  45. }
  46. const char* DynamicNode::GetTitle() const
  47. {
  48. return m_config.m_title.c_str();
  49. }
  50. const char* DynamicNode::GetSubTitle() const
  51. {
  52. return m_config.m_subTitle.c_str();
  53. }
  54. const AZ::Uuid& DynamicNode::GetConfigId() const
  55. {
  56. return m_configId;
  57. }
  58. const DynamicNodeConfig& DynamicNode::GetConfig() const
  59. {
  60. return m_config;
  61. }
  62. AZStd::string DynamicNode::GetTitlePaletteName() const
  63. {
  64. return !m_config.m_titlePaletteName.empty() ? m_config.m_titlePaletteName : "DefaultNodeTitlePalette";
  65. }
  66. void DynamicNode::RegisterSlots()
  67. {
  68. m_config = {};
  69. AtomToolsFramework::DynamicNodeManagerRequestBus::EventResult(
  70. m_config, m_toolId, &AtomToolsFramework::DynamicNodeManagerRequestBus::Events::GetConfigById, m_configId);
  71. const bool enablePropertyEditingOnNodeUI =
  72. GetSettingsValue("/O3DE/AtomToolsFramework/DynamicNode/EnablePropertyEditingOnNodeUI", true);
  73. for (const auto& slotConfig : m_config.m_propertySlots)
  74. {
  75. // Property slots only support one data type. Search for the first valid supported data type.
  76. if (!slotConfig.GetDefaultDataType())
  77. {
  78. AZ_Error(
  79. "DynamicNode",
  80. false,
  81. "Unable to register property slot \"%s\" with no supported data types, from DynamicNodeConfig \"%s\"",
  82. slotConfig.m_displayName.c_str(),
  83. m_configId.ToFixedString().c_str());
  84. continue;
  85. }
  86. // Assigning the default value from the slot configuration or the first data type
  87. if (slotConfig.GetDefaultValue().empty())
  88. {
  89. AZ_Error(
  90. "DynamicNode",
  91. false,
  92. "Unable to register property slot \"%s\" with invalid default value, from DynamicNodeConfig \"%s\"",
  93. slotConfig.m_displayName.c_str(),
  94. m_configId.ToFixedString().c_str());
  95. continue;
  96. }
  97. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  98. GraphModel::SlotDirection::Input,
  99. GraphModel::SlotType::Property,
  100. slotConfig.m_name,
  101. slotConfig.m_displayName,
  102. slotConfig.m_description,
  103. slotConfig.GetSupportedDataTypes(),
  104. slotConfig.GetDefaultValue(),
  105. 1, 1, "", "",
  106. slotConfig.m_enumValues,
  107. slotConfig.m_visibleOnNode,
  108. slotConfig.m_editableOnNode && enablePropertyEditingOnNodeUI));
  109. }
  110. // Register all of the input data slots with the dynamic node
  111. for (const auto& slotConfig : m_config.m_inputSlots)
  112. {
  113. // Input slots support incoming connections from multiple data types. We must build a container of all of the data type objects
  114. // for all of the supported types to create the input slot.
  115. if (slotConfig.GetSupportedDataTypes().empty())
  116. {
  117. AZ_Error(
  118. "DynamicNode",
  119. false,
  120. "Unable to register input slot \"%s\" with no supported data types, from DynamicNodeConfig \"%s\"",
  121. slotConfig.m_displayName.c_str(),
  122. m_configId.ToFixedString().c_str());
  123. continue;
  124. }
  125. // Assigning the default value from the slot configuration or the first data type
  126. if (slotConfig.GetDefaultValue().empty())
  127. {
  128. AZ_Error(
  129. "DynamicNode",
  130. false,
  131. "Unable to register input slot \"%s\" with invalid default value, from DynamicNodeConfig \"%s\"",
  132. slotConfig.m_displayName.c_str(),
  133. m_configId.ToFixedString().c_str());
  134. continue;
  135. }
  136. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  137. GraphModel::SlotDirection::Input,
  138. GraphModel::SlotType::Data,
  139. slotConfig.m_name,
  140. slotConfig.m_displayName,
  141. slotConfig.m_description,
  142. slotConfig.GetSupportedDataTypes(),
  143. slotConfig.GetDefaultValue(),
  144. 1, 1, "", "",
  145. slotConfig.m_enumValues,
  146. slotConfig.m_visibleOnNode,
  147. slotConfig.m_editableOnNode && enablePropertyEditingOnNodeUI));
  148. }
  149. for (const auto& slotConfig : m_config.m_outputSlots)
  150. {
  151. // Output slots only support one data type. Search for the first valid supported data type.
  152. if (!slotConfig.GetDefaultDataType())
  153. {
  154. AZ_Error(
  155. "DynamicNode",
  156. false,
  157. "Unable to register output slot \"%s\" with no supported data types, from DynamicNodeConfig \"%s\"",
  158. slotConfig.m_displayName.c_str(),
  159. m_configId.ToFixedString().c_str());
  160. continue;
  161. }
  162. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  163. GraphModel::SlotDirection::Output,
  164. GraphModel::SlotType::Data,
  165. slotConfig.m_name,
  166. slotConfig.m_displayName,
  167. slotConfig.m_description,
  168. slotConfig.GetSupportedDataTypes(),
  169. slotConfig.GetDefaultValue(),
  170. 1, 1, "", "",
  171. slotConfig.m_enumValues,
  172. slotConfig.m_visibleOnNode,
  173. slotConfig.m_editableOnNode && enablePropertyEditingOnNodeUI));
  174. }
  175. }
  176. } // namespace AtomToolsFramework