source: trunk/src/input/input.h @ 1795

Last change on this file since 1795 was 1795, checked in by touky, 11 years ago

Fixed Entity::TickGame log flood.
Added BulletCharacterController.cpp & BulletCharacterController.h stubs.

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