Cockatrice 2026-09-01-Development-3.1.0-beta.10
A virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
table_zone.h
Go to the documentation of this file.
1
6
7#ifndef TABLEZONE_H
8#define TABLEZONE_H
9
11#include "../animated_item.h"
13#include "select_zone.h"
14
15#include <QElapsedTimer>
16
23class TableZone : public SelectZone, public IAnimatedItem
24{
25 Q_OBJECT
26
27signals:
29
30private:
31 static const int TABLEROWS = 3;
32
33 /*
34 Margins between table edges and cards, paddings between cards
35 */
36 static const int MARGIN_LEFT = 20;
37 static const int MARGIN_RIGHT = 15;
38 static const int MARGIN_TOP = 10;
39 static const int MARGIN_BOTTOM = 30;
40 static const int PADDING_X = 35;
41 static const int PADDING_Y = 30;
42
43 /*
44 Minimum width of the table zone including margins.
45 */
47
48 /*
49 Offset sizes when cards are stacked on each other in the grid
50 */
52 static const int STACKED_CARD_OFFSET_Y = PADDING_Y / 3;
53
54 /*
55 Width of the box line drawn in the margin around the active player's area
56 */
57 static const int BOX_LINE_WIDTH = 10;
58
59 /*
60 Default inactive mask and border gradient
61 */
62 static const QColor BACKGROUND_COLOR;
63 static const QColor FADE_MASK;
64 static const QColor GRADIENT_COLOR;
65 static const QColor GRADIENT_COLORLESS;
66
67 /*
68 Size and shape variables
69 */
70 int width;
71 int height;
73
74 /*
75 Internal cache for widths of stacks of cards by row and column.
76 */
77 QMap<int, int> cardStackWidth;
78
79 /*
80 Holds any custom background image for the TableZone
81 */
83
84 /*
85 If this TableZone is currently active
86 */
87 bool active = false;
88 bool mirrored = false;
89 bool playmatActive = false;
90
91 [[nodiscard]] bool isInverted() const;
92
93private slots:
97 void updateBg();
98
99public slots:
100 void onPlaymatChanged(bool active);
101
102public slots:
106 void reorganizeCards() override;
107 void setMirrored(bool isMirrored);
108
109public:
116 explicit TableZone(TableZoneLogic *_logic, bool mirrored, QGraphicsItem *parent = nullptr);
117
121 [[nodiscard]] QRectF boundingRect() const override;
122
129 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
130
137
139 bool animationEvent() override;
140
144 void toggleTapped();
145
149 void
150 handleDropEvent(const QList<CardDragItem *> &dragItems, CardZoneLogic *startZone, const QPoint &dropPoint) override;
151
155 void
156 handleDropEventByGrid(const QList<CardDragItem *> &dragItems, CardZoneLogic *startZone, const QPoint &gridPoint);
157
161 [[nodiscard]] CardItem *getCardFromGrid(const QPoint &gridPoint) const;
162
166 [[nodiscard]] CardItem *getCardFromCoords(const QPointF &point) const;
167
168 QPointF closestGridPoint(const QPointF &point) override;
169
170 static int clampValidTableRow(const int row);
171
177 static int tableRowToGridY(int tableRow);
178
183 void resizeToContents();
184
185 [[nodiscard]] int getMinimumWidth() const
186 {
187 return currentMinimumWidth;
188 }
189 void setWidth(qreal _width)
190 {
191 // The width is stored as an int; truncate to match the previous implicit conversion.
192 const int newWidth = static_cast<int>(_width);
193 if (width == newWidth) {
194 return;
195 }
196 prepareGeometryChange();
197 width = newWidth;
198 // The parent player item's boundingRect (which clips the playmat painting) is
199 // derived from this zone's size. Without this signal the playmat is cut off at
200 // the stale boundingRect edge whenever the scene is resized wider.
201 emit sizeChanged();
202 }
203 [[nodiscard]] qreal getWidth() const
204 {
205 return width;
206 }
207 void setActive(bool _active)
208 {
209 active = _active;
210 update();
211 }
212
213private:
214 static constexpr qreal shimmerDurationMs = 450.0;
215
216 QElapsedTimer shimmerClock;
218
219 void paintZoneOutline(QPainter *painter);
220 void paintLandDivider(QPainter *painter);
221
222 /*
223 Calculates card stack widths so mapping functions work properly
224 */
226
227 /*
228 Mapping functions for points to/from gridpoints.
229 */
230 [[nodiscard]] QPointF mapFromGrid(QPoint gridPoint) const;
231 [[nodiscard]] QPoint mapToGrid(const QPointF &mapPoint) const;
232
233 /*
234 Helper function to create a single key from a card stack location.
235 */
236 [[nodiscard]] int getCardStackMapKey(int x, int y) const
237 {
238 return x + (y * 1000);
239 }
240};
241
242#endif
Base class for graphical card items, providing shared rendering, identity, and interaction logic.
Interface for scene items driven by GameScene's shared animation timer.
Definition card_item.h:27
Definition card_zone_logic.h:26
Definition animated_item.h:15
SelectZone(CardZoneLogic *logic, QGraphicsItem *parent=nullptr)
Definition select_zone.cpp:144
Definition table_zone_logic.h:12
CardItem * getCardFromGrid(const QPoint &gridPoint) const
Definition table_zone.cpp:309
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition table_zone.cpp:93
void reorganizeCards() override
Definition table_zone.cpp:209
CardItem * getCardFromCoords(const QPointF &point) const
Definition table_zone.cpp:319
int getCardStackMapKey(int x, int y) const
Definition table_zone.h:236
void setActive(bool _active)
Definition table_zone.h:207
static const int MARGIN_BOTTOM
Definition table_zone.h:39
static int clampValidTableRow(const int row)
Definition table_zone.cpp:448
int width
Definition table_zone.h:70
static const int STACKED_CARD_OFFSET_Y
Definition table_zone.h:52
static constexpr qreal shimmerDurationMs
Definition table_zone.h:214
void handleDropEventByGrid(const QList< CardDragItem * > &dragItems, CardZoneLogic *startZone, const QPoint &gridPoint)
Definition table_zone.cpp:180
void triggerDamageShimmer()
Definition table_zone.cpp:51
QMap< int, int > cardStackWidth
Definition table_zone.h:77
static const int PADDING_Y
Definition table_zone.h:41
void updateBg()
Definition table_zone.cpp:46
static const QColor FADE_MASK
Definition table_zone.h:63
static const int PADDING_X
Definition table_zone.h:40
void setMirrored(bool isMirrored)
Definition table_zone.cpp:81
void paintZoneOutline(QPainter *painter)
Definition table_zone.cpp:137
static int tableRowToGridY(int tableRow)
Definition table_zone.cpp:459
static const int TABLEROWS
Definition table_zone.h:31
bool mirrored
Definition table_zone.h:88
static const QColor GRADIENT_COLOR
Definition table_zone.h:64
static const int STACKED_CARD_OFFSET_X
Definition table_zone.h:51
int height
Definition table_zone.h:71
void resizeToContents()
Definition table_zone.cpp:283
static const int MARGIN_LEFT
Definition table_zone.h:36
void computeCardStackWidths()
Definition table_zone.cpp:325
qreal getWidth() const
Definition table_zone.h:203
QRectF boundingRect() const override
Definition table_zone.cpp:76
QPixmap backgroundPixelMap
Definition table_zone.h:82
bool animationEvent() override
Decays the damage shimmer by one timer tick.
Definition table_zone.cpp:65
QElapsedTimer shimmerClock
Definition table_zone.h:216
QPointF closestGridPoint(const QPointF &point) override
Definition table_zone.cpp:435
TableZone(TableZoneLogic *_logic, bool mirrored, QGraphicsItem *parent=nullptr)
Definition table_zone.cpp:27
int getMinimumWidth() const
Definition table_zone.h:185
void handleDropEvent(const QList< CardDragItem * > &dragItems, CardZoneLogic *startZone, const QPoint &dropPoint) override
Definition table_zone.cpp:173
void paintLandDivider(QPainter *painter)
Definition table_zone.cpp:161
static const int MIN_WIDTH
Definition table_zone.h:46
bool active
Definition table_zone.h:87
static const int BOX_LINE_WIDTH
Definition table_zone.h:57
void toggleTapped()
Definition table_zone.cpp:250
QPointF mapFromGrid(QPoint gridPoint) const
Definition table_zone.cpp:359
static const int MARGIN_RIGHT
Definition table_zone.h:37
static const QColor GRADIENT_COLORLESS
Definition table_zone.h:65
qreal damageShimmerAlpha
Definition table_zone.h:217
static const QColor BACKGROUND_COLOR
Definition table_zone.h:62
int currentMinimumWidth
Definition table_zone.h:72
bool playmatActive
Definition table_zone.h:89
void onPlaymatChanged(bool active)
Definition table_zone.cpp:121
void setWidth(qreal _width)
Definition table_zone.h:189
QPoint mapToGrid(const QPointF &mapPoint) const
Definition table_zone.cpp:387
bool isInverted() const
Definition table_zone.cpp:87
static const int MARGIN_TOP
Definition table_zone.h:38
void sizeChanged()
constexpr int WIDTH
Card width in pixels.
Definition card_dimensions.h:16
Base class for zones where cards are laid out and individually interactable.