UiInteractableComponent.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/UiInteractableBus.h>
  10. #include <LyShine/Bus/UiCanvasUpdateNotificationBus.h>
  11. #include <LyShine/Bus/UiElementBus.h>
  12. #include <LyShine/Bus/UiNavigationBus.h>
  13. #include <LyShine/Bus/UiInteractableActionsBus.h>
  14. #include "UiInteractableState.h"
  15. #include "UiStateActionManager.h"
  16. #include "UiNavigationSettings.h"
  17. #include <AzCore/Component/Component.h>
  18. #include <AzCore/Serialization/SerializeContext.h>
  19. #include <AzCore/Serialization/EditContext.h>
  20. #include <LmbrCentral/Rendering/TextureAsset.h>
  21. // Forward declarations
  22. class ISprite;
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////
  24. class UiInteractableComponent
  25. : public AZ::Component
  26. , public UiInteractableBus::Handler
  27. , public UiCanvasUpdateNotificationBus::Handler
  28. , public UiElementNotificationBus::Handler
  29. , public UiInteractableActionsBus::Handler
  30. {
  31. public: // member functions
  32. // NOTE: We don't use AZ_COMPONENT here because this is not a concrete class
  33. AZ_RTTI(UiInteractableComponent, "{A42EB486-1C89-434C-AD22-A3FC6CEEC46F}", AZ::Component);
  34. UiInteractableComponent();
  35. ~UiInteractableComponent() override;
  36. // UiInteractableInterface
  37. bool CanHandleEvent(AZ::Vector2 point) override;
  38. bool HandlePressed(AZ::Vector2 point, bool& shouldStayActive) override;
  39. bool HandleReleased(AZ::Vector2 point) override;
  40. bool HandleMultiTouchPressed(AZ::Vector2 point, int multiTouchIndex) override;
  41. bool HandleMultiTouchReleased(AZ::Vector2 point, int multiTouchIndex) override;
  42. bool HandleEnterPressed(bool& shouldStayActive) override;
  43. bool HandleEnterReleased() override;
  44. void InputPositionUpdate(AZ::Vector2 point) override;
  45. void MultiTouchPositionUpdate(AZ::Vector2 point, int multiTouchIndex) override;
  46. void LostActiveStatus() override;
  47. void HandleHoverStart() override;
  48. void HandleHoverEnd() override;
  49. void HandleReceivedHoverByNavigatingFromDescendant(AZ::EntityId descendantEntityId) override;
  50. bool IsPressed() override;
  51. bool IsHandlingEvents() override;
  52. void SetIsHandlingEvents(bool isHandlingEvents) override;
  53. bool IsHandlingMultiTouchEvents() override;
  54. void SetIsHandlingMultiTouchEvents(bool isHandlingMultiTouchEvents) override;
  55. bool GetIsAutoActivationEnabled() override;
  56. void SetIsAutoActivationEnabled(bool isEnabled) override;
  57. // ~UiInteractableInterface
  58. // UiCanvasUpdateNotification
  59. void Update(float deltaTime) override;
  60. // ~UiCanvasUpdateNotification
  61. // UiElementNotifications
  62. void OnUiElementFixup(AZ::EntityId canvasEntityId, AZ::EntityId parentEntityId) override;
  63. void OnUiElementAndAncestorsEnabledChanged(bool areElementAndAncestorsEnabled) override;
  64. // ~UiElementNotifications
  65. // UiInteractableActionsInterface
  66. const LyShine::ActionName& GetHoverStartActionName() override;
  67. void SetHoverStartActionName(const LyShine::ActionName& actionName) override;
  68. const LyShine::ActionName& GetHoverEndActionName() override;
  69. void SetHoverEndActionName(const LyShine::ActionName& actionName) override;
  70. const LyShine::ActionName& GetPressedActionName() override;
  71. void SetPressedActionName(const LyShine::ActionName& actionName) override;
  72. const LyShine::ActionName& GetReleasedActionName() override;
  73. void SetReleasedActionName(const LyShine::ActionName& actionName) override;
  74. const LyShine::ActionName& GetOutsideReleasedActionName() const override;
  75. void SetOutsideReleasedActionName(const LyShine::ActionName& actionName) override;
  76. OnActionCallback GetHoverStartActionCallback() override;
  77. void SetHoverStartActionCallback(OnActionCallback onActionCallback) override;
  78. OnActionCallback GetHoverEndActionCallback() override;
  79. void SetHoverEndActionCallback(OnActionCallback onActionCallback) override;
  80. OnActionCallback GetPressedActionCallback() override;
  81. void SetPressedActionCallback(OnActionCallback onActionCallback) override;
  82. OnActionCallback GetReleasedActionCallback() override;
  83. void SetReleasedActionCallback(OnActionCallback onActionCallback) override;
  84. // ~UiInteractableActionsInterface
  85. public: // static member functions
  86. static void Reflect(AZ::ReflectContext* context);
  87. protected: // types
  88. using StateActions = AZStd::vector<UiInteractableStateAction*>;
  89. protected: // member functions
  90. // AZ::Component
  91. void Init() override;
  92. void Activate() override;
  93. void Deactivate() override;
  94. // ~AZ::Component
  95. //! Compute the current Interactable state based on internal state flags
  96. virtual UiInteractableStatesInterface::State ComputeInteractableState();
  97. void OnHoverStateActionsChanged();
  98. void OnPressedStateActionsChanged();
  99. void OnDisabledStateActionsChanged();
  100. void TriggerHoverStartAction();
  101. void TriggerHoverEndAction();
  102. void TriggerPressedAction();
  103. void TriggerReleasedAction(bool releasedOutside = false);
  104. void TriggerReceivedHoverByNavigatingFromDescendantAction(AZ::EntityId descendantEntityId);
  105. virtual bool IsAutoActivationSupported();
  106. protected: // data members
  107. ////////////////////////////////////////////////////////////////////////////////////////////////////
  108. // persistent data
  109. //! Selected/Hover state properties
  110. StateActions m_hoverStateActions;
  111. //! Pressed state properties
  112. StateActions m_pressedStateActions;
  113. //! Disabled state properties
  114. StateActions m_disabledStateActions;
  115. //! Action triggered on hover start
  116. LyShine::ActionName m_hoverStartActionName;
  117. //! Action triggered on hover end
  118. LyShine::ActionName m_hoverEndActionName;
  119. //! Action triggered on pressed
  120. LyShine::ActionName m_pressedActionName;
  121. //! Action triggered on release
  122. LyShine::ActionName m_releasedActionName;
  123. //! Action triggered on release outside
  124. LyShine::ActionName m_outsideReleasedActionName;
  125. //! If true, the interactable automatically becomes active when navigated to via gamepad/keyboard.
  126. //! Otherwise, a key press is needed to put the interactable in an active state
  127. bool m_isAutoActivationEnabled;
  128. ////////////////////////////////////////////////////////////////////////////////////////////////////
  129. // non-persistent data
  130. //! True if this interactable is accepting input (i.e. not in disabled state)
  131. bool m_isHandlingEvents;
  132. //! True if this interactable is handling multi-touch input events
  133. bool m_isHandlingMultiTouchEvents;
  134. //! True if this interactable is being hovered (can be true at the same time as m_isPressed)
  135. bool m_isHover;
  136. //! True if the interactable is in the pressed state (which can be true while dragging)
  137. bool m_isPressed;
  138. //! the viewport position at which the press event occured (only valid if m_isPressed is true)
  139. AZ::Vector2 m_pressedPoint;
  140. //! the multitouch position at which the press event occured (only valid if m_isPressed is true)
  141. int m_pressedMultiTouchIndex;
  142. //! The current interactable state. This is stored so that we can detect state changes.
  143. UiInteractableStatesInterface::State m_state;
  144. //! Callback triggered on hover start
  145. OnActionCallback m_hoverStartActionCallback;
  146. //! Callback triggered on hover end
  147. OnActionCallback m_hoverEndActionCallback;
  148. //! Callback triggered on pressed
  149. OnActionCallback m_pressedActionCallback;
  150. //! Callback triggered on release
  151. OnActionCallback m_releasedActionCallback;
  152. UiStateActionManager m_stateActionManager;
  153. UiNavigationSettings m_navigationSettings;
  154. private: // static member functions
  155. //! Get the interactables that could be valid options for custom navigation from this interactable
  156. static LyShine::EntityArray GetNavigableInteractables(AZ::EntityId sourceEntity);
  157. static bool VersionConverter(AZ::SerializeContext& context,
  158. AZ::SerializeContext::DataElementNode& classElement);
  159. private: // types
  160. using EntityComboBoxVec = AZStd::vector< AZStd::pair< AZ::EntityId, AZStd::string > >;
  161. private: // member functions
  162. //! Methods used for controlling the Edit Context (the properties pane)
  163. EntityComboBoxVec PopulateNavigableEntityList();
  164. bool IsNavigationModeCustom() const;
  165. };