Cockatrice 2026-10-19-Development-3.1.2-beta.2
A virtual tabletop for multiplayer card games
Loading...
Searching...
No Matches
game_scene.h
Go to the documentation of this file.
1#ifndef GAMESCENE_H
2#define GAMESCENE_H
3
5
6#include <QGraphicsScene>
7#include <QHash>
8#include <QList>
9#include <QLoggingCategory>
10#include <QMap>
11#include <QMetaObject>
12#include <QPointF>
13#include <QPointer>
14#include <QSize>
15#include <QSizeF>
16#include <QString>
17#include <QTransform>
18#include <QtGlobal>
19#include <qtmetamacros.h>
20
21// IWYU pragma: keep
22// ServerInfo_Card appears inside QList<...> in signal and slot
23// signatures, which the moc-generated code needs to be complete.
24#include <libcockatrice/protocol/pb/serverinfo_card.pb.h>
25
26class CardZoneLogic;
27class IAnimatedItem;
28class QGraphicsItem;
29class QPoint;
30struct ArrowData;
31template <class T> class QSharedPointer;
32
33inline Q_LOGGING_CATEGORY(GameSceneLog, "game_scene");
34inline Q_LOGGING_CATEGORY(GameScenePlayerAdditionRemovalLog, "game_scene.player_addition_removal");
35
36class PlayerLogic;
38class ZoneViewWidget;
39class CardZone;
41class CardItem;
42class ServerInfo_Card;
43class PhasesToolbar;
44class QBasicTimer;
45class QObject;
46
59class GameScene : public QGraphicsScene
60{
61 Q_OBJECT
62private:
63 static const int playerAreaSpacing = 5;
64
70 QSize viewSize;
71 QPointer<CardItem> hoveredCard;
72 QBasicTimer *animationTimer;
77 bool rearranging = false;
78 bool needsReArrange = false;
79
84 void updateHover(const QPointF &scenePos);
85
90 void beginCardHover(CardItem *card);
92 void endCardHover(CardItem *card);
93
94public:
100 explicit GameScene(PhasesToolbar *_phasesToolbar, QObject *parent = nullptr);
101
103 ~GameScene() override;
104
106 void retranslateUi();
107
110
115 void addPlayer(PlayerLogic *player);
116
121 void removePlayer(PlayerLogic *player);
122
127
129 {
130 return playerViews.value(playerId);
131 }
132
137 void adjustPlayerRotation(int rotationAdjustment);
138
140 void rearrange();
141
146 void processViewSizeChange(const QSize &newSize);
147
153 QList<PlayerLogic *> collectActivePlayers(int &firstPlayerIndex) const;
154
161 QList<PlayerLogic *> rotatePlayers(const QList<PlayerLogic *> &players, int firstPlayerIndex) const;
162
168 static int determineColumnCount(int playerCount);
169
176 QSizeF computeSceneSizeAndPlayerLayout(const QList<PlayerLogic *> &playersPlaying, int columns);
177
183
190 qreal calculateNewSceneWidth(const QSize &newSize, qreal minWidth) const;
191
197 void resizeColumnsAndPlayers(const QList<qreal> &minWidthByColumn, qreal newWidth);
198
200 static CardZone *findTopmostZone(const QList<QGraphicsItem *> &items);
201
204
206 void updateHoveredCard(CardItem *newCard);
207
215
218 void startRubberBand(const QPointF &selectionOrigin);
219 void resizeRubberBand(const QPointF &cursorPoint, int selectedCount);
220 void stopRubberBand();
221
222private slots:
224 void removeAnimatedItem(QObject *item);
225
226public slots:
227 void onCardSelectionChanged(AbstractCardItem *card, bool selected);
228 void onCardRightClicked(AbstractCardItem *card, QPoint screenPos);
229 void playSelected(AbstractCardItem *card);
231 void hideSelected(AbstractCardItem *card);
233 void toggleZoneView(PlayerLogic *player, const QString &zoneName, int numberCards, bool isReversed = false);
234
236 void addRevealedZoneView(PlayerLogic *player,
237 CardZoneLogic *zone,
238 const QList<const ServerInfo_Card *> &cardList,
239 bool withWritePermission);
240
242 void removeZoneView(ZoneViewWidget *item);
243
245 void clearViews();
246
248 void emptyActiveViews();
249
252 QTransform getViewTransform() const;
253 QTransform getViewportTransform() const;
254
257 void deleteArrow(int playerId, int arrowId);
258 void clearArrowsForPlayer(int playerId);
259 void clearArrowsForPlayerLocally(int playerId);
260
262 void requestArrowDeletion(int playerId, int arrowId);
263
264 void onCardZoneChanged(CardItem *card, bool sameZone);
265
266protected:
268 bool event(QEvent *event) override;
269
271 void timerEvent(QTimerEvent *event) override;
272
273signals:
274 void sigStartRubberBand(const QPointF &selectionOrigin);
275 void sigResizeRubberBand(const QPointF &cursorPoint, int selectedCount);
277 void arrowDeletionRequested(int creatorId, int arrowId);
278};
279
280#endif
Definition abstract_card_item.h:29
Definition arrow_registry.h:27
Definition card_item.h:33
Definition card_zone_logic.h:27
Definition card_zone.h:35
void onCardRightClicked(AbstractCardItem *card, QPoint screenPos)
Definition game_scene.cpp:147
qreal calculateNewSceneWidth(const QSize &newSize, qreal minWidth) const
Calculates new scene width considering window aspect ratio.
Definition game_scene.cpp:480
static int determineColumnCount(int playerCount)
Determines the number of columns to display players in.
Definition game_scene.cpp:388
void toggleZoneView(PlayerLogic *player, const QString &zoneName, int numberCards, bool isReversed=false)
Toggles a zone view for a player.
Definition game_scene.cpp:695
void removeZoneView(ZoneViewWidget *item)
Removes a zone view widget from the scene.
Definition game_scene.cpp:747
void sigStopRubberBand()
void removePlayer(PlayerLogic *player)
Removes a player from the scene.
Definition game_scene.cpp:243
QList< CardItem * > selectedCards() const
Gets all selected CardItems.
Definition game_scene.cpp:115
void requestArrowDeletion(int playerId, int arrowId)
Queues up arrow deletion but doesn't directly modify the scene.
Definition game_scene.cpp:568
QList< PlayerLogic * > collectActivePlayers(int &firstPlayerIndex) const
Collects all active (non-conceded) players.
Definition game_scene.cpp:346
QHash< QObject *, IAnimatedItem * > animatedItems
Items currently animating.
Definition game_scene.h:73
QSizeF computeSceneSizeAndPlayerLayout(const QList< PlayerLogic * > &playersPlaying, int columns)
Computes layout positions and scene size based on players and columns.
Definition game_scene.cpp:406
void resizeRubberBand(const QPointF &cursorPoint, int selectedCount)
Definition game_scene.cpp:868
QList< PlayerLogic * > rotatePlayers(const QList< PlayerLogic * > &players, int firstPlayerIndex) const
Rotates the list of players for layout.
Definition game_scene.cpp:373
void startRubberBand(const QPointF &selectionOrigin)
Definition game_scene.cpp:863
static CardZone * findTopmostZone(const QList< QGraphicsItem * > &items)
Finds the topmost card zone under the cursor.
Definition game_scene.cpp:646
void timerEvent(QTimerEvent *event) override
Handles animation timer updates.
Definition game_scene.cpp:808
PhasesToolbar * phasesToolbar
Toolbar showing game phases.
Definition game_scene.h:65
QList< qreal > calculateMinWidthByColumn() const
Computes the minimum width for each column based on player minimum widths.
Definition game_scene.cpp:461
void updateHoveredCard(CardItem *newCard)
Updates hovered card highlighting.
Definition game_scene.cpp:619
void clearViews()
Closes all zone views.
Definition game_scene.cpp:756
void deleteArrow(int playerId, int arrowId)
Definition game_scene.cpp:561
void onCardZoneChanged(CardItem *card, bool sameZone)
Definition game_scene.cpp:575
QTransform getViewTransform() const
Definition game_scene.cpp:785
static CardItem * findTopmostCardInZone(const QList< QGraphicsItem * > &items, CardZone *zone)
Finds the topmost card in a given zone, considering attachments and Z-order.
Definition game_scene.cpp:656
void beginCardHover(CardItem *card)
Activates hover state and escapes the card from its clip container so hover scaling is visible beyond...
Definition game_scene.cpp:630
void clearArrowsForPlayer(int playerId)
Definition game_scene.cpp:592
void arrowDeletionRequested(int creatorId, int arrowId)
void addRevealedZoneView(PlayerLogic *player, CardZoneLogic *zone, const QList< const ServerInfo_Card * > &cardList, bool withWritePermission)
Adds a revealed zone view (for shown cards).
Definition game_scene.cpp:731
void playSelectedFaceDown(AbstractCardItem *card)
Definition game_scene.cpp:179
void unregisterAnimationItem(IAnimatedItem *item)
Unregisters an item from animation updates.
Definition game_scene.cpp:844
static const int playerAreaSpacing
Space between player areas.
Definition game_scene.h:63
void registerAnimationItem(IAnimatedItem *item)
Registers an item for animation updates with the shared scene timer.
Definition game_scene.cpp:822
bool needsReArrange
Pending rearrange requested during a pass.
Definition game_scene.h:78
QSize viewSize
Current view size.
Definition game_scene.h:70
void stopRubberBand()
Definition game_scene.cpp:873
void clearArrowsForPlayerLocally(int playerId)
Definition game_scene.cpp:599
QHash< QObject *, QMetaObject::Connection > animationItemConnections
destroyed->removeAnimatedItem handles per animated item
Definition game_scene.h:75
void closeMostRecentZoneView()
Closes the most recently added zone view.
Definition game_scene.cpp:776
bool event(QEvent *event) override
Handles hover updates.
Definition game_scene.cpp:797
void emptyActiveViews()
Empties the contents of all non-reveal zone views. Used for replay rewinding.
Definition game_scene.cpp:763
void endCardHover(CardItem *card)
Deactivates hover state and restores the card to its clip container.
Definition game_scene.cpp:638
int playerRotation
Rotation offset for player layout.
Definition game_scene.h:76
QPointer< CardItem > hoveredCard
Currently hovered card.
Definition game_scene.h:71
void retranslateUi()
Updates UI text for all zone views.
Definition game_scene.cpp:108
void processViewSizeChange(const QSize &newSize)
Handles view resize and redistributes player positions.
Definition game_scene.cpp:324
QList< QList< PlayerGraphicsItem * > > playersByColumn
Players organized by column.
Definition game_scene.h:67
void addArrow(QSharedPointer< ArrowData > data)
Directly modifies the scene.
Definition game_scene.cpp:522
QMap< int, PlayerGraphicsItem * > getPlayers() const
Definition game_scene.h:123
void adjustPlayerRotation(int rotationAdjustment)
Adjusts the global rotation offset for player layout.
Definition game_scene.cpp:271
QBasicTimer * animationTimer
Timer for scene animations.
Definition game_scene.h:72
QTransform getViewportTransform() const
Definition game_scene.cpp:790
void addPlayer(PlayerLogic *player)
Adds a player to the scene and stores their graphics item.
Definition game_scene.cpp:207
PlayerGraphicsItem * viewForPlayer(int playerId)
Definition game_scene.h:128
QMap< int, PlayerGraphicsItem * > playerViews
ID lookup for player graphics items.
Definition game_scene.h:66
void resizeColumnsAndPlayers(const QList< qreal > &minWidthByColumn, qreal newWidth)
Resizes columns and distributes extra width to players.
Definition game_scene.cpp:500
void hideSelected(AbstractCardItem *card)
Definition game_scene.cpp:190
void playSelected(AbstractCardItem *card)
Definition game_scene.cpp:168
void removeAnimatedItem(QObject *item)
Removes a destroyed item from the animation set.
Definition game_scene.cpp:852
ArrowRegistry arrowRegistry
ID registry for arrow graphics items.
Definition game_scene.h:68
QList< ZoneViewWidget * > zoneViews
Active zone view widgets.
Definition game_scene.h:69
void sigResizeRubberBand(const QPointF &cursorPoint, int selectedCount)
bool rearranging
Guard against re-entrant rearrange.
Definition game_scene.h:77
void rearrange()
Recomputes the layout of players and the scene size.
Definition game_scene.cpp:288
void sigStartRubberBand(const QPointF &selectionOrigin)
GameScene(PhasesToolbar *_phasesToolbar, QObject *parent=nullptr)
Constructs the GameScene.
Definition game_scene.cpp:59
void updateHover(const QPointF &scenePos)
Updates which card is currently hovered based on scene coordinates.
Definition game_scene.cpp:610
void onCardSelectionChanged(AbstractCardItem *card, bool selected)
Definition game_scene.cpp:127
Definition animated_item.h:15
Definition phases_toolbar.h:67
Definition player_graphics_item.h:39
Definition player_logic.h:56
Definition mana_curve_category_widget.h:11
Definition remote_connection_controller.h:20
Definition game.h:18
Definition arrow_registry.h:10
Definition view_zone_widget.h:55
Q_LOGGING_CATEGORY(CrashReportLog, "crash_report")
Definition arrow_data.h:10
Definition serverinfo_card.proto:5