]> git.lizzy.rs Git - minetest.git/blob - src/client/joystick_controller.h
4a2cdf555d24aaa4a6feb81979d21bb7955d794d
[minetest.git] / src / client / joystick_controller.h
1 /*
2 Minetest
3 Copyright (C) 2016 est31, <MTest31@outlook.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef JOYSTICK_HEADER
21 #define JOYSTICK_HEADER
22
23 #include "irrlichttypes_extrabloated.h"
24 #include "keys.h"
25 #include <bitset>
26 #include <vector>
27
28 enum JoystickAxis {
29         JA_SIDEWARD_MOVE,
30         JA_FORWARD_MOVE,
31
32         JA_FRUSTUM_HORIZONTAL,
33         JA_FRUSTUM_VERTICAL,
34
35         // To know the count of enum values
36         JA_COUNT,
37 };
38
39 struct JoystickAxisLayout {
40         u16 axis_id;
41         // -1 if to invert, 1 if to keep it.
42         int invert;
43 };
44
45
46 struct JoystickCombination {
47
48         virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const=0;
49
50         GameKeyType key;
51 };
52
53 struct JoystickButtonCmb : public JoystickCombination {
54
55         JoystickButtonCmb() = default;
56
57         JoystickButtonCmb(GameKeyType key, u32 filter_mask, u32 compare_mask) :
58                 filter_mask(filter_mask),
59                 compare_mask(compare_mask)
60         {
61                 this->key = key;
62         }
63
64         virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const;
65
66         u32 filter_mask;
67         u32 compare_mask;
68 };
69
70 struct JoystickAxisCmb : public JoystickCombination {
71
72         JoystickAxisCmb() = default;
73
74         JoystickAxisCmb(GameKeyType key, u16 axis_to_compare, int direction, s16 thresh) :
75                 axis_to_compare(axis_to_compare),
76                 direction(direction),
77                 thresh(thresh)
78         {
79                 this->key = key;
80         }
81
82         virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const;
83
84         u16 axis_to_compare;
85
86         // if -1, thresh must be smaller than the axis value in order to trigger
87         // if  1, thresh must be bigger  than the axis value in order to trigger
88         int direction;
89         s16 thresh;
90 };
91
92 struct JoystickLayout {
93         std::vector<JoystickButtonCmb> button_keys;
94         std::vector<JoystickAxisCmb> axis_keys;
95         JoystickAxisLayout axes[JA_COUNT];
96         s16 axes_dead_border;
97 };
98
99 class JoystickController {
100
101 public:
102         JoystickController();
103
104         void onJoystickConnect(const std::vector<irr::SJoystickInfo> &joystick_infos);
105
106         bool handleEvent(const irr::SEvent::SJoystickEvent &ev);
107         void clear();
108
109         bool wasKeyDown(GameKeyType b)
110         {
111                 bool r = m_past_pressed_keys[b];
112                 m_past_pressed_keys[b] = false;
113                 return r;
114         }
115         bool getWasKeyDown(GameKeyType b)
116         {
117                 return m_past_pressed_keys[b];
118         }
119         void clearWasKeyDown(GameKeyType b)
120         {
121                 m_past_pressed_keys[b] = false;
122         }
123
124         bool wasKeyReleased(GameKeyType b)
125         {
126                 bool r = m_past_released_keys[b];
127                 m_past_released_keys[b] = false;
128                 return r;
129         }
130         bool getWasKeyReleased(GameKeyType b)
131         {
132                 return m_past_pressed_keys[b];
133         }
134         void clearWasKeyReleased(GameKeyType b)
135         {
136                 m_past_pressed_keys[b] = false;
137         }
138
139         bool isKeyDown(GameKeyType b)
140         {
141                 return m_pressed_keys[b];
142         }
143
144         s16 getAxis(JoystickAxis axis)
145         {
146                 return m_axes_vals[axis];
147         }
148
149         s16 getAxisWithoutDead(JoystickAxis axis);
150
151         f32 doubling_dtime;
152
153 private:
154         void setLayoutFromControllerName(const std::string &name);
155
156         JoystickLayout m_layout;
157
158         s16 m_axes_vals[JA_COUNT];
159
160         u8 m_joystick_id = 0;
161
162         std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_pressed_keys;
163
164         f32 m_internal_time;
165
166         f32 m_past_pressed_time[KeyType::INTERNAL_ENUM_COUNT];
167
168         std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_pressed_keys;
169         std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_released_keys;
170 };
171
172 #endif