DataConnectionComponent.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <Components/Connections/DataConnections/DataConnectionComponent.h>
  9. #include <Components/Connections/DataConnections/DataConnectionVisualComponent.h>
  10. #include <Components/StylingComponent.h>
  11. #include <Source/Components/Connections/ConnectionLayerControllerComponent.h>
  12. #include <GraphCanvas/Components/Slots/SlotBus.h>
  13. #include <GraphCanvas/Components/Slots/Data/DataSlotBus.h>
  14. namespace GraphCanvas
  15. {
  16. ////////////////////////////
  17. // DataConnectionComponent
  18. ////////////////////////////
  19. void DataConnectionComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  22. if (serializeContext)
  23. {
  24. serializeContext->Class<DataConnectionComponent, ConnectionComponent>()
  25. ->Version(1)
  26. ;
  27. }
  28. }
  29. AZ::Entity* DataConnectionComponent::CreateDataConnection(const Endpoint& sourceEndpoint, const Endpoint& targetEndpoint, bool createModelConnection, const AZStd::string& substyle)
  30. {
  31. // Create this Connection's entity.
  32. AZ::Entity* entity = aznew AZ::Entity("Connection");
  33. entity->CreateComponent<DataConnectionComponent>(sourceEndpoint, targetEndpoint, createModelConnection);
  34. entity->CreateComponent<StylingComponent>(Styling::Elements::Connection, AZ::EntityId(), substyle);
  35. entity->CreateComponent<DataConnectionVisualComponent>();
  36. entity->CreateComponent<ConnectionLayerControllerComponent>();
  37. return entity;
  38. }
  39. DataConnectionComponent::DataConnectionComponent(const Endpoint& sourceEndpoint, const Endpoint& targetEndpoint, bool createModelConnection)
  40. : ConnectionComponent(sourceEndpoint, targetEndpoint, createModelConnection)
  41. {
  42. }
  43. bool DataConnectionComponent::AllowNodeCreation() const
  44. {
  45. DataSlotType slotType = DataSlotType::Value;
  46. if (m_sourceEndpoint.IsValid())
  47. {
  48. DataSlotRequestBus::EventResult(slotType, m_sourceEndpoint.GetSlotId(), &DataSlotRequests::GetDataSlotType);
  49. }
  50. else if (m_targetEndpoint.IsValid())
  51. {
  52. DataSlotRequestBus::EventResult(slotType, m_targetEndpoint.GetSlotId(), &DataSlotRequests::GetDataSlotType);
  53. }
  54. return slotType == DataSlotType::Value;
  55. }
  56. ConnectionComponent::ConnectionMoveResult DataConnectionComponent::OnConnectionMoveComplete(const QPointF& scenePos, const QPoint& screenPos, AZ::EntityId groupTarget)
  57. {
  58. ConnectionMoveResult retVal = ConnectionMoveResult::DeleteConnection;
  59. // If we are missing an endpoint, default to the normal behavior
  60. if (!m_sourceEndpoint.IsValid() || !m_targetEndpoint.IsValid())
  61. {
  62. retVal = ConnectionComponent::OnConnectionMoveComplete(scenePos, screenPos, groupTarget);
  63. }
  64. else
  65. {
  66. DataSlotType sourceSlotType = DataSlotType::Unknown;
  67. DataSlotRequestBus::EventResult(sourceSlotType, GetSourceSlotId(), &DataSlotRequests::GetDataSlotType);
  68. DataSlotType targetSlotType = DataSlotType::Unknown;
  69. DataSlotRequestBus::EventResult(targetSlotType, GetTargetSlotId(), &DataSlotRequests::GetDataSlotType);
  70. bool converted = false;
  71. if (m_dragContext == DragContext::MoveTarget)
  72. {
  73. if (sourceSlotType == DataSlotType::Value)
  74. {
  75. DataSlotRequestBus::EventResult(converted, GetTargetSlotId(), &DataSlotRequests::ConvertToValue);
  76. }
  77. else if (sourceSlotType == DataSlotType::Reference)
  78. {
  79. DataSlotRequestBus::EventResult(converted, GetTargetSlotId(), &DataSlotRequests::ConvertToReference, false);
  80. }
  81. }
  82. else if (m_dragContext == DragContext::MoveSource)
  83. {
  84. if (targetSlotType == DataSlotType::Value)
  85. {
  86. DataSlotRequestBus::EventResult(converted, GetSourceSlotId(), &DataSlotRequests::ConvertToValue);
  87. }
  88. else if (targetSlotType == DataSlotType::Reference)
  89. {
  90. DataSlotRequestBus::EventResult(converted, GetSourceSlotId(), &DataSlotRequests::ConvertToReference, false);
  91. }
  92. }
  93. else if (m_dragContext == DragContext::TryConnection)
  94. {
  95. converted = true;
  96. }
  97. if (converted)
  98. {
  99. DataSlotType targetSlotType2 = DataSlotType::Unknown;
  100. DataSlotRequestBus::EventResult(targetSlotType2, GetTargetSlotId(), &DataSlotRequests::GetDataSlotType);
  101. if (targetSlotType2 == DataSlotType::Value)
  102. {
  103. retVal = ConnectionComponent::OnConnectionMoveComplete(scenePos, screenPos, groupTarget);
  104. }
  105. else if (targetSlotType2 == DataSlotType::Reference)
  106. {
  107. GraphId graphId;
  108. SceneMemberRequestBus::EventResult(graphId, GetEntityId(), &SceneMemberRequests::GetScene);
  109. if (m_dragContext == DragContext::MoveSource)
  110. {
  111. GraphModelRequestBus::Event(graphId, &GraphModelRequests::SynchronizeReferences, GetTargetEndpoint(), GetSourceEndpoint());
  112. }
  113. else if (m_dragContext == DragContext::MoveTarget)
  114. {
  115. GraphModelRequestBus::Event(graphId, &GraphModelRequests::SynchronizeReferences, GetSourceEndpoint(), GetTargetEndpoint());
  116. }
  117. // We don't want the connection to persist.
  118. retVal = ConnectionMoveResult::DeleteConnection;
  119. }
  120. }
  121. }
  122. return retVal;
  123. }
  124. }