3
0

BarrierInputMouse.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 <BarrierInputMouse.h>
  9. #include <Atom/RPI.Public/ViewportContext.h>
  10. #include <Atom/RPI.Public/ViewportContextBus.h>
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. namespace BarrierInput
  13. {
  14. using namespace AzFramework;
  15. ////////////////////////////////////////////////////////////////////////////////////////////////
  16. InputDeviceMouse::Implementation* InputDeviceMouseBarrier::Create(InputDeviceMouse& inputDevice)
  17. {
  18. return aznew InputDeviceMouseBarrier(inputDevice);
  19. }
  20. ////////////////////////////////////////////////////////////////////////////////////////////////
  21. InputDeviceMouseBarrier::InputDeviceMouseBarrier(InputDeviceMouse& inputDevice)
  22. : InputDeviceMouse::Implementation(inputDevice)
  23. , m_systemCursorState(SystemCursorState::Unknown)
  24. , m_systemCursorPositionNormalized(0.5f, 0.5f)
  25. , m_threadAwareRawButtonEventQueuesById()
  26. , m_threadAwareRawButtonEventQueuesByIdMutex()
  27. , m_threadAwareRawMovementEventQueuesById()
  28. , m_threadAwareRawMovementEventQueuesByIdMutex()
  29. , m_threadAwareSystemCursorPosition(0.0f, 0.0f)
  30. , m_threadAwareSystemCursorPositionMutex()
  31. {
  32. RawInputNotificationBusBarrier::Handler::BusConnect();
  33. }
  34. ////////////////////////////////////////////////////////////////////////////////////////////////
  35. InputDeviceMouseBarrier::~InputDeviceMouseBarrier()
  36. {
  37. RawInputNotificationBusBarrier::Handler::BusDisconnect();
  38. }
  39. ////////////////////////////////////////////////////////////////////////////////////////////////
  40. bool InputDeviceMouseBarrier::IsConnected() const
  41. {
  42. // We could check the validity of the socket connection to the Barrier server
  43. return true;
  44. }
  45. ////////////////////////////////////////////////////////////////////////////////////////////////
  46. void InputDeviceMouseBarrier::SetSystemCursorState(SystemCursorState systemCursorState)
  47. {
  48. // This doesn't apply when using Barrier, but we'll store it so it can be queried
  49. m_systemCursorState = systemCursorState;
  50. }
  51. ////////////////////////////////////////////////////////////////////////////////////////////////
  52. SystemCursorState InputDeviceMouseBarrier::GetSystemCursorState() const
  53. {
  54. return m_systemCursorState;
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////////////////////
  57. void InputDeviceMouseBarrier::SetSystemCursorPositionNormalized(AZ::Vector2 positionNormalized)
  58. {
  59. // This will simply get overridden by the next call to OnRawMousePositionEvent, but there's
  60. // not much we can do about it, and Barrier mouse input is only for debug purposes anyway.
  61. m_systemCursorPositionNormalized = positionNormalized;
  62. }
  63. ////////////////////////////////////////////////////////////////////////////////////////////////
  64. AZ::Vector2 InputDeviceMouseBarrier::GetSystemCursorPositionNormalized() const
  65. {
  66. return m_systemCursorPositionNormalized;
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////////////////////
  69. void InputDeviceMouseBarrier::TickInputDevice()
  70. {
  71. {
  72. // Queue all mouse button events that were received in the other thread
  73. AZStd::scoped_lock lock(m_threadAwareRawButtonEventQueuesByIdMutex);
  74. for (const auto& buttonEventQueuesById : m_threadAwareRawButtonEventQueuesById)
  75. {
  76. const InputChannelId& inputChannelId = buttonEventQueuesById.first;
  77. for (bool rawButtonState : buttonEventQueuesById.second)
  78. {
  79. QueueRawButtonEvent(inputChannelId, rawButtonState);
  80. }
  81. }
  82. m_threadAwareRawButtonEventQueuesById.clear();
  83. }
  84. bool receivedRawMovementEvents = false;
  85. {
  86. // Queue all mouse movement events that were received in the other thread
  87. AZStd::scoped_lock lock(m_threadAwareRawMovementEventQueuesByIdMutex);
  88. for (const auto& movementEventQueuesById : m_threadAwareRawMovementEventQueuesById)
  89. {
  90. const InputChannelId& inputChannelId = movementEventQueuesById.first;
  91. for (float rawMovementDelta : movementEventQueuesById.second)
  92. {
  93. QueueRawMovementEvent(inputChannelId, rawMovementDelta);
  94. receivedRawMovementEvents = true;
  95. }
  96. }
  97. m_threadAwareRawMovementEventQueuesById.clear();
  98. }
  99. // Update the system cursor position
  100. auto atomViewportRequests = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  101. AZ::RPI::ViewportContextPtr viewportContext = atomViewportRequests->GetDefaultViewportContext();
  102. if (viewportContext)
  103. {
  104. const AzFramework::WindowSize windowSize = viewportContext->GetViewportSize();
  105. const float windowWidth = static_cast<float>(windowSize.m_width);
  106. const float windowHeight = static_cast<float>(windowSize.m_height);
  107. const AZ::Vector2 oldSystemCursorPositionNormalized = m_systemCursorPositionNormalized;
  108. AZStd::scoped_lock lock(m_threadAwareSystemCursorPositionMutex);
  109. {
  110. const AZ::Vector2 normalizedPosition(m_threadAwareSystemCursorPosition.GetX() / windowWidth,
  111. m_threadAwareSystemCursorPosition.GetY() / windowHeight);
  112. m_systemCursorPositionNormalized = normalizedPosition;
  113. }
  114. // In theory Barrier should send relative mouse movement events as 'DMRM' messages, which are
  115. // forwarded to InputDeviceMouseBarrier::OnRawMouseMovementEvent, but this does not appear to
  116. // be happening, so if we didn't receive any relative mouse movement events this frame we can
  117. // just approximate the movement ourselves. Unlike other mouse implementations where movement
  118. // events are sent 'raw' before any operating system ballistics/smoothing is applied, Barrier
  119. // seems to calculate relative mouse movement events by taking the delta between the previous
  120. // system cursor position and the current one, so we should obtain the same result regardless.
  121. if (!receivedRawMovementEvents)
  122. {
  123. const AZ::Vector2 mouseMovementDelta = m_systemCursorPositionNormalized - oldSystemCursorPositionNormalized;
  124. QueueRawMovementEvent(InputDeviceMouse::Movement::X, mouseMovementDelta.GetX() * windowWidth);
  125. QueueRawMovementEvent(InputDeviceMouse::Movement::Y, mouseMovementDelta.GetY() * windowHeight);
  126. }
  127. }
  128. // Process raw event queues once each frame
  129. ProcessRawEventQueues();
  130. }
  131. ////////////////////////////////////////////////////////////////////////////////////////////////
  132. void InputDeviceMouseBarrier::OnRawMouseButtonDownEvent(uint32_t buttonIndex)
  133. {
  134. ThreadSafeQueueRawButtonEvent(buttonIndex, true);
  135. }
  136. ////////////////////////////////////////////////////////////////////////////////////////////////
  137. void InputDeviceMouseBarrier::OnRawMouseButtonUpEvent(uint32_t buttonIndex)
  138. {
  139. ThreadSafeQueueRawButtonEvent(buttonIndex, false);
  140. }
  141. ////////////////////////////////////////////////////////////////////////////////////////////////
  142. void InputDeviceMouseBarrier::OnRawMouseMovementEvent(float movementX, float movementY)
  143. {
  144. AZStd::scoped_lock lock(m_threadAwareRawMovementEventQueuesByIdMutex);
  145. m_threadAwareRawMovementEventQueuesById[InputDeviceMouse::Movement::X].push_back(movementX);
  146. m_threadAwareRawMovementEventQueuesById[InputDeviceMouse::Movement::Y].push_back(movementY);
  147. }
  148. ////////////////////////////////////////////////////////////////////////////////////////////////
  149. void InputDeviceMouseBarrier::OnRawMousePositionEvent(float positionX,
  150. float positionY)
  151. {
  152. AZStd::scoped_lock lock(m_threadAwareSystemCursorPositionMutex);
  153. m_threadAwareSystemCursorPosition = AZ::Vector2(positionX, positionY);
  154. }
  155. ////////////////////////////////////////////////////////////////////////////////////////////////
  156. void InputDeviceMouseBarrier::ThreadSafeQueueRawButtonEvent(uint32_t buttonIndex,
  157. bool rawButtonState)
  158. {
  159. const InputChannelId* inputChannelId = nullptr;
  160. switch (buttonIndex)
  161. {
  162. case 1: { inputChannelId = &InputDeviceMouse::Button::Left; } break;
  163. case 2: { inputChannelId = &InputDeviceMouse::Button::Middle; } break;
  164. case 3: { inputChannelId = &InputDeviceMouse::Button::Right; } break;
  165. }
  166. if (inputChannelId)
  167. {
  168. AZStd::scoped_lock lock(m_threadAwareRawButtonEventQueuesByIdMutex);
  169. m_threadAwareRawButtonEventQueuesById[*inputChannelId].push_back(rawButtonState);
  170. }
  171. }
  172. } // namespace BarrierInput