-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cpp
111 lines (82 loc) · 2.82 KB
/
Button.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
#include "Button.h"
#include "Game.h"
Button::Button(int index) {
_index = index;
_addingTrain = false;
_mousePos = QPoint(WINDOW_WIDTH / 2 + (floor(MAX_LINES / 2) - _index) * (-BUTTONS_SPACING - BUTTON_SIZE), MENU_HEIGHT);;
switch (_index) {
case(-1):
_type = TRAIN;
break;
default:
_type = LINE;
break;
}
_position = QPoint(WINDOW_WIDTH / 2 + (floor(MAX_LINES / 2) - _index) * (-BUTTONS_SPACING - BUTTON_SIZE), MENU_HEIGHT);
_color = setColor(_index);
setZValue(2);
}
void Button::paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
QWidget* widget){
if (_type == LINE) {
QPen pen;
pen.setWidth(10);
pen.setColor(_color);
painter->setBrush(QBrush(_color));
painter->setPen(pen);
painter->setRenderHint(QPainter::Antialiasing);
if(Game::instance()->lineExists(_index))
painter->drawEllipse(_position, BUTTON_SIZE_BIG / 2, BUTTON_SIZE_BIG / 2);
else
painter->drawEllipse(_position, BUTTON_SIZE / 2, BUTTON_SIZE / 2);
}
else {
QPen pen;
pen.setWidth(10);
pen.setColor(QColor(0, 0, 0));
painter->setBrush(QBrush(QColor(0, 0, 0)));
painter->setPen(pen);
painter->setRenderHint(QPainter::Antialiasing);
QPixmap train(":/Graphics/metroTrain.png");
train = train.scaledToHeight(60, Qt::TransformationMode::SmoothTransformation);
//painter->drawEllipse(_position, BUTTON_SIZE_BIG / 2, BUTTON_SIZE_BIG / 2);
painter->drawPixmap(QPoint(_position.x() - BUTTON_SIZE_BIG / 2, _position.y() - BUTTON_SIZE_BIG / 2), train);
if (_addingTrain) {
pen.setWidth(1);
pen.setColor(QColor(0, 0, 0));
pen.setJoinStyle(Qt::MiterJoin);
painter->setBrush(QBrush(QColor(0, 0, 0)));
painter->setPen(pen);
painter->drawRect(QRect(_mousePos.x() - TRAIN_HEIGHT/2, _mousePos.y() - TRAIN_WIDTH/2, TRAIN_HEIGHT, TRAIN_WIDTH));
}
}
}
QRectF Button::boundingRect() const{
//da cambiare
return QRectF(_position.x() - BUTTON_SIZE_BIG /2, _position.y() - BUTTON_SIZE_BIG / 2,
BUTTON_SIZE_BIG, BUTTON_SIZE_BIG);
}
void Button::mousePressEvent(QGraphicsSceneMouseEvent* e) {
if (_type == LINE) {
// printf("stai cliccando sul tasto %d\n", _index);
Game::instance()->deleteLine(_index);
QGraphicsItem::mousePressEvent(e);
}
else {
if(Game::instance()->availableTrains())
_addingTrain = true;
}
}
void Button::mouseMoveEvent(QGraphicsSceneMouseEvent* e){
if (_addingTrain) {
_mousePos = QPoint(e->pos().x(), e->pos().y());
update();
}
QGraphicsItem::mouseMoveEvent(e);
}
void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent* e){
Game::instance()->addTrain(QRect(_mousePos.x() - TRAIN_HEIGHT / 2, _mousePos.y() - TRAIN_WIDTH / 2, TRAIN_HEIGHT, TRAIN_WIDTH));
_addingTrain = false;
_mousePos = QPoint(WINDOW_WIDTH / 2 + (floor(MAX_LINES / 2) - _index) * (-BUTTONS_SPACING - BUTTON_SIZE), MENU_HEIGHT);;
}