-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystickwidget.cpp
377 lines (322 loc) · 9.54 KB
/
joystickwidget.cpp
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "joystickwidget.hpp"
JoystickWidget::JoystickWidget(QWidget *parent) : QWidget(parent)
{
QSizePolicy policy;
policy.setHorizontalPolicy(QSizePolicy::Policy::Expanding);
policy.setVerticalPolicy(QSizePolicy::Policy::Expanding);
setSizePolicy(policy);
m_state = m_prevState = {Direction::None, 0, 0};
m_scrollWheelPos = 0;
m_maxDistFromCenter = 200;
m_shouldBindToMousePos = false;
setMaxMoveSpeed(17);
setMaxZoomSpeed(7);
m_moveAdjustment = m_zoomAdjustment = 0;
m_moveAdjustmentWeight = m_zoomAdjustmentWeight = 0;
m_degPerZoom = 15;
m_zoomArcSpan = 90;
updateMoveSpeedSteps();
updateZoomSpeedSteps();
}
QPointF JoystickWidget::centerPoint()
{
return QPointF(size().width() / 2, size().height() / 2);
}
QRectF JoystickWidget::centerCircleBounds()
{
float size = calcMoveSpeedStepAt(m_maxMoveSpeed - 1);
QRectF bounds(-(size / 2), -(size / 2), size, size);
if (m_shouldBindToMousePos)
return bounds.translated(m_mousePos);
return bounds.translated(centerPoint());
}
float JoystickWidget::calcMoveSpeedStepAt(int index)
{
float xVal = (float)index / m_maxMoveSpeed;
float multiplier = ((m_moveAdjustmentWeight *
(std::powf(xVal, m_moveAdjustment))) +
((1 - m_moveAdjustmentWeight) * xVal));
return (m_maxDistFromCenter - ((multiplier * m_maxMoveSpeed) *
(m_maxDistFromCenter / m_maxMoveSpeed)));
}
float JoystickWidget::calcZoomSpeedStepAt(int index)
{
float xVal = (float)(m_maxZoomSpeed - index) / m_maxZoomSpeed;
float multiplier = 1 - (((m_zoomAdjustmentWeight *
(std::powf(xVal, m_zoomAdjustment))) +
((1 - m_zoomAdjustmentWeight) * xVal)));
return ((m_maxZoomSpeed * m_degPerZoom) -
((multiplier * m_maxZoomSpeed) * m_degPerZoom));
}
void JoystickWidget::updateMoveSpeedSteps()
{
m_moveSpeedSteps.clear();
for (int i = 0; i < m_maxMoveSpeed; i++) {
MoveSpeedStep step;
step.speed = m_maxMoveSpeed - i;
step.radius = calcMoveSpeedStepAt(i);
QRectF bounds(-(step.radius), -(step.radius), (step.radius * 2),
(step.radius * 2));
step.bounds = bounds.translated(centerPoint());
QColor background = palette().window().color();
QColor secondary = palette().window().color();
secondary = (secondary.lightness() > 128)
? (secondary.darker(115))
: (secondary.lighter(140));
step.color = (m_maxMoveSpeed % 2 == 0)
? (i % 2 == 0 ? secondary : background)
: (i % 2 == 0 ? background : secondary);
m_moveSpeedSteps.push_back(step);
}
update();
}
void JoystickWidget::updateZoomSpeedSteps()
{
m_zoomSpeedSteps.clear();
for (int i = 0; i < m_maxZoomSpeed; i++) {
ZoomSpeedStep step;
step.speed = m_maxZoomSpeed - i;
step.deg = calcZoomSpeedStepAt(i);
step.dispDeg = (m_zoomArcSpan * step.deg) /
(m_maxZoomSpeed * m_degPerZoom);
m_zoomSpeedSteps.push_back(step);
}
update();
}
void JoystickWidget::updateState()
{
if (m_shouldBindToMousePos) {
QLineF normVector(centerPoint(), m_mousePos);
qreal angle = normVector.angle();
float length = (float)normVector.length();
if (length < m_moveSpeedSteps.last().radius)
m_state.direction = Direction::None;
else if (292.5 <= angle && angle < 337.5)
m_state.direction = Direction::DownRight;
else if (247.5 <= angle && angle < 292.5)
m_state.direction = Direction::Down;
else if (202.5 <= angle && angle < 247.5)
m_state.direction = Direction::DownLeft;
else if (157.5 <= angle && angle < 202.5)
m_state.direction = Direction::Left;
else if (112.5 <= angle && angle < 157.5)
m_state.direction = Direction::UpLeft;
else if (67.5 <= angle && angle < 112.5)
m_state.direction = Direction::Up;
else if (22.5 <= angle && angle < 67.5)
m_state.direction = Direction::UpRight;
else
m_state.direction = Direction::Right;
for (int i = (m_moveSpeedSteps.length() - 1); i >= 0; i--) {
if (m_moveSpeedSteps.at(i).radius < length)
m_state.speed = m_moveSpeedSteps.at(i).speed;
}
} else {
m_state.direction = Direction::None;
m_state.speed = 0;
}
if (m_zoomSpeedSteps.last().deg < std::abs(m_scrollWheelPos)) {
for (int i = (m_zoomSpeedSteps.length() - 1); i >= 0; i--) {
if (m_zoomSpeedSteps.at(i).deg <
std::abs(m_scrollWheelPos))
m_state.zoom = m_zoomSpeedSteps.at(i).speed *
m_zoomDirection;
}
} else {
m_state.zoom = 0;
}
}
void JoystickWidget::emitChanges()
{
updateState();
if (m_state.direction != m_prevState.direction ||
m_state.speed != m_prevState.speed) {
switch (m_state.direction) {
case Direction::Up:
emit movedUp(m_state.speed);
#if DEBUG_MOVE
qDebug() << "Moved Up at speed" << m_state.speed;
#endif
break;
case Direction::UpRight:
emit movedUpRight(m_state.speed);
#if DEBUG_MOVE
qDebug() << "Moved UpRight at speed" << m_state.speed;
#endif
break;
case Direction::Right:
emit movedRight(m_state.speed);
#if DEBUG_MOVE
qDebug() << "Moved Right at speed" << m_state.speed;
#endif
break;
case Direction::DownRight:
emit movedDownRight(m_state.speed);
#if DEBUG_MOVE
qDebug() << "Moved DownRight at speed" << m_state.speed;
#endif
break;
case Direction::Down:
emit movedDown(m_state.speed);
#if DEBUG_MOVE
qDebug() << "Moved Down at speed" << m_state.speed;
#endif
break;
case Direction::DownLeft:
emit movedDownLeft(m_state.speed);
#if DEBUG_MOVE
qDebug() << "Moved DownLeft at speed" << m_state.speed;
#endif
break;
case Direction::Left:
emit movedLeft(m_state.speed);
#if DEBUG_MOVE
qDebug() << "Moved Left at speed" << m_state.speed;
#endif
break;
case Direction::UpLeft:
emit movedUpLeft(m_state.speed);
#if DEBUG_MOVE
qDebug() << "Moved UpLeft at speed" << m_state.speed;
#endif
break;
case Direction::None:
default:
emit stoppedMoving();
#if DEBUG_MOVE
qDebug() << "Stopped Moving";
#endif
}
m_prevState.direction = m_state.direction;
m_prevState.speed = m_state.speed;
}
if (m_state.zoom != m_prevState.zoom) {
if (m_state.zoom > 0) {
emit zoomedIn(m_state.zoom);
#if DEBUG_ZOOM
qDebug() << "Zoomed In at speed" << m_state.zoom;
#endif
} else if (m_state.zoom < 0) {
emit zoomedOut(m_state.zoom * -1);
#if DEBUG_ZOOM
qDebug() << "Zoomed Out at speed" << m_state.zoom * -1;
#endif
} else {
emit stoppedZooming();
#if DEBUG_ZOOM
qDebug() << "Stopped Zooming";
#endif
}
m_prevState.zoom = m_state.zoom;
}
update();
}
void JoystickWidget::setMousePos(QPointF position)
{
QLineF limitLine(centerPoint(), position);
if (limitLine.length() > m_maxDistFromCenter)
limitLine.setLength(m_maxDistFromCenter);
m_mousePos = limitLine.p2();
}
void JoystickWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_shouldBindToMousePos =
QLineF(centerPoint(), event->pos()).length() <=
m_maxDistFromCenter;
setMousePos(event->pos());
emitChanges();
} else if (event->button() == Qt::MiddleButton) {
m_scrollWheelPos = 0;
emitChanges();
} else if (event->button() == Qt::RightButton) {
m_shouldBindToMousePos = false;
setMousePos(event->pos());
m_scrollWheelPos = 0;
emitChanges();
}
}
void JoystickWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_shouldBindToMousePos = false;
setMousePos(event->pos());
emitChanges();
}
}
void JoystickWidget::mouseMoveEvent(QMouseEvent *event)
{
setMousePos(event->pos());
emitChanges();
}
void JoystickWidget::wheelEvent(QWheelEvent *event)
{
m_scrollWheelPos += (float)event->angleDelta().y() / 8;
m_zoomDirection = (m_scrollWheelPos < 0 ? -1 : 1);
if (std::abs(m_scrollWheelPos) > m_zoomSpeedSteps.first().deg)
m_scrollWheelPos =
m_zoomSpeedSteps.first().deg * m_zoomDirection;
emitChanges();
}
void JoystickWidget::paintEvent(QPaintEvent * /*event*/)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QColor primary = palette().text().color();
QColor secondary = palette().window().color();
secondary = (secondary.lightness() > 128) ? (secondary.darker(115))
: (secondary.lighter(140));
if (m_scrollWheelPos) {
painter.setPen(
QPen(m_scrollWheelPos > 0 ? Qt::green : Qt::red, 2));
painter.drawArc(m_moveSpeedSteps.first().bounds.adjusted(-2, -2,
2, 2),
90 * 16,
(m_zoomArcSpan * std::abs(m_scrollWheelPos)) /
m_zoomSpeedSteps.first().deg *
m_zoomDirection * -16);
}
painter.setPen(primary);
QLineF line;
line.setP1(centerPoint());
line.setLength(m_maxDistFromCenter + 4);
line.setAngle(90);
painter.drawLine(line);
for (int i = 0; i < m_zoomSpeedSteps.length(); i++) {
line.setAngle(90 + m_zoomSpeedSteps.at(i).dispDeg);
painter.drawLine(line);
line.setAngle(90 - m_zoomSpeedSteps.at(i).dispDeg);
painter.drawLine(line);
}
for (int i = 0; i < (m_moveSpeedSteps.length() - 1); i++) {
painter.setBrush(m_moveSpeedSteps.at(i).color);
painter.drawEllipse(m_moveSpeedSteps.at(i).bounds);
}
line.setLength(m_maxDistFromCenter);
float angles[8] = {337.5, 292.5, 247.5, 202.5,
157.5, 112.5, 67.5, 22.5};
for (int i = 0; i < 8; i++) {
line.setAngle(angles[i]);
painter.drawLine(line);
}
painter.setBrush(m_moveSpeedSteps.last().color);
painter.drawEllipse(m_moveSpeedSteps.last().bounds);
painter.setBrush(secondary);
painter.drawEllipse(centerCircleBounds());
}
void JoystickWidget::resizeEvent(QResizeEvent * /*event*/)
{
int width = size().width();
int height = size().height();
int offset = centerCircleBounds().width() + 2;
m_maxDistFromCenter = ((width > height ? height : width) - offset) / 2;
updateMoveSpeedSteps();
updateZoomSpeedSteps();
}
QSize JoystickWidget::minimumSizeHint() const
{
QSize size;
size.setWidth(m_maxDistFromCenter);
size.setHeight(m_maxDistFromCenter);
return size;
}