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

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

input: some refactoring in the action bindings.

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