VirtualGamepadThumbStickComponent.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include "VirtualGamepadThumbStickComponent.h"
  9. #include <VirtualGamepad/VirtualGamepadBus.h>
  10. #include <LyShine/Bus/UiElementBus.h>
  11. #include <LyShine/Bus/UiTransformBus.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/std/sort.h>
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. namespace VirtualGamepad
  17. {
  18. ////////////////////////////////////////////////////////////////////////////////////////////////
  19. static const int InactiveTouchIndex = -1;
  20. ////////////////////////////////////////////////////////////////////////////////////////////////
  21. static const int PrimaryTouchIndex = 0;
  22. ////////////////////////////////////////////////////////////////////////////////////////////////
  23. void VirtualGamepadThumbStickComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  24. {
  25. provided.push_back(AZ_CRC_CE("VirtualGamepadThumbStickService"));
  26. provided.push_back(AZ_CRC_CE("UiInteractableService"));
  27. }
  28. ////////////////////////////////////////////////////////////////////////////////////////////////
  29. void VirtualGamepadThumbStickComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  30. {
  31. incompatible.push_back(AZ_CRC_CE("VirtualGamepadThumbStickService"));
  32. incompatible.push_back(AZ_CRC_CE("UiInteractableService"));
  33. }
  34. ////////////////////////////////////////////////////////////////////////////////////////////////
  35. void VirtualGamepadThumbStickComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  36. {
  37. required.push_back(AZ_CRC_CE("UiTransformService"));
  38. }
  39. ////////////////////////////////////////////////////////////////////////////////////////////////
  40. void VirtualGamepadThumbStickComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  41. {
  42. AZ_UNUSED(dependent);
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////////////////////
  45. void VirtualGamepadThumbStickComponent::Reflect(AZ::ReflectContext* context)
  46. {
  47. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  48. {
  49. serialize->Class<VirtualGamepadThumbStickComponent, AZ::Component>()
  50. ->Version(0)
  51. ->Field("AssignedInputChannelName", &VirtualGamepadThumbStickComponent::m_assignedInputChannelName)
  52. ->Field("ThumbStickImageCentre", &VirtualGamepadThumbStickComponent::m_thumbStickImageCentre)
  53. ->Field("ThumbStickImageRadial", &VirtualGamepadThumbStickComponent::m_thumbStickImageRadial)
  54. ->Field("CentreWhenPressed", &VirtualGamepadThumbStickComponent::m_centreWhenPressed)
  55. ->Field("AdjustPositionWhilePressed", &VirtualGamepadThumbStickComponent::m_adjustPositionWhilePressed)
  56. ;
  57. if (AZ::EditContext* ec = serialize->GetEditContext())
  58. {
  59. ec->Class<VirtualGamepadThumbStickComponent>("VirtualGamepadThumbStick", "A component that designates this entity as a virtual gamepad thumb-stick")
  60. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  61. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/UiVirtualThumbStick.png")
  62. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/UiVirtualThumbStick.png")
  63. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("UI"))
  64. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  65. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &VirtualGamepadThumbStickComponent::m_assignedInputChannelName,
  66. "Input Channel", "The input channel that will be updated when the user interacts with this virtual control")
  67. ->Attribute(AZ::Edit::Attributes::StringList, &VirtualGamepadThumbStickComponent::GetAssignableInputChannelNames)
  68. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &VirtualGamepadThumbStickComponent::m_thumbStickImageCentre,
  69. "Thumb Stick Image Centre", "The child element that will be positioned at the centre of the virtual thumb-stick.")
  70. ->Attribute(AZ::Edit::Attributes::EnumValues, &VirtualGamepadThumbStickComponent::GetChildEntityIdNamePairs)
  71. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &VirtualGamepadThumbStickComponent::m_thumbStickImageRadial,
  72. "Thumb Stick Image Radial", "The child element that will be positioned under the user's finger while the virtual thumb-stick is active.\n"
  73. "The position of this image will always be clamped to the radial edge of the virtual thumb-stick centre image.")
  74. ->Attribute(AZ::Edit::Attributes::EnumValues, &VirtualGamepadThumbStickComponent::GetChildEntityIdNamePairs)
  75. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &VirtualGamepadThumbStickComponent::m_centreWhenPressed,
  76. "Centre When Pressed", "Whether or not to centre the virtual thumb-stick when it is pressed.")
  77. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &VirtualGamepadThumbStickComponent::m_adjustPositionWhilePressed,
  78. "Adjust Position While Pressed", "Whether or not to adjust the position of the virtual thumb-stick while it is active,\n"
  79. "such that it will track the user's finger when it moves outside the thumb-stick radius.")
  80. ;
  81. }
  82. }
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////////////////////
  85. void VirtualGamepadThumbStickComponent::Init()
  86. {
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////////////////////
  89. void VirtualGamepadThumbStickComponent::Activate()
  90. {
  91. m_activeTouchIndex = InactiveTouchIndex;
  92. m_currentAxisValuesNormalized = AZ::Vector2::CreateZero();
  93. m_currentViewportPositionPixels = AZ::Vector2::CreateZero();
  94. VirtualGamepadThumbStickRequestBus::Handler::BusConnect(m_assignedInputChannelName);
  95. UiInteractableBus::Handler::BusConnect(GetEntityId());
  96. }
  97. ////////////////////////////////////////////////////////////////////////////////////////////////
  98. void VirtualGamepadThumbStickComponent::Deactivate()
  99. {
  100. UiInteractableBus::Handler::BusDisconnect(GetEntityId());
  101. VirtualGamepadThumbStickRequestBus::Handler::BusDisconnect(m_assignedInputChannelName);
  102. m_currentViewportPositionPixels = AZ::Vector2::CreateZero();
  103. m_currentAxisValuesNormalized = AZ::Vector2::CreateZero();
  104. m_activeTouchIndex = InactiveTouchIndex;
  105. }
  106. ////////////////////////////////////////////////////////////////////////////////////////////////
  107. bool VirtualGamepadThumbStickComponent::CanHandleEvent(AZ::Vector2 point)
  108. {
  109. AZ_UNUSED(point);
  110. return m_activeTouchIndex == InactiveTouchIndex;
  111. }
  112. ////////////////////////////////////////////////////////////////////////////////////////////////
  113. bool VirtualGamepadThumbStickComponent::HandlePressed(AZ::Vector2 point, bool& shouldStayActive)
  114. {
  115. shouldStayActive = false;
  116. return OnAnyTouchPressed(point, PrimaryTouchIndex);
  117. }
  118. ////////////////////////////////////////////////////////////////////////////////////////////////
  119. bool VirtualGamepadThumbStickComponent::HandleReleased(AZ::Vector2 point)
  120. {
  121. return OnAnyTouchReleased(point, PrimaryTouchIndex);
  122. }
  123. ////////////////////////////////////////////////////////////////////////////////////////////////
  124. bool VirtualGamepadThumbStickComponent::HandleMultiTouchPressed(AZ::Vector2 point, int multiTouchIndex)
  125. {
  126. return OnAnyTouchPressed(point, multiTouchIndex);
  127. }
  128. ////////////////////////////////////////////////////////////////////////////////////////////////
  129. bool VirtualGamepadThumbStickComponent::HandleMultiTouchReleased(AZ::Vector2 point, int multiTouchIndex)
  130. {
  131. return OnAnyTouchReleased(point, multiTouchIndex);
  132. }
  133. ////////////////////////////////////////////////////////////////////////////////////////////////
  134. void VirtualGamepadThumbStickComponent::InputPositionUpdate(AZ::Vector2 point)
  135. {
  136. OnAnyTouchPositionUpdate(point, PrimaryTouchIndex);
  137. }
  138. ////////////////////////////////////////////////////////////////////////////////////////////////
  139. void VirtualGamepadThumbStickComponent::MultiTouchPositionUpdate(AZ::Vector2 point, int multiTouchIndex)
  140. {
  141. OnAnyTouchPositionUpdate(point, multiTouchIndex);
  142. }
  143. ////////////////////////////////////////////////////////////////////////////////////////////////
  144. AZ::Vector2 VirtualGamepadThumbStickComponent::GetCurrentAxisValuesNormalized() const
  145. {
  146. return m_currentAxisValuesNormalized;
  147. }
  148. ////////////////////////////////////////////////////////////////////////////////////////////////
  149. bool VirtualGamepadThumbStickComponent::OnAnyTouchPressed(AZ::Vector2 viewportPositionPixels,
  150. int touchIndex)
  151. {
  152. if (m_activeTouchIndex != InactiveTouchIndex)
  153. {
  154. return false;
  155. }
  156. // Set the active touch index, current thumb-stick position, and axis values
  157. m_activeTouchIndex = touchIndex;
  158. m_currentAxisValuesNormalized = AZ::Vector2::CreateZero();
  159. // Store the default thumb-stick position and radius
  160. UiTransformInterface::RectPoints rectPoints;
  161. UiTransformBus::Event(m_thumbStickImageCentre,
  162. &UiTransformInterface::GetViewportSpacePoints,
  163. rectPoints);
  164. m_thumbStickPixelRadius = AZStd::max(rectPoints.GetAxisAlignedSize().GetX() * 0.5f, 1.0f);
  165. UiTransformBus::EventResult(m_defaultViewportPositionPixels,
  166. m_thumbStickImageCentre,
  167. &UiTransformInterface::GetViewportPosition);
  168. if (m_centreWhenPressed)
  169. {
  170. // Position both thumb-stick images at the touch start position
  171. m_currentViewportPositionPixels = viewportPositionPixels;
  172. UiTransformBus::Event(m_thumbStickImageCentre,
  173. &UiTransformInterface::SetViewportPosition,
  174. m_currentViewportPositionPixels);
  175. UiTransformBus::Event(m_thumbStickImageRadial,
  176. &UiTransformInterface::SetViewportPosition,
  177. m_currentViewportPositionPixels);
  178. }
  179. else
  180. {
  181. // Leave both thumb-sticks at their default position
  182. m_currentViewportPositionPixels = m_defaultViewportPositionPixels;
  183. }
  184. return true;
  185. }
  186. ////////////////////////////////////////////////////////////////////////////////////////////////
  187. bool VirtualGamepadThumbStickComponent::OnAnyTouchReleased(AZ::Vector2 viewportPositionPixels,
  188. int touchIndex)
  189. {
  190. AZ_UNUSED(viewportPositionPixels);
  191. if (m_activeTouchIndex != touchIndex)
  192. {
  193. return false;
  194. }
  195. // Reset the active touch index, current thumb-stick position, and axis values
  196. m_activeTouchIndex = InactiveTouchIndex;
  197. m_currentViewportPositionPixels = AZ::Vector2::CreateZero();
  198. m_currentAxisValuesNormalized = AZ::Vector2::CreateZero();
  199. // Position both thumb-stick images at their default position
  200. UiTransformBus::Event(m_thumbStickImageCentre,
  201. &UiTransformInterface::SetViewportPosition,
  202. m_defaultViewportPositionPixels);
  203. UiTransformBus::Event(m_thumbStickImageRadial,
  204. &UiTransformInterface::SetViewportPosition,
  205. m_defaultViewportPositionPixels);
  206. return true;
  207. }
  208. ////////////////////////////////////////////////////////////////////////////////////////////////
  209. void VirtualGamepadThumbStickComponent::OnAnyTouchPositionUpdate(AZ::Vector2 viewportPositionPixels,
  210. [[maybe_unused]] int touchIndex)
  211. {
  212. // Calculate the current virtual thumb-stick axis values
  213. AZ::Vector2 pixelDelta = viewportPositionPixels - m_currentViewportPositionPixels;
  214. const float deltaLength = pixelDelta.GetLength();
  215. if (deltaLength > m_thumbStickPixelRadius)
  216. {
  217. if (m_adjustPositionWhilePressed)
  218. {
  219. // Reposition the centre virtual thumb-stick image so the press stays at the edge
  220. AZ::Vector2 radialDelta = pixelDelta;
  221. radialDelta.SetLength(deltaLength - m_thumbStickPixelRadius);
  222. m_currentViewportPositionPixels += radialDelta;
  223. UiTransformBus::Event(m_thumbStickImageCentre,
  224. &UiTransformInterface::SetViewportPosition,
  225. m_currentViewportPositionPixels);
  226. }
  227. // Clamp the pixel delta to the radius of the thumb-stick
  228. pixelDelta *= m_thumbStickPixelRadius / deltaLength;
  229. }
  230. // Position the radial thumb-stick image accordingly
  231. const AZ::Vector2 radialImagePosition = m_currentViewportPositionPixels + pixelDelta;
  232. UiTransformBus::Event(m_thumbStickImageRadial,
  233. &UiTransformInterface::SetViewportPosition,
  234. radialImagePosition);
  235. // Set the current normalized axis values
  236. m_currentAxisValuesNormalized.SetX(pixelDelta.GetX() / m_thumbStickPixelRadius);
  237. m_currentAxisValuesNormalized.SetY(-pixelDelta.GetY() / m_thumbStickPixelRadius);
  238. }
  239. ////////////////////////////////////////////////////////////////////////////////////////////////
  240. AZStd::vector<AZStd::string> VirtualGamepadThumbStickComponent::GetAssignableInputChannelNames() const
  241. {
  242. AZStd::unordered_set<AZStd::string> buttonNames;
  243. VirtualGamepadRequestBus::BroadcastResult(buttonNames, &VirtualGamepadRequests::GetThumbStickNames);
  244. AZStd::vector<AZStd::string> assignableInputChannelNames;
  245. for (const AZStd::string& buttonName : buttonNames)
  246. {
  247. assignableInputChannelNames.push_back(buttonName);
  248. }
  249. AZStd::sort(assignableInputChannelNames.begin(), assignableInputChannelNames.end());
  250. return assignableInputChannelNames;
  251. }
  252. ////////////////////////////////////////////////////////////////////////////////////////////////
  253. AZStd::vector<AZStd::pair<AZ::EntityId, AZStd::string>> VirtualGamepadThumbStickComponent::GetChildEntityIdNamePairs() const
  254. {
  255. AZStd::vector<AZStd::pair<AZ::EntityId, AZStd::string>> result;
  256. // Add a first entry for "None"
  257. result.push_back(AZStd::make_pair(AZ::EntityId(AZ::EntityId()), "<None>"));
  258. // Get a list of all child elements and add them to the result
  259. LyShine::EntityArray childElements;
  260. UiElementBus::EventResult(childElements, GetEntityId(), &UiElementInterface::GetChildElements);
  261. for (const auto& childElement : childElements)
  262. {
  263. result.push_back(AZStd::make_pair(AZ::EntityId(childElement->GetId()), childElement->GetName()));
  264. }
  265. return result;
  266. }
  267. } // namespace VirtualGamepad