Cockatrice 2026-04-21-Development-2.11.0-beta.61
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
12#include "select_zone.h"
13
14/*
15 * TableZone is the grid based rect where CardItems may be placed.
16 * It is the main play zone and can be customized with background images.
17 *
18 * TODO: Refactor methods to make more readable, extract some logic to
19 * private methods (Im looking at you TableZone::reorganizeCards())
20 */
21class TableZone : public SelectZone
22{
23 Q_OBJECT
24
25signals:
27
28private:
29 static const int TABLEROWS = 3;
30
31 /*
32 Margins between table edges and cards, paddings between cards
33 */
34 static const int MARGIN_LEFT = 20;
35 static const int MARGIN_RIGHT = 15;
36 static const int MARGIN_TOP = 10;
37 static const int MARGIN_BOTTOM = 30;
38 static const int PADDING_X = 35;
39 static const int PADDING_Y = 30;
40
41 /*
42 Minimum width of the table zone including margins.
43 */
45
46 /*
47 Offset sizes when cards are stacked on each other in the grid
48 */
50 static const int STACKED_CARD_OFFSET_Y = PADDING_Y / 3;
51
52 /*
53 Width of the box line drawn in the margin around the active player's area
54 */
55 static const int BOX_LINE_WIDTH = 10;
56
57 /*
58 Default inactive mask and border gradient
59 */
60 static const QColor BACKGROUND_COLOR;
61 static const QColor FADE_MASK;
62 static const QColor GRADIENT_COLOR;
63 static const QColor GRADIENT_COLORLESS;
64
65 /*
66 Size and shape variables
67 */
68 int width;
69 int height;
71
72 /*
73 Internal cache for widths of stacks of cards by row and column.
74 */
75 QMap<int, int> cardStackWidth;
76
77 /*
78 Holds any custom background image for the TableZone
79 */
81
82 /*
83 If this TableZone is currently active
84 */
85 bool active = false;
86
87 [[nodiscard]] bool isInverted() const;
88
89private slots:
93 void updateBg();
94
95public slots:
99 void reorganizeCards() override;
100
101public:
108 explicit TableZone(TableZoneLogic *_logic, QGraphicsItem *parent = nullptr);
109
113 [[nodiscard]] QRectF boundingRect() const override;
114
121 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
122
126 void toggleTapped();
127
131 void
132 handleDropEvent(const QList<CardDragItem *> &dragItems, CardZoneLogic *startZone, const QPoint &dropPoint) override;
133
137 void
138 handleDropEventByGrid(const QList<CardDragItem *> &dragItems, CardZoneLogic *startZone, const QPoint &gridPoint);
139
143 [[nodiscard]] CardItem *getCardFromGrid(const QPoint &gridPoint) const;
144
148 [[nodiscard]] CardItem *getCardFromCoords(const QPointF &point) const;
149
150 QPointF closestGridPoint(const QPointF &point) override;
151
152 static int clampValidTableRow(const int row);
153
159 static int tableRowToGridY(int tableRow);
160
165 void resizeToContents();
166
167 [[nodiscard]] int getMinimumWidth() const
168 {
169 return currentMinimumWidth;
170 }
171 void setWidth(qreal _width)
172 {
173 prepareGeometryChange();
174 width = _width;
175 }
176 [[nodiscard]] qreal getWidth() const
177 {
178 return width;
179 }
180 void setActive(bool _active)
181 {
182 active = _active;
183 update();
184 }
185
186private:
187 void paintZoneOutline(QPainter *painter);
188 void paintLandDivider(QPainter *painter);
189
190 /*
191 Calculates card stack widths so mapping functions work properly
192 */
194
195 /*
196 Mapping functions for points to/from gridpoints.
197 */
198 [[nodiscard]] QPointF mapFromGrid(QPoint gridPoint) const;
199 [[nodiscard]] QPoint mapToGrid(const QPointF &mapPoint) const;
200
201 /*
202 Helper function to create a single key from a card stack location.
203 */
204 [[nodiscard]] int getCardStackMapKey(int x, int y) const
205 {
206 return x + (y * 1000);
207 }
208};
209
210#endif
TODO: Document this.
Definition card_item.h:27
Definition card_zone_logic.h:26
SelectZone(CardZoneLogic *logic, QGraphicsItem *parent=nullptr)
Definition select_zone.cpp:35
Definition table_zone_logic.h:12
CardItem * getCardFromGrid(const QPoint &gridPoint) const
Definition table_zone.cpp:248
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition table_zone.cpp:61
void reorganizeCards() override
Definition table_zone.cpp:153
CardItem * getCardFromCoords(const QPointF &point) const
Definition table_zone.cpp:256
int getCardStackMapKey(int x, int y) const
Definition table_zone.h:204
void setActive(bool _active)
Definition table_zone.h:180
static const int MARGIN_BOTTOM
Definition table_zone.h:37
static int clampValidTableRow(const int row)
Definition table_zone.cpp:377
int width
Definition table_zone.h:68
static const int STACKED_CARD_OFFSET_Y
Definition table_zone.h:50
void handleDropEventByGrid(const QList< CardDragItem * > &dragItems, CardZoneLogic *startZone, const QPoint &gridPoint)
Definition table_zone.cpp:124
QMap< int, int > cardStackWidth
Definition table_zone.h:75
static const int PADDING_Y
Definition table_zone.h:39
void updateBg()
Definition table_zone.cpp:43
static const QColor FADE_MASK
Definition table_zone.h:61
static const int PADDING_X
Definition table_zone.h:38
void paintZoneOutline(QPainter *painter)
Definition table_zone.cpp:82
static int tableRowToGridY(int tableRow)
Definition table_zone.cpp:386
static const int TABLEROWS
Definition table_zone.h:29
TableZone(TableZoneLogic *_logic, QGraphicsItem *parent=nullptr)
Definition table_zone.cpp:25
static const QColor GRADIENT_COLOR
Definition table_zone.h:62
static const int STACKED_CARD_OFFSET_X
Definition table_zone.h:49
int height
Definition table_zone.h:69
void resizeToContents()
Definition table_zone.cpp:225
static const int MARGIN_LEFT
Definition table_zone.h:34
void computeCardStackWidths()
Definition table_zone.cpp:262
qreal getWidth() const
Definition table_zone.h:176
QRectF boundingRect() const override
Definition table_zone.cpp:48
QPixmap backgroundPixelMap
Definition table_zone.h:80
QPointF closestGridPoint(const QPointF &point) override
Definition table_zone.cpp:366
int getMinimumWidth() const
Definition table_zone.h:167
void handleDropEvent(const QList< CardDragItem * > &dragItems, CardZoneLogic *startZone, const QPoint &dropPoint) override
Definition table_zone.cpp:117
void paintLandDivider(QPainter *painter)
Definition table_zone.cpp:106
static const int MIN_WIDTH
Definition table_zone.h:44
bool active
Definition table_zone.h:85
static const int BOX_LINE_WIDTH
Definition table_zone.h:55
void toggleTapped()
Definition table_zone.cpp:192
QPointF mapFromGrid(QPoint gridPoint) const
Definition table_zone.cpp:293
static const int MARGIN_RIGHT
Definition table_zone.h:35
static const QColor GRADIENT_COLORLESS
Definition table_zone.h:63
static const QColor BACKGROUND_COLOR
Definition table_zone.h:60
int currentMinimumWidth
Definition table_zone.h:70
void setWidth(qreal _width)
Definition table_zone.h:171
QPoint mapToGrid(const QPointF &mapPoint) const
Definition table_zone.cpp:319
bool isInverted() const
Definition table_zone.cpp:53
static const int MARGIN_TOP
Definition table_zone.h:36
void sizeChanged()
constexpr int WIDTH
Card width in pixels.
Definition card_dimensions.h:16
TODO: Document this.
TODO: Document this.