UiLayoutColumnComponent.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/UiLayoutColumnBus.h>
  12. #include <LyShine/Bus/UiLayoutCellDefaultBus.h>
  13. #include <LyShine/UiComponentTypes.h>
  14. #include <AzCore/Component/Component.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <UiLayoutHelpers.h>
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////
  18. //! This component overrides the transforms of immediate children to organize them into a horizontal row
  19. class UiLayoutColumnComponent
  20. : public AZ::Component
  21. , public UiLayoutControllerBus::Handler
  22. , public UiLayoutBus::Handler
  23. , public UiLayoutColumnBus::Handler
  24. , public UiLayoutCellDefaultBus::Handler
  25. , public UiTransformChangeNotificationBus::Handler
  26. {
  27. public: // member functions
  28. AZ_COMPONENT(UiLayoutColumnComponent, LyShine::UiLayoutColumnComponentUuid, AZ::Component);
  29. UiLayoutColumnComponent();
  30. ~UiLayoutColumnComponent() override;
  31. // UiLayoutControllerInterface
  32. virtual void ApplyLayoutWidth() override;
  33. virtual void ApplyLayoutHeight() override;
  34. // ~UiLayoutControllerInterface
  35. // UiLayoutInterface
  36. virtual bool IsUsingLayoutCellsToCalculateLayout() override;
  37. virtual bool GetIgnoreDefaultLayoutCells() override;
  38. virtual void SetIgnoreDefaultLayoutCells(bool ignoreDefaultLayoutCells) override;
  39. virtual IDraw2d::HAlign GetHorizontalChildAlignment() override;
  40. virtual void SetHorizontalChildAlignment(IDraw2d::HAlign alignment) override;
  41. virtual IDraw2d::VAlign GetVerticalChildAlignment() override;
  42. virtual void SetVerticalChildAlignment(IDraw2d::VAlign alignment) override;
  43. virtual bool IsControllingChild(AZ::EntityId childId) override;
  44. virtual AZ::Vector2 GetSizeToFitChildElements(const AZ::Vector2& childElementSize, int numChildElements) override;
  45. // ~UiLayoutInterface
  46. // UiLayoutColumnInterface
  47. virtual UiLayoutInterface::Padding GetPadding() override;
  48. virtual void SetPadding(UiLayoutInterface::Padding padding) override;
  49. virtual float GetSpacing() override;
  50. virtual void SetSpacing(float spacing) override;
  51. virtual UiLayoutInterface::VerticalOrder GetOrder() override;
  52. virtual void SetOrder(UiLayoutInterface::VerticalOrder order) override;
  53. // ~UiLayoutColumnInterface
  54. // UiLayoutCellDefaultInterface
  55. float GetMinWidth() override;
  56. float GetMinHeight() override;
  57. float GetTargetWidth(float maxWidth) override;
  58. float GetTargetHeight(float maxHeight) override;
  59. float GetExtraWidthRatio() override;
  60. float GetExtraHeightRatio() override;
  61. // ~UiLayoutCellDefaultInterface
  62. // UiTransformChangeNotificationBus
  63. void OnCanvasSpaceRectChanged(AZ::EntityId entityId, const UiTransformInterface::Rect& oldRect, const UiTransformInterface::Rect& newRect) override;
  64. // ~UiTransformChangeNotificationBus
  65. public: // static member functions
  66. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  67. {
  68. provided.push_back(AZ_CRC("UiLayoutService", 0xac613bbc));
  69. }
  70. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  71. {
  72. incompatible.push_back(AZ_CRC("UiLayoutService", 0xac613bbc));
  73. }
  74. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  75. {
  76. required.push_back(AZ_CRC("UiElementService", 0x3dca7ad4));
  77. required.push_back(AZ_CRC("UiTransformService", 0x3a838e34));
  78. }
  79. static void Reflect(AZ::ReflectContext* context);
  80. protected: // member functions
  81. // AZ::Component
  82. void Activate() override;
  83. void Deactivate() override;
  84. // ~AZ::Component
  85. AZ_DISABLE_COPY_MOVE(UiLayoutColumnComponent);
  86. //! Called on a property change that has caused this element's layout to be invalid
  87. void InvalidateLayout();
  88. //! Called when a property that is used to calculate default layout cell values has changed
  89. void InvalidateParentLayout();
  90. //! Refresh the transform properties in the editor's properties pane
  91. void CheckLayoutFitterAndRefreshEditorTransformProperties() const;
  92. //! Helper functions to set the child elements' transform properties.
  93. //! Element widths are calculated first since layout cell height
  94. //! properties can depend on element widths
  95. void ApplyLayoutWidth(float availableWidth);
  96. void ApplyLayoutHeight(float availableHeight);
  97. protected: // data
  98. //! the padding (in pixels) inside the edges of this element
  99. UiLayoutInterface::Padding m_padding;
  100. //! the spacing (in pixels) between child elements
  101. float m_spacing;
  102. //! the order that the child elements are placed in
  103. UiLayoutInterface::VerticalOrder m_order;
  104. //! Child alignment
  105. IDraw2d::HAlign m_childHAlignment;
  106. IDraw2d::VAlign m_childVAlignment;
  107. //! Whether the layout is to ignore the children's default layout cell values
  108. bool m_ignoreDefaultLayoutCells;
  109. private: // static member functions
  110. static bool VersionConverter(AZ::SerializeContext& context,
  111. AZ::SerializeContext::DataElementNode& classElement);
  112. };