VirtualGamepadThumbStickComponent.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "VirtualGamepadThumbStickRequestBus.h"
  10. #include <LyShine/Bus/UiInteractableBus.h>
  11. #include <AzCore/Component/Component.h>
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. namespace VirtualGamepad
  14. {
  15. ////////////////////////////////////////////////////////////////////////////////////////////////
  16. class VirtualGamepadThumbStickComponent : public AZ::Component
  17. , public UiInteractableBus::Handler
  18. , public VirtualGamepadThumbStickRequestBus::Handler
  19. {
  20. public:
  21. ////////////////////////////////////////////////////////////////////////////////////////////
  22. // AZ::Component Setup
  23. AZ_COMPONENT(VirtualGamepadThumbStickComponent, "{F3B59A92-BD6F-9CEC-A751-2EBC699992C5}", AZ::Component);
  24. ////////////////////////////////////////////////////////////////////////////////////////////
  25. // AZ::ComponentDescriptor Services
  26. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  27. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  28. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  29. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  30. ////////////////////////////////////////////////////////////////////////////////////////////
  31. //! \ref AZ::ComponentDescriptor::Reflect
  32. static void Reflect(AZ::ReflectContext* context);
  33. protected:
  34. ////////////////////////////////////////////////////////////////////////////////////////////
  35. //! \ref AZ::Component::Init
  36. void Init() override;
  37. ////////////////////////////////////////////////////////////////////////////////////////////
  38. //! \ref AZ::Component::Activate
  39. void Activate() override;
  40. ////////////////////////////////////////////////////////////////////////////////////////////
  41. //! \ref AZ::Component::Deactivate
  42. void Deactivate() override;
  43. ////////////////////////////////////////////////////////////////////////////////////////////
  44. //! \ref UiInteractableInterface::CanHandleEvent
  45. bool CanHandleEvent(AZ::Vector2 point) override;
  46. ////////////////////////////////////////////////////////////////////////////////////////////
  47. //! \ref UiInteractableInterface::HandlePressed
  48. bool HandlePressed(AZ::Vector2 point, bool& shouldStayActive) override;
  49. ////////////////////////////////////////////////////////////////////////////////////////////
  50. //! \ref UiInteractableInterface::HandleReleased
  51. bool HandleReleased(AZ::Vector2 point) override;
  52. ////////////////////////////////////////////////////////////////////////////////////////////
  53. //! \ref UiInteractableInterface::HandleMultiTouchPressed
  54. bool HandleMultiTouchPressed(AZ::Vector2 point, int multiTouchIndex) override;
  55. ////////////////////////////////////////////////////////////////////////////////////////////
  56. //! \ref UiInteractableInterface::HandleMultiTouchReleased
  57. bool HandleMultiTouchReleased(AZ::Vector2 point, int multiTouchIndex) override;
  58. ////////////////////////////////////////////////////////////////////////////////////////////
  59. //! \ref UiInteractableInterface::InputPositionUpdate
  60. void InputPositionUpdate(AZ::Vector2 point) override;
  61. ////////////////////////////////////////////////////////////////////////////////////////////
  62. //! \ref UiInteractableInterface::MultiTouchPositionUpdate
  63. void MultiTouchPositionUpdate(AZ::Vector2 point, int multiTouchIndex) override;
  64. ////////////////////////////////////////////////////////////////////////////////////////////
  65. //! \ref UiInteractableInterface::HandleHoverStart
  66. void HandleHoverStart() override {}
  67. ////////////////////////////////////////////////////////////////////////////////////////////
  68. //! \ref UiInteractableInterface::HandleHoverEnd
  69. void HandleHoverEnd() override {}
  70. ////////////////////////////////////////////////////////////////////////////////////////////
  71. //! \ref UiInteractableInterface::GetIsAutoActivationEnabled
  72. bool GetIsAutoActivationEnabled() override { return false; }
  73. ////////////////////////////////////////////////////////////////////////////////////////////
  74. //! \ref UiInteractableInterface::SetIsAutoActivationEnabled
  75. void SetIsAutoActivationEnabled(bool) override {}
  76. ////////////////////////////////////////////////////////////////////////////////////////////
  77. //! \ref VirtualGamepad::VirtualGamepadThumbStickRequests::GetCurrentAxisValuesNormalized
  78. AZ::Vector2 GetCurrentAxisValuesNormalized() const override;
  79. ////////////////////////////////////////////////////////////////////////////////////////////
  80. //! Called when any touch is pressed
  81. //! \param[in] viewportPositionPixels The viewport position of the touch in pixels
  82. //! \param[in] touchIndex The touch index (0 based)
  83. //! \return True if the touch was handled, false otherwise
  84. bool OnAnyTouchPressed(AZ::Vector2 viewportPositionPixels, int touchIndex);
  85. ////////////////////////////////////////////////////////////////////////////////////////////
  86. //! Called when any touch is released
  87. //! \param[in] viewportPositionPixels The viewport position of the touch in pixels
  88. //! \param[in] touchIndex The touch index (0 based)
  89. //! \return True if the touch was handled, false otherwise
  90. bool OnAnyTouchReleased(AZ::Vector2 viewportPositionPixels, int touchIndex);
  91. ////////////////////////////////////////////////////////////////////////////////////////////
  92. //! Called when any touch position is updated
  93. //! \param[in] viewportPositionPixels The viewport position of the touch in pixels
  94. //! \param[in] touchIndex The touch index (0 based)
  95. void OnAnyTouchPositionUpdate(AZ::Vector2 viewportPositionPixels, int touchIndex);
  96. ////////////////////////////////////////////////////////////////////////////////////////////
  97. //! Get all potentially assignable input channel names
  98. AZStd::vector<AZStd::string> GetAssignableInputChannelNames() const;
  99. ////////////////////////////////////////////////////////////////////////////////////////////
  100. //! Get all child entity id/name pairs
  101. AZStd::vector<AZStd::pair<AZ::EntityId, AZStd::string>> GetChildEntityIdNamePairs() const;
  102. private:
  103. ////////////////////////////////////////////////////////////////////////////////////////////
  104. //! The input channel that will be updated when the user interacts with this virtual control
  105. AZStd::string m_assignedInputChannelName;
  106. ////////////////////////////////////////////////////////////////////////////////////////////
  107. //! The ui element that will be drawn at the centre of the virtual thumb-stick while active
  108. AZ::EntityId m_thumbStickImageCentre;
  109. ////////////////////////////////////////////////////////////////////////////////////////////
  110. //! The ui element that will be drawn at the radius of the virtual thumb-stick while active
  111. AZ::EntityId m_thumbStickImageRadial;
  112. ////////////////////////////////////////////////////////////////////////////////////////////
  113. //! The default viewport position of the virtual thumb-stick in pixels
  114. AZ::Vector2 m_defaultViewportPositionPixels;
  115. ////////////////////////////////////////////////////////////////////////////////////////////
  116. //! The current viewport position of the virtual thumb-stick in pixels
  117. AZ::Vector2 m_currentViewportPositionPixels;
  118. ////////////////////////////////////////////////////////////////////////////////////////////
  119. //! The current virtual thumb-stick axis values normalized
  120. AZ::Vector2 m_currentAxisValuesNormalized;
  121. ////////////////////////////////////////////////////////////////////////////////////////////
  122. //! The pixel radius of the virtual thumb-stick in pixels
  123. float m_thumbStickPixelRadius;
  124. ////////////////////////////////////////////////////////////////////////////////////////////
  125. //! The index of the currently active touch index
  126. int m_activeTouchIndex;
  127. ////////////////////////////////////////////////////////////////////////////////////////////
  128. //! Whether or not to centre the virtual thumb-stick when it is pressed
  129. bool m_centreWhenPressed = true;
  130. ////////////////////////////////////////////////////////////////////////////////////////////
  131. //! Whether or not to adjust the position of the virtual thumb-stick while it is pressed, so
  132. //! that the pressed finger will always remain within the radius of the thumb-stick image.
  133. bool m_adjustPositionWhilePressed = true;
  134. };
  135. } // namespace VirtualGamepad