input.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <string>
  7. #include <tuple>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include "common/logging/log.h"
  11. #include "common/param_package.h"
  12. #include "common/vector_math.h"
  13. namespace Input {
  14. enum class AnalogDirection : u8 {
  15. RIGHT,
  16. LEFT,
  17. UP,
  18. DOWN,
  19. };
  20. /// An abstract class template for an input device (a button, an analog input, etc.).
  21. template <typename StatusType>
  22. class InputDevice {
  23. public:
  24. virtual ~InputDevice() = default;
  25. virtual StatusType GetStatus() const {
  26. return {};
  27. }
  28. virtual bool GetAnalogDirectionStatus(AnalogDirection direction) const {
  29. return {};
  30. }
  31. };
  32. /// An abstract class template for a factory that can create input devices.
  33. template <typename InputDeviceType>
  34. class Factory {
  35. public:
  36. virtual ~Factory() = default;
  37. virtual std::unique_ptr<InputDeviceType> Create(const Common::ParamPackage&) = 0;
  38. };
  39. namespace Impl {
  40. template <typename InputDeviceType>
  41. using FactoryListType = std::unordered_map<std::string, std::shared_ptr<Factory<InputDeviceType>>>;
  42. template <typename InputDeviceType>
  43. struct FactoryList {
  44. static FactoryListType<InputDeviceType> list;
  45. };
  46. template <typename InputDeviceType>
  47. FactoryListType<InputDeviceType> FactoryList<InputDeviceType>::list;
  48. } // namespace Impl
  49. /**
  50. * Registers an input device factory.
  51. * @tparam InputDeviceType the type of input devices the factory can create
  52. * @param name the name of the factory. Will be used to match the "engine" parameter when creating
  53. * a device
  54. * @param factory the factory object to register
  55. */
  56. template <typename InputDeviceType>
  57. void RegisterFactory(const std::string& name, std::shared_ptr<Factory<InputDeviceType>> factory) {
  58. auto pair = std::make_pair(name, std::move(factory));
  59. if (!Impl::FactoryList<InputDeviceType>::list.insert(std::move(pair)).second) {
  60. LOG_ERROR(Input, "Factory '{}' already registered", name);
  61. }
  62. }
  63. /**
  64. * Unregisters an input device factory.
  65. * @tparam InputDeviceType the type of input devices the factory can create
  66. * @param name the name of the factory to unregister
  67. */
  68. template <typename InputDeviceType>
  69. void UnregisterFactory(const std::string& name) {
  70. if (Impl::FactoryList<InputDeviceType>::list.erase(name) == 0) {
  71. LOG_ERROR(Input, "Factory '{}' not registered", name);
  72. }
  73. }
  74. /**
  75. * Create an input device from given paramters.
  76. * @tparam InputDeviceType the type of input devices to create
  77. * @param params a serialized ParamPackage string contains all parameters for creating the device
  78. */
  79. template <typename InputDeviceType>
  80. std::unique_ptr<InputDeviceType> CreateDevice(const std::string& params) {
  81. const Common::ParamPackage package(params);
  82. const std::string engine = package.Get("engine", "null");
  83. const auto& factory_list = Impl::FactoryList<InputDeviceType>::list;
  84. const auto pair = factory_list.find(engine);
  85. if (pair == factory_list.end()) {
  86. if (engine != "null") {
  87. LOG_ERROR(Input, "Unknown engine name: {}", engine);
  88. }
  89. return std::make_unique<InputDeviceType>();
  90. }
  91. return pair->second->Create(package);
  92. }
  93. /**
  94. * A button device is an input device that returns bool as status.
  95. * true for pressed; false for released.
  96. */
  97. using ButtonDevice = InputDevice<bool>;
  98. /**
  99. * An analog device is an input device that returns a tuple of x and y coordinates as status. The
  100. * coordinates are within the unit circle. x+ is defined as right direction, and y+ is defined as up
  101. * direction
  102. */
  103. using AnalogDevice = InputDevice<std::tuple<float, float>>;
  104. /**
  105. * A motion device is an input device that returns a tuple of accelerometer state vector and
  106. * gyroscope state vector.
  107. *
  108. * For both vectors:
  109. * x+ is the same direction as LEFT on D-pad.
  110. * y+ is normal to the touch screen, pointing outward.
  111. * z+ is the same direction as UP on D-pad.
  112. *
  113. * For accelerometer state vector
  114. * Units: g (gravitational acceleration)
  115. *
  116. * For gyroscope state vector:
  117. * Orientation is determined by right-hand rule.
  118. * Units: deg/sec
  119. */
  120. using MotionDevice = InputDevice<std::tuple<Common::Vec3<float>, Common::Vec3<float>>>;
  121. /**
  122. * A touch device is an input device that returns a tuple of two floats and a bool. The floats are
  123. * x and y coordinates in the range 0.0 - 1.0, and the bool indicates whether it is pressed.
  124. */
  125. using TouchDevice = InputDevice<std::tuple<float, float, bool>>;
  126. /**
  127. * A mouse device is an input device that returns a tuple of two floats and four ints.
  128. * The first two floats are X and Y device coordinates of the mouse (from 0-1).
  129. * The s32s are the mouse wheel.
  130. */
  131. using MouseDevice = InputDevice<std::tuple<float, float, s32, s32>>;
  132. } // namespace Input