Nebula
Loading...
Searching...
No Matches
gamepadbase.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "input/inputhandler.h"
14#include "threading/thread.h"
15#include "math/mat4.h"
16#include "util/dictionary.h"
17
18#define MAX_GAMEPADS 4
19
20//------------------------------------------------------------------------------
21namespace Base
22{
24{
26public:
28 enum Button
29 {
30 DPadUpButton = 0, // all platforms
31 DPadDownButton, // all platforms
32 DPadLeftButton, // all platforms
33 DPadRightButton, // all platforms
34 StartButton, // all platforms
35 BackButton, // Xbox360
36 LeftThumbButton, // Xbox360, PS3: L3
37 RightThumbButton, // Xbox360, PS3: R3
38 LeftShoulderButton, // Xbox360, PS3: L1, Wii: ClassicController ZL
39 RightShoulderButton, // Xbox360, PS3: R1, Wii: ClassicController ZR
40
41 AButton, // Xbox360: A, Wii: A/a, PS3: Cross
42 BButton, // Xbox360: B, Wii: b(pad), PS3: Circle
43 XButton, // Xbox360: X, Wii: x(pad), PS3: Square
44 YButton, // Xbox360: Y, Wii: y(pad), PS3: Triangle
45
46 // Wii specific buttons
47 HomeButton, // Xbox360: /, Wii: Home, PS3: /
48 PlusButton, // Xbox360: /, Wii: +, PS3: /
49 MinusButton, // Xbox360: /, Wii: -, PS3: /
50 OneButton, // Xbox360: /, Wii: 1, PS3: /
51 TwoButton, // Xbox360: /, Wii: 2, PS3: /
52 ZButton, // Xbox360: /, Wii: Z(Nunchuk), PS3: /
53 CButton, // Xbox360: /, Wii: C(Nunchuk), PS3: /
54
55 // PS3 specific buttons
56 SelectButton, // Xbox360: /, Wii: /, PS3: Select
57
60
61
62 // PS3 aliases !!! MUST BE after NumButtons & InvalidButton !!!
71 };
72
74 enum Axis
75 {
76 // general axi
77 LeftTriggerAxis = 0, // 0.0f .. 1.0f
78 RightTriggerAxis, // 0.0f .. 1.0f
79 LeftThumbXAxis, // -1.0f .. 1.0f
80 LeftThumbYAxis, // -1.0f .. 1.0f
81 RightThumbXAxis, // -1.0f .. 1.0f
82 RightThumbYAxis, // -1.0f .. 1.0f
83
86 };
87
91 virtual ~GamePadBase();
92
98 static Button ButtonFromString(const Util::String& str);
100 static Axis AxisFromString(const Util::String& str);
101
103 bool IsConnected() const;
105 void SetIndex(IndexT i);
107 IndexT GetIndex() const;
110
112 bool ButtonPressed(Button btn) const;
114 bool ButtonDown(Button btn) const;
116 bool ButtonUp(Button btn) const;
118 float GetAxisValue(Axis axis) const;
120 bool HasTransform() const;
122 const Math::mat4 & GetTransform() const;
123
125 void SetLowFrequencyVibrator(float f);
127 float GetLowFrequencyVibrator() const;
129 void SetHighFrequencyVibrator(float f);
131 float GetHighFrequencyVibrator() const;
132
135
136protected:
138 virtual void OnAttach();
140 virtual void OnBeginFrame();
142 virtual void OnReset();
143
145 {
146 public:
148 ButtonState() : pressed(false), down(false), up(false) {};
149
151 bool down;
152 bool up;
153 };
154
163 Threading::CriticalSection critSect;
166
167private:
170 static void SetupDicts();
171};
172
173//------------------------------------------------------------------------------
176inline bool
178{
179 n_assert2(creatorThreadId == Threading::Thread::GetMyThreadId(), "IsConnected can't be called from any thread but the creator thread!");
180 return this->isConnected;
181}
182
183//------------------------------------------------------------------------------
186inline bool
188{
189 return this->hasTransform;
190}
191
192//------------------------------------------------------------------------------
195inline
196const Math::mat4 &
198{
199 return this->transform;
200}
201
202//------------------------------------------------------------------------------
205inline void
207{
208 n_assert(i < this->GetMaxNumControllers());
209 this->index = i;
210}
211
212//------------------------------------------------------------------------------
215inline IndexT
217{
218 return this->index;
219}
220
221//------------------------------------------------------------------------------
224inline SizeT
229
230//------------------------------------------------------------------------------
233inline bool
235{
236 n_assert2(creatorThreadId == Threading::Thread::GetMyThreadId(), "ButtonPressed can't be called from any thread but the creator thread!");
237 return this->buttonStates[btn].pressed;
238}
239
240//------------------------------------------------------------------------------
243inline bool
245{
246 n_assert2(creatorThreadId == Threading::Thread::GetMyThreadId(), "ButtonDown can't be called from any thread but the creator thread!");
247 return this->buttonStates[btn].down;
248}
249
250//------------------------------------------------------------------------------
253inline bool
255{
256 n_assert2(creatorThreadId == Threading::Thread::GetMyThreadId(), "ButtonUp can't be called from any thread but the creator thread!");
257 return this->buttonStates[btn].up;
258}
259
260//------------------------------------------------------------------------------
263inline float
265{
266 n_assert2(creatorThreadId == Threading::Thread::GetMyThreadId(), "GetAxisValue can't be called from any thread but the creator thread!");
267 return this->axisValues[axis];
268}
269
270//------------------------------------------------------------------------------
273inline void
275{
276 critSect.Enter();
277 this->vibratorsDirty = true;
278 this->lowFreqVibrator = Math::clamp(f, 0.0f, 1.0f);
279 critSect.Leave();
280}
281
282//------------------------------------------------------------------------------
285inline float
287{
288 critSect.Enter();
289 float result = this->lowFreqVibrator;
290 critSect.Leave();
291 return result;
292}
293
294//------------------------------------------------------------------------------
297inline void
299{
300 critSect.Enter();
301 this->vibratorsDirty = true;
302 this->highFreqVibrator = Math::clamp(f, 0.0f, 1.0f);;
303 critSect.Leave();
304}
305
306//------------------------------------------------------------------------------
309inline float
311{
312 critSect.Enter();
313 float result = this->highFreqVibrator;
314 critSect.Leave();
315 return result;
316}
317
318} // namespace Base
319//------------------------------------------------------------------------------
320
An input handler which represents one of at most 4 game pads.
Definition gamepadbase.h:24
bool ButtonDown(Button btn) const
return true if button was down at least once in current frame
Definition gamepadbase.h:244
bool HasTransform() const
check if device has 3d location sensor
Definition gamepadbase.h:187
Util::FixedArray< float > axisValues
Definition gamepadbase.h:158
static void SetupDicts()
Definition gamepadbase.cc:191
static Util::Dictionary< Util::String, Button > buttonDict
Definition gamepadbase.h:168
__DeclareClass(GamePadBase)
static Axis AxisFromString(const Util::String &str)
convert string to axis code. InvalidAxis when the string is not one of the axes
Definition gamepadbase.cc:176
Button
gamepad buttons
Definition gamepadbase.h:29
@ HomeButton
Definition gamepadbase.h:47
@ L3Button
Definition gamepadbase.h:69
@ DPadUpButton
Definition gamepadbase.h:30
@ L1Button
Definition gamepadbase.h:67
@ BButton
Definition gamepadbase.h:42
@ XButton
Definition gamepadbase.h:43
@ OneButton
Definition gamepadbase.h:50
@ MinusButton
Definition gamepadbase.h:49
@ ZButton
Definition gamepadbase.h:52
@ StartButton
Definition gamepadbase.h:34
@ RightShoulderButton
Definition gamepadbase.h:39
@ InvalidButton
Definition gamepadbase.h:59
@ SquareButton
Definition gamepadbase.h:65
@ NumButtons
Definition gamepadbase.h:58
@ DPadLeftButton
Definition gamepadbase.h:32
@ CircleButton
Definition gamepadbase.h:64
@ AButton
Definition gamepadbase.h:41
@ R3Button
Definition gamepadbase.h:70
@ LeftThumbButton
Definition gamepadbase.h:36
@ CrossButton
Definition gamepadbase.h:63
@ TriangleButton
Definition gamepadbase.h:66
@ TwoButton
Definition gamepadbase.h:51
@ CButton
Definition gamepadbase.h:53
@ DPadRightButton
Definition gamepadbase.h:33
@ DPadDownButton
Definition gamepadbase.h:31
@ YButton
Definition gamepadbase.h:44
@ LeftShoulderButton
Definition gamepadbase.h:38
@ BackButton
Definition gamepadbase.h:35
@ RightThumbButton
Definition gamepadbase.h:37
@ R1Button
Definition gamepadbase.h:68
@ PlusButton
Definition gamepadbase.h:48
@ SelectButton
Definition gamepadbase.h:56
Util::Array< Input::InputEvent > GetStateAsInputEvents() const
get current state as an array of input events (override in subclass!)
Definition gamepadbase.cc:231
Math::mat4 transform
Definition gamepadbase.h:165
static Button ButtonFromString(const Util::String &str)
convert string to button code. InvalidButton when the string is not one of the buttons
Definition gamepadbase.cc:161
float highFreqVibrator
Definition gamepadbase.h:162
static Util::Dictionary< Util::String, Axis > axisDict
Definition gamepadbase.h:169
void SetHighFrequencyVibrator(float f)
set high-frequency vibration effect (0.0f .. 1.0f)
Definition gamepadbase.h:298
bool IsConnected() const
return true if this game pad is currently connected
Definition gamepadbase.h:177
bool isConnected
Definition gamepadbase.h:156
float GetAxisValue(Axis axis) const
get current axis value
Definition gamepadbase.h:264
void SetIndex(IndexT i)
set index -> TODO make threadsafe
Definition gamepadbase.h:206
const Math::mat4 & GetTransform() const
get current position (if supported by device, identity otherwise)
Definition gamepadbase.h:197
float GetLowFrequencyVibrator() const
get low-frequency vibration
Definition gamepadbase.h:286
bool ButtonUp(Button btn) const
return true if button was up at least once in current frame
Definition gamepadbase.h:254
float lowFreqVibrator
Definition gamepadbase.h:161
static SizeT GetMaxNumControllers()
get maximum number of controllers
Definition gamepadbase.h:225
Util::FixedArray< ButtonState > buttonStates
Definition gamepadbase.h:157
IndexT GetIndex() const
get the index of this game pad
Definition gamepadbase.h:216
Axis
gamepad axis
Definition gamepadbase.h:75
@ NumAxes
Definition gamepadbase.h:84
@ RightThumbYAxis
Definition gamepadbase.h:82
@ LeftTriggerAxis
Definition gamepadbase.h:77
@ LeftThumbXAxis
Definition gamepadbase.h:79
@ InvalidAxis
Definition gamepadbase.h:85
@ LeftThumbYAxis
Definition gamepadbase.h:80
@ RightThumbXAxis
Definition gamepadbase.h:81
@ RightTriggerAxis
Definition gamepadbase.h:78
Threading::ThreadId creatorThreadId
Definition gamepadbase.h:164
bool hasTransform
Definition gamepadbase.h:160
static Util::String ButtonAsString(Button btn)
convert button code to string
Definition gamepadbase.cc:105
virtual void OnAttach()
called when the handler is attached to the input server
Definition gamepadbase.cc:48
virtual void OnBeginFrame()
called on InputServer::BeginFrame()
Definition gamepadbase.cc:62
bool vibratorsDirty
Definition gamepadbase.h:159
IndexT index
Definition gamepadbase.h:155
static Util::String AxisAsString(Axis a)
convert axis to string
Definition gamepadbase.cc:141
float GetHighFrequencyVibrator() const
get high-frequency vibration
Definition gamepadbase.h:310
GamePadBase()
constructor
Definition gamepadbase.cc:22
void SetLowFrequencyVibrator(float f)
set low-frequency vibration effect (0.0f .. 1.0f)
Definition gamepadbase.h:274
virtual void OnReset()
reset the input handler
Definition gamepadbase.cc:77
bool ButtonPressed(Button btn) const
return true if a button is currently pressed
Definition gamepadbase.h:234
Threading::CriticalSection critSect
Definition gamepadbase.h:163
Input handlers receive and process input events.
Definition inputhandler.h:29
Nebula's dynamic array class.
Definition array.h:61
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:35
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
Nebula's universal string class.
Definition string.h:50
#define n_assert2(exp, msg)
Definition debug.h:59
#define n_assert(exp)
Definition debug.h:58
#define MAX_GAMEPADS
Definition gamepadbase.h:18
Definition gamecontentserverbase.cc:10
__forceinline float clamp(float val, float minVal, float maxVal)
Float clamping.
Definition scalar.h:506
pthread_t ThreadId
Definition linuxthreadid.h:15
ButtonState()
constructor
Definition gamepadbase.h:148
bool pressed
Definition gamepadbase.h:150
bool up
Definition gamepadbase.h:152
bool down
Definition gamepadbase.h:151
A 4x4 single point precision float matrix.
Definition mat4.h:55
int SizeT
Definition types.h:50
int IndexT
Definition types.h:49