25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

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