-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeck.cpp
More file actions
365 lines (294 loc) · 11.5 KB
/
deck.cpp
File metadata and controls
365 lines (294 loc) · 11.5 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
#include "deck.h"
#include "define.h"
#include "resourcepaths.h"
#include <QDebug>
#include <QDir>
#include <QRectF>
#include <QFile>
#include <QPainter>
#include <QSvgRenderer>
#include <QGraphicsSvgItem>
#include <QCoreApplication>
Deck::Deck(QObject *parent) : QObject(parent)
{
}
Deck::~Deck() {
if (renderer) {
delete renderer;
renderer = nullptr;
}
}
void Deck::delete_current_deck() {
current_deck_style = UNSET_DECK;
// Safe cleanup: qDeleteAll deletes each QGraphicsItem
// QGraphicsItem destructor automatically calls scene()->removeItem(this)
// → No dangling pointers, no manual removeItem needed
// → Scene count drops correctly, permanent items stay
qDeleteAll(deck);
deck.clear();
pixImages.clear();
}
void Deck::createPixmap(int cardId, bool suitFirst, int format)
{
if (!renderer || !renderer->isValid()) {
return;
}
QSize targetSize = QSize(90, 130);
QPixmap pixmap(targetSize);
pixmap.fill(Qt::transparent); // important for transparency in cards
QPainter painter(&pixmap);
// Enable high-quality rendering
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
// Optional but often nice too:
painter.setRenderHint(QPainter::TextAntialiasing, true);
// Render the whole document (if no specific element)
if (format == SVG_1_FILE_FORMAT) {
QString elementId = getElementIdForCard(cardId, suitFirst);
renderer->render(&painter, elementId, QRectF(0, 0, targetSize.width(), targetSize.height()));
} else {
renderer->render(&painter, QRectF(0, 0, targetSize.width(), targetSize.height()));
}
pixImages.append(pixmap);
}
QString Deck::getElementIdForCard(int cardId, bool suitFirst) const {
if (cardId < 0 || cardId >= DECK_SIZE) return QString();
static const QStringList textSuit = QStringList{"club", "spade", "diamond", "heart"};
static const QStringList textRank = QStringList{"2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "1"};
int suit = cardId / 13;
int rank = cardId % 13;
if (suitFirst) {
return textSuit[suit] + "_" + textRank[rank];
} else {
return textRank[rank] + "_" + textSuit[suit];
}
}
QGraphicsSvgItem *Deck::createSvgCardElement(int cardId, bool suitFirst)
{
QString elementId;
if (cardId == BACK_CARD) {
elementId = "back";
} else {
elementId = getElementIdForCard(cardId, suitFirst);
}
if (!renderer->elementExists(elementId)) {
qWarning() << "Element ID not found:" << elementId;
return nullptr;
}
QGraphicsSvgItem *item = new QGraphicsSvgItem();
item->setSharedRenderer(renderer);
item->setElementId(elementId);
QRectF native = item->boundingRect();
if (native.width() > 0) {
qreal scale = TARGET_CARD_WIDTH / native.width();
item->setScale(scale);
}
item->setTransformOriginPoint(QPointF(native.width() / 2, 0));
return item;
}
QGraphicsSvgItem* Deck::loadGraphicsItem(const QString &filePath)
{
QGraphicsSvgItem *item = nullptr;
// const qreal TARGET_CARD_WIDTH = 140.0; // Logical width you want for all cards
if (filePath.endsWith(".svg", Qt::CaseInsensitive)) {
// SVG: Native vector – no rasterization needed
QGraphicsSvgItem *svgItem = new QGraphicsSvgItem(filePath);
// Auto-scale to target width (preserves aspect ratio)
QRectF native = svgItem->boundingRect();
if (native.width() > 0) {
qreal scale = TARGET_CARD_WIDTH / native.width();
svgItem->setScale(scale);
}
// Align by top-center instead of full center
svgItem->setTransformOriginPoint(QPointF(native.width() / 2, 0)); // Top-center anchor
// svgItem->setTransformOriginPoint(svgItem->boundingRect().center());
item = svgItem;
} else {
// I dropped PNG support. All cards images are now QGraphicsSvgItems, switching back to
// QGraphicsItems would break the animation: highlightAIPassedCards() code, because it is using QPropertyAnimation(cardItem, "pos");
/*
// PNG etc.: Load at a reasonable size or full res
QPixmap pixmap(filePath);
if (pixmap.isNull()) return nullptr;
// Optional: pre-scale large PNGs if needed
// pixmap = pixmap.scaled(400, 400, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QGraphicsPixmapItem *pixItem = new QGraphicsPixmapItem(pixmap);
// Auto-scale PNG to same logical width
if (pixmap.width() > 0) {
qreal scale = TARGET_CARD_WIDTH / pixmap.width();
pixItem->setScale(scale);
}
// Align by top-center instead of full center
QRectF native = pixItem->boundingRect();
pixItem->setTransformOriginPoint(QPointF(native.width() / 2, 0)); // Top-center anchor
// pixItem->setTransformOriginPoint(pixmap.rect().center());
pixItem->setTransformationMode(Qt::SmoothTransformation);
item = pixItem;
*/
}
return item;
}
QGraphicsSvgItem *Deck::get_card_item(int cardId, bool revealed) {
int size = deck.size() / 2;
if (cardId < 0 || cardId > size) {
qCritical() << "FATAL: Invalid card ID requested:" << cardId
<< "(deck size:" << deck.size() << ")";
qCritical() << "This indicates a serious bug in game logic.";
// Force quit — better than continuing with invalid state
QCoreApplication::quit();
return nullptr; // Never reached, but keeps compiler happy
}
if (revealed) {
return deck.at(cardId);
}
else {
return deck.at(cardId + 52);
}
}
const QPixmap &Deck::get_card_pixmap(int cardId) const {
static QPixmap empty;
if ((cardId < 0) || (cardId >= pixImages.size())) {
return empty;
}
return pixImages.at(cardId);
}
void Deck::apply_settings(QGraphicsItem *item, int cardId) {
item->setData(0, cardId); // save the cardId at Data(0)
item->setData(1, false); // the card is not selected
item->setFlag(QGraphicsItem::ItemIsFocusable, false); // prevent glitch green trace during animation
item->setFlag(QGraphicsItem::ItemIsSelectable, true); // Optional highlight
item->setFlag(QGraphicsItem::ItemIsMovable, false); // Prevent drag
item->setAcceptedMouseButtons(Qt::LeftButton);
item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false); // ← important
// Désactive complètement l'effet de sélection visuel
item->setSelected(false);
item->setFlag(QGraphicsItem::ItemHasNoContents, false);
item->hide();
}
void Deck::reset_selections()
{
for (QGraphicsItem *item : deck)
item->setData(1, false);
}
bool Deck::set_deck(int style) {
int format;
bool suitFirst = true;
bool built_in = false;
QString path, location, fullpath;
const char cards_name[13][10] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "1"};
const char image_format[3][5] = {".PNG", ".SVG", ".SVG"};
const char card_suit_name [4][10] = {"club_", "spade_", "diamond_", "heart_"};
if (style == current_deck_style)
return true;
if (current_deck_style != UNSET_DECK) {
delete_current_deck();
}
current_deck_style = style;
QGraphicsSvgItem *item;
path = QString("SVG-cards/");
switch (style) {
case KAISER_JUBILAUM: format = SVG_1_FILE_FORMAT;
fullpath = path + QString("kaiser-jubilaum/kaiser-jubilaum.svg");
suitFirst = true;
break;
case MITTELALTER_DECK: format = SVG_53_FILES_FORMAT;
path += "mittelalter/";
break;
case ENGLISH_DECK: format = SVG_1_FILE_FORMAT;
fullpath = path + QString("English/English.svg");
suitFirst = true;
break;
case RUSSIAN_DECK: format = SVG_1_FILE_FORMAT;
fullpath = path + QString("Russian/Russian.svg");
suitFirst = true;
break;
case NICU_WHITE_DECK: format = SVG_53_FILES_FORMAT;
path += "white/";
break;
case NEOCLASSICAL_DECK: format = SVG_53_FILES_FORMAT;
path += "neoclassical/";
break;
case TIGULLIO_INTERNATIONAL_DECK: format = SVG_53_FILES_FORMAT;
path += "tigullio-international/";
// format = SVG_1_FILE_FORMAT;
break;
case TIGULLIO_MODERN_DECK: return false; // unsupported
default:
case STANDARD_DECK: format = SVG_1_FILE_FORMAT;
location = QString(":/SVG-cards/Default/Default.svg");
suitFirst = true;
built_in = true;
break;
}
if (renderer) {
delete renderer;
}
renderer = new QSvgRenderer(this);
if (format == SVG_1_FILE_FORMAT) {
if (!built_in) {
location = getResourceFile(fullpath);
if (location.isEmpty()) {
delete_current_deck();
return false;
}
}
renderer->load(location);
if (!renderer->isValid()) {
delete_current_deck();
qCritical() << "Failed to load deck SVG! [" << location << "]";
return false;
} else {
qDebug() << "SVG_1_FILE_FORMAT loaded successfully : " << location;
}
}
int cpt = 0, suit = 0;
for (int i = 0; i < DECK_SIZE; i++) {
switch (format) {
case PNG_FILE_FORMAT:
case SVG_53_FILES_FORMAT: fullpath = path + QString(card_suit_name[suit]) + QString(cards_name[cpt]) + QString(image_format[format]).toLower();
location = getResourceFile(fullpath);
if (location.isEmpty()) {
delete_current_deck();
return false;
}
item = loadGraphicsItem(location);
renderer->load(location); // This will be needed to create the pixmap
break;
case SVG_1_FILE_FORMAT: item = createSvgCardElement(i, suitFirst);
break;
}
if (!item) {
delete_current_deck(); // delete incomplete deck (safe if empty)
return false;
}
apply_settings(item, i);
deck.append(item);
createPixmap(i, suitFirst, format);
if (++cpt == 13) {
suit++;
cpt = 0;
}
}
// clone 52 back cards
for (int clone = 0; clone < DECK_SIZE; clone++) {
if (format == SVG_1_FILE_FORMAT) {
item = createSvgCardElement(BACK_CARD, suitFirst);
} else {
fullpath = path + QString("back") + QString(image_format[format]).toLower();
location = getResourceFile(fullpath);
if (location.isEmpty()) {
delete_current_deck();
return false;
}
item = loadGraphicsItem(location);
}
if (!item) {
delete_current_deck(); // delete incomplete deck (safe if empty)
return false;
}
apply_settings(item, BACK_CARD);
deck.append(item);
}
emit deckChange(current_deck_style);
return true;
}