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.
 
 
 

462 lines
9.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. //
  11. // The Input static class
  12. // ----------------------
  13. //
  14. #if !defined __LOL_INPUT_INPUT_H__
  15. #define __LOL_INPUT_INPUT_H__
  16. #include <cstring>
  17. #include <string.h>
  18. #include "core.h"
  19. #include "lol/math/vector.h"
  20. #include "input/stick.h"
  21. namespace lol
  22. {
  23. #define ACTION_TYPE std::string
  24. #define ACTION_CMP(A, B) (A.compare(B) == 0)
  25. class WorldEntity;
  26. //FULL Key rip-off of the SDLK enum
  27. struct Key
  28. {
  29. enum Value
  30. {
  31. /** @name ASCII mapped keysyms
  32. * The keyboard syms have been cleverly chosen to map to ASCII
  33. */
  34. /*@{*/
  35. K_UNKNOWN = 0,
  36. K_FIRST = 0,
  37. K_BACKSPACE = 8,
  38. K_TAB = 9,
  39. K_CLEAR = 12,
  40. K_RETURN = 13,
  41. K_PAUSE = 19,
  42. K_ESCAPE = 27,
  43. K_SPACE = 32,
  44. K_EXCLAIM = 33,
  45. K_QUOTEDBL = 34,
  46. K_HASH = 35,
  47. K_DOLLAR = 36,
  48. K_AMPERSAND = 38,
  49. K_QUOTE = 39,
  50. K_LEFTPAREN = 40,
  51. K_RIGHTPAREN = 41,
  52. K_ASTERISK = 42,
  53. K_PLUS = 43,
  54. K_COMMA = 44,
  55. K_MINUS = 45,
  56. K_PERIOD = 46,
  57. K_SLASH = 47,
  58. K_0 = 48,
  59. K_1 = 49,
  60. K_2 = 50,
  61. K_3 = 51,
  62. K_4 = 52,
  63. K_5 = 53,
  64. K_6 = 54,
  65. K_7 = 55,
  66. K_8 = 56,
  67. K_9 = 57,
  68. K_COLON = 58,
  69. K_SEMICOLON = 59,
  70. K_LESS = 60,
  71. K_EQUALS = 61,
  72. K_GREATER = 62,
  73. K_QUESTION = 63,
  74. K_AT = 64,
  75. /*
  76. Skip uppercase letters
  77. */
  78. K_LEFTBRACKET = 91,
  79. K_BACKSLASH = 92,
  80. K_RIGHTBRACKET = 93,
  81. K_CARET = 94,
  82. K_UNDERSCORE = 95,
  83. K_BACKQUOTE = 96,
  84. K_a = 97,
  85. K_b = 98,
  86. K_c = 99,
  87. K_d = 100,
  88. K_e = 101,
  89. K_f = 102,
  90. K_g = 103,
  91. K_h = 104,
  92. K_i = 105,
  93. K_j = 106,
  94. K_k = 107,
  95. K_l = 108,
  96. K_m = 109,
  97. K_n = 110,
  98. K_o = 111,
  99. K_p = 112,
  100. K_q = 113,
  101. K_r = 114,
  102. K_s = 115,
  103. K_t = 116,
  104. K_u = 117,
  105. K_v = 118,
  106. K_w = 119,
  107. K_x = 120,
  108. K_y = 121,
  109. K_z = 122,
  110. K_DELETE = 127,
  111. /* End of ASCII mapped keysyms */
  112. /*@}*/
  113. /** @name International keyboard syms */
  114. /*@{*/
  115. K_WORLD_0 = 160, /* 0xA0 */
  116. K_WORLD_1 = 161,
  117. K_WORLD_2 = 162,
  118. K_WORLD_3 = 163,
  119. K_WORLD_4 = 164,
  120. K_WORLD_5 = 165,
  121. K_WORLD_6 = 166,
  122. K_WORLD_7 = 167,
  123. K_WORLD_8 = 168,
  124. K_WORLD_9 = 169,
  125. K_WORLD_10 = 170,
  126. K_WORLD_11 = 171,
  127. K_WORLD_12 = 172,
  128. K_WORLD_13 = 173,
  129. K_WORLD_14 = 174,
  130. K_WORLD_15 = 175,
  131. K_WORLD_16 = 176,
  132. K_WORLD_17 = 177,
  133. K_WORLD_18 = 178,
  134. K_WORLD_19 = 179,
  135. K_WORLD_20 = 180,
  136. K_WORLD_21 = 181,
  137. K_WORLD_22 = 182,
  138. K_WORLD_23 = 183,
  139. K_WORLD_24 = 184,
  140. K_WORLD_25 = 185,
  141. K_WORLD_26 = 186,
  142. K_WORLD_27 = 187,
  143. K_WORLD_28 = 188,
  144. K_WORLD_29 = 189,
  145. K_WORLD_30 = 190,
  146. K_WORLD_31 = 191,
  147. K_WORLD_32 = 192,
  148. K_WORLD_33 = 193,
  149. K_WORLD_34 = 194,
  150. K_WORLD_35 = 195,
  151. K_WORLD_36 = 196,
  152. K_WORLD_37 = 197,
  153. K_WORLD_38 = 198,
  154. K_WORLD_39 = 199,
  155. K_WORLD_40 = 200,
  156. K_WORLD_41 = 201,
  157. K_WORLD_42 = 202,
  158. K_WORLD_43 = 203,
  159. K_WORLD_44 = 204,
  160. K_WORLD_45 = 205,
  161. K_WORLD_46 = 206,
  162. K_WORLD_47 = 207,
  163. K_WORLD_48 = 208,
  164. K_WORLD_49 = 209,
  165. K_WORLD_50 = 210,
  166. K_WORLD_51 = 211,
  167. K_WORLD_52 = 212,
  168. K_WORLD_53 = 213,
  169. K_WORLD_54 = 214,
  170. K_WORLD_55 = 215,
  171. K_WORLD_56 = 216,
  172. K_WORLD_57 = 217,
  173. K_WORLD_58 = 218,
  174. K_WORLD_59 = 219,
  175. K_WORLD_60 = 220,
  176. K_WORLD_61 = 221,
  177. K_WORLD_62 = 222,
  178. K_WORLD_63 = 223,
  179. K_WORLD_64 = 224,
  180. K_WORLD_65 = 225,
  181. K_WORLD_66 = 226,
  182. K_WORLD_67 = 227,
  183. K_WORLD_68 = 228,
  184. K_WORLD_69 = 229,
  185. K_WORLD_70 = 230,
  186. K_WORLD_71 = 231,
  187. K_WORLD_72 = 232,
  188. K_WORLD_73 = 233,
  189. K_WORLD_74 = 234,
  190. K_WORLD_75 = 235,
  191. K_WORLD_76 = 236,
  192. K_WORLD_77 = 237,
  193. K_WORLD_78 = 238,
  194. K_WORLD_79 = 239,
  195. K_WORLD_80 = 240,
  196. K_WORLD_81 = 241,
  197. K_WORLD_82 = 242,
  198. K_WORLD_83 = 243,
  199. K_WORLD_84 = 244,
  200. K_WORLD_85 = 245,
  201. K_WORLD_86 = 246,
  202. K_WORLD_87 = 247,
  203. K_WORLD_88 = 248,
  204. K_WORLD_89 = 249,
  205. K_WORLD_90 = 250,
  206. K_WORLD_91 = 251,
  207. K_WORLD_92 = 252,
  208. K_WORLD_93 = 253,
  209. K_WORLD_94 = 254,
  210. K_WORLD_95 = 255, /* 0xFF */
  211. /*@}*/
  212. /** @name Numeric keypad */
  213. /*@{*/
  214. K_KP0 = 256,
  215. K_KP1 = 257,
  216. K_KP2 = 258,
  217. K_KP3 = 259,
  218. K_KP4 = 260,
  219. K_KP5 = 261,
  220. K_KP6 = 262,
  221. K_KP7 = 263,
  222. K_KP8 = 264,
  223. K_KP9 = 265,
  224. K_KP_PERIOD = 266,
  225. K_KP_DIVIDE = 267,
  226. K_KP_MULTIPLY = 268,
  227. K_KP_MINUS = 269,
  228. K_KP_PLUS = 270,
  229. K_KP_ENTER = 271,
  230. K_KP_EQUALS = 272,
  231. /*@}*/
  232. /** @name Arrows + Home/End pad */
  233. /*@{*/
  234. K_UP = 273,
  235. K_DOWN = 274,
  236. K_RIGHT = 275,
  237. K_LEFT = 276,
  238. K_INSERT = 277,
  239. K_HOME = 278,
  240. K_END = 279,
  241. K_PAGEUP = 280,
  242. K_PAGEDOWN = 281,
  243. /*@}*/
  244. /** @name Function keys */
  245. /*@{*/
  246. K_F1 = 282,
  247. K_F2 = 283,
  248. K_F3 = 284,
  249. K_F4 = 285,
  250. K_F5 = 286,
  251. K_F6 = 287,
  252. K_F7 = 288,
  253. K_F8 = 289,
  254. K_F9 = 290,
  255. K_F10 = 291,
  256. K_F11 = 292,
  257. K_F12 = 293,
  258. K_F13 = 294,
  259. K_F14 = 295,
  260. K_F15 = 296,
  261. /*@}*/
  262. /** @name Key state modifier keys */
  263. /*@{*/
  264. K_NUMLOCK = 300,
  265. K_CAPSLOCK = 301,
  266. K_SCROLLOCK = 302,
  267. K_RSHIFT = 303,
  268. K_LSHIFT = 304,
  269. K_RCTRL = 305,
  270. K_LCTRL = 306,
  271. K_RALT = 307,
  272. K_LALT = 308,
  273. K_RMETA = 309,
  274. K_LMETA = 310,
  275. K_LSUPER = 311, /**< Left "Windows" key */
  276. K_RSUPER = 312, /**< Right "Windows" key */
  277. K_MODE = 313, /**< "Alt Gr" key */
  278. K_COMPOSE = 314, /**< Multi-key compose key */
  279. /*@}*/
  280. /** @name Miscellaneous function keys */
  281. /*@{*/
  282. K_HELP = 315,
  283. K_PRINT = 316,
  284. K_SYSREQ = 317,
  285. K_BREAK = 318,
  286. K_MENU = 319,
  287. K_POWER = 320, /**< Power Macintosh power key */
  288. K_EURO = 321, /**< Some european keyboards */
  289. K_UNDO = 322, /**< Atari keyboard has Undo */
  290. /*@}*/
  291. /* Add any other keys here */
  292. K_LAST
  293. }
  294. m_value;
  295. //BH : Removed KMod from main enum, because I don't have any idea about handling them correctly for now.
  296. /*
  297. //Enumeration of valid key mods (possibly OR'd together)
  298. KM_NONE = 0x0000,
  299. KM_LSHIFT = 0x0001,
  300. KM_RSHIFT = 0x0002,
  301. KM_LCTRL = 0x0040,
  302. KM_RCTRL = 0x0080,
  303. KM_LALT = 0x0100,
  304. KM_RALT = 0x0200,
  305. KM_LMETA = 0x0400,
  306. KM_RMETA = 0x0800,
  307. KM_NUM = 0x1000,
  308. KM_CAPS = 0x2000,
  309. KM_MODE = 0x4000,
  310. KM_RESERVED = 0x8000,
  311. //Left/Right independent key mods definition
  312. KM_CTRL = (KM_LCTRL|KM_RCTRL),
  313. KM_SHIFT = (KM_LSHIFT|KM_RSHIFT),
  314. KM_ALT = (KM_LALT|KM_RALT),
  315. KM_META = (KM_LMETA|KM_RMETA),
  316. */
  317. inline Key(Value v) { m_value = v; }
  318. inline operator Value() { return m_value; }
  319. inline bool operator==(const Key& CompareButton) { return m_value == CompareButton.m_value; }
  320. };
  321. struct ActionSetting
  322. {
  323. ACTION_TYPE m_action;
  324. float m_buffering_time;
  325. float m_buffered_since;
  326. ActionSetting(ACTION_TYPE NewAction) :
  327. m_action(NewAction),
  328. m_buffering_time(.0f),
  329. m_buffered_since(.0f)
  330. { }
  331. };
  332. struct ButtonSetting
  333. {
  334. Key m_raw_button;
  335. Array<ActionSetting> m_associated_action_list;
  336. ButtonSetting(Key NewRawButton)
  337. : m_raw_button(NewRawButton) { }
  338. int GetActionSettingIdx(ACTION_TYPE SearchAction);
  339. };
  340. class InputTracker : public Entity
  341. {
  342. friend class Input;
  343. public:
  344. InputTracker();
  345. private:
  346. Array<uint8_t> m_input_status;
  347. Array<ButtonSetting> m_input_assocation_list;
  348. int GetButtonSettingIdx(struct Key Button);
  349. int GetCurrentButtonStatus(struct Key Button);
  350. int GetPreviousButtonStatus(struct Key Button);
  351. void UpdateActionStatus(float seconds);
  352. protected:
  353. virtual char const * GetName()
  354. {
  355. return "<InputTracker>";
  356. }
  357. virtual void TickGame(float seconds)
  358. {
  359. Entity::TickGame(seconds);
  360. UpdateActionStatus(seconds);
  361. }
  362. void LinkActionToKey(ACTION_TYPE Action, struct Key Button);
  363. void UnlinkAction(ACTION_TYPE Action);
  364. int GetStatus(ACTION_TYPE Action);
  365. bool WasPressed(ACTION_TYPE Action);
  366. bool WasReleased(ACTION_TYPE Action);
  367. //You should probably use the Action System
  368. int GetStatus(Key Button);
  369. bool WasPressed(Key Button);
  370. bool WasReleased(Key Button);
  371. };
  372. class Input
  373. {
  374. private:
  375. static InputTracker* m_input_tracker;
  376. static bool CheckInputTrackerInit()
  377. {
  378. if (Input::m_input_tracker)
  379. return true;
  380. Input::m_input_tracker = new InputTracker();
  381. return true;
  382. }
  383. public:
  384. /* These methods are general queries */
  385. static ivec2 GetMousePos();
  386. static ivec3 GetMouseButtons();
  387. //BH : Shouldn't use this
  388. static int GetButtonState(int button);
  389. /* Action management */
  390. static void LinkActionToKey(ACTION_TYPE Action, struct Key Button);
  391. static void UnlinkAction(ACTION_TYPE Action);
  392. static int GetStatus(ACTION_TYPE Action);
  393. static bool WasPressed(ACTION_TYPE Action);
  394. static bool WasReleased(ACTION_TYPE Action);
  395. /* Raw Button management. You should use actions. */
  396. static int GetStatus(Key Button);
  397. static bool WasPressed(Key Button);
  398. static bool WasReleased(Key Button);
  399. /* Entities can subscribe to events */
  400. static void TrackMouse(WorldEntity *e);
  401. static void UntrackMouse(WorldEntity *e);
  402. /* These methods are called by the underlying input listeners */
  403. static void SetMousePos(ivec2 coord);
  404. static void SetMouseButton(int index);
  405. static void UnsetMouseButton(int index);
  406. /* Joystick handling */
  407. static Stick *CreateStick();
  408. static void DestroyStick(Stick *stick);
  409. static Stick *TrackStick(int desired);
  410. static void UntrackStick(Stick *stick);
  411. };
  412. } /* namespace lol */
  413. #endif // __LOL_INPUT_INPUT_H__