InputDeviceGestures.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "InputDeviceGestures.h"
  9. #include <AzFramework/Input/Devices/Mouse/InputDeviceMouse.h>
  10. #include <AzFramework/Input/Devices/Touch/InputDeviceTouch.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. namespace Gestures
  14. {
  15. using namespace AzFramework;
  16. ////////////////////////////////////////////////////////////////////////////////////////////////
  17. const InputDeviceId InputDeviceGestures::Id("gestures");
  18. ////////////////////////////////////////////////////////////////////////////////////////////////
  19. bool InputDeviceGestures::IsGesturesDevice(const InputDeviceId& inputDeviceId)
  20. {
  21. return (inputDeviceId.GetNameCrc32() == Id.GetNameCrc32());
  22. }
  23. ////////////////////////////////////////////////////////////////////////////////////////////////
  24. const InputChannelId InputDeviceGestures::Gesture::DoublePress("gesture_double_press");
  25. const InputChannelId InputDeviceGestures::Gesture::Drag("gesture_drag");
  26. const InputChannelId InputDeviceGestures::Gesture::Hold("gesture_hold");
  27. const InputChannelId InputDeviceGestures::Gesture::Pinch("gesture_pinch");
  28. const InputChannelId InputDeviceGestures::Gesture::Rotate("gesture_rotate");
  29. const InputChannelId InputDeviceGestures::Gesture::Swipe("gesture_swipe");
  30. const AZStd::array<InputChannelId, 6> InputDeviceGestures::Gesture::All =
  31. {{
  32. DoublePress,
  33. Drag,
  34. Hold,
  35. Pinch,
  36. Rotate,
  37. Swipe
  38. }};
  39. ////////////////////////////////////////////////////////////////////////////////////////////////
  40. void InputDeviceGestures::Reflect(AZ::ReflectContext* context)
  41. {
  42. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  43. {
  44. // Unfortunately it doesn't seem possible to reflect anything through BehaviorContext
  45. // using lambdas which capture variables from the enclosing scope. So we are manually
  46. // reflecting all input channel names, instead of just iterating over them like this:
  47. //
  48. // auto classBuilder = behaviorContext->Class<InputDeviceGestures>();
  49. // for (const InputChannelId& channelId : Button::All)
  50. // {
  51. // const char* channelName = channelId.GetName();
  52. // classBuilder->Constant(channelName, [channelName]() { return channelName; });
  53. // }
  54. behaviorContext->Class<InputDeviceGestures>()
  55. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::RuntimeOwn)
  56. ->Constant("name", BehaviorConstant(Id.GetName()))
  57. ->Constant(Gesture::DoublePress.GetName(), BehaviorConstant(Gesture::DoublePress.GetName()))
  58. ->Constant(Gesture::Drag.GetName(), BehaviorConstant(Gesture::Drag.GetName()))
  59. ->Constant(Gesture::Hold.GetName(), BehaviorConstant(Gesture::Hold.GetName()))
  60. ->Constant(Gesture::Pinch.GetName(), BehaviorConstant(Gesture::Pinch.GetName()))
  61. ->Constant(Gesture::Rotate.GetName(), BehaviorConstant(Gesture::Rotate.GetName()))
  62. ->Constant(Gesture::Swipe.GetName(), BehaviorConstant(Gesture::Swipe.GetName()))
  63. ;
  64. }
  65. }
  66. ////////////////////////////////////////////////////////////////////////////////////////////////
  67. InputDeviceGestures::InputDeviceGestures(const ConfigsByNameMap& gestureConfigsByName)
  68. : InputDevice(Id)
  69. , m_allChannelsById()
  70. {
  71. // Create all gesture input channels
  72. for (const auto& gestureConfigByName : gestureConfigsByName)
  73. {
  74. const InputChannelId channelId(gestureConfigByName.first.c_str());
  75. m_allChannelsById[channelId] = gestureConfigByName.second->CreateInputChannel(channelId,
  76. *this);
  77. }
  78. }
  79. ////////////////////////////////////////////////////////////////////////////////////////////////
  80. InputDeviceGestures::~InputDeviceGestures()
  81. {
  82. // Destroy all gesture input channels
  83. for (const auto& channelById : m_allChannelsById)
  84. {
  85. delete channelById.second;
  86. }
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////////////////////
  89. const InputDevice::InputChannelByIdMap& InputDeviceGestures::GetInputChannelsById() const
  90. {
  91. return m_allChannelsById;
  92. }
  93. ////////////////////////////////////////////////////////////////////////////////////////////////
  94. bool InputDeviceGestures::IsSupported() const
  95. {
  96. // Mouse or touch input must be supported
  97. const InputDevice* inputDeviceMouse = nullptr;
  98. const InputDevice* inputDeviceTouch = nullptr;
  99. InputDeviceRequestBus::EventResult(inputDeviceMouse,
  100. InputDeviceMouse::Id,
  101. &InputDeviceRequests::GetInputDevice);
  102. InputDeviceRequestBus::EventResult(inputDeviceTouch,
  103. InputDeviceTouch::Id,
  104. &InputDeviceRequests::GetInputDevice);
  105. return (inputDeviceMouse && inputDeviceMouse->IsSupported()) ||
  106. (inputDeviceTouch && inputDeviceTouch->IsSupported());
  107. }
  108. ////////////////////////////////////////////////////////////////////////////////////////////////
  109. bool InputDeviceGestures::IsConnected() const
  110. {
  111. // Mouse or touch input must be connected
  112. const InputDevice* inputDeviceMouse = nullptr;
  113. const InputDevice* inputDeviceTouch = nullptr;
  114. InputDeviceRequestBus::EventResult(inputDeviceMouse,
  115. InputDeviceMouse::Id,
  116. &InputDeviceRequests::GetInputDevice);
  117. InputDeviceRequestBus::EventResult(inputDeviceTouch,
  118. InputDeviceTouch::Id,
  119. &InputDeviceRequests::GetInputDevice);
  120. return (inputDeviceMouse && inputDeviceMouse->IsConnected()) ||
  121. (inputDeviceTouch && inputDeviceTouch->IsConnected());
  122. }
  123. ////////////////////////////////////////////////////////////////////////////////////////////////
  124. void InputDeviceGestures::TickInputDevice()
  125. {
  126. // All InputChannelGesture* classes listen for and process mouse and touch input directly,
  127. // so we don't actually need to do anything here.
  128. }
  129. } // namespace Gestures