aboutsummaryrefslogtreecommitdiff
path: root/src/client/joystick_controller.cpp
blob: ef8d18ab05616346e1467f1195918194b66a733b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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;
}