MappingComponent.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <GraphCanvas/Components/Slots/SlotBus.h>
  9. #include <GraphCanvas/Components/Slots/Data/DataSlotBus.h>
  10. #include <GraphCanvas/Components/Nodes/Wrapper/WrapperNodeBus.h>
  11. #include <Editor/GraphCanvas/Components/MappingComponent.h>
  12. #include <Editor/Include/ScriptCanvas/Bus/RequestBus.h>
  13. #include <ScriptCanvas/Core/Slot.h>
  14. #include <ScriptCanvas/Core/Node.h>
  15. #include <ScriptCanvas/Variable/GraphVariable.h>
  16. #include <ScriptCanvas/Variable/VariableBus.h>
  17. #include <GraphCanvas/Components/Nodes/NodeTitleBus.h>
  18. namespace ScriptCanvasEditor
  19. {
  20. ////////////////////////////////
  21. // SceneMemberMappingComponent
  22. ////////////////////////////////
  23. void SceneMemberMappingComponent::Reflect(AZ::ReflectContext* context)
  24. {
  25. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  26. if (serializeContext)
  27. {
  28. serializeContext->Class<SceneMemberMappingComponent, AZ::Component>()
  29. ->Field("SourceId", &SceneMemberMappingComponent::m_sourceId)
  30. ->Version(1)
  31. ;
  32. }
  33. }
  34. SceneMemberMappingComponent::SceneMemberMappingComponent(const AZ::EntityId& sourceId)
  35. : m_sourceId(sourceId)
  36. {
  37. }
  38. void SceneMemberMappingComponent::Activate()
  39. {
  40. GraphCanvas::NodeNotificationBus::Handler::BusConnect(GetEntityId());
  41. SceneMemberMappingConfigurationRequestBus::Handler::BusConnect(GetEntityId());
  42. ConfigureMapping(m_sourceId);
  43. }
  44. void SceneMemberMappingComponent::Deactivate()
  45. {
  46. SceneMemberMappingRequestBus::Handler::BusDisconnect();
  47. SceneMemberMappingConfigurationRequestBus::Handler::BusDisconnect();
  48. GraphCanvas::NodeNotificationBus::Handler::BusDisconnect();
  49. }
  50. void SceneMemberMappingComponent::ConfigureMapping(const AZ::EntityId& scriptCanvasMemberId)
  51. {
  52. if (m_sourceId.IsValid())
  53. {
  54. SceneMemberMappingRequestBus::Handler::BusDisconnect();
  55. }
  56. m_sourceId = scriptCanvasMemberId;
  57. if (m_sourceId.IsValid())
  58. {
  59. SceneMemberMappingRequestBus::Handler::BusConnect(m_sourceId);
  60. }
  61. }
  62. AZ::EntityId SceneMemberMappingComponent::GetGraphCanvasEntityId() const
  63. {
  64. return GetEntityId();
  65. }
  66. void SceneMemberMappingComponent::OnBatchedConnectionManipulationBegin()
  67. {
  68. ScriptCanvas::NodeRequestBus::Event(m_sourceId, &ScriptCanvas::NodeRequests::SignalBatchedConnectionManipulationBegin);
  69. }
  70. void SceneMemberMappingComponent::OnBatchedConnectionManipulationEnd()
  71. {
  72. ScriptCanvas::NodeRequestBus::Event(m_sourceId, &ScriptCanvas::NodeRequests::SignalBatchedConnectionManipulationEnd);
  73. }
  74. /////////////////////////
  75. // SlotMappingComponent
  76. /////////////////////////
  77. void SlotMappingComponent::Reflect(AZ::ReflectContext* context)
  78. {
  79. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  80. if (serializeContext)
  81. {
  82. serializeContext->Class<SlotMappingComponent, AZ::Component>()
  83. ->Version(1)
  84. ->Field("SourceId", &SlotMappingComponent::m_sourceId)
  85. ;
  86. }
  87. }
  88. SlotMappingComponent::SlotMappingComponent(const AZ::EntityId& sourceId)
  89. : m_sourceId(sourceId)
  90. {
  91. }
  92. void SlotMappingComponent::Activate()
  93. {
  94. m_slotMapping.clear();
  95. GraphCanvas::NodeNotificationBus::Handler::BusConnect(GetEntityId());
  96. SceneMemberMappingConfigurationRequestBus::Handler::BusConnect(GetEntityId());
  97. ConfigureMapping(m_sourceId);
  98. }
  99. void SlotMappingComponent::Deactivate()
  100. {
  101. GraphCanvas::NodeNotificationBus::Handler::BusDisconnect();
  102. SlotMappingRequestBus::MultiHandler::BusDisconnect();
  103. }
  104. void SlotMappingComponent::OnAddedToScene(const AZ::EntityId&)
  105. {
  106. AZStd::vector< AZ::EntityId > slotIds;
  107. GraphCanvas::NodeRequestBus::EventResult(slotIds, GetEntityId(), &GraphCanvas::NodeRequests::GetSlotIds);
  108. for (const AZ::EntityId& slotId : slotIds)
  109. {
  110. OnSlotAddedToNode(slotId);
  111. }
  112. SlotMappingRequestBus::MultiHandler::BusConnect(GetEntityId());
  113. SlotMappingRequestBus::MultiHandler::BusConnect(m_sourceId);
  114. }
  115. void SlotMappingComponent::OnSlotAddedToNode(const AZ::EntityId& slotId)
  116. {
  117. AZStd::any* userData = nullptr;
  118. GraphCanvas::SlotRequestBus::EventResult(userData, slotId, &GraphCanvas::SlotRequests::GetUserData);
  119. AZStd::string displayTitle;
  120. GraphCanvas::NodeTitleRequestBus::EventResult(displayTitle, GetEntityId(), &GraphCanvas::NodeTitleRequests::GetTitle);
  121. if (userData && userData->is<ScriptCanvas::SlotId>())
  122. {
  123. m_slotMapping[(*AZStd::any_cast<ScriptCanvas::SlotId>(userData))] = slotId;
  124. }
  125. }
  126. void SlotMappingComponent::OnSlotRemovedFromNode(const AZ::EntityId& slotId)
  127. {
  128. AZStd::any* userData = nullptr;
  129. GraphCanvas::SlotRequestBus::EventResult(userData, slotId, &GraphCanvas::SlotRequests::GetUserData);
  130. if (userData && userData->is<ScriptCanvas::SlotId>())
  131. {
  132. m_slotMapping.erase((*AZStd::any_cast<ScriptCanvas::SlotId>(userData)));
  133. }
  134. }
  135. AZ::EntityId SlotMappingComponent::MapToGraphCanvasId(const ScriptCanvas::SlotId& slotId)
  136. {
  137. AZ::EntityId mappedId;
  138. auto mapIter = m_slotMapping.find(slotId);
  139. if (mapIter != m_slotMapping.end())
  140. {
  141. mappedId = mapIter->second;
  142. }
  143. else
  144. {
  145. if (GraphCanvas::GraphUtils::IsWrapperNode(GetEntityId()))
  146. {
  147. AZStd::vector< AZ::EntityId > wrappedNodes;
  148. GraphCanvas::WrapperNodeRequestBus::EventResult(wrappedNodes, GetEntityId(), &GraphCanvas::WrapperNodeRequests::GetWrappedNodeIds);
  149. for (const auto& wrappedId : wrappedNodes)
  150. {
  151. SlotMappingRequestBus::EventResult(mappedId, wrappedId, &SlotMappingRequests::MapToGraphCanvasId, slotId);
  152. if (mappedId.IsValid())
  153. {
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. return mappedId;
  160. }
  161. void SlotMappingComponent::OnSlotRenamed(const ScriptCanvas::SlotId& slotId, AZStd::string_view newName)
  162. {
  163. AZ::EntityId graphCanvasSlotId = MapToGraphCanvasId(slotId);
  164. if (graphCanvasSlotId.IsValid())
  165. {
  166. GraphCanvas::SlotRequestBus::Event(graphCanvasSlotId, &GraphCanvas::SlotRequests::SetName, newName);
  167. }
  168. }
  169. void SlotMappingComponent::OnSlotDisplayTypeChanged(const ScriptCanvas::SlotId& slotId, const ScriptCanvas::Data::Type& slotType)
  170. {
  171. AZ::EntityId graphCanvasSlotId = MapToGraphCanvasId(slotId);
  172. if (graphCanvasSlotId.IsValid())
  173. {
  174. GraphCanvas::DataValueType valueType = GraphCanvas::DataValueType::Primitive;
  175. AZ::Uuid typeId = ScriptCanvas::Data::ToAZType(slotType);
  176. if (ScriptCanvas::Data::IsContainerType(typeId))
  177. {
  178. valueType = GraphCanvas::DataValueType::Container;
  179. }
  180. else if (typeId.IsNull())
  181. {
  182. ScriptCanvas::Slot* slot = nullptr;
  183. ScriptCanvas::NodeRequestBus::EventResult(slot, m_sourceId, &ScriptCanvas::NodeRequests::GetSlot, slotId);
  184. if (slot && slot->IsDynamicSlot())
  185. {
  186. auto dynamicSlotType = slot->GetDynamicDataType();
  187. if (dynamicSlotType == ScriptCanvas::DynamicDataType::Container)
  188. {
  189. valueType = GraphCanvas::DataValueType::Container;
  190. }
  191. }
  192. }
  193. GraphCanvas::DataSlotRequestBus::Event(graphCanvasSlotId, &GraphCanvas::DataSlotRequests::SetDataAndContainedTypeIds, typeId, ScriptCanvas::Data::GetContainedTypes(typeId), valueType);
  194. }
  195. }
  196. void SlotMappingComponent::ConfigureMapping(const AZ::EntityId& scriptCanvasMemberId)
  197. {
  198. if (m_sourceId.IsValid())
  199. {
  200. ScriptCanvas::NodeNotificationsBus::Handler::BusDisconnect();
  201. SlotMappingRequestBus::MultiHandler::BusDisconnect(m_sourceId);
  202. }
  203. m_sourceId = scriptCanvasMemberId;
  204. if (m_sourceId.IsValid())
  205. {
  206. ScriptCanvas::NodeNotificationsBus::Handler::BusConnect(m_sourceId);
  207. SlotMappingRequestBus::MultiHandler::BusConnect(m_sourceId);
  208. }
  209. }
  210. }