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

Last change on this file since 1803 was 1803, checked in by sam, 7 years ago

input: switch all key names to camel case, like our other enums.

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