-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputDirection.cpp
More file actions
249 lines (210 loc) · 6.3 KB
/
InputDirection.cpp
File metadata and controls
249 lines (210 loc) · 6.3 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/////////////////////////////////////////////////////////////////////////////
// Manage one input direction
/////////////////////////////////////////////////////////////////////////////
#include "InputDirection.h"
/////////////////////////////////////////////////////////////////////////////
// Construction
/////////////////////////////////////////////////////////////////////////////
CInputDirection::CInputDirection()
{
m_dVectorValue = 0.0f;
m_KeyDown = WXK_NONE;
m_KeyUp = WXK_NONE;
m_pJoystick = NULL;
m_Axe = AXIS_NONE;
m_bInverted = false;
m_dJoystickMin = 0.0f;
m_dJoystickMax = 0.0f;
m_dJoystickMiddle = 0.0f;
m_dDeadZoneMin = 0.0f;
m_dDeadZoneMax = 0.0f;
}
/////////////////////////////////////////////////////////////////////////////
// Destruction
/////////////////////////////////////////////////////////////////////////////
CInputDirection::~CInputDirection()
{
}
/////////////////////////////////////////////////////////////////////////////
// Init the control for keys
/////////////////////////////////////////////////////////////////////////////
void CInputDirection::SetKeys(const wxKeyCode& KeyDown, const wxKeyCode& KeyUp)
{
m_KeyDown = KeyDown;
m_KeyUp = KeyUp;
}
/////////////////////////////////////////////////////////////////////////////
// Init the controls for joystick
/////////////////////////////////////////////////////////////////////////////
void CInputDirection::SetJoystick(const eJoystickAxis& AxeToUse, CJoystick* pJoystick, bool bIsInverted)
{
// Joystick not set, break
if(NULL == pJoystick)
{
return;
}
if(AXIS_NONE == AxeToUse)
{
// Axis need to be reseted
m_Axe = AXIS_NONE;
}
else if(pJoystick->HasAxis(AxeToUse))
{
m_pJoystick = pJoystick;
m_Axe = AxeToUse;
m_bInverted = bIsInverted;
// Get min and max value of the joystick
switch(m_Axe)
{
case AXIS_X:
{
m_dJoystickMin = (double)m_pJoystick->GetXMin();
m_dJoystickMax = (double)m_pJoystick->GetXMax();
}
break;
case AXIS_Y:
{
m_dJoystickMin = (double)m_pJoystick->GetYMin();
m_dJoystickMax = (double)m_pJoystick->GetYMax();
}
break;
case AXIS_RUDDER:
{
m_dJoystickMin = (double)m_pJoystick->GetRudderMin();
m_dJoystickMax = (double)m_pJoystick->GetRudderMax();
}
break;
case AXIS_U:
{
m_dJoystickMin = (double)m_pJoystick->GetUMin();
m_dJoystickMax = (double)m_pJoystick->GetUMax();
}
break;
case AXIS_V:
{
m_dJoystickMin = (double)m_pJoystick->GetVMin();
m_dJoystickMax = (double)m_pJoystick->GetVMax();
}
break;
case AXIS_Z:
{
m_dJoystickMin = (double)m_pJoystick->GetZMin();
m_dJoystickMax = (double)m_pJoystick->GetZMax();
}
break;
default:
break;
}
// Get middle position of the joystick
m_dJoystickMiddle = (m_dJoystickMax - m_dJoystickMin)/2.0f;
// Get a dead zone range (5% of middle position)
double dDeadZone = m_dJoystickMiddle * 0.05f;
// Set min and max value for the dead zone
m_dDeadZoneMin = m_dJoystickMiddle - dDeadZone;
m_dDeadZoneMax = m_dJoystickMiddle + dDeadZone;
}
}
/////////////////////////////////////////////////////////////////////////////
// Get the value up key
/////////////////////////////////////////////////////////////////////////////
wxKeyCode CInputDirection::GetKeyUp()
{
return m_KeyUp;
}
/////////////////////////////////////////////////////////////////////////////
// Get the value down key
/////////////////////////////////////////////////////////////////////////////
wxKeyCode CInputDirection::GetKeyDown()
{
return m_KeyDown;
}
/////////////////////////////////////////////////////////////////////////////
// Get joystick axe
/////////////////////////////////////////////////////////////////////////////
eJoystickAxis CInputDirection::GetJoystickAxe()
{
return m_Axe;
}
/////////////////////////////////////////////////////////////////////////////
// Get the joystick inverted flag
/////////////////////////////////////////////////////////////////////////////
bool CInputDirection::GetJoystickInvertedFlag()
{
return m_bInverted;
}
/////////////////////////////////////////////////////////////////////////////
// Update values
/////////////////////////////////////////////////////////////////////////////
void CInputDirection::UpdateDirection(const double& dAcceleration, const double& dDecceleration, const double& dMaxSpeed)
{
bool bUseKeyboard = true;
bool bLetValueGoDown = true;
// If a joystick is set, check if it is currently in use
if(m_Axe != AXIS_NONE)
{
double dValue = (double)m_pJoystick->GetRawValue(m_Axe);
// Joystick is in use if the value is out of the dead zone
if( (dValue > m_dDeadZoneMax) || (dValue < m_dDeadZoneMin) )
{
bUseKeyboard = false;
bLetValueGoDown = false;
dValue = (dValue-m_dJoystickMiddle)/m_dJoystickMiddle;
// Inverted axe
if(m_bInverted)
{
dValue = -1.0f * dValue;
}
// Reduce to max speed
m_dVectorValue = dValue * dMaxSpeed;
}
}
// Joystick is not present, not used for this axe or has not move, so check now for keys
if(bUseKeyboard)
{
// Vector up key pressed
if(wxGetKeyState(m_KeyUp))
{
bLetValueGoDown = false;
if(m_dVectorValue < dMaxSpeed)
{
m_dVectorValue += dAcceleration;
}
}
// Vector down key pressed
else if(wxGetKeyState(m_KeyDown))
{
bLetValueGoDown = false;
if(m_dVectorValue > -dMaxSpeed)
{
m_dVectorValue -= dAcceleration;
}
}
}
// For this axe, the joystick and the keyboard are not used, so let the value go down
if(bLetValueGoDown)
{
if(m_dVectorValue != 0.0f)
{
if(m_dVectorValue <= -0.01f)
{
m_dVectorValue += dDecceleration;
}
else if(m_dVectorValue >= 0.01f)
{
m_dVectorValue -= dDecceleration;
}
else
{
// Between -0,01 and 0,01 set the vector to 0, otherwise it may never be 0 because of rounding
m_dVectorValue = 0.0f;
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Get current vector value
/////////////////////////////////////////////////////////////////////////////
const double& CInputDirection::GetVectorValue()
{
return m_dVectorValue;
}