ConnectionVisualComponent.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #pragma once
  9. #include <AzCore/PlatformDef.h>
  10. AZ_PUSH_DISABLE_WARNING(4251 4800, "-Wunknown-warning-option")
  11. #include <QGraphicsPathItem>
  12. #include <QPainterPath>
  13. AZ_POP_DISABLE_WARNING
  14. #include <AzCore/Component/Component.h>
  15. #include <AzCore/std/smart_ptr/unique_ptr.h>
  16. #include <AzCore/std/chrono/chrono.h>
  17. #include <GraphCanvas/Components/Connections/ConnectionBus.h>
  18. #include <GraphCanvas/Components/ViewBus.h>
  19. #include <GraphCanvas/Components/VisualBus.h>
  20. #include <GraphCanvas/Editor/AssetEditorBus.h>
  21. #include <GraphCanvas/Widgets/RootGraphicsItem.h>
  22. #include <GraphCanvas/Styling/StyleHelper.h>
  23. class QKeyEvent;
  24. namespace GraphCanvas
  25. {
  26. class ConnectionGraphicsItem;
  27. //! The NodeVisual is the QGraphicsItem for a given node, any components that are created
  28. //! on a Node will all be children QGraphicsItem of this one.
  29. class ConnectionVisualComponent
  30. : public AZ::Component
  31. , public VisualRequestBus::Handler
  32. , public SceneMemberUIRequestBus::Handler
  33. {
  34. public:
  35. AZ_COMPONENT(ConnectionVisualComponent, "{BF9691F8-7EF8-4A94-9321-2EB877634D22}");
  36. static void Reflect(AZ::ReflectContext* context);
  37. ConnectionVisualComponent();
  38. ~ConnectionVisualComponent() override = default;
  39. // AZ::Component
  40. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  41. {
  42. provided.push_back(AZ_CRC_CE("GraphCanvas_ConnectionVisualService"));
  43. provided.push_back(AZ_CRC_CE("GraphCanvas_RootVisualService"));
  44. provided.push_back(AZ_CRC_CE("GraphCanvas_VisualService"));
  45. }
  46. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  47. {
  48. incompatible.push_back(AZ_CRC_CE("GraphCanvas_ConnectionVisualService"));
  49. incompatible.push_back(AZ_CRC_CE("GraphCanvas_RootVisualService"));
  50. incompatible.push_back(AZ_CRC_CE("GraphCanvas_VisualService"));
  51. }
  52. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  53. {
  54. (void)dependent;
  55. }
  56. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  57. {
  58. required.push_back(AZ_CRC_CE("GraphCanvas_ConnectionService"));
  59. }
  60. void Init() override;
  61. void Activate() override;
  62. void Deactivate() override;
  63. ////
  64. // VisualRequestBus
  65. QGraphicsItem* AsGraphicsItem() override;
  66. bool Contains(const AZ::Vector2& point) const override;
  67. void SetVisible(bool visible) override;
  68. bool IsVisible() const override;
  69. ////
  70. // SceneMemberUIRequestBus
  71. QGraphicsItem* GetRootGraphicsItem() override;
  72. QGraphicsLayoutItem* GetRootGraphicsLayoutItem() override;
  73. void SetSelected(bool selected) override;
  74. bool IsSelected() const override;
  75. QPainterPath GetOutline() const override;
  76. void SetZValue(qreal zValue) override;
  77. qreal GetZValue() const override;
  78. ////
  79. protected:
  80. virtual void CreateConnectionVisual();
  81. friend class ConnectionVisualGraphicsItem;
  82. AZStd::unique_ptr<ConnectionGraphicsItem> m_connectionGraphicsItem;
  83. private:
  84. ConnectionVisualComponent(const ConnectionVisualComponent&) = delete;
  85. };
  86. //! The NodeVisual is the QGraphicsItem for a given node, any components that are created
  87. //! on a Node will all be children QGraphicsItem of this one.
  88. class ConnectionGraphicsItem
  89. : public RootGraphicsItem<QGraphicsPathItem>
  90. , public ConnectionNotificationBus::Handler
  91. , public ConnectionUIRequestBus::Handler
  92. , public VisualNotificationBus::MultiHandler
  93. , public StyleNotificationBus::Handler
  94. , public AZ::SystemTickBus::Handler
  95. , public SceneMemberNotificationBus::Handler
  96. , public AssetEditorSettingsNotificationBus::Handler
  97. {
  98. public:
  99. AZ_CLASS_ALLOCATOR(ConnectionGraphicsItem, AZ::SystemAllocator);
  100. // Helper function to return the length of a vector
  101. // (Distance from provided point to the origin)
  102. static qreal VectorLength(QPointF vectorPoint);
  103. ConnectionGraphicsItem(const AZ::EntityId& connectionEntityId);
  104. ~ConnectionGraphicsItem() override = default;
  105. void Activate();
  106. void Deactivate();
  107. void RefreshStyle();
  108. const Styling::StyleHelper& GetStyle() const;
  109. void UpdateOffset();
  110. // RootVisualNotificationsHelper
  111. QRectF GetBoundingRect() const override;
  112. ////
  113. // ConnectionNotificationBus
  114. void OnSourceSlotIdChanged(const AZ::EntityId& oldSlotId, const AZ::EntityId& newSlotId) override;
  115. void OnTargetSlotIdChanged(const AZ::EntityId& oldSlotId, const AZ::EntityId& newSlotId) override;
  116. void OnTooltipChanged(const AZStd::string& tooltip) override;
  117. ////
  118. // StyleNotificationBus
  119. void OnStyleChanged() override;
  120. ////
  121. // AZ::SystemTickBus
  122. void OnSystemTick() override;
  123. ////
  124. // VisualNotificationBus
  125. void OnItemChange(const AZ::EntityId& entityId, QGraphicsItem::GraphicsItemChange change, const QVariant& value) override;
  126. ////
  127. // ConnectionUIRequestBus
  128. void UpdateConnectionPath() override;
  129. void SetAltDeletionEnabled(bool enabled) override;
  130. void SetGraphicsItemFlags(QGraphicsItem::GraphicsItemFlags flags) override;
  131. ////
  132. // SceneMemberNotifications
  133. void OnSceneMemberHidden() override;
  134. void OnSceneMemberShown() override;
  135. void OnSceneSet(const GraphId& graphId) override;
  136. ////
  137. // AssetEditorSettingsNotifications
  138. void OnSettingsChanged() override;
  139. ////
  140. protected:
  141. const AZ::EntityId& GetConnectionEntityId() const;
  142. AZ::EntityId GetSourceSlotEntityId() const;
  143. AZ::EntityId GetTargetSlotEntityId() const;
  144. EditorId GetEditorId() const;
  145. void UpdateCurveStyle();
  146. virtual Styling::ConnectionCurveType GetCurveStyle() const;
  147. virtual void UpdatePen();
  148. virtual void OnActivate();
  149. virtual void OnDeactivate();
  150. virtual void OnPathChanged();
  151. // QGraphicsItem
  152. QPainterPath shape() const override;
  153. void mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent) override;
  154. void mouseMoveEvent(QGraphicsSceneMouseEvent* mouseEvent) override;
  155. void mouseReleaseEvent(QGraphicsSceneMouseEvent* mouseEvent) override;
  156. void focusOutEvent(QFocusEvent* focusEvent) override;
  157. ////
  158. void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
  159. private:
  160. ConnectionGraphicsItem(const ConnectionGraphicsItem&) = delete;
  161. bool m_trackMove;
  162. bool m_moveSource;
  163. QPointF m_initialPoint;
  164. Styling::ConnectionCurveType m_curveType;
  165. Styling::StyleHelper m_style;
  166. QPen m_pen;
  167. AZStd::chrono::milliseconds m_lastUpdate;
  168. double m_offset;
  169. AZ::EntityId m_connectionEntityId;
  170. EditorId m_editorId;
  171. };
  172. }