UiScrollBarComponent.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <AzCore/Math/Vector2.h>
  11. #include <LyShine/Bus/UiScrollBarBus.h>
  12. #include <LyShine/Bus/UiScrollerBus.h>
  13. #include <LyShine/Bus/UiInitializationBus.h>
  14. #include <LyShine/Bus/UiScrollableBus.h>
  15. #include <LyShine/Bus/UiTransformBus.h>
  16. #include <LyShine/UiComponentTypes.h>
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////
  18. class UiScrollBarComponent
  19. : public UiInteractableComponent
  20. , public UiScrollBarBus::Handler
  21. , public UiScrollerBus::Handler
  22. , public UiInitializationBus::Handler
  23. , public UiScrollableToScrollerNotificationBus::Handler
  24. , public UiTransformChangeNotificationBus::Handler
  25. {
  26. public: // member functions
  27. AZ_COMPONENT(UiScrollBarComponent, LyShine::UiScrollBarComponentUuid, AZ::Component);
  28. UiScrollBarComponent();
  29. ~UiScrollBarComponent() override;
  30. //UiInteractableComponent
  31. void Update(float deltaTime) override;
  32. //~UiInteractableComponent
  33. // UiScrollBarInterface
  34. float GetHandleSize() override;
  35. void SetHandleSize(float size) override;
  36. float GetMinHandlePixelSize() override;
  37. void SetMinHandlePixelSize(float size) override;
  38. AZ::EntityId GetHandleEntity() override;
  39. void SetHandleEntity(AZ::EntityId entityId) override;
  40. bool IsAutoFadeEnabled() override;
  41. void SetAutoFadeEnabled(bool isAutoFadeEnabled) override;
  42. float GetAutoFadeDelay() override;
  43. void SetAutoFadeDelay(float delay) override;
  44. float GetAutoFadeSpeed() override;
  45. void SetAutoFadeSpeed(float speed) override;
  46. // ~UiScrollBarInterface
  47. // UiScrollerInterface
  48. Orientation GetOrientation() override;
  49. void SetOrientation(Orientation orientation) override;
  50. AZ::EntityId GetScrollableEntity() override;
  51. void SetScrollableEntity(AZ::EntityId entityId) override;
  52. float GetValue() override;
  53. void SetValue(float value) override;
  54. ValueChangeCallback GetValueChangingCallback() override;
  55. void SetValueChangingCallback(ValueChangeCallback onChange) override;
  56. const LyShine::ActionName& GetValueChangingActionName() override;
  57. void SetValueChangingActionName(const LyShine::ActionName& actionName) override;
  58. ValueChangeCallback GetValueChangedCallback() override;
  59. void SetValueChangedCallback(ValueChangeCallback onChange) override;
  60. const LyShine::ActionName& GetValueChangedActionName() override;
  61. void SetValueChangedActionName(const LyShine::ActionName& actionName) override;
  62. // ~UiScrollerInterface
  63. // UiScrollableToScrollerNotifications
  64. void OnValueChangingByScrollable(AZ::Vector2 value) override;
  65. void OnValueChangedByScrollable(AZ::Vector2 value) override;
  66. void OnScrollableParentToContentRatioChanged(AZ::Vector2 parentToContentRatio) override;
  67. // ~UiScrollableToScrollerNotifications
  68. // UiInitializationInterface
  69. void InGamePostActivate() override;
  70. // ~UiInitializationInterface
  71. // UiInteractableInterface
  72. bool HandlePressed(AZ::Vector2 point, bool& shouldStayActive) override;
  73. bool HandleReleased(AZ::Vector2 point) override;
  74. bool HandleEnterPressed(bool& shouldStayActive) override;
  75. bool HandleAutoActivation() override;
  76. bool HandleKeyInputBegan(const AzFramework::InputChannel::Snapshot& inputSnapshot, AzFramework::ModifierKeyMask activeModifierKeys) override;
  77. void InputPositionUpdate(AZ::Vector2 point) override;
  78. bool DoesSupportDragHandOff(AZ::Vector2 startPoint) override;
  79. bool OfferDragHandOff(AZ::EntityId currentActiveInteractable, AZ::Vector2 startPoint, AZ::Vector2 currentPoint, float dragThreshold) override;
  80. void LostActiveStatus() override;
  81. // ~UiInteractableInterface
  82. // UiTransformChangeNotificationBus
  83. void OnCanvasSpaceRectChanged(AZ::EntityId entityId, const UiTransformInterface::Rect& oldRect, const UiTransformInterface::Rect& newRect) override;
  84. // ~UiTransformChangeNotificationBus
  85. protected: // member functions
  86. // AZ::Component
  87. void Activate() override;
  88. void Deactivate() override;
  89. // ~AZ::Component
  90. // UiInteractableComponent
  91. bool IsAutoActivationSupported() override;
  92. // ~UiInteractableComponent
  93. UiInteractableStatesInterface::State ComputeInteractableState() override;
  94. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  95. {
  96. provided.push_back(AZ_CRC("UiInteractableService", 0x1d474c98));
  97. provided.push_back(AZ_CRC("UiNavigationService"));
  98. provided.push_back(AZ_CRC("UiStateActionsService"));
  99. }
  100. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  101. {
  102. incompatible.push_back(AZ_CRC("UiInteractableService", 0x1d474c98));
  103. incompatible.push_back(AZ_CRC("UiNavigationService"));
  104. incompatible.push_back(AZ_CRC("UiStateActionsService"));
  105. }
  106. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  107. {
  108. required.push_back(AZ_CRC("UiElementService", 0x3dca7ad4));
  109. required.push_back(AZ_CRC("UiTransformService", 0x3a838e34));
  110. }
  111. static void Reflect(AZ::ReflectContext* context);
  112. private: // types
  113. enum class LocRelativeToHandle
  114. {
  115. BeforeHandle,
  116. OnHandle,
  117. AfterHandle
  118. };
  119. private: // member functions
  120. AZ_DISABLE_COPY_MOVE(UiScrollBarComponent);
  121. using EntityComboBoxVec = AZStd::vector< AZStd::pair< AZ::EntityId, AZStd::string > >;
  122. EntityComboBoxVec PopulateChildEntityList();
  123. float GetValidDragDistanceInPixels(AZ::Vector2 startPoint, AZ::Vector2 endPoint);
  124. bool CheckForDragOrHandOffToParent(AZ::EntityId currentActiveInteractable, AZ::Vector2 startPoint, AZ::Vector2 currentPoint, float childDragThreshold, bool& handOffDone);
  125. void DoSetValue(float value);
  126. void DoChangedActions();
  127. void DoChangingActions();
  128. void NotifyListenersOnValueChanged();
  129. void NotifyListenersOnValueChanging();
  130. void NotifyScrollableOnValueChanged();
  131. void NotifyScrollableOnValueChanging();
  132. LocRelativeToHandle GetLocationRelativeToHandle(const AZ::Vector2& point);
  133. float GetHandleParentLength();
  134. float GetPosAlongAxis(AZ::Vector2 point);
  135. bool MoveHandle(LocRelativeToHandle pointLoc);
  136. void ResetDragInfo();
  137. void SetImageComponentsAlpha(float fade);
  138. void ResetFade();
  139. private: // data
  140. float m_value;
  141. float m_handleSize;
  142. float m_minHandlePixelSize;
  143. Orientation m_orientation;
  144. float m_displayedHandleSize;
  145. bool m_isDragging;
  146. bool m_isActive; // true when interactable can be manipulated by key input
  147. float m_pressedValue;
  148. float m_pressedPosAlongAxis;
  149. bool m_pressedOnHandle;
  150. float m_lastMoveTime;
  151. float m_moveDelayTime;
  152. bool m_isAutoFadeEnabled = false;
  153. bool m_isFading = false;
  154. float m_fadeSpeed = 1.0f;
  155. float m_inactiveSecondsBeforeFade = 1.0f;
  156. float m_secondsRemainingBeforeFade = 1.0f;
  157. float m_initialScrollBarAlpha = 1.0f;
  158. float m_initialHandleAlpha = 1.0f;
  159. float m_currFade = 1.0f;
  160. AZ::Vector2 m_lastDragPoint; // the point of the last drag
  161. ValueChangeCallback m_onValueChanged;
  162. ValueChangeCallback m_onValueChanging;
  163. LyShine::ActionName m_valueChangedActionName;
  164. LyShine::ActionName m_valueChangingActionName;
  165. AZ::EntityId m_handleEntity;
  166. AZ::EntityId m_scrollableEntity;
  167. };