forked from cyberintruder/DGIPyDrOneQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoystickrect.cpp
93 lines (74 loc) · 1.96 KB
/
joystickrect.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
#include "joystickrect.h"
joystickRect::joystickRect(remoteController *controllerJ)
{
controller = controllerJ;
this->setPos(70, 70);
controller->updatePositionJoystick(this->pos().x(), this->pos().y());
}
QRectF joystickRect::boundingRect() const
{
return QRectF(0,0,100,100);
}
void joystickRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *widget)
{
QRectF rect = boundingRect();
QPen pen(Qt::blue, 3);
painter->setPen(pen);
painter->drawRect(rect);
}
void joystickRect::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
{
event->setAccepted(true);
this->setPos(70, 70);
_location = QPointF(70, 70);
controller->updatePositionJoystick(_location.x(), _location.y());
}
void joystickRect::setPosDirect(qreal x, qreal y)
{
this->setPos(x,y);
_location = QPointF(x,y);
controller->updatePositionJoystick(_location.x(), _location.y());
}
// for supporting moving the box across the scene
void joystickRect::mousePressEvent ( QGraphicsSceneMouseEvent * event )
{
event->setAccepted(true);
_dragStart = event->pos();
}
// for supporting moving the box across the scene
void joystickRect::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
{
QPointF newPos = event->pos();
_location += (newPos - _dragStart);
if(_location.x() >= 0)
{
if(_location.x() <= 140)
{
this->setPos(_location.x(), this->pos().y());
}
else
{
_location = QPointF(140, _location.y());
}
}
else
{
_location = QPointF(0, _location.y());
}
if(_location.y() >= 0)
{
if(_location.y() <= 140)
{
this->setPos(this->pos().x(), _location.y());
}
else
{
_location = QPointF(_location.x(), 140);
}
}
else
{
_location = QPointF(_location.x(), 0);
}
controller->updatePositionJoystick(_location.x(), _location.y());
}