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

Last change on this file since 2825 was 2825, checked in by benlitz, 7 years ago

fixed a crash when updating a binding that has been bound to an unexisting key/axis of an existing device

  • Property svn:eol-style set to LF
File size: 2.7 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
16#if defined LOL_INPUT_V2
17
18namespace lol
19{
20
21class KeyBinding
22{
23public:
24    KeyBinding() : m_device(nullptr), m_current(false), m_previous(false) {}
25
26    bool IsDown() const { return m_current; }
27    bool IsUp() const { return !m_current; }
28    bool IsPressed() const { return m_current && !m_previous; }
29    bool IsReleased() const { return !m_current && m_previous; }
30
31    void Bind(const char* device_name, const char* key_name);
32
33    bool IsBound() { return m_device && m_keyindex != -1; }
34
35protected:
36    void Update() { m_previous = m_current; m_current = IsBound() ? m_device->GetKey(m_keyindex) : false; }
37
38    const InputDevice* m_device;
39    int m_keyindex;
40    bool m_current;
41    bool m_previous;
42
43    friend class Controller;
44};
45
46class AxisBinding
47{
48public:
49    AxisBinding() : m_device(nullptr), m_current(0.0f), m_previous(0.0f) {}
50
51    float GetValue() const { return m_current; }
52    float GetDelta() const { return m_current - m_previous; }
53
54    void Bind(const char* device_name, const char* axis_name);
55
56    bool IsBound() { return m_device && m_axisindex != -1; }
57
58protected:
59    void Update() { m_previous = m_current; m_current = IsBound() ? m_device->GetAxis(m_axisindex) : 0.0f; }
60
61    const InputDevice* m_device;
62    int m_axisindex;
63    float m_current;
64    float m_previous;
65
66    friend class Controller;
67};
68
69
70class Controller : Entity
71{
72public:
73    Controller(int nb_keys, int nb_axis);
74    ~Controller();
75
76    virtual void TickGame(float seconds);
77
78    /** Activate the controller on next frame */
79    void Activate();
80    /** Deactivate the controller on next frame */
81    void Deactivate();
82    /** Deactivate every active controller on next frame and return an array of deactivated (previously active) controllers */
83    static Array<Controller*> DeactivateAll();
84
85    KeyBinding& GetKey(int index) { ASSERT(index >= 0 && index < m_nb_keys); return m_keys[index]; }
86    AxisBinding& GetAxis(int index) { ASSERT(index >= 0 && index < m_nb_axis); return m_axis[index]; }
87
88protected:
89    KeyBinding* m_keys;
90    AxisBinding* m_axis;
91    int m_nb_keys;
92    int m_nb_axis;
93
94private:
95    static Array<Controller*> controllers;
96    bool m_activate_nextframe;
97    bool m_deactivate_nextframe;
98    bool m_active;
99};
100
101} /* namespace lol */
102
103#endif // LOL_INPUT_V2
104
105#endif // __LOL_INPUT_CONTROLLER_H__
106
Note: See TracBrowser for help on using the repository browser.