GridVisualComponent.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <QGraphicsItem>
  10. #include <AzCore/Component/Component.h>
  11. #include <GraphCanvas/Components/GridBus.h>
  12. #include <GraphCanvas/Components/StyleBus.h>
  13. #include <GraphCanvas/Components/VisualBus.h>
  14. #include <GraphCanvas/Widgets/RootGraphicsItem.h>
  15. #include <GraphCanvas/Styling/StyleHelper.h>
  16. namespace GraphCanvas
  17. {
  18. class GridGraphicsItem;
  19. class GridVisualComponent
  20. : public AZ::Component
  21. , public VisualRequestBus::Handler
  22. , public SceneMemberUIRequestBus::Handler
  23. , public GridNotificationBus::Handler
  24. {
  25. public:
  26. AZ_COMPONENT(GridVisualComponent, "{9BAEAE14-A2D3-4D60-AEA8-A8BA3E4D5EC9}", AZ::Component);
  27. static void Reflect(AZ::ReflectContext* context);
  28. GridVisualComponent();
  29. ~GridVisualComponent() override = default;
  30. // AZ::Component
  31. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  32. {
  33. provided.push_back(AZ_CRC_CE("GraphCanvas_GridVisualService"));
  34. provided.push_back(AZ_CRC_CE("GraphCanvas_RootVisualService"));
  35. provided.push_back(AZ_CRC_CE("GraphCanvas_VisualService"));
  36. }
  37. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  38. {
  39. incompatible.push_back(AZ_CRC_CE("GraphCanvas_GridVisualService"));
  40. incompatible.push_back(AZ_CRC_CE("GraphCanvas_RootVisualService"));
  41. incompatible.push_back(AZ_CRC_CE("GraphCanvas_VisualService"));
  42. }
  43. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  44. {
  45. (void)dependent;
  46. }
  47. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  48. {
  49. required.push_back(AZ_CRC_CE("GraphCanvas_GridService"));
  50. }
  51. void Init() override;
  52. void Activate() override;
  53. void Deactivate() override;
  54. ////
  55. // VisualRequestBus
  56. QGraphicsItem* AsGraphicsItem() override;
  57. bool Contains(const AZ::Vector2&) const override;
  58. void SetVisible(bool visible) override;
  59. bool IsVisible() const override;
  60. ////
  61. // SceneMemberUIRequestBus
  62. QGraphicsItem* GetRootGraphicsItem() override;
  63. QGraphicsLayoutItem* GetRootGraphicsLayoutItem() override;
  64. void SetSelected(bool selected) override;
  65. bool IsSelected() const override;
  66. QPainterPath GetOutline() const override;
  67. void SetZValue(qreal zValue) override;
  68. qreal GetZValue() const override;
  69. ////
  70. // GridNotificationBus
  71. void OnMajorPitchChanged(const AZ::Vector2& pitch) override;
  72. void OnMinorPitchChanged(const AZ::Vector2& pitch) override;
  73. ////
  74. private:
  75. GridVisualComponent(const GridVisualComponent&) = delete;
  76. AZ::Vector2 m_majorPitch;
  77. AZ::Vector2 m_minorPitch;
  78. friend class GridGraphicsItem;
  79. AZStd::unique_ptr<GridGraphicsItem> m_gridVisualUi;
  80. };
  81. class GridGraphicsItem
  82. : public RootGraphicsItem<QGraphicsItem>
  83. , protected StyleNotificationBus::Handler
  84. {
  85. static const int k_stencilScaleFactor;
  86. public:
  87. AZ_TYPE_INFO(GridGraphicsItem, "{D483E19C-8046-472B-801D-A6B1A9F2FF33}");
  88. AZ_CLASS_ALLOCATOR(GridGraphicsItem, AZ::SystemAllocator);
  89. static void Reflect(AZ::ReflectContext* context) = delete;
  90. GridGraphicsItem(GridVisualComponent& gridVisual);
  91. ~GridGraphicsItem() override = default;
  92. void Init();
  93. void Activate();
  94. void Deactivate();
  95. // RootVisualNotificationsHelper
  96. QRectF GetBoundingRect() const override;
  97. ////
  98. // StyleNotificationBus
  99. void OnStyleChanged() override;
  100. ////
  101. // QGraphicsItem
  102. QRectF boundingRect(void) const override;
  103. ////
  104. protected:
  105. // QGraphicsItem
  106. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
  107. ////
  108. private:
  109. GridGraphicsItem(const GridGraphicsItem&) = delete;
  110. void CacheStencils();
  111. Styling::StyleHelper m_style;
  112. AZStd::fixed_vector< QPixmap*, 4> m_levelOfDetails;
  113. GridVisualComponent& m_gridVisual;
  114. };
  115. }