-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameWidget.cpp
187 lines (147 loc) · 5.57 KB
/
GameWidget.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
178
179
180
181
182
183
184
185
186
187
#include "GameWidget.h"
#include "GameMap.h"
#include "Game.h"
#include <QPainter>
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include <QMouseEvent>
GameWidget::GameWidget(QWidget *parent) : QGLWidget(parent)
{
setMouseTracking(true);
game = NULL;
map = NULL;
}
void GameWidget::appendToGame(Game* g)
{
highlightedFields.clear();
this->game = g;
this->map = game->map();
connect(game, SIGNAL(winner(Player*,QSet<QPoint>)), this, SLOT(highlightWinner(Player*,QSet<QPoint>)));
}
void GameWidget::highlightWinner(Player*, QSet<QPoint> fields)
{
highlightedFields = fields;
}
void GameWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
canvas.fill(Qt::transparent);
QPainter canvasPainter(&canvas);
auto drawFunction = [&](QPainter &target){
if(map == NULL)
return;
QPen standardPen = QPen(QBrush(QColor(81, 164, 64)), lineWidth, Qt::SolidLine, Qt::RoundCap);
QPen fadedPen = standardPen;
fadedPen.setColor(QColor(81, 164, 64, 100));
QPen highlightPen = standardPen;
highlightPen.setColor(QColor(230, 60, 60));
target.setPen(standardPen);
float linePosX = hCenter - wSquare / 2;
float linePosY = vCenter - wSquare / 2;
// Vertical Lines
target.drawLine(linePosX + wSquare * (1.0 / 3), linePosY, linePosX + wSquare * (1.0 / 3), linePosY + wSquare);
target.drawLine(linePosX + wSquare * (2.0 / 3), linePosY, linePosX + wSquare * (2.0 / 3), linePosY + wSquare);
// Horizontal Lines
target.drawLine(linePosX, linePosY + wSquare * (1.0 / 3), linePosX + wSquare, linePosY + wSquare * (1.0 / 3));
target.drawLine(linePosX, linePosY + wSquare * (2.0 / 3), linePosX + wSquare, linePosY + wSquare * (2.0 / 3));
// Player Icons
QPoint p = getCursorCell();
for(int x = 0; x < map->width(); x++) {
for(int y = 0; y < map->height(); y++) {
PlayerIcon playerOnField = map->iconAt(x, y);
PlayerIcon currentPlayerIcon = game->currentPlayerIcon();
PlayerIcon toDraw = playerOnField;
if(playerOnField == PlayerIcon::None && game->allowsInput() && p.x() == x && p.y() == y) {
toDraw = currentPlayerIcon;
target.setPen(fadedPen);
} else {
if(highlightedFields.contains(QPoint(x, y))) {
target.setPen(highlightPen);
} else {
target.setPen(standardPen);
}
}
QRect rect = cellRect(x, y, iconSize);
switch(toDraw) {
case PlayerIcon::None:
break;
case PlayerIcon::Circle:
target.drawEllipse(rect.x(), rect.y(), rect.width(), rect.height());
break;
case PlayerIcon::Cross:
target.drawLine(rect.x(), rect.y(), rect.x() + rect.width(), rect.y() + rect.height());
target.drawLine(rect.x() + rect.width(), rect.y(), rect.x(), rect.y() + rect.height());
break;
}
}
}
};
drawFunction(canvasPainter);
// draw shadow
shadow.fill(Qt::transparent);
QPainter shadowPainter(&shadow);
shadowPainter.translate(wSquare / 70, wSquare / 50);
drawFunction(shadowPainter);
// todo: Improve performance.
for(int r = 0; r < shadow.height(); r++) {
QRgb *pixel = (QRgb*)shadow.scanLine(r);
for(int c = 0; c < shadow.width(); c++) {
QRgb color = pixel[c];
pixel[c] = QColor(0, 0, 0, qAlpha(color) * 0.2).rgba();
}
}
painter.drawImage(0, 0, shadow);
painter.drawImage(0, 0, canvas);
}
void GameWidget::mouseMoveEvent(QMouseEvent* event)
{
update();
cursor = event->pos();
if(cursorOverAnyCell() && game->allowsInput()) {
setCursor(Qt::PointingHandCursor);
} else {
setCursor(Qt::ArrowCursor);
}
}
void GameWidget::resizeEvent(QResizeEvent*)
{
wSquare = (height() > width() ? width() : height()) * 0.8f;
hCenter = width() / 2;
vCenter = height() / 2;
lineWidth = wSquare / 30;
shadow = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
canvas = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
}
QPoint GameWidget::getCursorCell()
{
if(map != NULL) {
for(int x = 0; x < map->width(); x++) {
for(int y = 0; y < map->height(); y++) {
if(cellRect(x, y, qMin(iconSize + 0.1, 1.0)).contains(cursor)) {
return QPoint(x, y);
}
}
}
}
return QPoint(-1, -1);
}
bool GameWidget::cursorOverAnyCell()
{
return getCursorCell().x() != -1;
}
QRect GameWidget::cellRect(int x, int y, float sizeMod)
{
float w = wSquare / 3.0 * sizeMod;
float h = wSquare / 3.0 * sizeMod;
int posX = hCenter - wSquare / 2.0 + wSquare * ((float)x / 3) + (1.0 - sizeMod) * wSquare / 3.0 / 2.0;
int posY = vCenter - wSquare / 2.0 + wSquare * ((float)y / 3) + (1.0 - sizeMod) * wSquare / 3.0 / 2.0;
return QRect(posX, posY, w, h);
}
void GameWidget::mousePressEvent(QMouseEvent*)
{
QPoint cell = getCursorCell();
if(cell.x() != -1 && game->allowsInput()) {
game->activateField(cell.x(), cell.y());
update();
}
}