UiDropdownComponent.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/UiDropdownBus.h>
  11. #include <LyShine/Bus/UiInitializationBus.h>
  12. #include <LyShine/Bus/UiInteractableBus.h>
  13. #include <LyShine/Bus/UiCanvasBus.h>
  14. #include <LyShine/UiComponentTypes.h>
  15. #include <AzCore/Component/TickBus.h>
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////
  17. class UiDropdownComponent
  18. : public UiInteractableComponent
  19. , public UiDropdownBus::Handler
  20. , public UiInitializationBus::Handler
  21. , public UiCanvasInputNotificationBus::Handler
  22. , public UiInteractableNotificationBus::MultiHandler
  23. , public AZ::TickBus::Handler
  24. {
  25. public: // types
  26. //! These are the visual states
  27. enum
  28. {
  29. DropdownStateExpanded = UiInteractableStatesInterface::NumStates,
  30. };
  31. public: // member functions
  32. AZ_COMPONENT(UiDropdownComponent, LyShine::UiDropdownComponentUuid, AZ::Component);
  33. UiDropdownComponent();
  34. ~UiDropdownComponent() override;
  35. // UiDropdownInterface
  36. AZ::EntityId GetValue() override;
  37. void SetValue(AZ::EntityId value) override;
  38. AZ::EntityId GetContent() override;
  39. void SetContent(AZ::EntityId content) override;
  40. bool GetExpandOnHover() override;
  41. void SetExpandOnHover(bool expandOnHover) override;
  42. float GetWaitTime() override;
  43. void SetWaitTime(float waitTime) override;
  44. bool GetCollapseOnOutsideClick() override;
  45. void SetCollapseOnOutsideClick(bool collapseOnOutsideClick) override;
  46. AZ::EntityId GetExpandedParentId() override;
  47. void SetExpandedParentId(AZ::EntityId expandedParentId) override;
  48. AZ::EntityId GetTextElement() override;
  49. void SetTextElement(AZ::EntityId textElement) override;
  50. AZ::EntityId GetIconElement() override;
  51. void SetIconElement(AZ::EntityId iconElement) override;
  52. void Expand() override;
  53. void Collapse() override;
  54. const LyShine::ActionName& GetExpandedActionName() override;
  55. void SetExpandedActionName(const LyShine::ActionName& actionName) override;
  56. const LyShine::ActionName& GetCollapsedActionName() override;
  57. void SetCollapsedActionName(const LyShine::ActionName& actionName) override;
  58. const LyShine::ActionName& GetOptionSelectedActionName() override;
  59. void SetOptionSelectedActionName(const LyShine::ActionName& actionName) override;
  60. // ~UiDropdownInterface
  61. // UiInitializationInterface
  62. void InGamePostActivate() override;
  63. // ~UiInitializationInterface
  64. // UiInteractableInterface
  65. bool HandleReleased(AZ::Vector2 point) override;
  66. bool HandleEnterReleased() override;
  67. void HandleHoverStart() override;
  68. void HandleHoverEnd() override;
  69. // ~UiInteractableInterface
  70. // UiInteractableNotifications
  71. void OnReceivedHoverByNavigatingFromDescendant(AZ::EntityId descendantEntityId) override;
  72. // ~UiInteractableNotifications
  73. // UiCanvasInputNotificationBus
  74. void OnCanvasPrimaryReleased(AZ::EntityId entityId) override;
  75. void OnCanvasEnterReleased(AZ::EntityId entityId) override;
  76. void OnCanvasHoverStart(AZ::EntityId entityId) override;
  77. void OnCanvasHoverEnd(AZ::EntityId entityId) override;
  78. // ~UiCanvasInputNotificationBus
  79. // TickBus
  80. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  81. // ~TickBus
  82. protected: // member functions
  83. // AZ::Component
  84. void Activate() override;
  85. void Deactivate() override;
  86. // ~AZ::Component
  87. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  88. {
  89. provided.push_back(AZ_CRC("UiInteractableService", 0x1d474c98));
  90. provided.push_back(AZ_CRC("UiStateActionsService"));
  91. provided.push_back(AZ_CRC("UiNavigationService"));
  92. }
  93. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  94. {
  95. incompatible.push_back(AZ_CRC("UiInteractableService", 0x1d474c98));
  96. incompatible.push_back(AZ_CRC("UiNavigationService"));
  97. incompatible.push_back(AZ_CRC("UiStateActionsService"));
  98. }
  99. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  100. {
  101. required.push_back(AZ_CRC("UiElementService", 0x3dca7ad4));
  102. required.push_back(AZ_CRC("UiTransformService", 0x3a838e34));
  103. }
  104. static void Reflect(AZ::ReflectContext* context);
  105. private: // member functions
  106. AZ_DISABLE_COPY_MOVE(UiDropdownComponent);
  107. using EntityComboBoxVec = AZStd::vector< AZStd::pair< AZ::EntityId, AZStd::string > >;
  108. EntityComboBoxVec PopulateChildEntityList();
  109. void OnExpandedStateActionsChanged();
  110. void Expand(bool transferHover);
  111. void Collapse(bool transferHover);
  112. bool HandleReleasedCommon(const AZ::Vector2& point);
  113. void HandleCanvasReleasedCommon(AZ::EntityId entityId, bool positionalInput);
  114. void TransferHoverToDescendant();
  115. AZ::EntityId FindFirstDescendantInteractable(AZ::EntityId parentEntityId);
  116. AZ::EntityId CreateContentParentInteractable();
  117. bool ContentIsAncestor(AZ::EntityId entityId);
  118. bool ContentIsAncestor(AZ::EntityId entityId, AZ::EntityId contentId);
  119. AZ::Outcome<void, AZStd::string> ValidateTypeIsEntityId(const AZ::Uuid& valueType);
  120. AZ::Outcome<void, AZStd::string> ValidatePotentialContent(void* newValue, const AZ::Uuid& valueType);
  121. AZ::Outcome<void, AZStd::string> ValidatePotentialExpandedParent(void* newValue, const AZ::Uuid& valueType);
  122. bool IsNavigationSupported();
  123. // UiInteractableInterface
  124. UiInteractableStatesInterface::State ComputeInteractableState() override;
  125. // ~UiInteractableInterface
  126. private: // data
  127. AZ::EntityId m_value;
  128. AZ::EntityId m_content;
  129. bool m_expandOnHover;
  130. float m_waitTime;
  131. bool m_collapseOnOutsideClick;
  132. AZ::EntityId m_expandedParentId;
  133. AZ::EntityId m_textElement;
  134. AZ::EntityId m_iconElement;
  135. StateActions m_expandedStateActions;
  136. LyShine::ActionName m_expandedActionName;
  137. LyShine::ActionName m_collapsedActionName;
  138. LyShine::ActionName m_optionSelectedActionName;
  139. bool m_expanded;
  140. AZ::EntityId m_canvasEntityId;
  141. float m_delayTimer;
  142. AZ::EntityId m_baseParent;
  143. LyShine::EntityArray m_submenus;
  144. bool m_expandedByClick;
  145. // An interactable that is created when the dropdown is expanded to act as the parent
  146. // of the content element. The content element needs a parent interactable in order to constrain
  147. // navigation between the content's descendant iteractables. Since the content element is
  148. // reparented from the dropdown interactable when expanded, this temporary interactable takes
  149. // place as the parent interactable.
  150. AZ::EntityId m_tempContentParentInteractable;
  151. };