UiLayoutGridComponent.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <LyShine/Bus/UiLayoutBus.h>
  10. #include <LyShine/Bus/UiLayoutControllerBus.h>
  11. #include <LyShine/Bus/UiLayoutGridBus.h>
  12. #include <LyShine/Bus/UiTransform2dBus.h>
  13. #include <LyShine/Bus/UiLayoutCellDefaultBus.h>
  14. #include <LyShine/UiComponentTypes.h>
  15. #include <AzCore/Component/Component.h>
  16. #include <AzCore/Serialization/SerializeContext.h>
  17. #include <AzCore/Math/Vector2.h>
  18. #include <UiLayoutHelpers.h>
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////
  20. //! This component overrides the transforms of immediate children to organize them into a grid
  21. class UiLayoutGridComponent
  22. : public AZ::Component
  23. , public UiLayoutControllerBus::Handler
  24. , public UiLayoutBus::Handler
  25. , public UiLayoutGridBus::Handler
  26. , public UiLayoutCellDefaultBus::Handler
  27. , public UiTransformChangeNotificationBus::Handler
  28. {
  29. public: // member functions
  30. AZ_COMPONENT(UiLayoutGridComponent, LyShine::UiLayoutGridComponentUuid, AZ::Component);
  31. UiLayoutGridComponent();
  32. ~UiLayoutGridComponent() override;
  33. // UiLayoutControllerInterface
  34. virtual void ApplyLayoutWidth() override;
  35. virtual void ApplyLayoutHeight() override;
  36. // ~UiLayoutControllerInterface
  37. // UiLayoutInterface
  38. virtual bool IsUsingLayoutCellsToCalculateLayout() override;
  39. virtual bool GetIgnoreDefaultLayoutCells() override;
  40. virtual void SetIgnoreDefaultLayoutCells(bool ignoreDefaultLayoutCells) override;
  41. virtual IDraw2d::HAlign GetHorizontalChildAlignment() override;
  42. virtual void SetHorizontalChildAlignment(IDraw2d::HAlign alignment) override;
  43. virtual IDraw2d::VAlign GetVerticalChildAlignment() override;
  44. virtual void SetVerticalChildAlignment(IDraw2d::VAlign alignment) override;
  45. virtual bool IsControllingChild(AZ::EntityId childId) override;
  46. virtual AZ::Vector2 GetSizeToFitChildElements(const AZ::Vector2& childElementSize, int numChildElements) override;
  47. // ~UiLayoutInterface
  48. // UiLayoutGridInterface
  49. virtual UiLayoutInterface::Padding GetPadding() override;
  50. virtual void SetPadding(UiLayoutInterface::Padding padding) override;
  51. virtual AZ::Vector2 GetSpacing() override;
  52. virtual void SetSpacing(AZ::Vector2 spacing) override;
  53. virtual AZ::Vector2 GetCellSize() override;
  54. virtual void SetCellSize(AZ::Vector2 size) override;
  55. virtual UiLayoutInterface::HorizontalOrder GetHorizontalOrder() override;
  56. virtual void SetHorizontalOrder(UiLayoutInterface::HorizontalOrder order) override;
  57. virtual UiLayoutInterface::VerticalOrder GetVerticalOrder() override;
  58. virtual void SetVerticalOrder(UiLayoutInterface::VerticalOrder order) override;
  59. virtual StartingDirection GetStartingDirection() override;
  60. virtual void SetStartingDirection(StartingDirection direction) override;
  61. // ~UiLayoutGridInterface
  62. // UiLayoutCellDefaultInterface
  63. float GetMinWidth() override;
  64. float GetMinHeight() override;
  65. float GetTargetWidth(float maxWidth) override;
  66. float GetTargetHeight(float maxHeight) override;
  67. float GetExtraWidthRatio() override;
  68. float GetExtraHeightRatio() override;
  69. // ~UiLayoutCellDefaultInterface
  70. // UiTransformChangeNotificationBus
  71. void OnCanvasSpaceRectChanged(AZ::EntityId entityId, const UiTransformInterface::Rect& oldRect, const UiTransformInterface::Rect& newRect) override;
  72. // ~UiTransformChangeNotificationBus
  73. public: // static member functions
  74. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  75. {
  76. provided.push_back(AZ_CRC("UiLayoutService", 0xac613bbc));
  77. }
  78. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  79. {
  80. incompatible.push_back(AZ_CRC("UiLayoutService", 0xac613bbc));
  81. }
  82. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  83. {
  84. required.push_back(AZ_CRC("UiElementService", 0x3dca7ad4));
  85. required.push_back(AZ_CRC("UiTransformService", 0x3a838e34));
  86. }
  87. static void Reflect(AZ::ReflectContext* context);
  88. protected: // member functions
  89. // AZ::Component
  90. void Activate() override;
  91. void Deactivate() override;
  92. // ~AZ::Component
  93. AZ_DISABLE_COPY_MOVE(UiLayoutGridComponent);
  94. //! Get the bounding rect size of the children
  95. AZ::Vector2 GetChildrenBoundingRectSize(const AZ::Vector2 childElementSize, int numChildElements);
  96. //! Called on a property change that has caused this element's layout to be invalid
  97. void InvalidateLayout();
  98. //! Called when a property that is used to calculate default layout cell values has changed
  99. void InvalidateParentLayout();
  100. //! Refresh the transform properties in the editor's properties pane
  101. void CheckLayoutFitterAndRefreshEditorTransformProperties() const;
  102. private: // static member functions
  103. static bool VersionConverter(AZ::SerializeContext& context,
  104. AZ::SerializeContext::DataElementNode& classElement);
  105. protected: // data
  106. //! the padding (in pixels) inside the edges of this element
  107. UiLayoutInterface::Padding m_padding;
  108. //! the vertical and horizontal spacing between child elements in pixels
  109. AZ::Vector2 m_spacing;
  110. //! the width and height of child elements in pixels
  111. AZ::Vector2 m_cellSize;
  112. //! the order that the child elements are placed in
  113. UiLayoutInterface::HorizontalOrder m_horizontalOrder;
  114. UiLayoutInterface::VerticalOrder m_verticalOrder;
  115. StartingDirection m_startingDirection;
  116. //! Child alignment
  117. IDraw2d::HAlign m_childHAlignment;
  118. IDraw2d::VAlign m_childVAlignment;
  119. //! The original offsets. Used to get a bounding size that is used to calculate
  120. //! the number of rows or columns that fit within the bounds
  121. UiTransform2dInterface::Offsets m_origOffsets;
  122. bool m_origOffsetsInitialized;
  123. };