Cockatrice 2025-11-30-Development-2.11.0-beta.38
A cross-platform 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 */
44 static const int MIN_WIDTH = MARGIN_LEFT + (5 * CARD_WIDTH) + MARGIN_RIGHT;
45
46 /*
47 Offset sizes when cards are stacked on each other in the grid
48 */
49 static const int STACKED_CARD_OFFSET_X = CARD_WIDTH / 3;
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
158 void resizeToContents();
159
160 [[nodiscard]] int getMinimumWidth() const
161 {
162 return currentMinimumWidth;
163 }
164 void setWidth(qreal _width)
165 {
166 prepareGeometryChange();
167 width = _width;
168 }
169 [[nodiscard]] qreal getWidth() const
170 {
171 return width;
172 }
173 void setActive(bool _active)
174 {
175 active = _active;
176 update();
177 }
178
179private:
180 void paintZoneOutline(QPainter *painter);
181 void paintLandDivider(QPainter *painter);
182
183 /*
184 Calculates card stack widths so mapping functions work properly
185 */
187
188 /*
189 Mapping functions for points to/from gridpoints.
190 */
191 [[nodiscard]] QPointF mapFromGrid(QPoint gridPoint) const;
192 [[nodiscard]] QPoint mapToGrid(const QPointF &mapPoint) const;
193
194 /*
195 Helper function to create a single key from a card stack location.
196 */
197 [[nodiscard]] int getCardStackMapKey(int x, int y) const
198 {
199 return x + (y * 1000);
200 }
201};
202
203#endif
TODO: Document this.
const int CARD_WIDTH
Definition abstract_card_item.h:18
Definition card_item.h:29
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:244
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition table_zone.cpp:59
void reorganizeCards() override
Definition table_zone.cpp:149
CardItem * getCardFromCoords(const QPointF &point) const
Definition table_zone.cpp:252
int getCardStackMapKey(int x, int y) const
Definition table_zone.h:197
void setActive(bool _active)
Definition table_zone.h:173
static const int MARGIN_BOTTOM
Definition table_zone.h:37
static int clampValidTableRow(const int row)
Definition table_zone.cpp:373
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:122
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:41
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:80
static const int TABLEROWS
Definition table_zone.h:29
TableZone(TableZoneLogic *_logic, QGraphicsItem *parent=nullptr)
Definition table_zone.cpp:23
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:221
static const int MARGIN_LEFT
Definition table_zone.h:34
void computeCardStackWidths()
Definition table_zone.cpp:258
qreal getWidth() const
Definition table_zone.h:169
QRectF boundingRect() const override
Definition table_zone.cpp:46
QPixmap backgroundPixelMap
Definition table_zone.h:80
QPointF closestGridPoint(const QPointF &point) override
Definition table_zone.cpp:362
int getMinimumWidth() const
Definition table_zone.h:160
void handleDropEvent(const QList< CardDragItem * > &dragItems, CardZoneLogic *startZone, const QPoint &dropPoint) override
Definition table_zone.cpp:115
void paintLandDivider(QPainter *painter)
Definition table_zone.cpp:104
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:188
QPointF mapFromGrid(QPoint gridPoint) const
Definition table_zone.cpp:289
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:164
QPoint mapToGrid(const QPointF &mapPoint) const
Definition table_zone.cpp:315
bool isInverted() const
Definition table_zone.cpp:51
static const int MARGIN_TOP
Definition table_zone.h:36
void sizeChanged()
TODO: Document this.
TODO: Document this.