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
17#define MAX_GAMEPADS 4
18
19//------------------------------------------------------------------------------
20namespace Base
21{
23{
25public:
27 enum Button
28 {
29 DPadUpButton = 0, // all platforms
30 DPadDownButton, // all platforms
31 DPadLeftButton, // all platforms
32 DPadRightButton, // all platforms
33 StartButton, // all platforms
34 BackButton, // Xbox360
35 LeftThumbButton, // Xbox360, PS3: L3
36 RightThumbButton, // Xbox360, PS3: R3
37 LeftShoulderButton, // Xbox360, PS3: L1, Wii: ClassicController ZL
38 RightShoulderButton, // Xbox360, PS3: R1, Wii: ClassicController ZR
39
40 AButton, // Xbox360: A, Wii: A/a, PS3: Cross
41 BButton, // Xbox360: B, Wii: b(pad), PS3: Circle
42 XButton, // Xbox360: X, Wii: x(pad), PS3: Square
43 YButton, // Xbox360: Y, Wii: y(pad), PS3: Triangle
44
45 // Wii specific buttons
46 HomeButton, // Xbox360: /, Wii: Home, PS3: /
47 PlusButton, // Xbox360: /, Wii: +, PS3: /
48 MinusButton, // Xbox360: /, Wii: -, PS3: /
49 OneButton, // Xbox360: /, Wii: 1, PS3: /
50 TwoButton, // Xbox360: /, Wii: 2, PS3: /
51 ZButton, // Xbox360: /, Wii: Z(Nunchuk), PS3: /
52 CButton, // Xbox360: /, Wii: C(Nunchuk), PS3: /
53
54 // PS3 specific buttons
55 SelectButton, // Xbox360: /, Wii: /, PS3: Select
56
59
60
61 // PS3 aliases !!! MUST BE after NumButtons & InvalidButton !!!
70 };
71
73 enum Axis
74 {
75 // general axi
76 LeftTriggerAxis = 0, // 0.0f .. 1.0f
77 RightTriggerAxis, // 0.0f .. 1.0f
78 LeftThumbXAxis, // -1.0f .. 1.0f
79 LeftThumbYAxis, // -1.0f .. 1.0f
80 RightThumbXAxis, // -1.0f .. 1.0f
81 RightThumbYAxis, // -1.0f .. 1.0f
82
85 };
86
90 virtual ~GamePadBase();
91
96
98 bool IsConnected() const;
100 void SetIndex(IndexT i);
102 IndexT GetIndex() const;
105
107 bool ButtonPressed(Button btn) const;
109 bool ButtonDown(Button btn) const;
111 bool ButtonUp(Button btn) const;
113 float GetAxisValue(Axis axis) const;
115 bool HasTransform() const;
117 const Math::mat4 & GetTransform() const;
118
120 void SetLowFrequencyVibrator(float f);
122 float GetLowFrequencyVibrator() const;
124 void SetHighFrequencyVibrator(float f);
126 float GetHighFrequencyVibrator() const;
127
130
131protected:
133 virtual void OnAttach();
135 virtual void OnReset();
136
138 {
139 public:
141 ButtonState() : pressed(false), down(false), up(false) {};
142
144 bool down;
145 bool up;
146 };
147
159};
160
161//------------------------------------------------------------------------------
164inline bool
166{
167 n_assert2(creatorThreadId == Threading::Thread::GetMyThreadId(), "IsConnected can't be called from any thread but the creator thread!");
168 return this->isConnected;
169}
170
171//------------------------------------------------------------------------------
174inline bool
176{
177 return this->hasTransform;
178}
179
180//------------------------------------------------------------------------------
183inline
184const Math::mat4 &
186{
187 return this->transform;
188}
189
190//------------------------------------------------------------------------------
193inline void
195{
196 n_assert(i < this->GetMaxNumControllers());
197 this->index = i;
198}
199
200//------------------------------------------------------------------------------
203inline IndexT
205{
206 return this->index;
207}
208
209//------------------------------------------------------------------------------
212inline SizeT
217
218//------------------------------------------------------------------------------
221inline bool
223{
224 n_assert2(creatorThreadId == Threading::Thread::GetMyThreadId(), "ButtonPressed can't be called from any thread but the creator thread!");
225 return this->buttonStates[btn].pressed;
226}
227
228//------------------------------------------------------------------------------
231inline bool
233{
234 n_assert2(creatorThreadId == Threading::Thread::GetMyThreadId(), "ButtonDown can't be called from any thread but the creator thread!");
235 return this->buttonStates[btn].down;
236}
237
238//------------------------------------------------------------------------------
241inline bool
243{
244 n_assert2(creatorThreadId == Threading::Thread::GetMyThreadId(), "ButtonUp can't be called from any thread but the creator thread!");
245 return this->buttonStates[btn].up;
246}
247
248//------------------------------------------------------------------------------
251inline float
253{
254 n_assert2(creatorThreadId == Threading::Thread::GetMyThreadId(), "GetAxisValue can't be called from any thread but the creator thread!");
255 return this->axisValues[axis];
256}
257
258//------------------------------------------------------------------------------
261inline void
263{
264 critSect.Enter();
265 this->vibratorsDirty = true;
266 this->lowFreqVibrator = Math::clamp(f, 0.0f, 1.0f);
267 critSect.Leave();
268}
269
270//------------------------------------------------------------------------------
273inline float
275{
276 critSect.Enter();
277 float result = this->lowFreqVibrator;
278 critSect.Leave();
279 return result;
280}
281
282//------------------------------------------------------------------------------
285inline void
287{
288 critSect.Enter();
289 this->vibratorsDirty = true;
290 this->highFreqVibrator = Math::clamp(f, 0.0f, 1.0f);;
291 critSect.Leave();
292}
293
294//------------------------------------------------------------------------------
297inline float
299{
300 critSect.Enter();
301 float result = this->highFreqVibrator;
302 critSect.Leave();
303 return result;
304}
305
306} // namespace Base
307//------------------------------------------------------------------------------
308
An input handler which represents one of at most 4 game pads.
Definition gamepadbase.h:23
bool ButtonDown(Button btn) const
return true if button was down at least once in current frame
Definition gamepadbase.h:232
bool HasTransform() const
check if device has 3d location sensor
Definition gamepadbase.h:175
Util::FixedArray< float > axisValues
Definition gamepadbase.h:151
__DeclareClass(GamePadBase)
Button
gamepad buttons
Definition gamepadbase.h:28
@ HomeButton
Definition gamepadbase.h:46
@ L3Button
Definition gamepadbase.h:68
@ DPadUpButton
Definition gamepadbase.h:29
@ L1Button
Definition gamepadbase.h:66
@ BButton
Definition gamepadbase.h:41
@ XButton
Definition gamepadbase.h:42
@ OneButton
Definition gamepadbase.h:49
@ MinusButton
Definition gamepadbase.h:48
@ ZButton
Definition gamepadbase.h:51
@ StartButton
Definition gamepadbase.h:33
@ RightShoulderButton
Definition gamepadbase.h:38
@ InvalidButton
Definition gamepadbase.h:58
@ SquareButton
Definition gamepadbase.h:64
@ NumButtons
Definition gamepadbase.h:57
@ DPadLeftButton
Definition gamepadbase.h:31
@ CircleButton
Definition gamepadbase.h:63
@ AButton
Definition gamepadbase.h:40
@ R3Button
Definition gamepadbase.h:69
@ LeftThumbButton
Definition gamepadbase.h:35
@ CrossButton
Definition gamepadbase.h:62
@ TriangleButton
Definition gamepadbase.h:65
@ TwoButton
Definition gamepadbase.h:50
@ CButton
Definition gamepadbase.h:52
@ DPadRightButton
Definition gamepadbase.h:32
@ DPadDownButton
Definition gamepadbase.h:30
@ YButton
Definition gamepadbase.h:43
@ LeftShoulderButton
Definition gamepadbase.h:37
@ BackButton
Definition gamepadbase.h:34
@ RightThumbButton
Definition gamepadbase.h:36
@ R1Button
Definition gamepadbase.h:67
@ PlusButton
Definition gamepadbase.h:47
@ SelectButton
Definition gamepadbase.h:55
Util::Array< Input::InputEvent > GetStateAsInputEvents() const
get current state as an array of input events (override in subclass!)
Definition gamepadbase.cc:146
Math::mat4 transform
Definition gamepadbase.h:158
float highFreqVibrator
Definition gamepadbase.h:155
void SetHighFrequencyVibrator(float f)
set high-frequency vibration effect (0.0f .. 1.0f)
Definition gamepadbase.h:286
bool IsConnected() const
return true if this game pad is currently connected
Definition gamepadbase.h:165
bool isConnected
Definition gamepadbase.h:149
float GetAxisValue(Axis axis) const
get current axis value
Definition gamepadbase.h:252
void SetIndex(IndexT i)
set index -> TODO make threadsafe
Definition gamepadbase.h:194
const Math::mat4 & GetTransform() const
get current position (if supported by device, identity otherwise)
Definition gamepadbase.h:185
virtual ~GamePadBase()
destructor
Definition gamepadbase.cc:36
float GetLowFrequencyVibrator() const
get low-frequency vibration
Definition gamepadbase.h:274
bool ButtonUp(Button btn) const
return true if button was up at least once in current frame
Definition gamepadbase.h:242
float lowFreqVibrator
Definition gamepadbase.h:154
static SizeT GetMaxNumControllers()
get maximum number of controllers
Definition gamepadbase.h:213
Util::FixedArray< ButtonState > buttonStates
Definition gamepadbase.h:150
IndexT GetIndex() const
get the index of this game pad
Definition gamepadbase.h:204
Axis
gamepad axis
Definition gamepadbase.h:74
@ NumAxes
Definition gamepadbase.h:83
@ RightThumbYAxis
Definition gamepadbase.h:81
@ LeftTriggerAxis
Definition gamepadbase.h:76
@ LeftThumbXAxis
Definition gamepadbase.h:78
@ InvalidAxis
Definition gamepadbase.h:84
@ LeftThumbYAxis
Definition gamepadbase.h:79
@ RightThumbXAxis
Definition gamepadbase.h:80
@ RightTriggerAxis
Definition gamepadbase.h:77
Threading::ThreadId creatorThreadId
Definition gamepadbase.h:157
bool hasTransform
Definition gamepadbase.h:153
static Util::String ButtonAsString(Button btn)
convert button code to string
Definition gamepadbase.cc:87
virtual void OnAttach()
called when the handler is attached to the input server
Definition gamepadbase.cc:45
bool vibratorsDirty
Definition gamepadbase.h:152
IndexT index
Definition gamepadbase.h:148
static Util::String AxisAsString(Axis a)
convert axis to string
Definition gamepadbase.cc:123
float GetHighFrequencyVibrator() const
get high-frequency vibration
Definition gamepadbase.h:298
GamePadBase()
constructor
Definition gamepadbase.cc:19
void SetLowFrequencyVibrator(float f)
set low-frequency vibration effect (0.0f .. 1.0f)
Definition gamepadbase.h:262
virtual void OnReset()
reset the input handler
Definition gamepadbase.cc:59
bool ButtonPressed(Button btn) const
return true if a button is currently pressed
Definition gamepadbase.h:222
Threading::CriticalSection critSect
Definition gamepadbase.h:156
Input handlers receive and process input events.
Definition inputhandler.h:29
Critical section objects are used to protect a portion of code from parallel execution.
Nebula's dynamic array class.
Definition array.h:60
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
#define n_assert2(exp, msg)
Definition debug.h:51
#define n_assert(exp)
Definition debug.h:50
#define MAX_GAMEPADS
Definition gamepadbase.h:17
Definition gamecontentserverbase.cc:10
__forceinline float clamp(float val, float minVal, float maxVal)
Float clamping.
Definition scalar.h:487
pthread_t ThreadId
Definition linuxthreadid.h:15
Definition gamepadbase.h:138
ButtonState()
constructor
Definition gamepadbase.h:141
bool pressed
Definition gamepadbase.h:143
bool up
Definition gamepadbase.h:145
bool down
Definition gamepadbase.h:144
A 4x4 single point precision float matrix.
Definition mat4.h:49
Nebula's universal string class.
Definition string.h:50
int SizeT
Definition types.h:49
int IndexT
Definition types.h:48