source: trunk/src/input/inputdevice.h @ 2810

Last change on this file since 2810 was 2810, checked in by benlitz, 8 years ago

Completely reworked input system. Unified device interface, button, axis and cursor support, controller and binding system, mouse capture. Tutorial 07_input provided. Require to compile lolcore with LOL_INPUT_V2 to be activated (also needed app-side before including core.h)

File size: 4.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Benjamin Litzelmann
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#if !defined __LOL_INPUT_DEVICE_H__
12#define __LOL_INPUT_DEVICE_H__
13
14#if defined LOL_INPUT_V2
15
16#include "core.h"
17
18namespace lol
19{
20
21class InputDevice
22{
23public:
24    /** Get the name of this input device */
25    const String& GetName();
26
27    /** Get the index of the corresponding key, needed to call GetKey */
28    int GetKeyIndex(const char* name) const { return GetItemIndex(name, m_keynames); }
29    /** Get the index of the corresponding axis, needed to call GetAxis */
30    int GetAxisIndex(const char* name) const { return GetItemIndex(name, m_axisnames); }
31    /** Get the index of the corresponding cursor, needed to call GetCursor */
32    int GetCursorIndex(const char* name) const { return GetItemIndex(name, m_cursornames); }
33
34    /** Get the current state of the given key, true being pressed and false being released */
35    bool GetKey(int index) const { return m_keys[index]; }
36    /** Get the current value of the given axis. Devices should cap this value between -1 and 1 as much as possible, through it is not guaranteed */
37    float GetAxis(int index) const { return m_axis[index].m1 * m_axis[index].m2; }
38    /** Get the current value of the given cursor, 0,0 being the bottom-left corner and 1,1 being the top-right corner */
39    vec2 GetCursor(int index) const { return m_cursors[index].m1; }
40    /** Get the coordinate of the pixel the cursor is currently over, 0,0 being the bottom-left corner. */
41    ivec2 GetCursorPixel(int index) const { return m_cursors[index].m2; }
42
43
44    /** Set a per-device-axis sensitivity factor. The value returned by the operating system will be multiplied by this value before being returned by GetAxis */
45    void SetAxisSensitivity(int index, float sensitivity) { m_axis[index].m2 = sensitivity; }
46    /** Get the per-device-axis sensitivity factor. The value returned by the operating system will be multiplied by this value before being returned by GetAxis */
47    float GetAxisSensitivity(int index) const { return m_axis[index].m2; }
48
49    /** Get a list of the name of all available keys in this device */
50    const Array<String>& GetAllKeys() const { return m_keynames; }
51    /** Get a list of the name of all available axis in this device */
52    const Array<String>& GetAllAxis() const { return m_axisnames; }
53    /** Get a list of the name of all available cursors in this device */
54    const Array<String>& GetAllCursors() const { return m_cursornames; }
55
56    /** Get an input device by its name */
57    static InputDevice* Get(const char* name) { return GetDevice(name); }
58        /** Set whether the mouse cursor should be captured. */
59        static void CaptureMouse(bool activated) { m_capturemouse = activated; }
60
61protected:
62        // TODO: hide all of this in a InputDeviceData?
63
64    String m_name;
65
66    Array<String> m_keynames;
67    Array<String> m_axisnames;
68    Array<String> m_cursornames;
69
70    /** key states (pressed/released) */
71    Array<bool> m_keys;
72    /** axis states (value and sensitivity) */
73    Array<float, float> m_axis;
74    /** cursor position */
75    Array<vec2, ivec2> m_cursors;
76
77        static bool m_capturemouse;
78
79    InputDevice(const char* name) : m_name(name)
80    {
81        devices.Push(this);
82    }
83
84    ~InputDevice()
85    {
86        for (int i = 0; i < devices.Count(); ++i)
87        {
88            if (devices[i] == this)
89            {
90                devices.Remove(i);
91                return;
92            }
93        }
94    }
95
96private:
97    static Array<InputDevice*> devices;
98
99    template <typename T>
100    int GetItemIndex(const char* name, const Array<String, T>& Array) const
101    {
102        for (int i = 0; i < Array.Count(); ++i)
103        {
104            if (Array[i] == name)
105                return i;
106        }
107        return -1;
108    }
109
110    static InputDevice* GetDevice(const char* name)
111    {
112        for (int i = 0; i < devices.Count(); ++i)
113        {
114            if (devices[i]->m_name == name)
115                return devices[i];
116        }
117        return nullptr;
118    }
119};
120
121} /* namespace lol */
122
123#endif // LOL_INPUT_V2
124
125#endif // __LOL_INPUT_DEVICE_H__
126
Note: See TracBrowser for help on using the repository browser.