This repository has been archived by the owner on Mar 14, 2021. It is now read-only.
/
mass.h
189 lines (163 loc) · 5.04 KB
/
mass.h
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
/**
* Bajo la licencia GPL-3 ó superior.
*
* Copyright (C) 2008 Rafael Fernández López
*/
#ifndef MASS_H
#define MASS_H
#include "vector2d.h"
#include "surface.h"
#include <QtGui/QGraphicsItem>
#include <QtGui/QPainter>
#include <QtGui/QGraphicsSceneMouseEvent>
#include <QtGui/QRadialGradient>
#include <QtGui/QStyleOptionGraphicsItem>
class Mass;
class Arrow
: public QGraphicsItem
{
public:
Arrow(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0, Mass *source = 0, Mass *dest = 0)
: QGraphicsItem(parent, scene)
, source(source)
, dest(dest)
{
}
~Arrow()
{
}
virtual QRectF boundingRect() const;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
virtual QPainterPath shape() const
{
QPainterPath path;
path.addRect(boundingRect());
return path;
}
private:
Mass *source;
Mass *dest;
};
class Mass
: public QGraphicsItem
{
public:
Mass(QGraphicsItem *parent = 0, QGraphicsScene *scene = 0, bool isDragMass = false)
: QGraphicsItem(parent, scene)
, massRadio(5)
, surface(dynamic_cast<Surface*>(scene))
, beingDragged(false)
, isDragMass(isDragMass)
{
setFlag(QGraphicsItem::ItemIsMovable);
Q_ASSERT_X(surface, "Mass", "You need to pass a Surface as the second parameter to the "
"mass object");
pos = QPoint(random() % 400, random() % 400);
movement.x = random() % 3;
movement.y = random() % 3;
futureMovement = movement;
if (!isDragMass) {
setPos(movement.x, movement.y);
}
}
~Mass()
{
}
Vector2D movementVector() const
{
return movement;
}
// Reimplemented from QGraphicsItem
virtual void advance (int phase)
{
if (beingDragged || isDragMass) {
return;
}
if (!phase) {
futureMovement.x *= surface->friction();
futureMovement.y *= surface->friction();
pos.setX(pos.x() + futureMovement.x);
pos.setY(pos.y() + futureMovement.y);
if (pos.y() <= (massRadio * 2) || pos.y() >= (surface->height() - massRadio * 2)) {
futureMovement.y *= -1;
}
if (pos.x() <= (massRadio * 2) || pos.x() >= (surface->width() - massRadio * 2)) {
futureMovement.x *= -1;
}
} else {
movement = futureMovement;
setPos(pos.x(), pos.y());
}
}
virtual QRectF boundingRect() const
{
return QRectF(-massRadio, -massRadio, massRadio * 2, massRadio * 2);
}
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
{
painter->save();
if (isDragMass) {
painter->setOpacity(0.5);
}
QRadialGradient grad(0, 0, massRadio);
grad.setColorAt(0, Qt::transparent);
grad.setColorAt(1, Qt::yellow);
painter->setBrush(grad);
painter->drawRoundedRect(-massRadio, -massRadio, massRadio * 2, massRadio * 2, massRadio, massRadio);
painter->restore();
}
virtual QPainterPath shape() const
{
QPainterPath path;
path.addEllipse(boundingRect());
return path;
}
protected:
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
{
dragMass = new Mass(parentItem(), const_cast<Surface*>(surface), true);
dragMass->setPos(event->scenePos());
dragArrow = new Arrow(parentItem(), const_cast<Surface*>(surface), this, dragMass);
dragArrow->setPos(event->scenePos());
beingDragged = true;
dragPoint = event->scenePos();
QGraphicsItem::mousePressEvent(event);
}
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
delete dragMass;
delete dragArrow;
QGraphicsItem::mouseReleaseEvent(event);
movement.x = (event->scenePos().x() - dragPoint.x()) / 50.0;
movement.y = (event->scenePos().y() - dragPoint.y()) / 50.0;
futureMovement = movement;
pos = event->scenePos();
setPos(event->scenePos());
beingDragged = false;
}
private:
QPointF pos;
Vector2D movement;
Vector2D futureMovement;
const int massRadio;
const Surface *surface;
bool beingDragged;
QPointF dragPoint;
Mass *dragMass;
Arrow *dragArrow;
bool isDragMass;
};
QRectF Arrow::boundingRect() const
{
QPointF topLeft(qMin(source->scenePos().x(), dest->scenePos().x()),
qMin(source->scenePos().y(), dest->scenePos().y()));
QPointF bottomRight(qMax(source->scenePos().x(), dest->scenePos().x()),
qMax(source->scenePos().y(), dest->scenePos().y()));
return QRectF(topLeft, bottomRight);
}
void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setPen(Qt::red);
painter->drawLine(option->rect.topLeft(), option->rect.bottomRight());
}
#endif