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.
 
 
 

461 line
10 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 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://www.wtfpl.net/ 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/keyboard.h"
  21. #include "input/stick.h"
  22. namespace lol
  23. {
  24. typedef std::string Action;
  25. static inline int ACTION_CMP(Action a, Action b) { return a.compare(b) == 0; }
  26. class WorldEntity;
  27. /*
  28. * The key enumeration values match libSDL's so that we can
  29. * easily pass information between Lol and SDL.
  30. */
  31. struct Key
  32. {
  33. enum Value
  34. {
  35. /* ASCII mapped keys */
  36. Unknown = 0,
  37. First = 0,
  38. Backspace = 8,
  39. Tab = 9,
  40. Clear = 12,
  41. Return = 13,
  42. Pause = 19,
  43. Escape = 27,
  44. Space = 32,
  45. Exclaim = 33,
  46. DoubleQuote = 34,
  47. Hash = 35,
  48. Dollar = 36,
  49. Ampersand = 38,
  50. Quote = 39,
  51. LeftParen = 40,
  52. RightParen = 41,
  53. Asterisk = 42,
  54. Plus = 43,
  55. Comma = 44,
  56. Minus = 45,
  57. Period = 46,
  58. Slash = 47,
  59. K0 = 48,
  60. K1 = 49,
  61. K2 = 50,
  62. K3 = 51,
  63. K4 = 52,
  64. K5 = 53,
  65. K6 = 54,
  66. K7 = 55,
  67. K8 = 56,
  68. K9 = 57,
  69. Colon = 58,
  70. Semicolon = 59,
  71. Less = 60,
  72. Equals = 61,
  73. Greater = 62,
  74. Question = 63,
  75. At = 64,
  76. /* XXX: SDL decides to skip uppercase characters */
  77. LeftBracket = 91,
  78. BackSlash = 92,
  79. RightBracket = 93,
  80. Caret = 94,
  81. Underscore = 95,
  82. Backquote = 96,
  83. A = 97,
  84. B = 98,
  85. C = 99,
  86. D = 100,
  87. E = 101,
  88. F = 102,
  89. G = 103,
  90. H = 104,
  91. I = 105,
  92. J = 106,
  93. K = 107,
  94. L = 108,
  95. M = 109,
  96. N = 110,
  97. O = 111,
  98. P = 112,
  99. Q = 113,
  100. R = 114,
  101. S = 115,
  102. T = 116,
  103. U = 117,
  104. V = 118,
  105. W = 119,
  106. X = 120,
  107. Y = 121,
  108. Z = 122,
  109. Delete = 127,
  110. /* International keyboard syms */
  111. World0 = 160, /* 0xA0 */
  112. World1 = 161,
  113. World2 = 162,
  114. World3 = 163,
  115. World4 = 164,
  116. World5 = 165,
  117. World6 = 166,
  118. World7 = 167,
  119. World8 = 168,
  120. World9 = 169,
  121. World10 = 170,
  122. World11 = 171,
  123. World12 = 172,
  124. World13 = 173,
  125. World14 = 174,
  126. World15 = 175,
  127. World16 = 176,
  128. World17 = 177,
  129. World18 = 178,
  130. World19 = 179,
  131. World20 = 180,
  132. World21 = 181,
  133. World22 = 182,
  134. World23 = 183,
  135. World24 = 184,
  136. World25 = 185,
  137. World26 = 186,
  138. World27 = 187,
  139. World28 = 188,
  140. World29 = 189,
  141. World30 = 190,
  142. World31 = 191,
  143. World32 = 192,
  144. World33 = 193,
  145. World34 = 194,
  146. World35 = 195,
  147. World36 = 196,
  148. World37 = 197,
  149. World38 = 198,
  150. World39 = 199,
  151. World40 = 200,
  152. World41 = 201,
  153. World42 = 202,
  154. World43 = 203,
  155. World44 = 204,
  156. World45 = 205,
  157. World46 = 206,
  158. World47 = 207,
  159. World48 = 208,
  160. World49 = 209,
  161. World50 = 210,
  162. World51 = 211,
  163. World52 = 212,
  164. World53 = 213,
  165. World54 = 214,
  166. World55 = 215,
  167. World56 = 216,
  168. World57 = 217,
  169. World58 = 218,
  170. World59 = 219,
  171. World60 = 220,
  172. World61 = 221,
  173. World62 = 222,
  174. World63 = 223,
  175. World64 = 224,
  176. World65 = 225,
  177. World66 = 226,
  178. World67 = 227,
  179. World68 = 228,
  180. World69 = 229,
  181. World70 = 230,
  182. World71 = 231,
  183. World72 = 232,
  184. World73 = 233,
  185. World74 = 234,
  186. World75 = 235,
  187. World76 = 236,
  188. World77 = 237,
  189. World78 = 238,
  190. World79 = 239,
  191. World80 = 240,
  192. World81 = 241,
  193. World82 = 242,
  194. World83 = 243,
  195. World84 = 244,
  196. World85 = 245,
  197. World86 = 246,
  198. World87 = 247,
  199. World88 = 248,
  200. World89 = 249,
  201. World90 = 250,
  202. World91 = 251,
  203. World92 = 252,
  204. World93 = 253,
  205. World94 = 254,
  206. World95 = 255, /* 0xFF */
  207. /* Numeric keypad */
  208. KP0 = 256,
  209. KP1 = 257,
  210. KP2 = 258,
  211. KP3 = 259,
  212. KP4 = 260,
  213. KP5 = 261,
  214. KP6 = 262,
  215. KP7 = 263,
  216. KP8 = 264,
  217. KP9 = 265,
  218. KPPeriod = 266,
  219. KPDivide = 267,
  220. KPMultiply = 268,
  221. KPMinus = 269,
  222. KPPlus = 270,
  223. KPEnter = 271,
  224. KPEquals = 272,
  225. /* Arrows + Home/End pad */
  226. Up = 273,
  227. Down = 274,
  228. Right = 275,
  229. Left = 276,
  230. Insert = 277,
  231. Home = 278,
  232. End = 279,
  233. PageUp = 280,
  234. PageDown = 281,
  235. /* Function keys */
  236. F1 = 282,
  237. F2 = 283,
  238. F3 = 284,
  239. F4 = 285,
  240. F5 = 286,
  241. F6 = 287,
  242. F7 = 288,
  243. F8 = 289,
  244. F9 = 290,
  245. F10 = 291,
  246. F11 = 292,
  247. F12 = 293,
  248. F13 = 294,
  249. F14 = 295,
  250. F15 = 296,
  251. /* Modifier keys */
  252. NumLock = 300,
  253. CapsLock = 301,
  254. ScrollLock= 302,
  255. RightShift = 303,
  256. LeftShift = 304,
  257. RightCtrl = 305,
  258. LeftCtrl = 306,
  259. RightAlt = 307,
  260. LeftAlt = 308,
  261. RightMeta = 309,
  262. LeftMeta = 310,
  263. LeftSuper = 311, /* Left "Windows" key */
  264. RightSuper = 312, /* Right "Windows" key */
  265. Mode = 313, /* "Alt Gr" key */
  266. Compose = 314, /* Multi-key compose key */
  267. /* Miscellaneous function keys */
  268. Help = 315,
  269. Print = 316,
  270. SysReq = 317,
  271. Break = 318,
  272. Menu = 319,
  273. Power = 320, /* Power Macintosh power key */
  274. Euro = 321, /* Some european keyboards */
  275. Undo = 322, /* Atari keyboard has Undo */
  276. /* Add any other keys here */
  277. Last
  278. }
  279. m_value;
  280. //BH : Removed KMod from main enum, because I don't have any idea about handling them correctly for now.
  281. /*
  282. //Enumeration of valid key mods (possibly OR'd together)
  283. KM_NONE = 0x0000,
  284. KM_LSHIFT = 0x0001,
  285. KM_RSHIFT = 0x0002,
  286. KM_LCTRL = 0x0040,
  287. KM_RCTRL = 0x0080,
  288. KM_LALT = 0x0100,
  289. KM_RALT = 0x0200,
  290. KM_LMETA = 0x0400,
  291. KM_RMETA = 0x0800,
  292. KM_NUM = 0x1000,
  293. KM_CAPS = 0x2000,
  294. KM_MODE = 0x4000,
  295. KM_RESERVED = 0x8000,
  296. //Left/Right independent key mods definition
  297. KM_CTRL = (KM_LCTRL|KM_RCTRL),
  298. KM_SHIFT = (KM_LSHIFT|KM_RSHIFT),
  299. KM_ALT = (KM_LALT|KM_RALT),
  300. KM_META = (KM_LMETA|KM_RMETA),
  301. */
  302. inline Key(Value v) { m_value = v; }
  303. inline operator Value() { return m_value; }
  304. inline bool operator==(const Key& val)
  305. {
  306. return m_value == val.m_value;
  307. }
  308. };
  309. struct ActionSetting
  310. {
  311. Action m_action;
  312. float m_buffering_time;
  313. float m_buffered_since;
  314. ActionSetting(Action NewAction)
  315. : m_action(NewAction),
  316. m_buffering_time(.0f),
  317. m_buffered_since(.0f)
  318. { }
  319. };
  320. struct ButtonSetting
  321. {
  322. Key m_raw_button;
  323. Array<ActionSetting> m_associated_action_list;
  324. ButtonSetting(Key NewRawButton)
  325. : m_raw_button(NewRawButton)
  326. { }
  327. int GetActionSettingIdx(Action SearchAction);
  328. };
  329. class InputTracker : public Entity
  330. {
  331. friend class Input;
  332. public:
  333. InputTracker();
  334. private:
  335. Array<uint8_t> m_input_status;
  336. Array<ButtonSetting> m_input_assocation_list;
  337. int GetButtonSettingIdx(struct Key k);
  338. int GetCurrentButtonStatus(struct Key k);
  339. int GetPreviousButtonStatus(struct Key k);
  340. void UpdateActionStatus(float seconds);
  341. protected:
  342. virtual char const * GetName()
  343. {
  344. return "<InputTracker>";
  345. }
  346. virtual void TickGame(float seconds)
  347. {
  348. Entity::TickGame(seconds);
  349. UpdateActionStatus(seconds);
  350. }
  351. void LinkActionToKey(Action a, struct Key k);
  352. void UnlinkAction(Action a);
  353. int GetStatus(Action a);
  354. bool WasPressed(Action a);
  355. bool WasReleased(Action a);
  356. //You should probably use the Action System
  357. int GetStatus(Key k);
  358. bool WasPressed(Key k);
  359. bool WasReleased(Key k);
  360. };
  361. class Input
  362. {
  363. private:
  364. static InputTracker* m_input_tracker;
  365. static bool CheckInputTrackerInit()
  366. {
  367. if (Input::m_input_tracker)
  368. return true;
  369. Input::m_input_tracker = new InputTracker();
  370. return true;
  371. }
  372. public:
  373. /* These methods are general queries */
  374. static ivec2 GetMousePos();
  375. static ivec3 GetMouseButtons();
  376. //BH : Shouldn't use this
  377. static int GetButtonState(int button);
  378. /* Action management */
  379. static void LinkActionToKey(Action a, struct Key k);
  380. static void UnlinkAction(Action a);
  381. static int GetStatus(Action a);
  382. static bool WasPressed(Action a);
  383. static bool WasReleased(Action a);
  384. /* Raw Button management. You should use actions. */
  385. static int GetStatus(Key k);
  386. static bool WasPressed(Key k);
  387. static bool WasReleased(Key k);
  388. /* Entities can subscribe to events */
  389. static void TrackMouse(WorldEntity *e);
  390. static void UntrackMouse(WorldEntity *e);
  391. /* These methods are called by the underlying input listeners */
  392. /* FIXME: this should disappear and be replaced by an input
  393. * system that abstracts mice */
  394. static void SetMousePos(ivec2 coord);
  395. static void SetMouseButton(int index);
  396. static void UnsetMouseButton(int index);
  397. /* Keyboard handling */
  398. static Keyboard *CreateKeyboard();
  399. static void DestroyKeyboard(Keyboard *keyboard);
  400. static Keyboard *TrackKeyboard(int desired);
  401. static void UntrackKeyboard(Keyboard *keyboard);
  402. /* Joystick handling */
  403. static Stick *CreateStick();
  404. static void DestroyStick(Stick *stick);
  405. static Stick *TrackStick(int desired);
  406. static void UntrackStick(Stick *stick);
  407. };
  408. } /* namespace lol */
  409. #endif // __LOL_INPUT_INPUT_H__