💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
7.2 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_INPUT_MAPPING_HPP
  20. #define ANTKEEPER_INPUT_MAPPING_HPP
  21. #include "input/mapping-type.hpp"
  22. #include "input/gamepad-axis.hpp"
  23. #include "input/gamepad-button.hpp"
  24. #include "input/mouse-button.hpp"
  25. #include "input/mouse-motion-axis.hpp"
  26. #include "input/mouse-scroll-axis.hpp"
  27. #include "input/scancode.hpp"
  28. #include <cstdint>
  29. namespace input {
  30. class control;
  31. class gamepad;
  32. class keyboard;
  33. class mouse;
  34. /**
  35. * Abstract base class for input mappings.
  36. */
  37. class mapping
  38. {
  39. public:
  40. /**
  41. * Constructs an input mapping.
  42. */
  43. mapping() = default;
  44. /// Destructs an input mapping.
  45. virtual ~mapping() = default;
  46. /// Returns the input mapping type.
  47. [[nodiscard]] virtual constexpr mapping_type get_mapping_type() const noexcept = 0;
  48. };
  49. /**
  50. * Maps a direction along a gamepad axis to a control input value.
  51. */
  52. class gamepad_axis_mapping: public mapping
  53. {
  54. public:
  55. /**
  56. * Constructs a gamepad axis mapping.
  57. *
  58. * @param gamepad Pointer to the gamepad to map, or `nullptr` if input from any gamepad will be mapped.
  59. * @param axis Gamepad axis to map.
  60. * @param direction Sign bit of the direction to map.
  61. */
  62. /// @{
  63. gamepad_axis_mapping(input::gamepad* gamepad, gamepad_axis axis, bool direction);
  64. gamepad_axis_mapping() = default;
  65. /// @}
  66. /// Destructs a gamepad axis mapping.
  67. virtual ~gamepad_axis_mapping() = default;
  68. /// Returns mapping_type::gamepad_axis.
  69. [[nodiscard]] inline virtual constexpr mapping_type get_mapping_type() const noexcept
  70. {
  71. return mapping_type::gamepad_axis;
  72. }
  73. /// Pointer to the mapped gamepad, or `nullptr` if input from any gamepad is accepted.
  74. input::gamepad* gamepad;
  75. /// Mapped gamepad axis.
  76. gamepad_axis axis;
  77. /// Sign bit of the mapped direction.
  78. bool direction;
  79. };
  80. /**
  81. * Maps a gamepad button to a control input value.
  82. */
  83. class gamepad_button_mapping: public mapping
  84. {
  85. public:
  86. /**
  87. * Constructs a gamepad button mapping.
  88. *
  89. * @param gamepad Pointer to the gamepad to map, or `nullptr` if input from any gamepad will be mapped.
  90. * @param button Gamepad button to map.
  91. */
  92. /// @{
  93. gamepad_button_mapping(input::gamepad* gamepad, gamepad_button button);
  94. gamepad_button_mapping() = default;
  95. /// @}
  96. /// Destructs a gamepad button mapping.
  97. virtual ~gamepad_button_mapping() = default;
  98. /// Returns mapping_type::gamepad_button.
  99. [[nodiscard]] inline virtual constexpr mapping_type get_mapping_type() const noexcept
  100. {
  101. return mapping_type::gamepad_button;
  102. }
  103. /// Pointer to the mapped gamepad, or `nullptr` if input from any gamepad is accepted.
  104. input::gamepad* gamepad;
  105. /// Mapped gamepad button.
  106. gamepad_button button;
  107. };
  108. /**
  109. * Maps a keyboard key to a control input value.
  110. */
  111. class key_mapping: public mapping
  112. {
  113. public:
  114. /**
  115. * Constructs a key mapping.
  116. *
  117. * @param keyboard Pointer to the keyboard to map, or `nullptr` if input from any keyboard will be mapped.
  118. * @param scancode Scancode of the key to map.
  119. * @param repeat `false` if the mapping should ignore key repeats, `true` otherwise.
  120. */
  121. /// @{
  122. key_mapping(input::keyboard* keyboard, input::scancode scancode, bool repeat = false);
  123. key_mapping() = default;
  124. /// @}
  125. /// Destructs a keyboard key mapping.
  126. virtual ~key_mapping() = default;
  127. /// Returns mapping_type::key.
  128. [[nodiscard]] inline virtual constexpr mapping_type get_mapping_type() const noexcept
  129. {
  130. return mapping_type::key;
  131. }
  132. /// Pointer to the mapped keyboard, or `nullptr` if input from any keyboard is accepted.
  133. input::keyboard* keyboard;
  134. /// Scancode of the mapped key.
  135. scancode scancode;
  136. /// `false` if the mapping ignores key repeats, `true` otherwise.
  137. bool repeat;
  138. };
  139. /**
  140. * Maps a mouse button to a control input value.
  141. */
  142. class mouse_button_mapping: public mapping
  143. {
  144. public:
  145. /**
  146. * Constructs a mouse button mapping.
  147. *
  148. * @param mouse Pointer to the mouse to map, or `nullptr` if input from any mouse will be mapped.
  149. * @param button Mouse button to map.
  150. */
  151. /// @{
  152. mouse_button_mapping(input::mouse* mouse, mouse_button button);
  153. mouse_button_mapping() = default;
  154. /// @}
  155. /// Destructs a mouse button mapping.
  156. virtual ~mouse_button_mapping() = default;
  157. /// Returns mapping_type::mouse_button.
  158. [[nodiscard]] inline virtual constexpr mapping_type get_mapping_type() const noexcept
  159. {
  160. return mapping_type::mouse_button;
  161. }
  162. /// Pointer to the mapped mouse, or `nullptr` if input from any mouse is accepted.
  163. input::mouse* mouse;
  164. /// Mapped mouse button.
  165. mouse_button button;
  166. };
  167. /**
  168. * Maps a direction along a mouse motion axis to a control input value.
  169. */
  170. class mouse_motion_mapping: public mapping
  171. {
  172. public:
  173. /**
  174. * Constructs a mouse motion mapping.
  175. *
  176. * @param mouse Pointer to the mouse to map, or `nullptr` if input from any mouse will be mapped.
  177. * @param axis Mouse motion axis to map.
  178. * @param direction Sign bit of the direction to map.
  179. */
  180. /// @{
  181. mouse_motion_mapping(input::mouse* mouse, mouse_motion_axis axis, bool direction);
  182. mouse_motion_mapping() = default;
  183. /// @}
  184. /// Destructs a mouse motion mapping.
  185. virtual ~mouse_motion_mapping() = default;
  186. /// Returns mapping_type::mouse_motion.
  187. [[nodiscard]] inline virtual constexpr mapping_type get_mapping_type() const noexcept
  188. {
  189. return mapping_type::mouse_motion;
  190. }
  191. /// Pointer to the mapped mouse, or `nullptr` if input from any mouse is accepted.
  192. input::mouse* mouse;
  193. /// Mapped mouse motion axis.
  194. mouse_motion_axis axis;
  195. /// Sign bit of the mapped direction.
  196. bool direction;
  197. };
  198. /**
  199. * Maps a direction along a mouse scroll axis to a control input value.
  200. */
  201. class mouse_scroll_mapping: public mapping
  202. {
  203. public:
  204. /**
  205. * Constructs a mouse scroll mapping.
  206. *
  207. * @param control Control to which input will be mapped.
  208. * @param mouse Pointer to the mouse to map, or `nullptr` if input from any mouse will be mapped.
  209. * @param axis Mouse scroll axis to map.
  210. * @param direction Sign bit of the direction to map.
  211. */
  212. /// @{
  213. mouse_scroll_mapping(input::mouse* mouse, mouse_scroll_axis axis, bool direction);
  214. mouse_scroll_mapping() = default;
  215. /// @}
  216. /// Destructs a mouse scroll mapping.
  217. virtual ~mouse_scroll_mapping() = default;
  218. /// Returns mapping_type::mouse_scroll.
  219. [[nodiscard]] inline virtual constexpr mapping_type get_mapping_type() const noexcept
  220. {
  221. return mapping_type::mouse_scroll;
  222. }
  223. /// Pointer to the mapped mouse, or `nullptr` if input from any mouse is accepted.
  224. input::mouse* mouse;
  225. /// Mapped mouse scroll axis.
  226. mouse_scroll_axis axis;
  227. /// Sign bit of the mapped direction.
  228. bool direction;
  229. };
  230. } // namespace input
  231. #endif // ANTKEEPER_INPUT_MAPPING_HPP