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

Last change on this file since 2918 was 2918, checked in by benlitz, 6 years ago

input: added a GetAvailableDevices function to retrieve device names.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
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_H__
12#define __LOL_INPUT_H__
13
14#include "core.h"
15
16namespace lol
17{
18
19class InputDevice
20{
21public:
22    /** Gets the name of this input device */
23    const String& GetName();
24
25    /** Gets the index of the corresponding key, needed to call GetKey */
26    int GetKeyIndex(const char* name) const { return GetItemIndex(name, m_keynames); }
27    /** Gets the index of the corresponding axis, needed to call GetAxis */
28    int GetAxisIndex(const char* name) const { return GetItemIndex(name, m_axisnames); }
29    /** Gets the index of the corresponding cursor, needed to call GetCursor */
30    int GetCursorIndex(const char* name) const { return GetItemIndex(name, m_cursornames); }
31
32    /** Gets the current state of the given key, true being pressed and false being released */
33    bool GetKey(int index) const { return m_keys[index]; }
34    /** Gets 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 */
35    float GetAxis(int index) const { return m_axis[index].m1 * m_axis[index].m2; }
36    /** Gets the current value of the given cursor, 0,0 being the bottom-left corner and 1,1 being the top-right corner */
37    vec2 GetCursor(int index) const { return m_cursors[index].m1; }
38    /** Gets the coordinate of the pixel the cursor is currently over, 0,0 being the bottom-left corner. */
39    ivec2 GetCursorPixel(int index) const { return m_cursors[index].m2; }
40
41
42    /** Sets a per-device-axis sensitivity factor. The value returned by the operating system will be multiplied by this value before being returned by GetAxis */
43    void SetAxisSensitivity(int index, float sensitivity) { m_axis[index].m2 = sensitivity; }
44    /** Gets the per-device-axis sensitivity factor. The value returned by the operating system will be multiplied by this value before being returned by GetAxis */
45    float GetAxisSensitivity(int index) const { return m_axis[index].m2; }
46
47    /** Gets a list of the name of all available keys in this device */
48    const Array<String>& GetAllKeys() const { return m_keynames; }
49    /** Gets a list of the name of all available axis in this device */
50    const Array<String>& GetAllAxis() const { return m_axisnames; }
51    /** Gets a list of the name of all available cursors in this device */
52    const Array<String>& GetAllCursors() const { return m_cursornames; }
53
54    /** Gets a list of the name of all available input devices */
55    static Array<String> GetAvailableDevices();
56    /** Gets an input device by its name */
57    static InputDevice* Get(const char* name) { return GetDevice(name); }
58    /** Sets 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_H__
124
Note: See TracBrowser for help on using the repository browser.