InputDeviceVirtualGamepad.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <AzFramework/Input/Devices/InputDevice.h>
  10. #include <AzFramework/Input/Channels/InputChannelAnalog.h>
  11. #include <AzFramework/Input/Channels/InputChannelAxis1D.h>
  12. #include <AzFramework/Input/Channels/InputChannelAxis2D.h>
  13. #include <AzFramework/Input/Channels/InputChannelDigital.h>
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. namespace VirtualGamepad
  16. {
  17. ////////////////////////////////////////////////////////////////////////////////////////////////
  18. //! Implementation for a virtual gamepad input device that is controlled using a touch screen.
  19. class InputDeviceVirtualGamepad : public AzFramework::InputDevice
  20. {
  21. public:
  22. ////////////////////////////////////////////////////////////////////////////////////////////
  23. //! The id used to identify the primary virtual gamepad input device
  24. static const AzFramework::InputDeviceId Id;
  25. ////////////////////////////////////////////////////////////////////////////////////////////
  26. //! Check whether an input device id identifies a virtual gamepad (regardless of index)
  27. //! \param[in] inputDeviceId The input device id to check
  28. //! \return True if the input device id identifies a virtual gamepad, false otherwise
  29. static bool IsVirtualGamepadDevice(const AzFramework::InputDeviceId& inputDeviceId);
  30. ////////////////////////////////////////////////////////////////////////////////////////////
  31. // Allocator
  32. AZ_CLASS_ALLOCATOR(InputDeviceVirtualGamepad, AZ::SystemAllocator);
  33. ////////////////////////////////////////////////////////////////////////////////////////////
  34. // Type Info
  35. AZ_RTTI(InputDeviceVirtualGamepad, "{DC4B939E-66C7-4F76-B7DF-049A3F13A1C3}", InputDevice);
  36. ////////////////////////////////////////////////////////////////////////////////////////////
  37. //! Constructor
  38. //! \param[in] buttonNames The list of button names supported by the virtual gamepad
  39. //! \param[in] thumbStickNames The list of thumbstick names supported by the virtual gamepad
  40. explicit InputDeviceVirtualGamepad(const AZStd::unordered_set<AZStd::string>& buttonNames,
  41. const AZStd::unordered_set<AZStd::string>& thumbStickNames);
  42. public:
  43. ////////////////////////////////////////////////////////////////////////////////////////////
  44. //! Destructor
  45. ~InputDeviceVirtualGamepad() override;
  46. ////////////////////////////////////////////////////////////////////////////////////////////
  47. //! \ref AzFramework::InputDevice::GetInputChannelsById
  48. const InputChannelByIdMap& GetInputChannelsById() const override;
  49. ////////////////////////////////////////////////////////////////////////////////////////////
  50. //! \ref AzFramework::InputDevice::IsSupported
  51. bool IsSupported() const override;
  52. ////////////////////////////////////////////////////////////////////////////////////////////
  53. //! \ref AzFramework::InputDevice::IsConnected
  54. bool IsConnected() const override;
  55. ////////////////////////////////////////////////////////////////////////////////////////////
  56. //! \ref AzFramework::InputDeviceRequests::TickInputDevice
  57. void TickInputDevice() override;
  58. protected:
  59. ////////////////////////////////////////////////////////////////////////////////////////////
  60. //! Create a button input channel
  61. //! \param[in] channelName The input channel name
  62. void CreateButtonChannel(const AZStd::string& channelName);
  63. ////////////////////////////////////////////////////////////////////////////////////////////
  64. //! Create a thumb-stick axis 1D input channel
  65. //! \param[in] channelName The input channel name
  66. void CreateThumbStickAxis1DChannel(const AZStd::string& channelName);
  67. ////////////////////////////////////////////////////////////////////////////////////////////
  68. //! Create a thumb-stick axis 2D input channel
  69. //! \param[in] channelName The input channel name
  70. void CreateThumbStickAxis2DChannel(const AZStd::string& channelName);
  71. ////////////////////////////////////////////////////////////////////////////////////////////
  72. //! Create a thumb-stick direction input channel
  73. //! \param[in] channelName The input channel name
  74. void CreateThumbStickDirectionChannel(const AZStd::string& channelName);
  75. private:
  76. ////////////////////////////////////////////////////////////////////////////////////////////
  77. ///@{
  78. //! Alias for verbose container class
  79. using ButtonChannelByNameMap = AZStd::unordered_map<AZStd::string, AzFramework::InputChannelDigital*>;
  80. using ThumbStickAxis1DChannelByNameMap = AZStd::unordered_map<AZStd::string, AzFramework::InputChannelAxis1D*>;
  81. using ThumbStickAxis2DChannelByNameMap = AZStd::unordered_map<AZStd::string, AzFramework::InputChannelAxis2D*>;
  82. using ThumbStickDirectionChannelByNameMap = AZStd::unordered_map<AZStd::string, AzFramework::InputChannelAnalog*>;
  83. ///@}
  84. ////////////////////////////////////////////////////////////////////////////////////////////
  85. // Variables
  86. InputChannelByIdMap m_allChannelsById; //!< All virtual input channels by id
  87. ButtonChannelByNameMap m_buttonChannelsByName; //!< All virtual button channels by id
  88. ThumbStickAxis1DChannelByNameMap m_thumbStickAxis1DChannelsByName; //!< All thumb-stick axis 1D channels by id
  89. ThumbStickAxis2DChannelByNameMap m_thumbStickAxis2DChannelsByName; //!< All thumb-stick axis 2D channels by id
  90. ThumbStickDirectionChannelByNameMap m_thumbStickDirectionChannelsByName; //!< All thumb-stick direction channels by id
  91. };
  92. } // namespace VirtualGamepad