Cockatrice 2026-10-19-Development-3.1.2-beta.2
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
10#include "../animated_item.h"
11#include "../card_dimensions.h"
12#include "select_zone.h"
13
14#include <QElapsedTimer>
15#include <QMap>
16#include <QPixmap>
17#include <QPoint>
18#include <QPointF>
19#include <QtGlobal>
20#include <qtmetamacros.h>
21
22class CardDragItem;
23class CardItem;
24class CardZoneLogic;
25class QColor;
26class QGraphicsItem;
27class QPainter;
28class TableZoneLogic;
29template <typename T> class QList;
30
37class TableZone : public SelectZone, public IAnimatedItem
38{
39 Q_OBJECT
40
41signals:
43
44private:
45 static const int TABLEROWS = 3;
46
47 /*
48 Margins between table edges and cards, paddings between cards
49 */
50 static const int MARGIN_LEFT = 20;
51 static const int MARGIN_RIGHT = 15;
52 static const int MARGIN_TOP = 10;
53 static const int MARGIN_BOTTOM = 30;
54 static const int PADDING_X = 35;
55 static const int PADDING_Y = 30;
56
57 /*
58 Minimum width of the table zone including margins.
59 */
61
62 /*
63 Offset sizes when cards are stacked on each other in the grid
64 */
66 static const int STACKED_CARD_OFFSET_Y = PADDING_Y / 3;
67
68 /*
69 Width of the box line drawn in the margin around the active player's area
70 */
71 static const int BOX_LINE_WIDTH = 10;
72
73 /*
74 Default inactive mask and border gradient
75 */
76 static const QColor BACKGROUND_COLOR;
77 static const QColor FADE_MASK;
78 static const QColor GRADIENT_COLOR;
79 static const QColor GRADIENT_COLORLESS;
80
81 /*
82 Size and shape variables
83 */
84 int width;
85 int height;
87
88 /*
89 Internal cache for widths of stacks of cards by row and column.
90 */
92
93 /*
94 Holds any custom background image for the TableZone
95 */
97
98 /*
99 If this TableZone is currently active
100 */
101 bool active = false;
102 bool mirrored = false;
103 bool playmatActive = false;
104
105 [[nodiscard]] bool isInverted() const;
106
107private slots:
111 void updateBg();
112
113public slots:
114 void onPlaymatChanged(bool active);
115
116public slots:
120 void reorganizeCards() override;
121 void setMirrored(bool isMirrored);
122
123public:
130 explicit TableZone(TableZoneLogic *_logic, bool mirrored, QGraphicsItem *parent = nullptr);
131
135 [[nodiscard]] QRectF boundingRect() const override;
136
143 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
144
151
153 bool animationEvent() override;
154
158 void toggleTapped();
159
163 void
164 handleDropEvent(const QList<CardDragItem *> &dragItems, CardZoneLogic *startZone, const QPoint &dropPoint) override;
165
169 void
170 handleDropEventByGrid(const QList<CardDragItem *> &dragItems, CardZoneLogic *startZone, const QPoint &gridPoint);
171
175 [[nodiscard]] CardItem *getCardFromGrid(const QPoint &gridPoint) const;
176
180 [[nodiscard]] CardItem *getCardFromCoords(const QPointF &point) const;
181
182 QPointF closestGridPoint(const QPointF &point) override;
183
184 static int clampValidTableRow(const int row);
185
191 static int tableRowToGridY(int tableRow);
192
197 void resizeToContents();
198
199 [[nodiscard]] int getMinimumWidth() const
200 {
201 return currentMinimumWidth;
202 }
203 void setWidth(qreal _width)
204 {
205 // The width is stored as an int; truncate to match the previous implicit conversion.
206 const int newWidth = static_cast<int>(_width);
207 if (width == newWidth) {
208 return;
209 }
210 prepareGeometryChange();
211 width = newWidth;
212 // The parent player item's boundingRect (which clips the playmat painting) is
213 // derived from this zone's size. Without this signal the playmat is cut off at
214 // the stale boundingRect edge whenever the scene is resized wider.
215 emit sizeChanged();
216 }
217 [[nodiscard]] qreal getWidth() const
218 {
219 return width;
220 }
221 void setActive(bool _active)
222 {
223 active = _active;
224 update();
225 }
226
227private:
228 static constexpr qreal shimmerDurationMs = 450.0;
229
230 QElapsedTimer shimmerClock;
232
233 void paintZoneOutline(QPainter *painter);
234 void paintLandDivider(QPainter *painter);
235
236 /*
237 Calculates card stack widths so mapping functions work properly
238 */
240
241 /*
242 Mapping functions for points to/from gridpoints.
243 */
244 [[nodiscard]] QPointF mapFromGrid(QPoint gridPoint) const;
245 [[nodiscard]] QPoint mapToGrid(const QPointF &mapPoint) const;
246
247 /*
248 Helper function to create a single key from a card stack location.
249 */
250 [[nodiscard]] int getCardStackMapKey(int x, int y) const
251 {
252 return x + (y * 1000);
253 }
254};
255
256#endif
Interface for scene items driven by GameScene's shared animation timer.
Canonical card dimension constants for layout and Z-value calculations.
Definition card_drag_item.h:19
Definition card_item.h:33
Definition card_zone_logic.h:27
Definition animated_item.h:15
Definition remote_connection_controller.h:20
Definition game.h:18
SelectZone(CardZoneLogic *logic, QGraphicsItem *parent=nullptr)
Definition select_zone.cpp:156
Definition table_zone_logic.h:18
CardItem * getCardFromGrid(const QPoint &gridPoint) const
Definition table_zone.cpp:336
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition table_zone.cpp:120
void reorganizeCards() override
Definition table_zone.cpp:236
CardItem * getCardFromCoords(const QPointF &point) const
Definition table_zone.cpp:346
int getCardStackMapKey(int x, int y) const
Definition table_zone.h:250
void setActive(bool _active)
Definition table_zone.h:221
static const int MARGIN_BOTTOM
Definition table_zone.h:53
static int clampValidTableRow(const int row)
Definition table_zone.cpp:475
int width
Definition table_zone.h:84
static const int STACKED_CARD_OFFSET_Y
Definition table_zone.h:66
static constexpr qreal shimmerDurationMs
Definition table_zone.h:228
void handleDropEventByGrid(const QList< CardDragItem * > &dragItems, CardZoneLogic *startZone, const QPoint &gridPoint)
Definition table_zone.cpp:207
void triggerDamageShimmer()
Definition table_zone.cpp:78
QMap< int, int > cardStackWidth
Definition table_zone.h:91
static const int PADDING_Y
Definition table_zone.h:55
void updateBg()
Definition table_zone.cpp:73
static const QColor FADE_MASK
Definition table_zone.h:77
static const int PADDING_X
Definition table_zone.h:54
void setMirrored(bool isMirrored)
Definition table_zone.cpp:108
void paintZoneOutline(QPainter *painter)
Definition table_zone.cpp:164
static int tableRowToGridY(int tableRow)
Definition table_zone.cpp:486
static const int TABLEROWS
Definition table_zone.h:45
bool mirrored
Definition table_zone.h:102
static const QColor GRADIENT_COLOR
Definition table_zone.h:78
static const int STACKED_CARD_OFFSET_X
Definition table_zone.h:65
int height
Definition table_zone.h:85
void resizeToContents()
Definition table_zone.cpp:310
static const int MARGIN_LEFT
Definition table_zone.h:50
void computeCardStackWidths()
Definition table_zone.cpp:352
qreal getWidth() const
Definition table_zone.h:217
QRectF boundingRect() const override
Definition table_zone.cpp:103
QPixmap backgroundPixelMap
Definition table_zone.h:96
bool animationEvent() override
Decays the damage shimmer by one timer tick.
Definition table_zone.cpp:92
QElapsedTimer shimmerClock
Definition table_zone.h:230
QPointF closestGridPoint(const QPointF &point) override
Definition table_zone.cpp:462
TableZone(TableZoneLogic *_logic, bool mirrored, QGraphicsItem *parent=nullptr)
Definition table_zone.cpp:54
int getMinimumWidth() const
Definition table_zone.h:199
void handleDropEvent(const QList< CardDragItem * > &dragItems, CardZoneLogic *startZone, const QPoint &dropPoint) override
Definition table_zone.cpp:200
void paintLandDivider(QPainter *painter)
Definition table_zone.cpp:188
static const int MIN_WIDTH
Definition table_zone.h:60
bool active
Definition table_zone.h:101
static const int BOX_LINE_WIDTH
Definition table_zone.h:71
void toggleTapped()
Definition table_zone.cpp:277
QPointF mapFromGrid(QPoint gridPoint) const
Definition table_zone.cpp:386
static const int MARGIN_RIGHT
Definition table_zone.h:51
static const QColor GRADIENT_COLORLESS
Definition table_zone.h:79
qreal damageShimmerAlpha
Definition table_zone.h:231
static const QColor BACKGROUND_COLOR
Definition table_zone.h:76
int currentMinimumWidth
Definition table_zone.h:86
bool playmatActive
Definition table_zone.h:103
void onPlaymatChanged(bool active)
Definition table_zone.cpp:148
void setWidth(qreal _width)
Definition table_zone.h:203
QPoint mapToGrid(const QPointF &mapPoint) const
Definition table_zone.cpp:414
bool isInverted() const
Definition table_zone.cpp:114
static const int MARGIN_TOP
Definition table_zone.h:52
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.