ConnectionLayerControllerComponent.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <Source/Components/Connections/ConnectionLayerControllerComponent.h>
  9. namespace GraphCanvas
  10. {
  11. ///////////////////////////////////////
  12. // ConnectionLayerControllerComponent
  13. ///////////////////////////////////////
  14. void ConnectionLayerControllerComponent::Reflect(AZ::ReflectContext* context)
  15. {
  16. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  17. if (serializeContext)
  18. {
  19. serializeContext->Class<ConnectionLayerControllerComponent, LayerControllerComponent>()
  20. ->Version(0)
  21. ;
  22. }
  23. }
  24. ConnectionLayerControllerComponent::ConnectionLayerControllerComponent()
  25. : LayerControllerComponent("ConnectionLayer", ConnectionOffset)
  26. , m_sourceLayerController(nullptr)
  27. , m_targetLayerController(nullptr)
  28. {
  29. }
  30. void ConnectionLayerControllerComponent::Activate()
  31. {
  32. LayerControllerComponent::Activate();
  33. ConnectionNotificationBus::Handler::BusConnect(GetEntityId());
  34. }
  35. void ConnectionLayerControllerComponent::OnSceneSet(const AZ::EntityId& sceneId)
  36. {
  37. LayerControllerComponent::OnSceneSet(sceneId);
  38. UpdateEndpoints();
  39. }
  40. void ConnectionLayerControllerComponent::OnMoveBegin()
  41. {
  42. SetBaseModifier("editing");
  43. UpdateEndpoints();
  44. }
  45. void ConnectionLayerControllerComponent::OnMoveFinalized(bool isValidConnection)
  46. {
  47. if (isValidConnection)
  48. {
  49. SetBaseModifier("");
  50. UpdateEndpoints();
  51. }
  52. }
  53. void ConnectionLayerControllerComponent::OnSourceSlotIdChanged(const AZ::EntityId&, const AZ::EntityId&)
  54. {
  55. UpdateEndpoints();
  56. }
  57. void ConnectionLayerControllerComponent::OnTargetSlotIdChanged(const AZ::EntityId&, const AZ::EntityId&)
  58. {
  59. UpdateEndpoints();
  60. }
  61. void ConnectionLayerControllerComponent::OnOffsetsChanged(int selectionOffset, int groupOffset)
  62. {
  63. selectionOffset = 0;
  64. groupOffset = 0;
  65. if (m_sourceLayerController)
  66. {
  67. groupOffset = m_sourceLayerController->GetGroupLayerOffset();
  68. selectionOffset = m_sourceLayerController->GetGroupLayerOffset();
  69. }
  70. if (m_targetLayerController)
  71. {
  72. // Need to use min offset here, otherwise connections can be above nodes. Which causes some wierd UX issues
  73. groupOffset = AZStd::min(m_targetLayerController->GetGroupLayerOffset(), groupOffset);
  74. selectionOffset = AZStd::min(m_targetLayerController->GetGroupLayerOffset(), selectionOffset);
  75. }
  76. SetGroupLayerOffset(groupOffset);
  77. SetSelectionLayerOffset(selectionOffset);
  78. }
  79. void ConnectionLayerControllerComponent::UpdateEndpoints()
  80. {
  81. LayerControllerNotificationBus::MultiHandler::BusDisconnect();
  82. Endpoint sourceEndpoint;
  83. ConnectionRequestBus::EventResult(sourceEndpoint, GetEntityId(), &ConnectionRequests::GetSourceEndpoint);
  84. m_sourceLayerController = LayerControllerRequestBus::FindFirstHandler(sourceEndpoint.GetNodeId());
  85. if (m_sourceLayerController)
  86. {
  87. LayerControllerNotificationBus::MultiHandler::BusConnect(sourceEndpoint.GetNodeId());
  88. }
  89. Endpoint targetEndpoint;
  90. ConnectionRequestBus::EventResult(targetEndpoint, GetEntityId(), &ConnectionRequests::GetTargetEndpoint);
  91. m_targetLayerController = LayerControllerRequestBus::FindFirstHandler(targetEndpoint.GetNodeId());
  92. if (m_targetLayerController)
  93. {
  94. LayerControllerNotificationBus::MultiHandler::BusConnect(targetEndpoint.GetNodeId());
  95. }
  96. OnOffsetsChanged(0, 0);
  97. }
  98. }