-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathblock.cpp
More file actions
395 lines (326 loc) · 12.4 KB
/
Copy pathblock.cpp
File metadata and controls
395 lines (326 loc) · 12.4 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// SPDX-FileCopyrightText: 2012-2025 Thorsten Roth
// SPDX-License-Identifier: GPL-3.0-or-later
#include "./block.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneWheelEvent>
#include <QPainter>
#include "./settings.h"
Block::Block(const quint16 nID, QPolygonF shape, const QBrush &bgcolor,
QPen border, quint16 nGrid, QList<Block *> *pListBlocks,
QPointF posTopLeft, const bool bBarrier, QObject *pParentObj)
: m_nID(nID),
m_nZBlock(1000),
m_nZBarrier(1),
m_bBarrier(bBarrier),
m_PolyShape(std::move(shape)),
m_bgBrush(bgcolor),
m_borderPen(std::move(border)),
m_nGrid(nGrid),
m_pListBlocks(pListBlocks),
m_bActive(false) {
Q_UNUSED(pParentObj)
if (!m_PolyShape.isClosed()) {
qWarning() << "Shape" << m_nID << "is not closed";
}
if (!m_bBarrier) {
// qDebug() << "Creating BLOCK" << m_nID <<
// "\tPosition:" << posTopLeft * m_nGrid;
this->setFlag(ItemIsMovable);
m_CollTexture.load(QStringLiteral(":/collision_texture.png"));
this->setZValue(m_nZBlock);
} else {
// qDebug() << "Creating BARRIER" << m_nID <<
// "\tPosition:" << posTopLeft * m_nGrid;
this->setAcceptedMouseButtons(Qt::NoButton);
this->setAcceptTouchEvents(false);
this->setEnabled(false);
this->setZValue(m_nZBarrier);
}
m_pTransform = new QTransform();
// Scale object
this->setScale(m_nGrid);
// Move to start position
this->moveBlockGrid(posTopLeft);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Block::boundingRect() const -> QRectF {
return m_PolyShape.boundingRect();
}
auto Block::shape() const -> QPainterPath {
QPainterPath path;
path.addPolygon(m_PolyShape);
return path;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) {
Q_UNUSED(option)
Q_UNUSED(widget)
static const qreal OPACITY = 0.4;
m_borderPen.setWidth(1 / m_nGrid);
if (m_bActive) { // Barriers are ignored (not enabled)
painter->setOpacity(OPACITY);
} else {
painter->setOpacity(1);
}
QPainterPath tmpPath;
tmpPath.addPolygon(m_PolyShape);
painter->fillPath(tmpPath, m_bgBrush);
painter->setPen(m_borderPen);
painter->drawPolygon(m_PolyShape);
/*
// Adding block ID for debugging
m_ItemNumberText.setFont(QFont("Arial", 1));
m_ItemNumberText.setText(QString::number(m_nID));
m_ItemNumberText.setPos(0.2, -1.1);
m_ItemNumberText.setParentItem(this);
*/
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::mousePressEvent(QGraphicsSceneMouseEvent *p_Event) {
quint32 button = p_Event->button();
this->resetBrushStyle();
if (button == Qt::LeftButton) button |= p_Event->modifiers();
int nIndex(Settings::instance()->getMouseControls().indexOf(button));
if (nIndex >= 0) {
switch (nIndex) {
case 0:
m_posMouseSelected = p_Event->pos();
m_posMouseSelected = QPointF(m_posMouseSelected.x() * m_nGrid,
m_posMouseSelected.y() * m_nGrid);
this->moveBlock();
update();
break;
case 1:
this->rotateBlock();
update();
break;
case 2:
this->flipBlock();
update();
break;
default:
qWarning() << "Unexpected mouse press control:" << nIndex;
}
}
QGraphicsItem::mousePressEvent(p_Event);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::mouseMoveEvent(QGraphicsSceneMouseEvent *p_Event) {
if (0 ==
Settings::instance()->getMouseControls().indexOf(p_Event->buttons())) {
this->setPos(p_Event->scenePos() - m_posMouseSelected);
update();
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::mouseReleaseEvent(QGraphicsSceneMouseEvent *p_Event) {
if (0 ==
Settings::instance()->getMouseControls().indexOf(p_Event->button())) {
this->moveBlock(true);
update();
}
QGraphicsItem::mouseReleaseEvent(p_Event);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::wheelEvent(QGraphicsSceneWheelEvent *p_Event) {
this->resetBrushStyle();
int nIndex(Settings::instance()->getMouseControls().indexOf(
(quint32(p_Event->orientation()) | Settings::SHIFT)));
if (nIndex >= 0) {
switch (nIndex) {
case 1:
this->rotateBlock(p_Event->delta());
update();
break;
case 2:
this->flipBlock();
update();
break;
default:
qWarning() << "Unexpected mouse wheel control:" << nIndex;
}
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::moveBlock(const bool bRelease) {
if (!bRelease) {
m_bActive = true;
this->bringToForeground();
m_posBlockSelected = this->pos(); // Save last position
m_PolyShapeSelected = m_PolyShape; // Save shape
} else {
m_bActive = false;
this->prepareGeometryChange();
this->setPos(this->snapToGrid(this->pos()));
QPainterPath thisPath = this->shape();
thisPath.translate(
QPointF(this->pos().x() / m_nGrid, this->pos().y() / m_nGrid));
emit incrementMoves();
if (this->checkCollision(thisPath)) {
m_PolyShape = m_PolyShapeSelected; // Reset shape (flip / rotation)
this->setPos(this->snapToGrid(m_posBlockSelected)); // Reset position
this->checkBlockIntersection();
} else {
// Check if puzzle is solved
emit checkPuzzleSolved();
}
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::rotateBlock(const int nDelta) {
static const quint8 RIGHTANGLE = 90;
if (m_bActive || !this->isAnyBlockActive(m_pListBlocks)) {
qint8 nAngle(0);
qreal nTranslateX(0);
qreal nTranslateY(0);
if (nDelta < 0) {
nAngle = RIGHTANGLE;
nTranslateX = this->boundingRect().height();
nTranslateY = 0;
} else {
nAngle = -RIGHTANGLE;
nTranslateX = 0;
nTranslateY = this->boundingRect().width();
}
this->prepareGeometryChange();
// qDebug() << "Before rot.:" << m_nID << nAngle << "\n" << m_PolyShape;
m_pTransform->reset();
m_pTransform->rotate(nAngle);
m_PolyShape = m_pTransform->map(m_PolyShape); // Rotate
m_PolyShape.translate(nTranslateX, nTranslateY); // Move back
// qDebug() << "After rot.:" << m_PolyShape;
}
if (!this->isAnyBlockActive(m_pListBlocks)) {
this->checkBlockIntersection();
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::flipBlock() {
if (m_bActive || !this->isAnyBlockActive(m_pListBlocks)) {
this->prepareGeometryChange();
// qDebug() << "Before flip" << m_nID << "-" << m_PolyShape;
QTransform transform = QTransform::fromScale(-1, 1);
m_PolyShape = transform.map(m_PolyShape); // Flip
m_PolyShape.translate(this->boundingRect().width(), 0); // Move back
// qDebug() << "After flip:" << m_PolyShape;
}
if (!this->isAnyBlockActive(m_pListBlocks)) {
this->checkBlockIntersection();
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::checkBlockIntersection() {
QPainterPath thisPath = this->shape();
thisPath.translate(
QPointF(this->pos().x() / m_nGrid, this->pos().y() / m_nGrid));
if (this->checkCollision(thisPath)) {
m_bgBrush.setTexture(m_CollTexture);
m_bgBrush.setStyle(Qt::TexturePattern);
this->bringToForeground();
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::resetBrushStyle() const {
for (auto &pBlock : *m_pListBlocks) {
pBlock->setBrushStyle(Qt::SolidPattern);
}
}
void Block::setBrushStyle(Qt::BrushStyle style) { m_bgBrush.setStyle(style); }
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Block::checkCollision(const QPainterPath &thisPath) -> bool {
QPainterPath collidingPath;
QPainterPath intersectedPath;
for (auto &pBlock : *m_pListBlocks) {
if (pBlock->getIndex() != m_nID && this->collidesWithItem(pBlock)) {
collidingPath = pBlock->shape();
collidingPath.translate(
QPointF(pBlock->pos().x() / m_nGrid, pBlock->pos().y() / m_nGrid));
intersectedPath = thisPath.intersected(collidingPath);
// Path has to be simplified
// Otherwise paths with area = 0 might be found
intersectedPath = intersectedPath.simplified();
if (!intersectedPath.boundingRect().size().isEmpty()) {
/*
qDebug() << "SHAPE 1:" << thisPath << "SHAPE 2:" << collidingPath;
qDebug() << "Col" << m_nID << "with" << block->getIndex()
<< "Size" << intersectedPath.boundingRect().size();
qDebug() << "Intersection:" << intersectedPath;
*/
return true;
}
}
}
return false;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Block::snapToGrid(const QPointF point) const -> QPointF {
return QPointF(qRound(point.x() / m_nGrid) * m_nGrid, // x
qRound(point.y() / m_nGrid) * m_nGrid); // y
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::moveBlockGrid(const QPointF pos) { this->setPos(pos * m_nGrid); }
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::bringToForeground() {
if (!m_bBarrier) {
for (auto &pBlock : *m_pListBlocks) {
if (pBlock->isBarrier()) {
pBlock->setZValue(m_nZBarrier);
} else {
if (pBlock->getIndex() == m_nID) {
this->setZValue(m_nZBlock + 1);
} else {
pBlock->setZValue(m_nZBlock);
}
}
}
}
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void Block::rescaleBlock(const quint16 nNewScale) {
this->prepareGeometryChange();
QPointF tmpTopLeft = this->pos() / m_nGrid * nNewScale;
this->setScale(nNewScale);
m_nGrid = nNewScale;
this->setPos(this->snapToGrid(tmpTopLeft));
// Z value ++, otherwise board might be in foreground until block is moved
this->setZValue(this->zValue() + 1);
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Block::isAnyBlockActive(const QList<Block *> *listBlocks) -> bool {
for (const auto pBlock : *listBlocks) {
if (pBlock->isActive()) {
return true;
}
}
return false;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
auto Block::type() const -> int {
// Enable the use of qgraphicsitem_cast with this item
return Type;
}
auto Block::getIndex() const -> quint16 { return m_nID; }
auto Block::isBarrier() const -> bool { return m_bBarrier; }
auto Block::isActive() const -> bool { return m_bActive; }
auto Block::getPosition() const -> QPointF { return this->pos(); }
auto Block::getPolygon() const -> QPolygonF { return this->m_PolyShape; }