UiScrollBoxComponent.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 "UiInteractableComponent.h"
  10. #include <LyShine/Bus/UiScrollBoxBus.h>
  11. #include <LyShine/Bus/UiScrollableBus.h>
  12. #include <LyShine/Bus/UiInitializationBus.h>
  13. #include <LyShine/Bus/UiInteractableBus.h>
  14. #include <LyShine/Bus/UiScrollerBus.h>
  15. #include <LyShine/Bus/UiTransform2dBus.h>
  16. #include <LyShine/Bus/UiTransformBus.h>
  17. #include <LyShine/UiComponentTypes.h>
  18. #include <AzCore/Serialization/SerializeContext.h>
  19. #include <AzCore/Math/Vector3.h>
  20. #include <LmbrCentral/Rendering/TextureAsset.h>
  21. #include "UiNavigationHelpers.h"
  22. #include <AzCore/Component/TickBus.h>
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////
  24. class UiScrollBoxComponent
  25. : public UiInteractableComponent
  26. , public UiScrollBoxBus::Handler
  27. , public UiScrollableBus::Handler
  28. , public UiInitializationBus::Handler
  29. , public UiScrollerToScrollableNotificationBus::MultiHandler
  30. , public UiTransformChangeNotificationBus::MultiHandler
  31. , public AZ::TickBus::Handler
  32. {
  33. public: // member functions
  34. AZ_COMPONENT(UiScrollBoxComponent, LyShine::UiScrollBoxComponentUuid, AZ::Component);
  35. UiScrollBoxComponent();
  36. ~UiScrollBoxComponent() override;
  37. // UiScrollBoxInterface
  38. AZ::Vector2 GetScrollOffset() override;
  39. void SetScrollOffset(AZ::Vector2 scrollOffset) override;
  40. AZ::Vector2 GetNormalizedScrollValue() override;
  41. void ChangeContentSizeAndScrollOffset(AZ::Vector2 contentSize, AZ::Vector2 scrollOffset) override;
  42. bool HasHorizontalContentToScroll() override;
  43. bool HasVerticalContentToScroll() override;
  44. virtual bool GetIsHorizontalScrollingEnabled() override;
  45. virtual void SetIsHorizontalScrollingEnabled(bool isEnabled) override;
  46. virtual bool GetIsVerticalScrollingEnabled() override;
  47. virtual void SetIsVerticalScrollingEnabled(bool isEnabled) override;
  48. virtual bool GetIsScrollingConstrained() override;
  49. virtual void SetIsScrollingConstrained(bool isConstrained) override;
  50. virtual SnapMode GetSnapMode() override;
  51. virtual void SetSnapMode(SnapMode snapMode) override;
  52. virtual AZ::Vector2 GetSnapGrid() override;
  53. virtual void SetSnapGrid(AZ::Vector2 snapGrid) override;
  54. virtual ScrollBarVisibility GetHorizontalScrollBarVisibility() override;
  55. virtual void SetHorizontalScrollBarVisibility(ScrollBarVisibility visibility) override;
  56. virtual ScrollBarVisibility GetVerticalScrollBarVisibility() override;
  57. virtual void SetVerticalScrollBarVisibility(ScrollBarVisibility visibility) override;
  58. virtual AZ::Vector2 GetScrollSensitivity() override;
  59. virtual void SetScrollSensitivity(AZ::Vector2 scrollSensitivity) override;
  60. virtual float GetMomentumDuration() override;
  61. virtual void SetMomentumDuration(float scrollMomentumDuration) override;
  62. virtual void SetMomentumActive(bool active) override;
  63. virtual void StopMomentum() override;
  64. ScrollOffsetChangeCallback GetScrollOffsetChangingCallback() override;
  65. void SetScrollOffsetChangingCallback(ScrollOffsetChangeCallback onChange) override;
  66. const LyShine::ActionName& GetScrollOffsetChangingActionName() override;
  67. void SetScrollOffsetChangingActionName(const LyShine::ActionName& actionName) override;
  68. ScrollOffsetChangeCallback GetScrollOffsetChangedCallback() override;
  69. void SetScrollOffsetChangedCallback(ScrollOffsetChangeCallback onChange) override;
  70. const LyShine::ActionName& GetScrollOffsetChangedActionName() override;
  71. void SetScrollOffsetChangedActionName(const LyShine::ActionName& actionName) override;
  72. void SetContentEntity(AZ::EntityId entityId) override;
  73. AZ::EntityId GetContentEntity() override;
  74. void SetHorizontalScrollBarEntity(AZ::EntityId entityId) override;
  75. AZ::EntityId GetHorizontalScrollBarEntity() override;
  76. void SetVerticalScrollBarEntity(AZ::EntityId entityId) override;
  77. AZ::EntityId GetVerticalScrollBarEntity() override;
  78. AZ::EntityId FindClosestContentChildElement() override;
  79. AZ::EntityId FindNextContentChildElement(UiNavigationHelpers::Command command);
  80. // ~UiScrollBoxInterface
  81. // UiScrollableInterface
  82. bool GetScrollableParentToContentRatio(AZ::Vector2& ratioOut) override;
  83. // ~UiScrollableInterface
  84. // UiScrollerToScrollableNotifications
  85. void OnValueChangingByScroller(float value) override;
  86. void OnValueChangedByScroller(float value) override;
  87. // ~UiScrollerToScrollableNotifications
  88. // UiInitializationInterface
  89. void InGamePostActivate() override;
  90. // ~UiInitializationInterface
  91. // UiInteractableInterface
  92. bool HandlePressed(AZ::Vector2 point, bool& shouldStayActive) override;
  93. bool HandleReleased(AZ::Vector2 point) override;
  94. bool HandleEnterPressed(bool& shouldStayActive) override;
  95. bool HandleAutoActivation() override;
  96. bool HandleKeyInputBegan(const AzFramework::InputChannel::Snapshot& inputSnapshot, AzFramework::ModifierKeyMask activeModifierKeys) override;
  97. void InputPositionUpdate(AZ::Vector2 point) override;
  98. bool DoesSupportDragHandOff(AZ::Vector2 startPoint) override;
  99. bool OfferDragHandOff(AZ::EntityId currentActiveInteractable, AZ::Vector2 startPoint, AZ::Vector2 currentPoint, float dragThreshold) override;
  100. void LostActiveStatus() override;
  101. void HandleDescendantReceivedHoverByNavigation(AZ::EntityId descendantEntityId) override;
  102. // ~UiInteractableInterface
  103. // UiTransformChangeNotification
  104. void OnCanvasSpaceRectChanged(AZ::EntityId entityId, const UiTransformInterface::Rect& oldRect, const UiTransformInterface::Rect& newRect) override;
  105. // ~UiTransformChangeNotification
  106. // TickBus
  107. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  108. // ~TickBus
  109. protected: // member functions
  110. // AZ::Component
  111. void Activate() override;
  112. void Deactivate() override;
  113. // ~AZ::Component
  114. // UiInteractableComponent
  115. bool IsAutoActivationSupported() override;
  116. // ~UiInteractableComponent
  117. UiInteractableStatesInterface::State ComputeInteractableState() override;
  118. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  119. {
  120. provided.push_back(AZ_CRC("UiInteractableService", 0x1d474c98));
  121. provided.push_back(AZ_CRC("UiNavigationService"));
  122. provided.push_back(AZ_CRC("UiStateActionsService"));
  123. provided.push_back(AZ_CRC("UiScrollBoxService", 0xfdafc904));
  124. }
  125. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  126. {
  127. incompatible.push_back(AZ_CRC("UiInteractableService", 0x1d474c98));
  128. incompatible.push_back(AZ_CRC("UiNavigationService"));
  129. incompatible.push_back(AZ_CRC("UiStateActionsService"));
  130. }
  131. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  132. {
  133. required.push_back(AZ_CRC("UiElementService", 0x3dca7ad4));
  134. required.push_back(AZ_CRC("UiTransformService", 0x3a838e34));
  135. }
  136. static void Reflect(AZ::ReflectContext* context);
  137. private: // types
  138. using EntityComboBoxVec = AZStd::vector< AZStd::pair< AZ::EntityId, AZStd::string > >;
  139. private: // member functions
  140. AZ_DISABLE_COPY_MOVE(UiScrollBoxComponent);
  141. //! Methods used for controlling the Edit Context (the properties pane)
  142. EntityComboBoxVec PopulateChildEntityList();
  143. EntityComboBoxVec PopulateHScrollBarEntityList();
  144. EntityComboBoxVec PopulateVScrollBarEntityList();
  145. EntityComboBoxVec PopulateScrollBarEntityList(UiScrollerInterface::Orientation orientation);
  146. bool IsSnapToGrid() const;
  147. //! Given a proposed scroll offset, adjust it so that the area outside
  148. //! the content rectangle cannot be seen in its parent rectangle.
  149. //! I.e. prevent scrolling beyond the edges of the content
  150. AZ::Vector2 ConstrainOffset(AZ::Vector2 proposedOffset, AZ::Entity* contentParentEntity);
  151. //! Snap m_scrollOffset according to the snap mode
  152. bool DoSnap();
  153. //! Compute the offset from the content anchors to the child's pivot
  154. //! using the current scroll offset
  155. AZ::Vector2 ComputeCurrentOffsetToChild(AZ::EntityId child);
  156. //! Compute the offset of the current scroll offset from the closet
  157. //! snap grid point
  158. AZ::Vector2 ComputeCurrentOffsetFromGrid();
  159. //! Helper function to return the position of the content element's
  160. //! anchors in canvas space. The scroll offset is always relative to this
  161. //! point.
  162. AZ::Vector2 ComputeContentAnchorCenterInCanvasSpace() const;
  163. //! Helper function to calculate how far a float value is from a grid
  164. float ComputeOffsetOfValueFromGrid(float value, float gridStep);
  165. //! Get the drag distance along valid axes
  166. float GetValidDragDistanceInPixels(AZ::Vector2 startPoint, AZ::Vector2 endPoint);
  167. //! Given the latest input point, potentially initiate a drag or hand one off to a parent
  168. void CheckForDragOrHandOffToParent(AZ::Vector2 point);
  169. //! Set scroll offset value and update content's offsets
  170. void DoSetScrollOffset(AZ::Vector2 scrollOffset);
  171. //! Notify listeners, callbacks and canvas actions of a scroll offset and value change
  172. void DoChangedActions();
  173. void DoChangingActions();
  174. //! Notify scrollers that the scrollable caused the scroll value to change. Sends value (0 - 1)
  175. void NotifyScrollersOnValueChanged();
  176. void NotifyScrollersOnValueChanging();
  177. //! Notify listeners of a scroll value change. Sends value (0 - 1)
  178. void NotifyListenersOnScrollValueChanged();
  179. void NotifyListenersOnScrollValueChanging();
  180. //! Notify listeners of a scroll offset change. Sends new scroll offset
  181. void NotifyListenersOnScrollOffsetChanged();
  182. void NotifyListenersOnScrollOffsetChanging();
  183. //! Get the axis aligned rect of the content element
  184. UiTransformInterface::Rect GetAxisAlignedContentRect();
  185. //! Helper functions to convert between scroller and scrollable values
  186. bool ScrollOffsetToNormalizedScrollValue(AZ::Vector2 scrollOffset, AZ::Vector2& normalizedScrollValueOut);
  187. bool NormalizedScrollValueToScrollOffset(UiScrollerInterface::Orientation orientation, float normalizedScrollValue, float& scrollOffsetOut);
  188. bool ScrollerValueToScrollOffsets(AZ::EntityId scroller, float scrollerValue, AZ::Vector2& scrollOffsetsOut);
  189. //! Check which side the scrollbar is on
  190. bool IsVerticalScrollBarOnRight();
  191. bool IsHorizontalScrollBarOnBottom();
  192. // Set scrollbar visibility based on whether there is scrollable content
  193. void UpdateScrollBarVisiblity();
  194. // Set scrollbar anchors and offsets based on the other scrollbar's visibility
  195. void UpdateScrollBarAnchorsAndOffsets();
  196. // Set content parent (viewport) offsets based on scrollbar visibility. The content's parent (the viewport)
  197. // is shrunk when scrollbars are visible and expanded when scrollbars are not visible.
  198. // If checkScrollBarVisibility is false, the offsets are set as if the scrollbars are not visible
  199. void UpdateContentParentOffsets(bool checkScrollBarVisibility);
  200. // Setup based on the size of the content and its parent
  201. void ContentOrParentSizeChanged();
  202. private: // static member functions
  203. static bool VersionConverter(AZ::SerializeContext& context,
  204. AZ::SerializeContext::DataElementNode& classElement);
  205. private: // data
  206. //! The scroll offset is the distance from the content elements anchors to its pivot
  207. //! It is initialized from a property but changes at runtime as the element is scrolled
  208. AZ::Vector2 m_scrollOffset;
  209. //! Property values
  210. bool m_isHorizontalScrollingEnabled;
  211. bool m_isVerticalScrollingEnabled;
  212. bool m_isScrollingConstrained;
  213. SnapMode m_snapMode;
  214. AZ::Vector2 m_snapGrid;
  215. AZ::EntityId m_contentEntity;
  216. AZ::EntityId m_hScrollBarEntity;
  217. AZ::EntityId m_vScrollBarEntity;
  218. ScrollBarVisibility m_hScrollBarVisibility;
  219. ScrollBarVisibility m_vScrollBarVisibility;
  220. ScrollOffsetChangeCallback m_onScrollOffsetChanged;
  221. ScrollOffsetChangeCallback m_onScrollOffsetChanging;
  222. LyShine::ActionName m_scrollOffsetChangedActionName;
  223. LyShine::ActionName m_scrollOffsetChangingActionName;
  224. //! Interactable state
  225. bool m_isDragging;
  226. bool m_isActive; // true when interactable can be manipulated by key input
  227. AZ::Vector2 m_pressedScrollOffset; // the original value of scrollOffset when the press occurred
  228. AZ::Vector2 m_lastDragPoint; // the point of the last drag
  229. AZ::Vector2 m_scrollSensitivity = AZ::Vector2::CreateZero(); // Vector2(horizontal, vertical) factor applied to the dragging vector to adjust scroll speed
  230. AZ::Vector2 m_lastOffsetChange = AZ::Vector2::CreateZero(); // Last instant offset change
  231. AZ::Vector2 m_offsetChangeAccumulator = AZ::Vector2::CreateZero();
  232. float m_stoppingTimeAccumulator = 0.f;
  233. float m_draggingTimeAccumulator = 0.f;
  234. bool m_momentumIsActive = false;
  235. float m_momentumDuration = 0.f; // Time in seconds for which we keep scrolling after release
  236. float m_momentumTimeAccumulator = 0.f;
  237. const float MIN_OFFSET_THRESHOLD = 10.0f;
  238. const float MAX_STOPPING_DELAY = 0.12f;
  239. };