]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/joystick_controller.cpp
Add joystick layout for DragonRise GameCube controller (#11467)
[dragonfireclient.git] / src / client / joystick_controller.cpp
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 #include "joystick_controller.h"
21 #include "irrlichttypes_extrabloated.h"
22 #include "keys.h"
23 #include "settings.h"
24 #include "gettime.h"
25 #include "porting.h"
26 #include "util/string.h"
27
28 bool JoystickButtonCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
29 {
30         u32 buttons = ev.ButtonStates;
31
32         buttons &= filter_mask;
33         return buttons == compare_mask;
34 }
35
36 bool JoystickAxisCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
37 {
38         s16 ax_val = ev.Axis[axis_to_compare];
39
40         return (ax_val * direction < -thresh);
41 }
42
43 // spares many characters
44 #define JLO_B_PB(A, B, C)    jlo.button_keys.emplace_back(A, B, C)
45 #define JLO_A_PB(A, B, C, D) jlo.axis_keys.emplace_back(A, B, C, D)
46
47 JoystickLayout create_default_layout()
48 {
49         JoystickLayout jlo;
50
51         jlo.axes_deadzone = g_settings->getU16("joystick_deadzone");
52
53         const JoystickAxisLayout axes[JA_COUNT] = {
54                 {0, 1}, // JA_SIDEWARD_MOVE
55                 {1, 1}, // JA_FORWARD_MOVE
56                 {3, 1}, // JA_FRUSTUM_HORIZONTAL
57                 {4, 1}, // JA_FRUSTUM_VERTICAL
58         };
59         memcpy(jlo.axes, axes, sizeof(jlo.axes));
60
61         u32 sb = 1 << 7; // START button mask
62         u32 fb = 1 << 3; // FOUR button mask
63         u32 bm = sb | fb; // Mask for Both Modifiers
64
65         // The back button means "ESC".
66         JLO_B_PB(KeyType::ESC,        1 << 6,      1 << 6);
67
68         // The start button counts as modifier as well as use key.
69         // JLO_B_PB(KeyType::USE,        sb,          sb));
70
71         // Accessible without start modifier button pressed
72         // regardless whether four is pressed or not
73         JLO_B_PB(KeyType::SNEAK,      sb | 1 << 2, 1 << 2);
74
75         // Accessible without four modifier button pressed
76         // regardless whether start is pressed or not
77         JLO_B_PB(KeyType::DIG,        fb | 1 << 4, 1 << 4);
78         JLO_B_PB(KeyType::PLACE,      fb | 1 << 5, 1 << 5);
79
80         // Accessible without any modifier pressed
81         JLO_B_PB(KeyType::JUMP,       bm | 1 << 0, 1 << 0);
82         JLO_B_PB(KeyType::AUX1,       bm | 1 << 1, 1 << 1);
83
84         // Accessible with start button not pressed, but four pressed
85         // TODO find usage for button 0
86         JLO_B_PB(KeyType::DROP,        bm | 1 << 1, fb | 1 << 1);
87         JLO_B_PB(KeyType::HOTBAR_PREV, bm | 1 << 4, fb | 1 << 4);
88         JLO_B_PB(KeyType::HOTBAR_NEXT, bm | 1 << 5, fb | 1 << 5);
89
90         // Accessible with start button and four pressed
91         // TODO find usage for buttons 0, 1 and 4, 5
92
93         // Now about the buttons simulated by the axes
94
95         // Movement buttons, important for vessels
96         JLO_A_PB(KeyType::FORWARD,  1,  1, jlo.axes_deadzone);
97         JLO_A_PB(KeyType::BACKWARD, 1, -1, jlo.axes_deadzone);
98         JLO_A_PB(KeyType::LEFT,     0,  1, jlo.axes_deadzone);
99         JLO_A_PB(KeyType::RIGHT,    0, -1, jlo.axes_deadzone);
100
101         // Scroll buttons
102         JLO_A_PB(KeyType::HOTBAR_PREV, 2, -1, jlo.axes_deadzone);
103         JLO_A_PB(KeyType::HOTBAR_NEXT, 5, -1, jlo.axes_deadzone);
104
105         return jlo;
106 }
107
108 JoystickLayout create_xbox_layout()
109 {
110         JoystickLayout jlo;
111
112         jlo.axes_deadzone = 7000;
113
114         const JoystickAxisLayout axes[JA_COUNT] = {
115                 {0, 1}, // JA_SIDEWARD_MOVE
116                 {1, 1}, // JA_FORWARD_MOVE
117                 {2, 1}, // JA_FRUSTUM_HORIZONTAL
118                 {3, 1}, // JA_FRUSTUM_VERTICAL
119         };
120         memcpy(jlo.axes, axes, sizeof(jlo.axes));
121
122         // The back button means "ESC".
123         JLO_B_PB(KeyType::ESC,        1 << 8,  1 << 8); // back
124         JLO_B_PB(KeyType::ESC,        1 << 9,  1 << 9); // start
125
126         // 4 Buttons
127         JLO_B_PB(KeyType::JUMP,        1 << 0,  1 << 0); // A/green
128         JLO_B_PB(KeyType::ESC,         1 << 1,  1 << 1); // B/red
129         JLO_B_PB(KeyType::AUX1,        1 << 2,  1 << 2); // X/blue
130         JLO_B_PB(KeyType::INVENTORY,   1 << 3,  1 << 3); // Y/yellow
131
132         // Analog Sticks
133         JLO_B_PB(KeyType::AUX1,        1 << 11, 1 << 11); // left
134         JLO_B_PB(KeyType::SNEAK,       1 << 12, 1 << 12); // right
135
136         // Triggers
137         JLO_B_PB(KeyType::DIG,         1 << 6,  1 << 6); // lt
138         JLO_B_PB(KeyType::PLACE,       1 << 7,  1 << 7); // rt
139         JLO_B_PB(KeyType::HOTBAR_PREV, 1 << 4,  1 << 4); // lb
140         JLO_B_PB(KeyType::HOTBAR_NEXT, 1 << 5,  1 << 5); // rb
141
142         // D-PAD
143         JLO_B_PB(KeyType::ZOOM,        1 << 15, 1 << 15); // up
144         JLO_B_PB(KeyType::DROP,        1 << 13, 1 << 13); // left
145         JLO_B_PB(KeyType::SCREENSHOT,  1 << 14, 1 << 14); // right
146         JLO_B_PB(KeyType::FREEMOVE,    1 << 16, 1 << 16); // down
147
148         // Movement buttons, important for vessels
149         JLO_A_PB(KeyType::FORWARD,  1,  1, jlo.axes_deadzone);
150         JLO_A_PB(KeyType::BACKWARD, 1, -1, jlo.axes_deadzone);
151         JLO_A_PB(KeyType::LEFT,     0,  1, jlo.axes_deadzone);
152         JLO_A_PB(KeyType::RIGHT,    0, -1, jlo.axes_deadzone);
153
154         return jlo;
155 }
156
157 JoystickLayout create_dragonrise_gamecube_layout()
158 {
159         JoystickLayout jlo;
160
161         jlo.axes_deadzone = 7000;
162
163         const JoystickAxisLayout axes[JA_COUNT] = {
164                 // Control Stick
165                 {0, 1}, // JA_SIDEWARD_MOVE
166                 {1, 1}, // JA_FORWARD_MOVE
167
168                 // C-Stick
169                 {3, 1}, // JA_FRUSTUM_HORIZONTAL
170                 {4, 1}, // JA_FRUSTUM_VERTICAL
171         };
172         memcpy(jlo.axes, axes, sizeof(jlo.axes));
173
174         // The center button
175         JLO_B_PB(KeyType::ESC, 1 << 9, 1 << 9); // Start/Pause Button
176
177         // Front right buttons
178         JLO_B_PB(KeyType::JUMP,  1 << 2, 1 << 2); // A Button
179         JLO_B_PB(KeyType::SNEAK, 1 << 3, 1 << 3); // B Button
180         JLO_B_PB(KeyType::DROP,  1 << 0, 1 << 0); // Y Button
181         JLO_B_PB(KeyType::AUX1,  1 << 1, 1 << 1); // X Button
182
183         // Triggers
184         JLO_B_PB(KeyType::DIG,       1 << 4, 1 << 4); // L Trigger
185         JLO_B_PB(KeyType::PLACE,     1 << 5, 1 << 5); // R Trigger
186         JLO_B_PB(KeyType::INVENTORY, 1 << 6, 1 << 6); // Z Button
187
188         // D-Pad
189         JLO_A_PB(KeyType::HOTBAR_PREV, 5,  1, jlo.axes_deadzone); // left
190         JLO_A_PB(KeyType::HOTBAR_NEXT, 5, -1, jlo.axes_deadzone); // right
191         // Axis are hard to actuate independantly, best to leave up and down unused.
192         //JLO_A_PB(0, 6,  1, jlo.axes_deadzone); // up
193         //JLO_A_PB(0, 6, -1, jlo.axes_deadzone); // down
194
195         // Movements tied to Control Stick, important for vessels
196         JLO_A_PB(KeyType::LEFT,     0,  1, jlo.axes_deadzone);
197         JLO_A_PB(KeyType::RIGHT,    0, -1, jlo.axes_deadzone);
198         JLO_A_PB(KeyType::FORWARD,  1,  1, jlo.axes_deadzone);
199         JLO_A_PB(KeyType::BACKWARD, 1, -1, jlo.axes_deadzone);
200
201         return jlo;
202 }
203
204
205 JoystickController::JoystickController() :
206                 doubling_dtime(g_settings->getFloat("repeat_joystick_button_time"))
207 {
208         for (float &i : m_past_pressed_time) {
209                 i = 0;
210         }
211         m_layout.axes_deadzone = 0;
212         clear();
213 }
214
215 void JoystickController::onJoystickConnect(const std::vector<irr::SJoystickInfo> &joystick_infos)
216 {
217         s32         id     = g_settings->getS32("joystick_id");
218         std::string layout = g_settings->get("joystick_type");
219
220         if (id < 0 || (u16)id >= joystick_infos.size()) {
221                 // TODO: auto detection
222                 id = 0;
223         }
224
225         if (id >= 0 && (u16)id < joystick_infos.size()) {
226                 if (layout.empty() || layout == "auto")
227                         setLayoutFromControllerName(joystick_infos[id].Name.c_str());
228                 else
229                         setLayoutFromControllerName(layout);
230         }
231
232         m_joystick_id = id;
233 }
234
235 void JoystickController::setLayoutFromControllerName(const std::string &name)
236 {
237         if (lowercase(name).find("xbox") != std::string::npos) {
238                 m_layout = create_xbox_layout();
239         } else if (lowercase(name).find("dragonrise_gamecube") != std::string::npos) {
240                 m_layout = create_dragonrise_gamecube_layout();
241         } else {
242                 m_layout = create_default_layout();
243         }
244 }
245
246 bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev)
247 {
248         if (ev.Joystick != m_joystick_id)
249                 return false;
250
251         m_internal_time = porting::getTimeMs() / 1000.f;
252
253         std::bitset<KeyType::INTERNAL_ENUM_COUNT> keys_pressed;
254
255         // First generate a list of keys pressed
256
257         for (const auto &button_key : m_layout.button_keys) {
258                 if (button_key.isTriggered(ev)) {
259                         keys_pressed.set(button_key.key);
260                 }
261         }
262
263         for (const auto &axis_key : m_layout.axis_keys) {
264                 if (axis_key.isTriggered(ev)) {
265                         keys_pressed.set(axis_key.key);
266                 }
267         }
268
269         // Then update the values
270
271         for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
272                 if (keys_pressed[i]) {
273                         if (!m_past_keys_pressed[i] &&
274                                         m_past_pressed_time[i] < m_internal_time - doubling_dtime) {
275                                 m_past_keys_pressed[i] = true;
276                                 m_past_pressed_time[i] = m_internal_time;
277                         }
278                 } else if (m_keys_down[i]) {
279                         m_keys_released[i] = true;
280                 }
281
282                 if (keys_pressed[i] && !(m_keys_down[i]))
283                         m_keys_pressed[i] = true;
284
285                 m_keys_down[i] = keys_pressed[i];
286         }
287
288         for (size_t i = 0; i < JA_COUNT; i++) {
289                 const JoystickAxisLayout &ax_la = m_layout.axes[i];
290                 m_axes_vals[i] = ax_la.invert * ev.Axis[ax_la.axis_id];
291         }
292
293         return true;
294 }
295
296 void JoystickController::clear()
297 {
298         m_keys_pressed.reset();
299         m_keys_down.reset();
300         m_past_keys_pressed.reset();
301         m_keys_released.reset();
302         memset(m_axes_vals, 0, sizeof(m_axes_vals));
303 }
304
305 float JoystickController::getAxisWithoutDead(JoystickAxis axis)
306 {
307         s16 v = m_axes_vals[axis];
308
309         if (abs(v) < m_layout.axes_deadzone)
310                 return 0.0f;
311
312         v += (v < 0 ? m_layout.axes_deadzone : -m_layout.axes_deadzone);
313
314         return (float)v / ((float)(INT16_MAX - m_layout.axes_deadzone));
315 }
316
317 float JoystickController::getMovementDirection()
318 {
319         return atan2(getAxisWithoutDead(JA_SIDEWARD_MOVE), -getAxisWithoutDead(JA_FORWARD_MOVE));
320 }
321
322 float JoystickController::getMovementSpeed()
323 {
324         float speed = sqrt(pow(getAxisWithoutDead(JA_FORWARD_MOVE), 2) + pow(getAxisWithoutDead(JA_SIDEWARD_MOVE), 2));
325         if (speed > 1.0f)
326                 speed = 1.0f;
327         return speed;
328 }