rock_widget_collection  0.1
VirtualJoystick.cc
Go to the documentation of this file.
1 #include "VirtualJoystick.h"
2 #include <cmath>
3 #include <iostream>
4 
5 VirtualJoystick::VirtualJoystick(QWidget* parent, const std::string& name) : QWidget(parent)
6 {
7  filled = false;
8 
9  title = new QLabel(tr(name.c_str()), this);
10  axis1 = new QLabel("Axis 1: ", this);
11  axis1->setGeometry(150, 0, axis1->width(), axis1->height());
12  axis2 = new QLabel("Axis 2: ", this);
13  axis2->setGeometry(150, 13, axis1->width(), axis1->height());
14  setFixedSize(250, 250);
15  setMouseTracking(true);
16  followMouse = false;
17  pressed = false;
18  setCursor(Qt::CrossCursor);
19  widgetCenter = rect().center() + QPointF(0,25);
20  painterCenter = widgetCenter - QPointF(0,10);
21  painterAngle = 0;
22  mousePosition = painterCenter;
23  baseGrad = QLinearGradient(widgetCenter-QPoint(38,0), widgetCenter+QPoint(38,0));
24  baseGrad.setColorAt(0, QColor(160, 160, 160));
25  baseGrad.setColorAt(1, QColor(20, 20, 20));
26  baseGrad.setSpread(QGradient::ReflectSpread);
27  baseColor = QColor(80, 80, 80);
28  update();
29  filled = true;
30  startTimer(30);
31 }
32 
34 {
35 }
36 
37 
38 void VirtualJoystick::focus(bool in) {
39  setVisible(in);
40 }
41 
42 void VirtualJoystick::paintEvent(QPaintEvent* event)
43 {
44  (void)event;
45  QPainter painter(this);
46  painter.drawRect(0, 0, 250, 250);
47 
48  calculateCoordinates();
49  drawMouseRects(painter);
50  paintBase(painter);
51  paintStick(painter);
52 
53  axis1->setText("Axis1: " + QString().setNum(axisValues.x()));
54  axis2->setText("Axis2: " + QString().setNum(axisValues.y()));
55 }
56 
57 
58 void VirtualJoystick::calculateCoordinates(void) {
59  height = 0;
60  maxHeight = 1;
61  if (mousePosition.y() < painterCenter.y()-90)
62  maxHeight = 100;
63  else if (mousePosition.y() > painterCenter.y()+70)
64  maxHeight = 80;
65  else
66  maxHeight = 88 - (mousePosition.y()-painterCenter.y())/9;
67 
68  height = distance(painterCenter, mousePosition);
69  if (height > maxHeight)
70  height = maxHeight;
71 }
72 
73 void VirtualJoystick::paintCross(QPainter &painter)
74 {
75  painter.drawLine(0, 125, 250, 125);
76  painter.drawLine(125, 0, 125, 250);
77 }
78 
79 void VirtualJoystick::paintBase(QPainter& painter)
80 {
81  painter.setPen(Qt::transparent);
82  painter.setBrush(baseGrad);
83  painter.drawEllipse(widgetCenter, 45, 45);
84  painter.setBrush(QColor(110, 110, 110));
85  painter.drawEllipse(widgetCenter-QPoint(0, 5), 45, 45);
86  painter.setBrush(baseColor);
87  painter.drawEllipse(widgetCenter-QPoint(0, 5), 42, 42);
88  painter.setBrush(QColor(50, 50, 50));
89  painter.drawEllipse(widgetCenter-QPoint(0, 10), 16, 16);
90 }
91 
92 void VirtualJoystick::paintStick(QPainter& painter)
93 {
94  painterAngle = angle(painterCenter, mousePosition);
95 
96  if (followMouse == false && pressed == false) {
97  painterAngle = 0;
98  height = 50;
99  }
100 
101  painter.translate(painterCenter);
102  painter.rotate(painterAngle);
103 
104  QRect cylinder(-10, 10, 20, -height);//cylinderHeight());
105  QLinearGradient grad(cylinder.topLeft()+QPoint(5,0), cylinder.topRight()-QPoint(5,0));
106  grad.setColorAt(0, QColor(250, 250, 250));
107  grad.setColorAt(1, QColor(80, 80, 80));
108  grad.setSpread(QGradient::ReflectSpread);
109  painter.setBrush(grad);
110  painter.drawRoundedRect(cylinder, 9, 9);
111 
112  double radius = 80-height/5;
113  QRect ball(cylinder.center()+QPoint(0, cylinder.height()/2)-QPoint(radius/2, radius/2),
114  QSize(radius, radius));
115  QRadialGradient rad(ball.center(), radius, ball.center()-QPoint(10, 10));
116  rad.setColorAt(0, QColor(255, 200, 200));
117  rad.setColorAt(1, QColor(255, 63, 63));
118  painter.setBrush(rad);
119  painter.drawEllipse(ball);
120 
121 }
122 
123 
124 double VirtualJoystick::distance(QPointF A, QPointF B)
125 {
126  return sqrt(pow(A.x()-B.x(), 2) + pow(A.y()-B.y(), 2));
127 }
128 
129 double VirtualJoystick::angle(QPointF A, QPointF B)
130 {
131  return atan2(B.x()-A.x(), A.y()-B.y()) * 180 / M_PI;
132 }
133 
134 void VirtualJoystick::drawMouseRects(QPainter &painter)
135 {
136  painter.setPen(Qt::DashLine);
137  painter.drawLine(250/3, 0, 250/3, 250);
138  painter.drawLine(500/3, 0, 500/3, 250);
139  painter.drawLine(0, 250/3, 250, 250/3);
140  painter.drawLine(0, 500/3, 250, 500/3);
141 }
142 
143 QPointF VirtualJoystick::calculateAxis(bool active)
144 {
145  if(!active)
146  return QPointF(0,0);
147 
148  QPointF bc(-(mousePosition.y() - 125), mousePosition.x() - 125);
149 
150  double dist = sqrt(pow(bc.x(),2) + pow(bc.y(), 2));
151 
152  //normalize the balldistance
153  if(dist > 125)
154  bc /= dist;
155  else
156  bc /= 125.0;
157 
158  return bc;
159 }
160 
161 void VirtualJoystick::mouseMoveEvent(QMouseEvent* event)
162 {
163  mousePosition = event->posF();
164  axisValues = calculateAxis(followMouse);
165 
166  update();
167 }
168 
169 void VirtualJoystick::mousePressEvent(QMouseEvent *event)
170 {
171  switch (event->button()) {
172  case Qt::RightButton:
173  followMouse = !followMouse;
174  pressed = !pressed;
175  axisValues = calculateAxis(followMouse);
176  update();
177  emit axisChanged(axisValues.x(), axisValues.y());
178  break;
179 
180  case Qt::LeftButton:
181  followMouse = true;
182  update();
183  break;
184 
185  default:
186  break;
187  }
188 
189 }
190 
191 
192 void VirtualJoystick::mouseReleaseEvent(QMouseEvent* event) {
193  if (pressed)
194  return;
195  if (event->button() == Qt::LeftButton) {
196  followMouse = false;
197  axisValues = calculateAxis(followMouse);
198  update();
199  emit axisChanged(axisValues.x(), axisValues.y());
200  }
201 }
202 
203 void VirtualJoystick::timerEvent(QTimerEvent *event)
204 {
205  if (followMouse == false)
206  return;
207  else {
208  emit axisChanged(axisValues.x(), axisValues.y());
209  }
210 }
211 
VirtualJoystick(QWidget *parent, const std::string &name)
void axisChanged(double, double)