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

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

input: Removed previous input system

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 4.2 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    /** Get the name of this input device */
23    const String& GetName();
24
25    /** Get the index of the corresponding key, needed to call GetKey */
26    int GetKeyIndex(const char* name) const { return GetItemIndex(name, m_keynames); }
27    /** Get the index of the corresponding axis, needed to call GetAxis */
28    int GetAxisIndex(const char* name) const { return GetItemIndex(name, m_axisnames); }
29    /** Get the index of the corresponding cursor, needed to call GetCursor */
30    int GetCursorIndex(const char* name) const { return GetItemIndex(name, m_cursornames); }
31
32    /** Get 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    /** 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 */
35    float GetAxis(int index) const { return m_axis[index].m1 * m_axis[index].m2; }
36    /** Get 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    /** Get 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    /** 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 */
43    void SetAxisSensitivity(int index, float sensitivity) { m_axis[index].m2 = sensitivity; }
44    /** 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 */
45    float GetAxisSensitivity(int index) const { return m_axis[index].m2; }
46
47    /** Get a list of the name of all available keys in this device */
48    const Array<String>& GetAllKeys() const { return m_keynames; }
49    /** Get a list of the name of all available axis in this device */
50    const Array<String>& GetAllAxis() const { return m_axisnames; }
51    /** Get a list of the name of all available cursors in this device */
52    const Array<String>& GetAllCursors() const { return m_cursornames; }
53
54    /** Get an input device by its name */
55    static InputDevice* Get(const char* name) { return GetDevice(name); }
56    /** Set whether the mouse cursor should be captured. */
57    static void CaptureMouse(bool activated) { m_capturemouse = activated; }
58
59protected:
60    // TODO: hide all of this in a InputDeviceData?
61
62    String m_name;
63
64    Array<String> m_keynames;
65    Array<String> m_axisnames;
66    Array<String> m_cursornames;
67
68    /** key states (pressed/released) */
69    Array<bool> m_keys;
70    /** axis states (value and sensitivity) */
71    Array<float, float> m_axis;
72    /** cursor position */
73    Array<vec2, ivec2> m_cursors;
74
75    static bool m_capturemouse;
76
77    InputDevice(const char* name) : m_name(name)
78    {
79        devices.Push(this);
80    }
81
82    ~InputDevice()
83    {
84        for (int i = 0; i < devices.Count(); ++i)
85        {
86            if (devices[i] == this)
87            {
88                devices.Remove(i);
89                return;
90            }
91        }
92    }
93
94private:
95    static Array<InputDevice*> devices;
96
97    template <typename T>
98    int GetItemIndex(const char* name, const Array<String, T>& Array) const
99    {
100        for (int i = 0; i < Array.Count(); ++i)
101        {
102            if (Array[i] == name)
103                return i;
104        }
105        return -1;
106    }
107
108    static InputDevice* GetDevice(const char* name)
109    {
110        for (int i = 0; i < devices.Count(); ++i)
111        {
112            if (devices[i]->m_name == name)
113                return devices[i];
114        }
115        return nullptr;
116    }
117};
118
119} /* namespace lol */
120
121#endif // __LOL_INPUT_H__
122
Note: See TracBrowser for help on using the repository browser.