3
0

BarrierInputMouse.cpp 9.3 KB

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