-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquareitem.cpp
177 lines (157 loc) · 6.51 KB
/
squareitem.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
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
#include "squareitem.h"
#include <QApplication>
#include <QPainter>
#include <QMap>
#include <QtMath>
#include <QVector>
#include <QGraphicsSceneMouseEvent>
//#include <QDebug>
#include "darkdetect.h"
#include "fortuneavenuegraphicsscene.h"
static QMap<SquareType, QString> typeToFile = {
{Property, ":/squares/GroundProperty.png"},
{Bank, ":/squares/GroundBank.png"},
{VentureSquare, ":/squares/GroundVenture.png"},
{SuitSquareSpade, ":/squares/GroundSuit01.png"},
{SuitSquareHeart, ":/squares/GroundSuit02.png"},
{SuitSquareDiamond, ":/squares/GroundSuit03.png"},
{SuitSquareClub, ":/squares/GroundSuit04.png"},
{ChangeOfSuitSquareSpade, ":/squares/GroundCSuit01.png"},
{ChangeOfSuitSquareHeart, ":/squares/GroundCSuit02.png"},
{ChangeOfSuitSquareDiamond, ":/squares/GroundCSuit03.png"},
{ChangeOfSuitSquareClub, ":/squares/GroundCSuit04.png"},
{TakeABreakSquare, ":/squares/GroundTakeABreak.png"},
{BoonSquare, ":/squares/GroundBoon.png"},
{BoomSquare, ":/squares/GroundBoom.png"},
{StockBrokerSquare, ":/squares/GroundStockBroker.png"},
{RollOnSquare, ":/squares/GroundRollOn.png"},
{ArcadeSquare, ":/squares/GroundArcade.png"},
{SwitchSquare, ":/squares/GroundSwitch.png"},
{CannonSquare, ":/squares/GroundCannon.png"},
{BackStreetSquareA, ":/squares/GroundWarpA.png"},
{BackStreetSquareB, ":/squares/GroundWarpB.png"},
{BackStreetSquareC, ":/squares/GroundWarpC.png"},
{BackStreetSquareD, ":/squares/GroundWarpD.png"},
{BackStreetSquareE, ":/squares/GroundWarpE.png"},
{OneWayAlleyDoorA, ":/squares/GroundDoorBlue.png"},
{OneWayAlleyDoorB, ":/squares/GroundDoorRed.png"},
{OneWayAlleyDoorC, ":/squares/GroundDoorYellow.png"},
{OneWayAlleyDoorD, ":/squares/GroundDoorGreen.png"},
{LiftMagmaliceSquareStart, ":/squares/GroundLiftMagmaliceStart.png"},
{MagmaliceSquare, ":/squares/GroundMagmalice.png"},
{OneWayAlleySquare, ":/squares/GroundDoorMario.png"},
{LiftSquareEnd, ":/squares/GroundLift.png"},
{VacantPlot, ":/squares/GroundVacant.png"}
};
static QVector<QColor> districtColors = {
QColor("#FF0000"),
QColor("#00C0FF"),
QColor("#FFBB00"),
QColor("#00FF00"),
QColor("#0000FF"),
QColor("#FFA0C0"),
QColor("#8000A0"),
QColor("#FFFF60"),
QColor("#008000"),
QColor("#C080FF"),
QColor("#FF8040"),
QColor("#FF50A0")
};
static QFont valueFont("Lato");
static QFont idFont("Lato");
static QFont rentFont("Lato");
static bool staticsInitialized = false;
SquareItem::SquareItem(const SquareData &dataValue, QGraphicsItem *parent) : QGraphicsItem(parent), data(dataValue) {
if (!staticsInitialized) {
staticsInitialized = true;
valueFont.setPixelSize(18);
idFont.setPixelSize(10);
rentFont.setPixelSize(12);
}
setFlag(ItemIsMovable);
setFlag(ItemIsSelectable);
setPos(data.positionX, data.positionY);
setFlag(ItemSendsGeometryChanges);
updateZValueFromData();
}
QRectF SquareItem::boundingRect() const {
return QRectF(0, 0, 64, 64);
}
void SquareItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) {
if(data.squareType == EventSquare && 0 <= data.districtDestinationId && data.districtDestinationId <= 128) {
QString imageFile = QString(":/squares/EventSquare/eventsquare_%1.png").arg(data.districtDestinationId, 3, 10, QChar('0'));
painter->drawImage(QPoint(0, 0), QImage(imageFile));
} else {
painter->drawImage(QPoint(0, 0), QImage(typeToFile.value(data.squareType, ":/squares/GroundDefault.png")));
}
QPen pen(Qt::transparent, 2, Qt::SolidLine);
pen.setJoinStyle(Qt::MiterJoin);
if (isSelected()) {
pen.setColor(isDarkMode() ? QColor("#eeeeee") : QColor("#111111"));
painter->setPen(pen);
painter->drawRect(0 - 2/2, 0 - 2/2, 64 + 2, 64 + 2);
}
if (data.squareType == Property || data.squareType == VacantPlot) {
pen.setColor(districtColors.value(data.districtDestinationId, Qt::transparent));
}
painter->setPen(pen);
painter->drawRect(0 + 2/2, 0 + 2/2, 64 - 2, 64 - 2);
QColor backgroundPen(0,0,0,127);
if (data.squareType == Property) {
painter->setPen(Qt::white);
painter->setFont(valueFont);
drawBackgroundedTextCentered(painter, 32, 38, QString::number(data.value), backgroundPen);
painter->setFont(rentFont);
drawBackgroundedTextRightAligned(painter, 60, 60, QString::number(data.price), backgroundPen);
}
painter->setPen(Qt::white);
painter->setFont(idFont);
drawBackgroundedText(painter, 2, 12, QString::number(data.id), backgroundPen);
}
SquareData &SquareItem::getData() {
return data;
}
const SquareData &SquareItem::getData() const {
return data;
}
void SquareItem::drawBackgroundedTextCentered(QPainter *painter, int x, int y, const QString &text, const QBrush &bgBrush) {
auto boundingRect = painter->fontMetrics().boundingRect(text);
drawBackgroundedText(painter, x - boundingRect.width()/2, y, text, bgBrush);
}
void SquareItem::drawBackgroundedTextRightAligned(QPainter *painter, int x, int y, const QString &text, const QBrush &bgBrush) {
auto boundingRect = painter->fontMetrics().boundingRect(text);
drawBackgroundedText(painter, x - boundingRect.width(), y, text, bgBrush);
}
void SquareItem::drawBackgroundedText(QPainter *painter, int x, int y, const QString &text, const QBrush &bgBrush) {
int width = painter->fontMetrics().horizontalAdvance(text);
int ascent = painter->fontMetrics().ascent();
int height = painter->fontMetrics().height();
painter->fillRect(x, y - ascent, width, height, bgBrush);
painter->drawText(x, y, text);
}
QVariant SquareItem::itemChange(GraphicsItemChange change, const QVariant &value) {
if (change == ItemPositionChange) {
QPointF newPos = value.toPointF();
if (QApplication::mouseButtons() == Qt::LeftButton) {
newPos = getSnapLocation(newPos);
}
data.positionX = newPos.x();
data.positionY = newPos.y();
return newPos;
}
return QGraphicsItem::itemChange(change, value);
}
QPointF SquareItem::getSnapLocation(const QPointF &loc) {
FortuneAvenueGraphicsScene *fortuneScene = qobject_cast<FortuneAvenueGraphicsScene *>(scene());
if (fortuneScene) {
int gridSize = fortuneScene->getSnapSize();
qreal xV = qRound(loc.x()/gridSize)*gridSize;
qreal yV = qRound(loc.y()/gridSize)*gridSize;
return QPointF(xV, yV);
}
return loc;
}
void SquareItem::updateZValueFromData()
{
setZValue(data.id);
}