2
0

Input.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #pragma once
  2. #include "Defines.h"
  3. #include "Types.h"
  4. #include "Signal.h"
  5. namespace gameplay
  6. {
  7. class Window;
  8. enum class Key : uint32_t
  9. {
  10. KEY_UNKNOWN,
  11. KEY_SPACE,
  12. KEY_APOSTROPHE,
  13. KEY_COMMA,
  14. KEY_MINUS,
  15. KEY_PERIOD,
  16. KEY_SLASH,
  17. KEY_0,
  18. KEY_1,
  19. KEY_2,
  20. KEY_3,
  21. KEY_4,
  22. KEY_5,
  23. KEY_6,
  24. KEY_7,
  25. KEY_8,
  26. KEY_9,
  27. KEY_SEMICOLON,
  28. KEY_EQUAL,
  29. KEY_A,
  30. KEY_B,
  31. KEY_C,
  32. KEY_D,
  33. KEY_E,
  34. KEY_F,
  35. KEY_G,
  36. KEY_H,
  37. KEY_I,
  38. KEY_J,
  39. KEY_K,
  40. KEY_L,
  41. KEY_M,
  42. KEY_N,
  43. KEY_O,
  44. KEY_P,
  45. KEY_Q,
  46. KEY_R,
  47. KEY_S,
  48. KEY_T,
  49. KEY_U,
  50. KEY_V,
  51. KEY_W,
  52. KEY_X,
  53. KEY_Y,
  54. KEY_Z,
  55. KEY_LEFT_BRACKET,
  56. KEY_RIGHT_BRACKET,
  57. KEY_BACKSLASH,
  58. KEY_GRAVE_ACCENT,
  59. KEY_ESCAPE,
  60. KEY_TAB,
  61. KEY_ENTER,
  62. KEY_DELETE,
  63. KEY_RIGHT,
  64. KEY_LEFT,
  65. KEY_DOWN,
  66. KEY_UP,
  67. KEY_PAGE_UP,
  68. KEY_PAGE_DOWN,
  69. KEY_HOME,
  70. KEY_END,
  71. KEY_CAPS_LOCK,
  72. KEY_NUM_LOCK,
  73. KEY_PRINT_SCREEN,
  74. KEY_PAUSE,
  75. KEY_F1,
  76. KEY_F2,
  77. KEY_F3,
  78. KEY_F4,
  79. KEY_F5,
  80. KEY_F6,
  81. KEY_F7,
  82. KEY_F8,
  83. KEY_F9,
  84. KEY_F10,
  85. KEY_F11,
  86. KEY_F12,
  87. KEY_NUMPAD_0,
  88. KEY_NUMPAD_1,
  89. KEY_NUMPAD_2,
  90. KEY_NUMPAD_3,
  91. KEY_NUMPAD_4,
  92. KEY_NUMPAD_5,
  93. KEY_NUMPAD_6,
  94. KEY_NUMPAD_7,
  95. KEY_NUMPAD_8,
  96. KEY_NUMPAD_9,
  97. KEY_NUMPAD_DECIMAL,
  98. KEY_NUMPAD_DIVIDE,
  99. KEY_NUMPAD_MULTIPLY,
  100. KEY_NUMPAD_SUBTRACT,
  101. KEY_NUMPAD_ADD,
  102. KEY_NUMPAD_ENTER,
  103. KEY_NUMPAD_EQUAL,
  104. KEY_LEFT_SHIFT,
  105. KEY_LEFT_CONTROL,
  106. KEY_LEFT_ALT,
  107. KEY_LEFT_SUPER,
  108. KEY_RIGHT_SHIFT,
  109. KEY_RIGHT_CONTROL,
  110. KEY_RIGHT_ALT,
  111. KEY_RIGHT_SUPER,
  112. KEY_MENU
  113. };
  114. enum class MouseButton : uint32_t
  115. {
  116. LEFT,
  117. RIGHT,
  118. MIDDLE,
  119. X1,
  120. X2
  121. };
  122. enum class ButtonAction : uint8_t
  123. {
  124. RELEASE,
  125. PRESS
  126. };
  127. enum class KeyAction : uint8_t
  128. {
  129. RELEASE,
  130. PRESS,
  131. REPEAT
  132. };
  133. typedef uint32_t KeyModFlags;
  134. constexpr KeyModFlags KEY_MOD_FLAGS_NONE = 0;
  135. constexpr KeyModFlags KEY_MOD_FLAGS_SHIFT = 1 << 0;
  136. constexpr KeyModFlags KEY_MOD_FLAGS_CONTROL = 1 << 1;
  137. constexpr KeyModFlags KEY_MOD_FLAGS_ALT = 1 << 2;
  138. constexpr KeyModFlags KEY_MOD_FLAGS_SUPER = 1 << 3;
  139. constexpr KeyModFlags KEY_MOD_FLAGS_CAPS_LOCK = 1 << 4;
  140. constexpr KeyModFlags KEY_MOD_FLAGS_NUM_LOCK = 1 << 5;
  141. enum class GamepadButton : uint32_t
  142. {
  143. BUTTON_A,
  144. BUTTON_B,
  145. BUTTON_X,
  146. BUTTON_Y,
  147. LEFT_BUMPER,
  148. RIGHT_BUMPER,
  149. BACK,
  150. START,
  151. GUIDE,
  152. LEFT_THUMB,
  153. RIGHT_THUMB,
  154. DPAD_UP,
  155. DPAD_RIGHT,
  156. DPAD_DOWN,
  157. DPAD_LEFT,
  158. COUNT
  159. };
  160. enum class GamepadAxes : uint32_t
  161. {
  162. LEFT_X,
  163. LEFT_Y,
  164. RIGHT_X,
  165. RIGHT_Y,
  166. LEFT_TRIGGER,
  167. RIGHT_TRIGGER,
  168. COUNT
  169. };
  170. struct GamepadState
  171. {
  172. ButtonAction buttons[15];
  173. float axes[6];
  174. };
  175. enum class GamepadChangeEvent : uint32_t
  176. {
  177. UNKNOWN,
  178. CONNECTED,
  179. DISCONNECTED
  180. };
  181. enum class InputMode : uint32_t
  182. {
  183. STICKY_KEYS,
  184. STICKY_MOUSE_BUTTONS,
  185. LOCK_KEY_MODS,
  186. RAW_MOUSE_MOTION
  187. };
  188. enum class CursorMode : uint32_t
  189. {
  190. NORMAL,
  191. HIDDEN,
  192. DISABLED
  193. };
  194. enum class CursorStandardShape : uint32_t
  195. {
  196. ARROW,
  197. IBEAM,
  198. CROSSHAIR,
  199. HAND,
  200. HRESIZE,
  201. VRESIZE
  202. };
  203. /**
  204. * Defines all static utility for polling for input on the main window.
  205. *
  206. * Used for polling keyboard, mouse/cursor and joystick/gamepad actions.
  207. *
  208. * @see Window for event based input handling.
  209. */
  210. class Input
  211. {
  212. public:
  213. /**
  214. * Sets the position of the coordinates of the cursor.
  215. *
  216. * The position is in screen coordinates, of the cursor relative
  217. * to the upper-left corner of the client area of the specified window.
  218. * The window must have input focus. If the window does not have input
  219. * focus when this function is called, it fails silently.
  220. *
  221. * Do not use this function to implement things like camera controls.
  222. * @see set_cursor_mode with `CursorMode::DISABLED`
  223. * to hide the cursor, transparently re-centers it and provides
  224. * unconstrained cursor motion.
  225. *
  226. * @param pos The coorinates which to set the cursor position to.
  227. */
  228. static void set_cursor_pos(const Double2& pos);
  229. /**
  230. * Gets the position of the coordinates of the cursor.
  231. *
  232. * The position is in screen coordinates, of the cursor relative
  233. * to the upper-left corner of the client area of the specified window.
  234. * The window must have input focus. If the window does not have input
  235. * focus when this function is called, it fails silently.
  236. *
  237. * @return The cursor position coordinates
  238. */
  239. static Double2 get_cursor_pos();
  240. /**
  241. * Sets the mode for the cursor.
  242. *
  243. * CursorMode::NORMAL makes the cursor visible and behaving normally.
  244. * CursorMode::HIDDEN makes the cursor invisible when it is over the content area
  245. * of the window but does not restrict the cursor from leaving.
  246. * CursorMode::DISABLED makes the cursor invisible when it is over the
  247. * content area of the window but does not restrict the cursor from leaving.
  248. *
  249. * @param mode The cursor mode to set to.
  250. */
  251. static void set_cursor_mode(CursorMode mode);
  252. /**
  253. * Gets the mode for the cursor.
  254. *
  255. * CursorMode::NORMAL makes the cursor visible and behaving normally.
  256. * CursorMode::HIDDEN makes the cursor invisible when it is over the content area
  257. * of the window but does not restrict the cursor from leaving.
  258. * CursorMode::DISABLED makes the cursor invisible when it is over the
  259. * content area of the window but does not restrict the cursor from leaving.
  260. *
  261. * @return The current cursor mode.
  262. */
  263. static CursorMode get_cursor_mode();
  264. /**
  265. * Sets (enables/disables) any special input modes.
  266. *
  267. * @param mode The input mode to be set.
  268. * @param enabled true to enable the input mode, false to disable it.
  269. */
  270. static void set_input_mode_enabled(InputMode mode, bool enabled);
  271. /**
  272. * Checks if an input mode is enabled.
  273. *
  274. * @param mode The input mode to be enabled.
  275. * @return true if the input mode is enabled, false if disabled.
  276. */
  277. static bool is_input_mode_enabled(InputMode mode);
  278. /**
  279. * Get the current mouse button action (state).
  280. *
  281. * @param button The button to get the action (state).
  282. * @return The current mouse button action (state)
  283. */
  284. static ButtonAction get_mouse_button_action(MouseButton button);
  285. /**
  286. * Get the current key action (state) of a key.
  287. *
  288. * @param key The key to check.
  289. * @return The current key action (state) of a key.
  290. */
  291. static KeyAction get_key_action(Key key);
  292. /**
  293. * Gets the name of the specified printable key, encoded as UTF-8.
  294. *
  295. * @parm key The key to get the name of.
  296. * @return The name of the specified printable key, encoded as UTF-8.
  297. */
  298. static const char* get_key_name(Key key);
  299. /**
  300. * Signaled when the gamepad change event has occurred.
  301. *
  302. * emits:
  303. * <int32_t> The gamepad index
  304. * <GamepadChangeEvent> The gamepad chagne event.
  305. */
  306. static Signal<int32_t, GamepadChangeEvent> on_gamepad_changed;
  307. /**
  308. * Checks if a gamepad is currently connected.
  309. *
  310. * @param gamepadIndex The index of the gamepad to check.
  311. */
  312. static bool is_gamepad_connected(int32_t gamepadIndex);
  313. /**
  314. * Checks if a gamepad has a properly configured mappings entry.
  315. *
  316. * Based on SDL_GameControllerDB
  317. * https://github.com/gabomdq/SDL_GameControllerDB
  318. * A community sourced database of game controller mappings
  319. * to be used with SDL2 Game Controller functionality.
  320. *
  321. * If it is not mapped. Then it is consider unsupported.
  322. * @return true if the gamepad is supported in the
  323. */
  324. static bool has_gamepad_mappings_db(int32_t gamepadIndex);
  325. /**
  326. * Sets a gamepad mappings db.
  327. *
  328. * Based on SDL_GameControllerDB
  329. * https://github.com/gabomdq/SDL_GameControllerDB
  330. * A community sourced database of game controller mappings
  331. * to be used with SDL2 Game Controller functionality.
  332. *
  333. * @param str The string containing the gamepad mappings.
  334. * @return true if successfully set, false if an error occurred.
  335. */
  336. static bool set_gamepad_mappings_db(const char* str);
  337. /**
  338. * Gets the name of the supported gamepad.
  339. *
  340. * This will only be valid if has_gamepad_mappings_db() is true.
  341. *
  342. * @param gamepadIndex The index of the gamepad to get the name from.
  343. * @return The name of the supported gamepad.
  344. */
  345. static const char* get_gamepad_name(int32_t gamepadIndex);
  346. /**
  347. * Gets the GUID of the supported gamepad.
  348. *
  349. * This will only be valid if has_gamepad_mappings_db() is true.
  350. *
  351. * @param gamepadIndex The index of the gamepad to get the GUID from.
  352. * @return The GUID of the supported gamepad.
  353. */
  354. static const char* get_gamepad_guid(int32_t gamepadIndex);
  355. /**
  356. * Gets the gamepad state of the supported gamepad.
  357. *
  358. * This will only be valid if has_gamepad_mappings_db() is true.
  359. * @param gamepadIndex The index of the gamepad to get the gamepad state from.
  360. * @param gamepadState The pointer to the gamepad state to be updated with state.
  361. */
  362. static void get_gamepad_state(int32_t gamepadIndex, GamepadState* gamepadState);
  363. /**
  364. * Sets user pointer associated with this gamepad .
  365. *
  366. * @param gamepadIndex The gamepad index to associate the user pointer with.
  367. * @param userPtr The user pointer data to associate to the gamepad.
  368. */
  369. static void set_gamepad_user_ptr(int32_t gamepadIndex, void* userPtr);
  370. /**
  371. * Gets the user pointer associated with a gamepad at the specified index.
  372. *
  373. * @param gamepadIndex The gamepad index to get the associate user pointer from.
  374. * @return The user pointer to associated with the gamepad.
  375. */
  376. static void* get_gamepad_user_ptr(int32_t gamepadIndex);
  377. private:
  378. Input();
  379. ~Input();
  380. };
  381. }