InputOutputNodes.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // AZ
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. // Graph Model
  12. #include <GraphModel/Model/Module/InputOutputNodes.h>
  13. #include <GraphModel/Model/Graph.h>
  14. #include <GraphModel/Model/Slot.h>
  15. #include <GraphModel/Model/DataType.h>
  16. namespace GraphModel
  17. {
  18. //////////////////////////////////////////////////////////////////////////////
  19. // BaseInputOutputNode
  20. void BaseInputOutputNode::Reflect(AZ::ReflectContext* context)
  21. {
  22. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  23. if (serializeContext)
  24. {
  25. serializeContext->Class<BaseInputOutputNode, Node>()
  26. ->Version(0)
  27. ->Field("m_dataType", &BaseInputOutputNode::m_dataType)
  28. ;
  29. }
  30. }
  31. BaseInputOutputNode::BaseInputOutputNode(GraphPtr graph, DataTypePtr dataType)
  32. : Node(graph)
  33. {
  34. // Copy because m_dataType has to be non-const for use with SerializeContext, and dataType is const
  35. m_dataType = AZStd::make_shared<DataType>(*dataType);
  36. }
  37. const char* BaseInputOutputNode::GetTitle() const
  38. {
  39. return m_title.c_str();
  40. }
  41. GraphModel::DataTypePtr BaseInputOutputNode::GetNodeDataType() const
  42. {
  43. return m_dataType;
  44. }
  45. AZStd::string BaseInputOutputNode::GetName() const
  46. {
  47. return GetSlot("name")->GetValue<AZStd::string>();
  48. }
  49. AZStd::string BaseInputOutputNode::GetDisplayName() const
  50. {
  51. return GetSlot("displayName")->GetValue<AZStd::string>();
  52. }
  53. AZStd::string BaseInputOutputNode::GetDescription() const
  54. {
  55. return GetSlot("description")->GetValue<AZStd::string>();
  56. }
  57. void BaseInputOutputNode::RegisterCommonSlots(AZStd::string_view directionName)
  58. {
  59. GraphModel::DataTypePtr stringDataType = GetGraphContext()->GetDataType<AZStd::string>();
  60. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  61. GraphModel::SlotDirection::Input,
  62. GraphModel::SlotType::Property,
  63. "name",
  64. "Name",
  65. AZStd::string::format("The official name for this %s", directionName.data()),
  66. GraphModel::DataTypeList{ stringDataType },
  67. stringDataType->GetDefaultValue()));
  68. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  69. GraphModel::SlotDirection::Input,
  70. GraphModel::SlotType::Property,
  71. "displayName",
  72. "Display Name",
  73. AZStd::string::format("The name for this %s, displayed to the user. Will use the above Name if left blank.", directionName.data()),
  74. GraphModel::DataTypeList{ stringDataType },
  75. stringDataType->GetDefaultValue()));
  76. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  77. GraphModel::SlotDirection::Input,
  78. GraphModel::SlotType::Property,
  79. "description",
  80. "Description",
  81. AZStd::string::format("A description of this %s, used for tooltips", directionName.data()),
  82. GraphModel::DataTypeList{ stringDataType },
  83. stringDataType->GetDefaultValue()));
  84. }
  85. //////////////////////////////////////////////////////////////////////////////
  86. // GraphInputNode
  87. void GraphInputNode::Reflect(AZ::ReflectContext* context)
  88. {
  89. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  90. if (serializeContext)
  91. {
  92. serializeContext->Class<GraphInputNode, BaseInputOutputNode>()
  93. ->Version(0)
  94. ;
  95. }
  96. }
  97. GraphInputNode::GraphInputNode(GraphModel::GraphPtr graph, DataTypePtr dataType)
  98. : BaseInputOutputNode(graph, dataType)
  99. {
  100. m_title = m_dataType->GetDisplayName() + " Input";
  101. RegisterSlots();
  102. CreateSlotData();
  103. }
  104. void GraphInputNode::PostLoadSetup(GraphPtr graph, NodeId id)
  105. {
  106. m_title = m_dataType->GetDisplayName() + " Input";
  107. Node::PostLoadSetup(graph, id);
  108. }
  109. AZStd::any GraphInputNode::GetDefaultValue() const
  110. {
  111. return GetSlot("defaultValue")->GetValue();
  112. }
  113. void GraphInputNode::RegisterSlots()
  114. {
  115. // Register just a single output slot for the data that is input through this node
  116. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  117. GraphModel::SlotDirection::Output,
  118. GraphModel::SlotType::Data,
  119. "value",
  120. "Value",
  121. "An external value provided as input to this graph",
  122. GraphModel::DataTypeList{ m_dataType }));
  123. // Register meta-data properties
  124. RegisterCommonSlots("input");
  125. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  126. GraphModel::SlotDirection::Input,
  127. GraphModel::SlotType::Property,
  128. "defaultValue",
  129. "Default Value",
  130. "The default value for this input when no data is provided externally",
  131. GraphModel::DataTypeList{ m_dataType },
  132. m_dataType->GetDefaultValue()));
  133. }
  134. //////////////////////////////////////////////////////////////////////////////
  135. // GraphOutputNode
  136. void GraphOutputNode::Reflect(AZ::ReflectContext* context)
  137. {
  138. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  139. if (serializeContext)
  140. {
  141. serializeContext->Class<GraphOutputNode, BaseInputOutputNode>()
  142. ->Version(0)
  143. ;
  144. }
  145. }
  146. GraphOutputNode::GraphOutputNode(GraphModel::GraphPtr graph, DataTypePtr dataType)
  147. : BaseInputOutputNode(graph, dataType)
  148. {
  149. m_title = m_dataType->GetDisplayName() + " Output";
  150. RegisterSlots();
  151. CreateSlotData();
  152. }
  153. void GraphOutputNode::PostLoadSetup(GraphPtr graph, NodeId id)
  154. {
  155. m_title = m_dataType->GetDisplayName() + " Output";
  156. Node::PostLoadSetup(graph, id);
  157. }
  158. void GraphOutputNode::RegisterSlots()
  159. {
  160. // Register just a single input slot for the data that is output through this node
  161. RegisterSlot(AZStd::make_shared<GraphModel::SlotDefinition>(
  162. GraphModel::SlotDirection::Input,
  163. GraphModel::SlotType::Data,
  164. "value",
  165. "Value",
  166. "A value output by this graph for external use",
  167. GraphModel::DataTypeList{ m_dataType },
  168. m_dataType->GetDefaultValue()));
  169. // Register meta-data properties
  170. RegisterCommonSlots("output");
  171. }
  172. }