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);
18 setCursor(Qt::CrossCursor);
19 widgetCenter = rect().center() + QPointF(0,25);
20 painterCenter = widgetCenter - QPointF(0,10);
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);
38 void VirtualJoystick::focus(
bool in) {
42 void VirtualJoystick::paintEvent(QPaintEvent* event)
45 QPainter painter(
this);
46 painter.drawRect(0, 0, 250, 250);
48 calculateCoordinates();
49 drawMouseRects(painter);
53 axis1->setText(
"Axis1: " + QString().setNum(axisValues.x()));
54 axis2->setText(
"Axis2: " + QString().setNum(axisValues.y()));
58 void VirtualJoystick::calculateCoordinates(
void) {
61 if (mousePosition.y() < painterCenter.y()-90)
63 else if (mousePosition.y() > painterCenter.y()+70)
66 maxHeight = 88 - (mousePosition.y()-painterCenter.y())/9;
68 height = distance(painterCenter, mousePosition);
69 if (height > maxHeight)
73 void VirtualJoystick::paintCross(QPainter &painter)
75 painter.drawLine(0, 125, 250, 125);
76 painter.drawLine(125, 0, 125, 250);
79 void VirtualJoystick::paintBase(QPainter& painter)
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);
92 void VirtualJoystick::paintStick(QPainter& painter)
94 painterAngle = angle(painterCenter, mousePosition);
96 if (followMouse ==
false && pressed ==
false) {
101 painter.translate(painterCenter);
102 painter.rotate(painterAngle);
104 QRect cylinder(-10, 10, 20, -height);
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);
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);
124 double VirtualJoystick::distance(QPointF A, QPointF B)
126 return sqrt(pow(A.x()-B.x(), 2) + pow(A.y()-B.y(), 2));
129 double VirtualJoystick::angle(QPointF A, QPointF B)
131 return atan2(B.x()-A.x(), A.y()-B.y()) * 180 / M_PI;
134 void VirtualJoystick::drawMouseRects(QPainter &painter)
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);
143 QPointF VirtualJoystick::calculateAxis(
bool active)
148 QPointF bc(-(mousePosition.y() - 125), mousePosition.x() - 125);
150 double dist = sqrt(pow(bc.x(),2) + pow(bc.y(), 2));
161 void VirtualJoystick::mouseMoveEvent(QMouseEvent* event)
163 mousePosition =
event->posF();
164 axisValues = calculateAxis(followMouse);
169 void VirtualJoystick::mousePressEvent(QMouseEvent *event)
171 switch (event->button()) {
172 case Qt::RightButton:
173 followMouse = !followMouse;
175 axisValues = calculateAxis(followMouse);
192 void VirtualJoystick::mouseReleaseEvent(QMouseEvent* event) {
195 if (event->button() == Qt::LeftButton) {
197 axisValues = calculateAxis(followMouse);
203 void VirtualJoystick::timerEvent(QTimerEvent *event)
205 if (followMouse ==
false)
VirtualJoystick(QWidget *parent, const std::string &name)
void axisChanged(double, double)