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

Last change on this file since 2818 was 2818, checked in by sam, 8 years ago

build: fix a few compilation issues with input v2.

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