Skip to content

Commit 2060fd9

Browse files
committedJun 3, 2016
Initial Gamepad support
Adds initial ingame gamepad support to minetest. Full Formspec support is not implemented yet and can be added by a later change.
1 parent 1e86c89 commit 2060fd9

17 files changed

+576
-79
lines changed
 

Diff for: ‎builtin/settingtypes.txt

+11
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ random_input (Random input) bool false
104104
# Continuous forward movement (only used for testing).
105105
continuous_forward (Continuous forward) bool false
106106

107+
# Enable Joysticks
108+
enable_joysticks (Enable Joysticks) bool true
109+
110+
# The time in seconds it takes between repeated events
111+
# when holding down a joystick button combination.
112+
repeat_joystick_button_time (Joystick button repetition invterval) float 0.17
113+
114+
# The sensitivity of the joystick axes for moving the
115+
# ingame view frustum around.
116+
joystick_frustum_sensitivity (Joystick frustum sensitivity) float 170
117+
107118
# Key for moving the player forward.
108119
# See http://irrlicht.sourceforge.net/docu/namespaceirr.html#a54da2a0e231901735e3da1b0edf72eb3
109120
keymap_forward (Forward key) key KEY_KEY_W

Diff for: ‎src/client/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(client_SRCS
22
${CMAKE_CURRENT_SOURCE_DIR}/clientlauncher.cpp
33
${CMAKE_CURRENT_SOURCE_DIR}/tile.cpp
4+
${CMAKE_CURRENT_SOURCE_DIR}/joystick_controller.cpp
45
PARENT_SCOPE
56
)
67

Diff for: ‎src/client/clientlauncher.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3232
#include "guiEngine.h"
3333
#include "player.h"
3434
#include "fontengine.h"
35+
#include "joystick_controller.h"
3536
#include "clientlauncher.h"
3637

3738
/* mainmenumanager.h
@@ -499,7 +500,8 @@ void ClientLauncher::main_menu(MainMenuData *menudata)
499500
#endif
500501

501502
/* show main menu */
502-
GUIEngine mymenu(device, guiroot, &g_menumgr, smgr, menudata, *kill);
503+
GUIEngine mymenu(device, &input->joystick, guiroot,
504+
&g_menumgr, smgr, menudata, *kill);
503505

504506
smgr->clear(); /* leave scene manager in a clean state */
505507
}
@@ -558,6 +560,22 @@ bool ClientLauncher::create_engine_device()
558560
device = createDeviceEx(params);
559561

560562
if (device) {
563+
if (g_settings->getBool("enable_joysticks")) {
564+
irr::core::array<irr::SJoystickInfo> infos;
565+
std::vector<irr::SJoystickInfo> joystick_infos;
566+
// Make sure this is called maximum once per
567+
// irrlicht device, otherwise it will give you
568+
// multiple events for the same joystick.
569+
if (device->activateJoysticks(infos)) {
570+
infostream << "Joystick support enabled" << std::endl;
571+
joystick_infos.reserve(infos.size());
572+
for (u32 i = 0; i < infos.size(); i++) {
573+
joystick_infos.push_back(infos[i]);
574+
}
575+
} else {
576+
errorstream << "Could not activate joystick support." << std::endl;
577+
}
578+
}
561579
porting::initIrrlicht(device);
562580
}
563581

Diff for: ‎src/client/inputhandler.h

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121
#define INPUT_HANDLER_H
2222

2323
#include "irrlichttypes_extrabloated.h"
24+
#include "joystick_controller.h"
2425

2526
class MyEventReceiver : public IEventReceiver
2627
{
@@ -62,6 +63,14 @@ class MyEventReceiver : public IEventReceiver
6263
return true;
6364
}
6465
#endif
66+
67+
if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
68+
/* TODO add a check like:
69+
if (event.JoystickEvent != joystick_we_listen_for)
70+
return false;
71+
*/
72+
return joystick->handleEvent(event.JoystickEvent);
73+
}
6574
// handle mouse events
6675
if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
6776
if (noMenuActive() == false) {
@@ -172,6 +181,8 @@ class MyEventReceiver : public IEventReceiver
172181

173182
s32 mouse_wheel;
174183

184+
JoystickController *joystick;
185+
175186
#ifdef HAVE_TOUCHSCREENGUI
176187
TouchScreenGUI* m_touchscreengui;
177188
#endif
@@ -202,6 +213,7 @@ class RealInputHandler : public InputHandler
202213
m_receiver(receiver),
203214
m_mousepos(0,0)
204215
{
216+
m_receiver->joystick = &joystick;
205217
}
206218
virtual bool isKeyDown(const KeyPress &keyCode)
207219
{
@@ -288,6 +300,7 @@ class RealInputHandler : public InputHandler
288300

289301
void clear()
290302
{
303+
joystick.clear();
291304
m_receiver->clearInput();
292305
}
293306
private:

Diff for: ‎src/client/joystick_controller.cpp

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
26+
bool JoystickButtonCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
27+
{
28+
u32 buttons = ev.ButtonStates;
29+
30+
buttons &= filter_mask;
31+
return buttons == compare_mask;
32+
}
33+
34+
bool JoystickAxisCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
35+
{
36+
s16 ax_val = ev.Axis[axis_to_compare];
37+
38+
return (ax_val * direction < 0) && (thresh * direction > ax_val * direction);
39+
}
40+
41+
// spares many characters
42+
#define JLO_B_PB(A, B, C) jlo.button_keys.push_back(JoystickButtonCmb(A, B, C))
43+
#define JLO_A_PB(A, B, C, D) jlo.axis_keys.push_back(JoystickAxisCmb(A, B, C, D))
44+
45+
static JoystickLayout create_default_layout()
46+
{
47+
JoystickLayout jlo;
48+
49+
jlo.axes_dead_border = 1024;
50+
51+
const JoystickAxisLayout axes[JA_COUNT] = {
52+
{0, 1}, // JA_SIDEWARD_MOVE
53+
{1, 1}, // JA_FORWARD_MOVE
54+
{3, 1}, // JA_FRUSTUM_HORIZONTAL
55+
{4, 1}, // JA_FRUSTUM_VERTICAL
56+
};
57+
memcpy(jlo.axes, axes, sizeof(jlo.axes));
58+
59+
u32 sb = 1 << 7; // START button mask
60+
u32 fb = 1 << 3; // FOUR button mask
61+
u32 bm = sb | fb; // Mask for Both Modifiers
62+
63+
// The back button means "ESC".
64+
JLO_B_PB(KeyType::ESC, 1 << 6, 1 << 6);
65+
66+
// The start button counts as modifier as well as use key.
67+
// JLO_B_PB(KeyType::USE, sb, sb));
68+
69+
// Accessible without start modifier button pressed
70+
// regardless whether four is pressed or not
71+
JLO_B_PB(KeyType::SNEAK, sb | 1 << 2, 1 << 2);
72+
73+
// Accessible without four modifier button pressed
74+
// regardless whether start is pressed or not
75+
JLO_B_PB(KeyType::MOUSE_L, fb | 1 << 4, 1 << 4);
76+
JLO_B_PB(KeyType::MOUSE_R, fb | 1 << 5, 1 << 5);
77+
78+
// Accessible without any modifier pressed
79+
JLO_B_PB(KeyType::JUMP, bm | 1 << 0, 1 << 0);
80+
JLO_B_PB(KeyType::SPECIAL1, bm | 1 << 1, 1 << 1);
81+
82+
// Accessible with start button not pressed, but four pressed
83+
// TODO find usage for button 0
84+
JLO_B_PB(KeyType::DROP, bm | 1 << 1, fb | 1 << 1);
85+
JLO_B_PB(KeyType::SCROLL_UP, bm | 1 << 4, fb | 1 << 4);
86+
JLO_B_PB(KeyType::SCROLL_DOWN,bm | 1 << 5, fb | 1 << 5);
87+
88+
// Accessible with start button and four pressed
89+
// TODO find usage for buttons 0, 1 and 4, 5
90+
91+
// Now about the buttons simulated by the axes
92+
93+
// Movement buttons, important for vessels
94+
JLO_A_PB(KeyType::FORWARD, 1, 1, 1024);
95+
JLO_A_PB(KeyType::BACKWARD, 1, -1, 1024);
96+
JLO_A_PB(KeyType::LEFT, 0, 1, 1024);
97+
JLO_A_PB(KeyType::RIGHT, 0, -1, 1024);
98+
99+
// Scroll buttons
100+
JLO_A_PB(KeyType::SCROLL_UP, 2, -1, 1024);
101+
JLO_A_PB(KeyType::SCROLL_DOWN, 5, -1, 1024);
102+
103+
return jlo;
104+
}
105+
106+
static const JoystickLayout default_layout = create_default_layout();
107+
108+
JoystickController::JoystickController()
109+
{
110+
m_layout = &default_layout;
111+
doubling_dtime = g_settings->getFloat("repeat_joystick_button_time");
112+
113+
for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
114+
m_past_pressed_time[i] = 0;
115+
}
116+
clear();
117+
}
118+
119+
bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev)
120+
{
121+
m_internal_time = getTimeMs() / 1000.f;
122+
123+
std::bitset<KeyType::INTERNAL_ENUM_COUNT> keys_pressed;
124+
125+
// First generate a list of keys pressed
126+
127+
for (size_t i = 0; i < m_layout->button_keys.size(); i++) {
128+
if (m_layout->button_keys[i].isTriggered(ev)) {
129+
keys_pressed.set(m_layout->button_keys[i].key);
130+
}
131+
}
132+
133+
for (size_t i = 0; i < m_layout->axis_keys.size(); i++) {
134+
if (m_layout->axis_keys[i].isTriggered(ev)) {
135+
keys_pressed.set(m_layout->axis_keys[i].key);
136+
}
137+
}
138+
139+
// Then update the values
140+
141+
for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
142+
if (keys_pressed[i]) {
143+
if (!m_past_pressed_keys[i] &&
144+
m_past_pressed_time[i] < m_internal_time - doubling_dtime) {
145+
m_past_pressed_keys[i] = true;
146+
m_past_pressed_time[i] = m_internal_time;
147+
}
148+
} else if (m_pressed_keys[i]) {
149+
m_past_released_keys[i] = true;
150+
}
151+
152+
m_pressed_keys[i] = keys_pressed[i];
153+
}
154+
155+
for (size_t i = 0; i < JA_COUNT; i++) {
156+
const JoystickAxisLayout &ax_la = m_layout->axes[i];
157+
m_axes_vals[i] = ax_la.invert * ev.Axis[ax_la.axis_id];
158+
}
159+
160+
161+
return true;
162+
}
163+
164+
void JoystickController::clear()
165+
{
166+
m_pressed_keys.reset();
167+
m_past_pressed_keys.reset();
168+
m_past_released_keys.reset();
169+
memset(m_axes_vals, 0, sizeof(m_axes_vals));
170+
}
171+
172+
s16 JoystickController::getAxisWithoutDead(JoystickAxis axis)
173+
{
174+
s16 v = m_axes_vals[axis];
175+
if (((v > 0) && (v < m_layout->axes_dead_border)) ||
176+
((v < 0) && (v > -m_layout->axes_dead_border)))
177+
return 0;
178+
return v;
179+
}

0 commit comments

Comments
 (0)
Please sign in to comment.