3
0

InputEventMap.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <AzCore/RTTI/RTTI.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzFramework/Input/Buses/Notifications/InputDeviceNotificationBus.h>
  12. #include <AzFramework/Input/Events/InputChannelEventListener.h>
  13. #include <StartingPointInput/InputEventNotificationBus.h>
  14. namespace AZ
  15. {
  16. class ReflectContext;
  17. }
  18. namespace StartingPointInput
  19. {
  20. //////////////////////////////////////////////////////////////////////////
  21. /// Classes that inherit from this one will share the life-cycle API's
  22. /// with components. Components that contain the subclasses are expected
  23. /// to call these methods in their Init/Activate/Deactivate methods
  24. //////////////////////////////////////////////////////////////////////////
  25. class InputSubComponent
  26. {
  27. public:
  28. AZ_RTTI(InputSubComponent, "{3D0F14F8-AE29-4ECC-BC88-26B8F8168398}");
  29. virtual ~InputSubComponent() = default;
  30. //////////////////////////////////////////////////////////////////////////
  31. /// InputSubComponents will share the life-cycle API's of components.
  32. /// Any Component that contains an InputSubComponent is expected to call
  33. /// these methods in their Activate/Deactivate methods
  34. virtual void Activate(const InputEventNotificationId& channel) = 0;
  35. virtual void Deactivate(const InputEventNotificationId& channel) = 0;
  36. };
  37. //////////////////////////////////////////////////////////////////////////
  38. /// Maps raw input from any raw input source and outputs Pressed, Held, and Released input events
  39. class InputEventMap
  40. : public InputSubComponent
  41. , protected AzFramework::InputChannelEventListener
  42. {
  43. public:
  44. InputEventMap();
  45. ~InputEventMap() override = default;
  46. AZ_RTTI(InputEventMap, "{A14EA0A3-F053-469D-840E-A70002F51384}", InputSubComponent);
  47. static void Reflect(AZ::ReflectContext* reflection);
  48. //////////////////////////////////////////////////////////////////////////
  49. // InputSubComponent
  50. void Activate(const InputEventNotificationId& eventNotificationId) override;
  51. void Deactivate(const InputEventNotificationId& eventNotificationId) override;
  52. protected:
  53. AZStd::string GetEditorText() const;
  54. virtual const AZStd::vector<AZStd::string> GetInputDeviceTypes() const;
  55. virtual const AZStd::vector<AZStd::string> GetInputNamesBySelectedDevice() const;
  56. AZ::Crc32 OnDeviceSelected();
  57. //////////////////////////////////////////////////////////////////////////
  58. // AzFramework::InputChannelEventListener
  59. bool OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel) override;
  60. using InputEventType = void(InputEventNotificationBus::Events::*)(float);
  61. virtual float CalculateEventValue(const AzFramework::InputChannel& inputChannel) const;
  62. void SendEventsInternal(float value, const AzFramework::LocalUserId& localUserIdOfEvent, const InputEventNotificationId busId, InputEventType eventType);
  63. //////////////////////////////////////////////////////////////////////////
  64. // Non Reflected Data
  65. InputEventNotificationId m_outgoingBusId;
  66. bool m_wasPressed = false;
  67. //////////////////////////////////////////////////////////////////////////
  68. // Reflected Data
  69. float m_eventValueMultiplier = 1.f;
  70. AZStd::string m_inputName = "";
  71. AZStd::string m_inputDeviceType = "";
  72. float m_deadZone = 0.0f;
  73. };
  74. //////////////////////////////////////////////////////////////////////////
  75. /// ThumbstickInput handles raw input from thumbstick sources, applies any
  76. /// custom dead-zone or sensitivity curve calculations, and then outputs
  77. /// Pressed, Held, and Released input events for the specified axis
  78. class ThumbstickInputEventMap : public InputEventMap
  79. {
  80. public:
  81. ThumbstickInputEventMap();
  82. ~ThumbstickInputEventMap() override = default;
  83. AZ_RTTI(ThumbstickInputEventMap, "{4881FA7C-0667-476C-8C77-4DBB6C69F646}", InputEventMap);
  84. static void Reflect(AZ::ReflectContext* reflection);
  85. protected:
  86. AZStd::string GetEditorText() const;
  87. const AZStd::vector<AZStd::string> GetInputDeviceTypes() const override;
  88. const AZStd::vector<AZStd::string> GetInputNamesBySelectedDevice() const override;
  89. bool OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel) override;
  90. float CalculateEventValue(const AzFramework::InputChannel& inputChannel) const override;
  91. static AZ::Vector2 ApplyDeadZonesAndSensitivity(const AZ::Vector2& inputValues,
  92. float innerDeadZone,
  93. float outerDeadZone,
  94. float axisDeadZone,
  95. float sensitivityExponent);
  96. enum class OutputAxis
  97. {
  98. X,
  99. Y
  100. };
  101. //////////////////////////////////////////////////////////////////////////
  102. // Non Reflected Data
  103. const AzFramework::InputDeviceId* m_wasLastPressedByInputDeviceId = nullptr;
  104. //////////////////////////////////////////////////////////////////////////
  105. // Reflected Data
  106. float m_innerDeadZoneRadius = 0.0f;
  107. float m_outerDeadZoneRadius = 1.0f;
  108. float m_axisDeadZoneValue = 0.0f;
  109. float m_sensitivityExponent = 1.0f;
  110. OutputAxis m_outputAxis = OutputAxis::X;
  111. };
  112. } // namespace Input