1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

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.
This commit is contained in:
est31 2016-05-27 08:35:07 +02:00
parent 1e86c89f36
commit 2060fd9cbe
17 changed files with 577 additions and 80 deletions

View file

@ -1,6 +1,7 @@
set(client_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/clientlauncher.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tile.cpp
${CMAKE_CURRENT_SOURCE_DIR}/joystick_controller.cpp
PARENT_SCOPE
)

View file

@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "guiEngine.h"
#include "player.h"
#include "fontengine.h"
#include "joystick_controller.h"
#include "clientlauncher.h"
/* mainmenumanager.h
@ -499,7 +500,8 @@ void ClientLauncher::main_menu(MainMenuData *menudata)
#endif
/* show main menu */
GUIEngine mymenu(device, guiroot, &g_menumgr, smgr, menudata, *kill);
GUIEngine mymenu(device, &input->joystick, guiroot,
&g_menumgr, smgr, menudata, *kill);
smgr->clear(); /* leave scene manager in a clean state */
}
@ -558,6 +560,22 @@ bool ClientLauncher::create_engine_device()
device = createDeviceEx(params);
if (device) {
if (g_settings->getBool("enable_joysticks")) {
irr::core::array<irr::SJoystickInfo> infos;
std::vector<irr::SJoystickInfo> joystick_infos;
// Make sure this is called maximum once per
// irrlicht device, otherwise it will give you
// multiple events for the same joystick.
if (device->activateJoysticks(infos)) {
infostream << "Joystick support enabled" << std::endl;
joystick_infos.reserve(infos.size());
for (u32 i = 0; i < infos.size(); i++) {
joystick_infos.push_back(infos[i]);
}
} else {
errorstream << "Could not activate joystick support." << std::endl;
}
}
porting::initIrrlicht(device);
}

View file

@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define INPUT_HANDLER_H
#include "irrlichttypes_extrabloated.h"
#include "joystick_controller.h"
class MyEventReceiver : public IEventReceiver
{
@ -62,6 +63,14 @@ public:
return true;
}
#endif
if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
/* TODO add a check like:
if (event.JoystickEvent != joystick_we_listen_for)
return false;
*/
return joystick->handleEvent(event.JoystickEvent);
}
// handle mouse events
if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
if (noMenuActive() == false) {
@ -172,6 +181,8 @@ public:
s32 mouse_wheel;
JoystickController *joystick;
#ifdef HAVE_TOUCHSCREENGUI
TouchScreenGUI* m_touchscreengui;
#endif
@ -202,6 +213,7 @@ public:
m_receiver(receiver),
m_mousepos(0,0)
{
m_receiver->joystick = &joystick;
}
virtual bool isKeyDown(const KeyPress &keyCode)
{
@ -288,6 +300,7 @@ public:
void clear()
{
joystick.clear();
m_receiver->clearInput();
}
private:

View file

@ -0,0 +1,179 @@
/*
Minetest
Copyright (C) 2016 est31, <MTest31@outlook.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "joystick_controller.h"
#include "irrlichttypes_extrabloated.h"
#include "keys.h"
#include "settings.h"
#include "gettime.h"
bool JoystickButtonCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
{
u32 buttons = ev.ButtonStates;
buttons &= filter_mask;
return buttons == compare_mask;
}
bool JoystickAxisCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
{
s16 ax_val = ev.Axis[axis_to_compare];
return (ax_val * direction < 0) && (thresh * direction > ax_val * direction);
}
// spares many characters
#define JLO_B_PB(A, B, C) jlo.button_keys.push_back(JoystickButtonCmb(A, B, C))
#define JLO_A_PB(A, B, C, D) jlo.axis_keys.push_back(JoystickAxisCmb(A, B, C, D))
static JoystickLayout create_default_layout()
{
JoystickLayout jlo;
jlo.axes_dead_border = 1024;
const JoystickAxisLayout axes[JA_COUNT] = {
{0, 1}, // JA_SIDEWARD_MOVE
{1, 1}, // JA_FORWARD_MOVE
{3, 1}, // JA_FRUSTUM_HORIZONTAL
{4, 1}, // JA_FRUSTUM_VERTICAL
};
memcpy(jlo.axes, axes, sizeof(jlo.axes));
u32 sb = 1 << 7; // START button mask
u32 fb = 1 << 3; // FOUR button mask
u32 bm = sb | fb; // Mask for Both Modifiers
// The back button means "ESC".
JLO_B_PB(KeyType::ESC, 1 << 6, 1 << 6);
// The start button counts as modifier as well as use key.
// JLO_B_PB(KeyType::USE, sb, sb));
// Accessible without start modifier button pressed
// regardless whether four is pressed or not
JLO_B_PB(KeyType::SNEAK, sb | 1 << 2, 1 << 2);
// Accessible without four modifier button pressed
// regardless whether start is pressed or not
JLO_B_PB(KeyType::MOUSE_L, fb | 1 << 4, 1 << 4);
JLO_B_PB(KeyType::MOUSE_R, fb | 1 << 5, 1 << 5);
// Accessible without any modifier pressed
JLO_B_PB(KeyType::JUMP, bm | 1 << 0, 1 << 0);
JLO_B_PB(KeyType::SPECIAL1, bm | 1 << 1, 1 << 1);
// Accessible with start button not pressed, but four pressed
// TODO find usage for button 0
JLO_B_PB(KeyType::DROP, bm | 1 << 1, fb | 1 << 1);
JLO_B_PB(KeyType::SCROLL_UP, bm | 1 << 4, fb | 1 << 4);
JLO_B_PB(KeyType::SCROLL_DOWN,bm | 1 << 5, fb | 1 << 5);
// Accessible with start button and four pressed
// TODO find usage for buttons 0, 1 and 4, 5
// Now about the buttons simulated by the axes
// Movement buttons, important for vessels
JLO_A_PB(KeyType::FORWARD, 1, 1, 1024);
JLO_A_PB(KeyType::BACKWARD, 1, -1, 1024);
JLO_A_PB(KeyType::LEFT, 0, 1, 1024);
JLO_A_PB(KeyType::RIGHT, 0, -1, 1024);
// Scroll buttons
JLO_A_PB(KeyType::SCROLL_UP, 2, -1, 1024);
JLO_A_PB(KeyType::SCROLL_DOWN, 5, -1, 1024);
return jlo;
}
static const JoystickLayout default_layout = create_default_layout();
JoystickController::JoystickController()
{
m_layout = &default_layout;
doubling_dtime = g_settings->getFloat("repeat_joystick_button_time");
for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
m_past_pressed_time[i] = 0;
}
clear();
}
bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev)
{
m_internal_time = getTimeMs() / 1000.f;
std::bitset<KeyType::INTERNAL_ENUM_COUNT> keys_pressed;
// First generate a list of keys pressed
for (size_t i = 0; i < m_layout->button_keys.size(); i++) {
if (m_layout->button_keys[i].isTriggered(ev)) {
keys_pressed.set(m_layout->button_keys[i].key);
}
}
for (size_t i = 0; i < m_layout->axis_keys.size(); i++) {
if (m_layout->axis_keys[i].isTriggered(ev)) {
keys_pressed.set(m_layout->axis_keys[i].key);
}
}
// Then update the values
for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
if (keys_pressed[i]) {
if (!m_past_pressed_keys[i] &&
m_past_pressed_time[i] < m_internal_time - doubling_dtime) {
m_past_pressed_keys[i] = true;
m_past_pressed_time[i] = m_internal_time;
}
} else if (m_pressed_keys[i]) {
m_past_released_keys[i] = true;
}
m_pressed_keys[i] = keys_pressed[i];
}
for (size_t i = 0; i < JA_COUNT; i++) {
const JoystickAxisLayout &ax_la = m_layout->axes[i];
m_axes_vals[i] = ax_la.invert * ev.Axis[ax_la.axis_id];
}
return true;
}
void JoystickController::clear()
{
m_pressed_keys.reset();
m_past_pressed_keys.reset();
m_past_released_keys.reset();
memset(m_axes_vals, 0, sizeof(m_axes_vals));
}
s16 JoystickController::getAxisWithoutDead(JoystickAxis axis)
{
s16 v = m_axes_vals[axis];
if (((v > 0) && (v < m_layout->axes_dead_border)) ||
((v < 0) && (v > -m_layout->axes_dead_border)))
return 0;
return v;
}

View file

@ -0,0 +1,163 @@
/*
Minetest
Copyright (C) 2016 est31, <MTest31@outlook.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef JOYSTICK_HEADER
#define JOYSTICK_HEADER
#include "irrlichttypes_extrabloated.h"
#include "keys.h"
#include <bitset>
#include <vector>
enum JoystickAxis {
JA_SIDEWARD_MOVE,
JA_FORWARD_MOVE,
JA_FRUSTUM_HORIZONTAL,
JA_FRUSTUM_VERTICAL,
// To know the count of enum values
JA_COUNT,
};
struct JoystickAxisLayout {
u16 axis_id;
// -1 if to invert, 1 if to keep it.
int invert;
};
struct JoystickCombination {
virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const=0;
GameKeyType key;
};
struct JoystickButtonCmb : public JoystickCombination {
JoystickButtonCmb() {}
JoystickButtonCmb(GameKeyType key, u32 filter_mask, u32 compare_mask) :
filter_mask(filter_mask),
compare_mask(compare_mask)
{
this->key = key;
}
virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const;
u32 filter_mask;
u32 compare_mask;
};
struct JoystickAxisCmb : public JoystickCombination {
JoystickAxisCmb() {}
JoystickAxisCmb(GameKeyType key, u16 axis_to_compare, int direction, s16 thresh) :
axis_to_compare(axis_to_compare),
direction(direction),
thresh(thresh)
{
this->key = key;
}
virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const;
u16 axis_to_compare;
// if -1, thresh must be smaller than the axis value in order to trigger
// if 1, thresh must be bigger than the axis value in order to trigger
int direction;
s16 thresh;
};
struct JoystickLayout {
std::vector<JoystickButtonCmb> button_keys;
std::vector<JoystickAxisCmb> axis_keys;
JoystickAxisLayout axes[JA_COUNT];
s16 axes_dead_border;
};
class JoystickController {
public:
JoystickController();
bool handleEvent(const irr::SEvent::SJoystickEvent &ev);
void clear();
bool wasKeyDown(GameKeyType b)
{
bool r = m_past_pressed_keys[b];
m_past_pressed_keys[b] = false;
return r;
}
bool getWasKeyDown(GameKeyType b)
{
return m_past_pressed_keys[b];
}
void clearWasKeyDown(GameKeyType b)
{
m_past_pressed_keys[b] = false;
}
bool wasKeyReleased(GameKeyType b)
{
bool r = m_past_released_keys[b];
m_past_released_keys[b] = false;
return r;
}
bool getWasKeyReleased(GameKeyType b)
{
return m_past_pressed_keys[b];
}
void clearWasKeyReleased(GameKeyType b)
{
m_past_pressed_keys[b] = false;
}
bool isKeyDown(GameKeyType b)
{
return m_pressed_keys[b];
}
s16 getAxis(JoystickAxis axis)
{
return m_axes_vals[axis];
}
s16 getAxisWithoutDead(JoystickAxis axis);
f32 doubling_dtime;
private:
const JoystickLayout *m_layout;
s16 m_axes_vals[JA_COUNT];
std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_pressed_keys;
f32 m_internal_time;
f32 m_past_pressed_time[KeyType::INTERNAL_ENUM_COUNT];
std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_pressed_keys;
std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_released_keys;
};
#endif

View file

@ -67,6 +67,12 @@ public:
DEBUG_STACKS,
// joystick specific keys
MOUSE_L,
MOUSE_R,
SCROLL_UP,
SCROLL_DOWN,
// Fake keycode for array size and internal checks
INTERNAL_ENUM_COUNT