source: trunk/src/input/controller.h @ 2925

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

input: added a name the controllers, and a static Get() function to easily retrieve a registered controller

  • Property svn:eol-style set to LF
File size: 3.9 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_CONTROLLER_H__
12#define __LOL_INPUT_CONTROLLER_H__
13
14#include "core.h"
15
16namespace lol
17{
18
19class KeyBinding
20{
21public:
22    KeyBinding() : m_device(nullptr), m_current(false), m_previous(false), m_keyindex(-1) {}
23
24    /** Indicate wheither the key is currently down */
25    bool IsDown() const { return m_current; }
26    /** Indicate wheither the key is currently up */
27    bool IsUp() const { return !m_current; }
28    /** Indicate wheither the key has just been pressed */
29    bool IsPressed() const { return m_current && !m_previous; }
30    /** Indicate wheither the key has just been released */
31    bool IsReleased() const { return !m_current && m_previous; }
32
33    /** Bind a physical device and key */
34    void Bind(const char* device_name, const char* key_name);
35    /** Clear current binding */
36    void ClearBinding();
37
38    /** Indicate wheither a physical device and key has been bound */
39    bool IsBound() { return m_device && m_keyindex != -1; }
40
41protected:
42    void Update() { m_previous = m_current; m_current = IsBound() ? m_device->GetKey(m_keyindex) : false; }
43
44    const InputDevice* m_device;
45    int m_keyindex;
46    bool m_current;
47    bool m_previous;
48
49    friend class Controller;
50};
51
52class AxisBinding
53{
54public:
55    AxisBinding() : m_device(nullptr), m_current(0.0f), m_previous(0.0f), m_axisindex(-1), m_minkeyindex(-1), m_maxkeyindex(-1) {}
56
57    /** Gets the current absolute value of this axis */
58    float GetValue() const { return m_current; }
59    /** Gets the current delta value of this axis */
60    float GetDelta() const { return m_current - m_previous; }
61
62    /** Bind a physical device and axis */
63    void Bind(const char* device_name, const char* axis_name);
64    /** Bind a physical device and key over this axis. The axis value will be 0 if the key is up and 1 if it's down */
65    void BindKey(const char* device_name, const char* key_name);
66    /** Bind physical device and keys over this axis. The axis value will be 0 if both the key are up, -1 if minkey is down, and 1 if maxkey is down */
67    void BindKeys(const char* device_name, const char* min_key_name, const char* max_key_name);
68    /** Clear current binding */
69    void ClearBinding();
70
71    /** Indicate wheither a physical device and axis has been bound */
72    bool IsBound() { return m_device && m_axisindex != -1 || m_maxkeyindex != -1; }
73
74protected:
75    void Update() { m_previous = m_current; m_current = IsBound() ? RetrieveCurrentValue() : 0.0f; }
76    float RetrieveCurrentValue();
77
78    const InputDevice* m_device;
79    int m_axisindex;
80    int m_minkeyindex;
81    int m_maxkeyindex;
82    float m_current;
83    float m_previous;
84
85    friend class Controller;
86};
87
88
89class Controller : public Entity
90{
91public:
92    Controller(char const* name, int nb_keys, int nb_axis);
93    ~Controller();
94
95    virtual void TickGame(float seconds);
96
97    /** Activate the controller on next frame */
98    void Activate();
99    /** Deactivate the controller on next frame */
100    void Deactivate();
101    /** Deactivate every active controller on next frame and return an array of deactivated (previously active) controllers */
102    static Array<Controller*> DeactivateAll();
103
104    KeyBinding& GetKey(int index) { return m_keys[index]; }
105    AxisBinding& GetAxis(int index) { return m_axis[index]; }
106
107    static Controller* Get(char const* name);
108
109protected:
110    Array<KeyBinding> m_keys;
111    Array<AxisBinding> m_axis;
112
113private:
114    static Array<Controller*> controllers;
115    String m_name;
116    bool m_activate_nextframe;
117    bool m_deactivate_nextframe;
118    bool m_active;
119};
120
121} /* namespace lol */
122
123#endif // __LOL_INPUT_CONTROLLER_H__
124
Note: See TracBrowser for help on using the repository browser.