DynamicSlotComponent.cpp 6.6 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. #include <AzCore/Serialization/SerializeContext.h>
  9. #include <AzCore/std/any.h>
  10. #include <GraphCanvas/Components/Nodes/NodeBus.h>
  11. #include <ScriptCanvas/Core/Slot.h>
  12. #include <ScriptCanvas/GraphCanvas/NodeDescriptorBus.h>
  13. #include <Editor/GraphCanvas/Components/DynamicSlotComponent.h>
  14. #include <Editor/Nodes/NodeDisplayUtils.h>
  15. #include <Editor/Include/ScriptCanvas/GraphCanvas/MappingBus.h>
  16. namespace ScriptCanvasEditor
  17. {
  18. /////////////////////////
  19. // DynamicSlotComponent
  20. /////////////////////////
  21. void DynamicSlotComponent::Reflect(AZ::ReflectContext* reflectionContext)
  22. {
  23. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflectionContext);
  24. if (serializeContext)
  25. {
  26. serializeContext->Class<DynamicSlotComponent, AZ::Component>()
  27. ->Version(1)
  28. ->Field("SlotGroup", &DynamicSlotComponent::m_slotGroup)
  29. ;
  30. }
  31. }
  32. DynamicSlotComponent::DynamicSlotComponent()
  33. : m_slotGroup(GraphCanvas::SlotGroups::Invalid)
  34. , m_queueUpdates(false)
  35. {
  36. }
  37. DynamicSlotComponent::DynamicSlotComponent(GraphCanvas::SlotGroup slotGroup)
  38. : m_slotGroup(slotGroup)
  39. , m_queueUpdates(false)
  40. {
  41. }
  42. void DynamicSlotComponent::Init()
  43. {
  44. }
  45. void DynamicSlotComponent::Activate()
  46. {
  47. GraphCanvas::SceneMemberNotificationBus::Handler::BusConnect(GetEntityId());
  48. DynamicSlotRequestBus::Handler::BusConnect(GetEntityId());
  49. }
  50. void DynamicSlotComponent::Deactivate()
  51. {
  52. GraphCanvas::SceneMemberNotificationBus::Handler::BusDisconnect();
  53. }
  54. void DynamicSlotComponent::OnSceneSet(const AZ::EntityId&)
  55. {
  56. OnUserDataChanged();
  57. GraphCanvas::SceneMemberNotificationBus::Handler::BusDisconnect();
  58. }
  59. void DynamicSlotComponent::OnSlotAdded(const ScriptCanvas::SlotId& slotId)
  60. {
  61. // For EBuses we need to be a little tricky. Since we are going to be pointing at a single node from
  62. // multiple graph canvas nodes. So we want to convert the script canvas id into a graph canvas id
  63. //
  64. // Then check to see if we are the correct node for that particular event.
  65. const AZ::EntityId* scriptCanvasNodeId = ScriptCanvas::NodeNotificationsBus::GetCurrentBusId();
  66. if (scriptCanvasNodeId == nullptr)
  67. {
  68. return;
  69. }
  70. ScriptCanvas::Endpoint endpoint((*scriptCanvasNodeId), slotId);
  71. if (m_queueUpdates)
  72. {
  73. m_queuedEndpoints.insert(endpoint);
  74. return;
  75. }
  76. HandleSlotAdded(endpoint);
  77. }
  78. void DynamicSlotComponent::OnSlotRemoved(const ScriptCanvas::SlotId& slotId)
  79. {
  80. if (m_queueUpdates)
  81. {
  82. const AZ::EntityId* scriptCanvasNodeId = ScriptCanvas::NodeNotificationsBus::GetCurrentBusId();
  83. if (scriptCanvasNodeId)
  84. {
  85. ScriptCanvas::Endpoint endpoint((*scriptCanvasNodeId), slotId);
  86. m_queuedEndpoints.erase(endpoint);
  87. }
  88. }
  89. AZStd::vector< AZ::EntityId > slotIds;
  90. GraphCanvas::NodeRequestBus::EventResult(slotIds, GetEntityId(), &GraphCanvas::NodeRequests::GetSlotIds);
  91. for (AZ::EntityId entityId : slotIds)
  92. {
  93. AZStd::any* userData = nullptr;
  94. GraphCanvas::SlotRequestBus::EventResult(userData, entityId, &GraphCanvas::SlotRequests::GetUserData);
  95. if (userData)
  96. {
  97. ScriptCanvas::SlotId* testId = AZStd::any_cast<ScriptCanvas::SlotId>(userData);
  98. if (testId && (*testId) == slotId)
  99. {
  100. GraphCanvas::NodeRequestBus::Event(GetEntityId(), &GraphCanvas::NodeRequests::RemoveSlot, entityId);
  101. }
  102. }
  103. }
  104. }
  105. void DynamicSlotComponent::OnUserDataChanged()
  106. {
  107. AZStd::any* userData = nullptr;
  108. GraphCanvas::NodeRequestBus::EventResult(userData, GetEntityId(), &GraphCanvas::NodeRequests::GetUserData);
  109. if (userData)
  110. {
  111. AZ::EntityId* entityId = AZStd::any_cast<AZ::EntityId>(userData);
  112. if (entityId)
  113. {
  114. m_scriptCanvasNodeId = (*entityId);
  115. ScriptCanvas::NodeNotificationsBus::Handler::BusDisconnect();
  116. ScriptCanvas::NodeNotificationsBus::Handler::BusConnect(m_scriptCanvasNodeId);
  117. }
  118. }
  119. }
  120. void DynamicSlotComponent::StartQueueSlotUpdates()
  121. {
  122. if (!m_queueUpdates)
  123. {
  124. m_queueUpdates = true;
  125. m_queuedEndpoints.clear();
  126. }
  127. }
  128. void DynamicSlotComponent::StopQueueSlotUpdates()
  129. {
  130. if (m_queueUpdates)
  131. {
  132. m_queueUpdates = false;
  133. for (auto endpoint : m_queuedEndpoints)
  134. {
  135. HandleSlotAdded(endpoint);
  136. }
  137. m_queuedEndpoints.clear();
  138. }
  139. }
  140. void DynamicSlotComponent::ConfigureGraphCanvasSlot(const ScriptCanvas::Slot* slot, const GraphCanvas::SlotId& graphCanvasSlotId)
  141. {
  142. // By default no further configuration is necessary
  143. AZ_UNUSED(slot);
  144. AZ_UNUSED(graphCanvasSlotId);
  145. }
  146. void DynamicSlotComponent::HandleSlotAdded(const ScriptCanvas::Endpoint& endpoint)
  147. {
  148. AZ::EntityId graphCanvasNodeId;
  149. SceneMemberMappingRequestBus::EventResult(graphCanvasNodeId, endpoint.GetNodeId(), &SceneMemberMappingRequests::GetGraphCanvasEntityId);
  150. bool isEBusNode = false;
  151. NodeDescriptorRequestBus::EventResult(isEBusNode, graphCanvasNodeId, &NodeDescriptorRequests::IsType, NodeDescriptorType::EBusHandler);
  152. if (isEBusNode)
  153. {
  154. AZ::EntityId targetEventReceiverNode;
  155. EBusHandlerNodeDescriptorRequestBus::EventResult(targetEventReceiverNode, graphCanvasNodeId, &EBusHandlerNodeDescriptorRequests::FindGraphCanvasNodeIdForSlot, endpoint.GetSlotId());
  156. if (targetEventReceiverNode != GetEntityId())
  157. {
  158. return;
  159. }
  160. }
  161. const ScriptCanvas::Slot* slot = nullptr;
  162. ScriptCanvas::NodeRequestBus::EventResult(slot, endpoint.GetNodeId(), &ScriptCanvas::NodeRequests::GetSlot, endpoint.GetSlotId());
  163. if (slot && slot->IsVisible())
  164. {
  165. AZ::EntityId graphCanvasSlotId = Nodes::DisplayScriptCanvasSlot(GetEntityId(), (*slot), m_slotGroup);
  166. ConfigureGraphCanvasSlot(slot, graphCanvasSlotId);
  167. }
  168. }
  169. }