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