Skip to content

Commit 22e743b

Browse files
committed
organizing virtual joystick
1 parent 4ee659d commit 22e743b

2 files changed

Lines changed: 105 additions & 123 deletions

File tree

src/QJoysticks/VirtualJoystick.cpp

Lines changed: 86 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,25 @@ VirtualJoystick::VirtualJoystick (QObject* parent) : QObject (parent)
3030
m_joystick.blacklisted = false;
3131
m_joystick.name = tr ("Virtual Joystick");
3232

33-
axisStatus.clear();
34-
35-
axisStatus = std::vector<AxisState>(6);
36-
37-
axisValue = std::vector<qint16>(6);
38-
3933
/* Initialize POVs */
4034
m_joystick.povs.append (0);
4135

4236
/* Initialize axes */
43-
for (int i = 0; i < 6; ++i){
37+
m_axisStatus = QVector<AxisState>(NUMBER_OF_AXES, AxisState::STILL);
38+
m_axisValue = QVector<qint16>(NUMBER_OF_AXES, 0);
39+
for (int i = 0; i < NUMBER_OF_AXES; ++i)
40+
{
4441
m_joystick.axes.append (0);
45-
46-
axisStatus[i] = STILL;
47-
axisValue[i] = 0;
48-
4942
}
5043

5144
/* Initialize buttons */
52-
for (int i = 0; i < 10; ++i)
45+
for (int i = 0; i < NUMBER_OF_BUTTONS; ++i)
5346
m_joystick.buttons.append (false);
5447

5548

56-
timerUpdateAxis = new QTimer();
57-
connect(timerUpdateAxis, &QTimer::timeout,
49+
m_timerUpdateAxis.reset( new QTimer() );
50+
connect(m_timerUpdateAxis.get(), &QTimer::timeout,
5851
this, &VirtualJoystick::updateAxis);
59-
60-
// qApp->installEventFilter (this);
6152
}
6253

6354
/**
@@ -125,15 +116,17 @@ void VirtualJoystick::setAxisRange (qreal range)
125116
*/
126117
void VirtualJoystick::setJoystickEnabled (bool enabled)
127118
{
128-
if(enabled){
129-
timerUpdateAxis->start(10);
130-
119+
if(enabled)
120+
{
121+
// That means that the axes will be updated each 10 ms
122+
m_timerUpdateAxis->start(10);
131123
qApp->installEventFilter(this);
132124
}
133125

134-
else if( !enabled ){
135-
timerUpdateAxis->stop();
136-
126+
else
127+
{
128+
m_timerUpdateAxis->stop();
129+
// Removing the event filter since the joystick is no longer active
137130
qApp->removeEventFilter(this);
138131
}
139132

@@ -145,12 +138,14 @@ void VirtualJoystick::setAxisSensibility(qreal sensibility)
145138
{
146139
if(sensibility > 1 ||
147140
sensibility < 0)
141+
{
148142
qFatal("Fatal: VirtualJoystick Axis sensibility must be a value between 0 and 1");
143+
}
149144

150145
const qint16 stepMinimum = 50;
151146
const qint16 stepMaximum = 1000;
152147

153-
axisStep = static_cast<qint16>(( -stepMaximum + stepMinimum)*sensibility) + stepMaximum;
148+
m_axisStep = static_cast<qint16>(( -stepMaximum + stepMinimum)*sensibility) + stepMaximum;
154149

155150

156151
}
@@ -163,133 +158,116 @@ void VirtualJoystick::readAxes (int key, bool pressed)
163158
{
164159

165160
/* Horizontal axis on thumb 1 */
166-
if (key == Qt::Key_A){
167-
161+
if (key == Qt::Key_A)
162+
{
168163
if(pressed)
169-
axisStatus[0] = DECREASE;
170-
164+
m_axisStatus[AXIS_AD] = DECREASE;
171165
else
172-
axisStatus[0] = STILL;
173-
166+
m_axisStatus[AXIS_AD] = STILL;
174167
}
175-
else if (key == Qt::Key_D) {
176-
168+
else if (key == Qt::Key_D)
169+
{
177170
if(pressed)
178-
axisStatus[0] = INCREASE;
179-
171+
m_axisStatus[AXIS_AD] = INCREASE;
180172
else
181-
axisStatus[0] = STILL;
182-
173+
m_axisStatus[AXIS_AD] = STILL;
183174
}
184175

185176
/* Vertical axis on thumb 1 */
186-
if (key == Qt::Key_S){
187-
177+
if (key == Qt::Key_S)
178+
{
188179
if(pressed)
189-
axisStatus[1] = DECREASE;
190-
180+
m_axisStatus[AXIS_SW] = DECREASE;
191181
else
192-
axisStatus[1] = STILL;
193-
182+
m_axisStatus[AXIS_SW] = STILL;
194183
}
195-
else if (key == Qt::Key_W) {
196-
184+
else if (key == Qt::Key_W)
185+
{
197186
if(pressed)
198-
axisStatus[1] = INCREASE;
199-
187+
m_axisStatus[AXIS_SW] = INCREASE;
200188
else
201-
axisStatus[1] = STILL;
202-
189+
m_axisStatus[AXIS_SW] = STILL;
203190
}
204191

205192
/* Trigger 1 */
206-
if (key == Qt::Key_Q){
207-
193+
if (key == Qt::Key_Q)
194+
{
208195
if(pressed)
209-
axisStatus[2] = DECREASE;
210-
196+
m_axisStatus[AXIS_QE] = DECREASE;
211197
else
212-
axisStatus[2] = STILL;
198+
m_axisStatus[AXIS_QE] = STILL;
213199

214200
}
215-
else if (key == Qt::Key_E) {
216-
201+
else if (key == Qt::Key_E)
202+
{
217203
if(pressed)
218-
axisStatus[2] = INCREASE;
219-
204+
m_axisStatus[AXIS_QE] = INCREASE;
220205
else
221-
axisStatus[2] = STILL;
206+
m_axisStatus[AXIS_QE] = STILL;
222207

223208
}
224209

225210
/* Trigger 2 */
226-
if (key == Qt::Key_U){
227-
211+
if (key == Qt::Key_U)
212+
{
228213
if(pressed)
229-
axisStatus[3] = DECREASE;
230-
214+
m_axisStatus[AXIS_UO] = DECREASE;
231215
else
232-
axisStatus[3] = STILL;
216+
m_axisStatus[AXIS_UO] = STILL;
233217

234218
}
235-
else if (key == Qt::Key_O) {
236-
219+
else if (key == Qt::Key_O)
220+
{
237221
if(pressed)
238-
axisStatus[3] = INCREASE;
239-
222+
m_axisStatus[AXIS_UO] = INCREASE;
240223
else
241-
axisStatus[3] = STILL;
224+
m_axisStatus[AXIS_UO] = STILL;
242225

243226
}
244227

245228
/* Horizontal axis on thumb 2 */
246-
if (key == Qt::Key_J){
247-
229+
if (key == Qt::Key_J)
230+
{
248231
if(pressed)
249-
axisStatus[4] = DECREASE;
250-
232+
m_axisStatus[AXIS_JL] = DECREASE;
251233
else
252-
axisStatus[4] = STILL;
234+
m_axisStatus[AXIS_JL] = STILL;
253235

254236
}
255-
else if (key == Qt::Key_L) {
256-
237+
else if (key == Qt::Key_L)
238+
{
257239
if(pressed)
258-
axisStatus[4] = INCREASE;
259-
240+
m_axisStatus[AXIS_JL] = INCREASE;
260241
else
261-
axisStatus[4] = STILL;
242+
m_axisStatus[AXIS_JL] = STILL;
262243

263244
}
264245

265246
/* Vertical axis on thumb 2 */
266-
if (key == Qt::Key_K){
267-
247+
if (key == Qt::Key_K)
248+
{
268249
if(pressed)
269-
axisStatus[5] = DECREASE;
270-
250+
m_axisStatus[AXIS_KI] = DECREASE;
271251
else
272-
axisStatus[5] = STILL;
252+
m_axisStatus[AXIS_KI] = STILL;
273253

274254
}
275-
else if (key == Qt::Key_I) {
276-
255+
else if (key == Qt::Key_I)
256+
{
277257
if(pressed)
278-
axisStatus[5] = INCREASE;
279-
258+
m_axisStatus[AXIS_KI] = INCREASE;
280259
else
281-
axisStatus[5] = STILL;
282-
260+
m_axisStatus[AXIS_KI] = STILL;
283261
}
284262

285263

286264
}
287265

288266
void VirtualJoystick::updateAxis()
289267
{
290-
for (quint8 i=0; i<6; i++){
291-
292-
vChangeAxisValue(i);
268+
for (quint8 i=0; i<NUMBER_OF_AXES; i++)
269+
{
270+
changeAxisValue(i);
293271

294272
}
295273

@@ -333,9 +311,10 @@ void VirtualJoystick::readButtons (int key, bool pressed)
333311
{
334312
int button = -1;
335313

336-
if (key == Qt::Key_0){
314+
if (key == Qt::Key_0)
315+
{ // Special key that reset all axes
337316
button = 0;
338-
haltAxis();
317+
resetAllAxes();
339318
}
340319
else if (key == Qt::Key_1)
341320
button = 1;
@@ -407,49 +386,43 @@ bool VirtualJoystick::eventFilter (QObject* object, QEvent* event)
407386
return false;
408387
}
409388

410-
void VirtualJoystick::vChangeAxisValue(quint8 axis)
389+
void VirtualJoystick::changeAxisValue(quint8 axis)
411390
{
412-
switch (axisStatus[axis]) {
413-
391+
switch (m_axisStatus[axis])
392+
{
414393
case STILL:
415394
return;
416395

417396
case INCREASE:
418-
419-
if(axisValue[axis] > axisMaximumV - axisStep)
420-
axisValue[axis] = axisMaximumV;
421-
397+
if(m_axisValue[axis] > AXIS_MAXIMUM_VIRTUAL_JOYSTICK - m_axisStep)
398+
m_axisValue[axis] = AXIS_MAXIMUM_VIRTUAL_JOYSTICK;
422399
else
423-
axisValue[axis] += axisStep;
424-
400+
m_axisValue[axis] += m_axisStep;
425401
break;
426402

427403
case DECREASE:
428-
429-
if(axisValue[axis] < axisMinimumV + axisStep)
430-
axisValue[axis] = axisMinimumV;
431-
404+
if(m_axisValue[axis] < AXIS_MINIMUM_VIRTUAL_JOYSTICK + m_axisStep)
405+
m_axisValue[axis] = AXIS_MINIMUM_VIRTUAL_JOYSTICK;
432406
else
433-
axisValue[axis] -= axisStep;
434-
407+
m_axisValue[axis] -= m_axisStep;
435408
break;
436409
}
437410

438411
QJoystickAxisEvent event;
439412

440413
event.axis = axis;
441-
event.value = m_axisRange * static_cast<qreal>(axisValue[axis])/axisMaximumV ;
414+
event.value = m_axisRange * static_cast<qreal>(m_axisValue[axis])/AXIS_MAXIMUM_VIRTUAL_JOYSTICK ;
442415
event.joystick = joystick();
443416

444417
emit axisEvent(event);
445418

446419
}
447420

448-
void VirtualJoystick::haltAxis()
421+
void VirtualJoystick::resetAllAxes()
449422
{
450-
for (quint8 i=0; i<6; i++){
451-
452-
axisValue[i] = 0;
423+
for (quint8 i=0; i<NUMBER_OF_AXES; i++)
424+
{
425+
m_axisValue[i] = 0;
453426

454427
QJoystickAxisEvent event;
455428

@@ -458,6 +431,5 @@ void VirtualJoystick::haltAxis()
458431
event.joystick = joystick();
459432

460433
emit axisEvent(event);
461-
462434
}
463435
}

0 commit comments

Comments
 (0)