GeneralSlotLayoutComponent.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <QGraphicsLinearLayout>
  10. #include <QGraphicsWidget>
  11. #include <AzCore/Component/Component.h>
  12. #include <GraphCanvas/Components/Nodes/NodeBus.h>
  13. #include <GraphCanvas/Components/Nodes/NodeLayoutBus.h>
  14. #include <GraphCanvas/Components/SceneBus.h>
  15. #include <GraphCanvas/Components/Slots/SlotBus.h>
  16. #include <GraphCanvas/Components/StyleBus.h>
  17. #include <GraphCanvas/Styling/StyleHelper.h>
  18. namespace GraphCanvas
  19. {
  20. class GeneralSlotLayoutGraphicsWidget;
  21. //! Lays out the slots for the General Node
  22. class GeneralSlotLayoutComponent
  23. : public AZ::Component
  24. {
  25. public:
  26. AZ_COMPONENT(GeneralSlotLayoutComponent, "{F6554B50-A42A-4C79-8B1D-547EEA1EA52D}");
  27. static void Reflect(AZ::ReflectContext*);
  28. GeneralSlotLayoutComponent();
  29. ~GeneralSlotLayoutComponent();
  30. // AZ::Component
  31. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  32. {
  33. provided.push_back(AZ_CRC("GraphCanvas_SlotsContainerService", 0x948b6696));
  34. }
  35. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incombatible)
  36. {
  37. incombatible.push_back(AZ_CRC("GraphCanvas_SlotsContainerService", 0x948b6696));
  38. }
  39. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  40. {
  41. (void)dependent;
  42. }
  43. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  44. {
  45. required.push_back(AZ_CRC("GraphCanvas_StyledGraphicItemService", 0xeae4cdf4));
  46. required.push_back(AZ_CRC("GraphCanvas_SceneMemberService", 0xe9759a2d));
  47. }
  48. void Init() override;
  49. void Activate() override;
  50. void Deactivate() override;
  51. ////
  52. // NodeSlotsRequestBus
  53. QGraphicsWidget* GetGraphicsWidget();
  54. ////
  55. private:
  56. GeneralSlotLayoutComponent(const GeneralSlotLayoutComponent&) = delete;
  57. bool m_enableDividers;
  58. SlotGroupConfigurationMap m_slotGroupConfigurations;
  59. friend class GeneralSlotLayoutGraphicsWidget;
  60. GeneralSlotLayoutGraphicsWidget* m_nodeSlotsUi;
  61. };
  62. //! The slots QGraphicsWidget for displaying a the node slots
  63. //! QtWidgets cannot be serialized out, so the component wrapper
  64. //! stores the actual configuration map for serialization.
  65. class GeneralSlotLayoutGraphicsWidget
  66. : public QGraphicsWidget
  67. , public SlotLayoutRequestBus::Handler
  68. , public NodeSlotsRequestBus::Handler
  69. , public NodeNotificationBus::Handler
  70. , public StyleNotificationBus::Handler
  71. , public SceneMemberNotificationBus::Handler
  72. {
  73. public:
  74. class LayoutDividerWidget
  75. : public QGraphicsWidget
  76. {
  77. public:
  78. AZ_CLASS_ALLOCATOR(LayoutDividerWidget, AZ::SystemAllocator, 0);
  79. LayoutDividerWidget(QGraphicsItem* parent);
  80. void UpdateStyle(const Styling::StyleHelper& styleHelper);
  81. };
  82. class LinearSlotGroupWidget
  83. : public QGraphicsWidget
  84. , public SlotUINotificationBus::MultiHandler
  85. {
  86. public:
  87. AZ_CLASS_ALLOCATOR(LinearSlotGroupWidget, AZ::SystemAllocator, 0);
  88. LinearSlotGroupWidget(QGraphicsItem* parent);
  89. void DisplaySlot(const AZ::EntityId& slotId);
  90. void RemoveSlot(const AZ::EntityId& slotId);
  91. QGraphicsLinearLayout* GetLayout();
  92. QGraphicsWidget* GetSpacer();
  93. const AZStd::vector< SlotLayoutInfo >& GetInputSlots() const;
  94. const AZStd::vector< SlotLayoutInfo >& GetOutputSlots() const;
  95. bool IsEmpty() const;
  96. void UpdateStyle(const Styling::StyleHelper& styleHelper);
  97. // SlotUINotificationBus
  98. void OnSlotLayoutPriorityChanged(int layoutPriority) override;
  99. ////
  100. private:
  101. int LayoutSlot(QGraphicsLinearLayout* layout, AZStd::vector<SlotLayoutInfo>& slotList, const SlotLayoutInfo& slotInfo);
  102. QGraphicsLayoutItem* GetLayoutItem(const AZ::EntityId& slotId) const;
  103. QGraphicsLinearLayout* m_layout;
  104. QGraphicsWidget* m_horizontalSpacer;
  105. QGraphicsLinearLayout* m_inputs;
  106. AZStd::vector< SlotLayoutInfo > m_inputSlots;
  107. AZStd::unordered_set< SlotId > m_inputSlotSet;
  108. QGraphicsLinearLayout* m_outputs;
  109. AZStd::vector< SlotLayoutInfo > m_outputSlots;
  110. AZStd::unordered_set< SlotId > m_outputSlotSet;
  111. };
  112. public:
  113. AZ_TYPE_INFO(GeneralSlotLayoutGraphicsWidget, "{9DE7D3C0-D88C-47D8-85D4-5E0F619E60CB}");
  114. AZ_CLASS_ALLOCATOR(GeneralSlotLayoutGraphicsWidget, AZ::SystemAllocator, 0);
  115. static void Reflect(AZ::ReflectContext* context) = delete;
  116. GeneralSlotLayoutGraphicsWidget(GeneralSlotLayoutComponent& nodeSlots);
  117. ~GeneralSlotLayoutGraphicsWidget() override;
  118. void Activate();
  119. void Deactivate();
  120. // NodeNotificationBus
  121. void OnNodeActivated() override;
  122. void OnSlotAddedToNode(const AZ::EntityId& slot) override;
  123. void OnSlotRemovedFromNode(const AZ::EntityId& slot) override;
  124. ////
  125. // NodeSlotsRequestBus
  126. QGraphicsLayoutItem* GetGraphicsLayoutItem() override;
  127. QGraphicsLinearLayout* GetLinearLayout(const SlotGroup& slotGroup) override;
  128. QGraphicsWidget* GetSpacer(const SlotGroup& slotGroup) override;
  129. ////
  130. // SceneMemberNotificationBus
  131. void OnSceneSet(const AZ::EntityId& sceneId) override;
  132. ////
  133. // SlotLayoutRequestBus
  134. void SetDividersEnabled(bool enabled) override;
  135. void ConfigureSlotGroup(SlotGroup group, SlotGroupConfiguration configuration) override;
  136. int GetSlotGroupDisplayOrder(SlotGroup group) const override;
  137. bool IsSlotGroupVisible(SlotGroup group) const override;
  138. void SetSlotGroupVisible(SlotGroup group, bool visible) override;
  139. void ClearSlotGroup(SlotGroup group) override;
  140. ////
  141. // StyleNotificationBus
  142. void OnStyleChanged() override;
  143. ////
  144. protected:
  145. const AZ::EntityId& GetEntityId() const { return m_entityId; }
  146. private:
  147. bool DisplaySlot(const AZ::EntityId& slotId);
  148. bool RemoveSlot(const AZ::EntityId& slotId);
  149. void ActivateSlots();
  150. void ClearLayout();
  151. void UpdateLayout();
  152. void UpdateStyles();
  153. void RefreshDisplay();
  154. LinearSlotGroupWidget* FindCreateSlotGroupWidget(SlotGroup slotType);
  155. LayoutDividerWidget* FindCreateDividerWidget(int index);
  156. GeneralSlotLayoutGraphicsWidget(const GeneralSlotLayoutComponent&) = delete;
  157. GeneralSlotLayoutComponent& m_nodeSlots;
  158. QGraphicsLinearLayout* m_groupLayout;
  159. AZStd::unordered_map< SlotGroup, LinearSlotGroupWidget* > m_slotGroups;
  160. AZStd::vector< LayoutDividerWidget* > m_dividers;
  161. Styling::StyleHelper m_styleHelper;
  162. AZ::EntityId m_entityId;
  163. bool m_addedToScene;
  164. };
  165. }