Cockatrice 2026-09-01-Development-3.1.0-beta.10
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
7#include "animated_item.h"
8#include "board/arrow_item.h"
9
10#include <QGraphicsScene>
11#include <QHash>
12#include <QList>
13#include <QLoggingCategory>
14#include <QPointer>
15
16inline Q_LOGGING_CATEGORY(GameSceneLog, "game_scene");
17inline Q_LOGGING_CATEGORY(GameScenePlayerAdditionRemovalLog, "game_scene.player_addition_removal");
18
19class PlayerLogic;
21class ZoneViewWidget;
22class CardZone;
24class CardItem;
25class ServerInfo_Card;
26class PhasesToolbar;
27class QBasicTimer;
28class QObject;
29
42class GameScene : public QGraphicsScene
43{
44 Q_OBJECT
45private:
46 static const int playerAreaSpacing = 5;
47
49 QMap<int, PlayerGraphicsItem *> playerViews;
50 QList<QList<PlayerGraphicsItem *>> playersByColumn;
52 QList<ZoneViewWidget *> zoneViews;
53 QSize viewSize;
54 QPointer<CardItem> hoveredCard;
55 QBasicTimer *animationTimer;
56 QHash<QObject *, IAnimatedItem *> animatedItems;
57 QHash<QObject *, QMetaObject::Connection>
60 bool rearranging = false;
61 bool needsReArrange = false;
62
67 void updateHover(const QPointF &scenePos);
68
73 void beginCardHover(CardItem *card);
75 void endCardHover(CardItem *card);
76
77public:
83 explicit GameScene(PhasesToolbar *_phasesToolbar, QObject *parent = nullptr);
84
86 ~GameScene() override;
87
89 void retranslateUi();
90
92 QList<CardItem *> selectedCards() const;
93
98 void addPlayer(PlayerLogic *player);
99
104 void removePlayer(PlayerLogic *player);
105
106 QMap<int, PlayerGraphicsItem *> getPlayers() const
107 {
108 return playerViews;
109 }
110
112 {
113 return playerViews.value(playerId);
114 }
115
120 void adjustPlayerRotation(int rotationAdjustment);
121
123 void rearrange();
124
129 void processViewSizeChange(const QSize &newSize);
130
136 QList<PlayerLogic *> collectActivePlayers(int &firstPlayerIndex) const;
137
144 QList<PlayerLogic *> rotatePlayers(const QList<PlayerLogic *> &players, int firstPlayerIndex) const;
145
151 static int determineColumnCount(int playerCount);
152
159 QSizeF computeSceneSizeAndPlayerLayout(const QList<PlayerLogic *> &playersPlaying, int columns);
160
165 QList<qreal> calculateMinWidthByColumn() const;
166
173 qreal calculateNewSceneWidth(const QSize &newSize, qreal minWidth) const;
174
180 void resizeColumnsAndPlayers(const QList<qreal> &minWidthByColumn, qreal newWidth);
181
183 static CardZone *findTopmostZone(const QList<QGraphicsItem *> &items);
184
186 static CardItem *findTopmostCardInZone(const QList<QGraphicsItem *> &items, CardZone *zone);
187
189 void updateHoveredCard(CardItem *newCard);
190
198
201 void startRubberBand(const QPointF &selectionOrigin);
202 void resizeRubberBand(const QPointF &cursorPoint, int selectedCount);
203 void stopRubberBand();
204
205private slots:
207 void removeAnimatedItem(QObject *item);
208
209public slots:
210 void onCardSelectionChanged(AbstractCardItem *card, bool selected);
211 void onCardRightClicked(AbstractCardItem *card, QPoint screenPos);
212 void playSelected(AbstractCardItem *card);
214 void hideSelected(AbstractCardItem *card);
216 void toggleZoneView(PlayerLogic *player, const QString &zoneName, int numberCards, bool isReversed = false);
217
219 void addRevealedZoneView(PlayerLogic *player,
220 CardZoneLogic *zone,
221 const QList<const ServerInfo_Card *> &cardList,
222 bool withWritePermission);
223
225 void removeZoneView(ZoneViewWidget *item);
226
228 void clearViews();
229
232 QTransform getViewTransform() const;
233 QTransform getViewportTransform() const;
234
236 void addArrow(QSharedPointer<ArrowData> data);
237 void deleteArrow(int playerId, int arrowId);
238 void clearArrowsForPlayer(int playerId);
239 void clearArrowsForPlayerLocally(int playerId);
240
242 void requestArrowDeletion(int playerId, int arrowId);
243
244 void onCardZoneChanged(CardItem *card, bool sameZone);
245
246protected:
248 bool event(QEvent *event) override;
249
251 void timerEvent(QTimerEvent *event) override;
252
253signals:
254 void sigStartRubberBand(const QPointF &selectionOrigin);
255 void sigResizeRubberBand(const QPointF &cursorPoint, int selectedCount);
257 void arrowDeletionRequested(int creatorId, int arrowId);
258};
259
260#endif
Interface for scene items driven by GameScene's shared animation timer.
Definition abstract_card_item.h:21
Definition arrow_registry.h:27
Definition card_item.h:27
Definition card_zone_logic.h:26
Definition card_zone.h:26
void onCardRightClicked(AbstractCardItem *card, QPoint screenPos)
Definition game_scene.cpp:122
qreal calculateNewSceneWidth(const QSize &newSize, qreal minWidth) const
Calculates new scene width considering window aspect ratio.
Definition game_scene.cpp:450
static int determineColumnCount(int playerCount)
Determines the number of columns to display players in.
Definition game_scene.cpp:358
void toggleZoneView(PlayerLogic *player, const QString &zoneName, int numberCards, bool isReversed=false)
Toggles a zone view for a player.
Definition game_scene.cpp:665
void removeZoneView(ZoneViewWidget *item)
Removes a zone view widget from the scene.
Definition game_scene.cpp:714
void sigStopRubberBand()
void removePlayer(PlayerLogic *player)
Removes a player from the scene.
Definition game_scene.cpp:218
QList< CardItem * > selectedCards() const
Gets all selected CardItems.
Definition game_scene.cpp:90
void requestArrowDeletion(int playerId, int arrowId)
Queues up arrow deletion but doesn't directly modify the scene.
Definition game_scene.cpp:538
QList< PlayerLogic * > collectActivePlayers(int &firstPlayerIndex) const
Collects all active (non-conceded) players.
Definition game_scene.cpp:316
QHash< QObject *, IAnimatedItem * > animatedItems
Items currently animating.
Definition game_scene.h:56
QSizeF computeSceneSizeAndPlayerLayout(const QList< PlayerLogic * > &playersPlaying, int columns)
Computes layout positions and scene size based on players and columns.
Definition game_scene.cpp:376
void resizeRubberBand(const QPointF &cursorPoint, int selectedCount)
Definition game_scene.cpp:825
QList< PlayerLogic * > rotatePlayers(const QList< PlayerLogic * > &players, int firstPlayerIndex) const
Rotates the list of players for layout.
Definition game_scene.cpp:343
void startRubberBand(const QPointF &selectionOrigin)
Definition game_scene.cpp:820
static CardZone * findTopmostZone(const QList< QGraphicsItem * > &items)
Finds the topmost card zone under the cursor.
Definition game_scene.cpp:616
void timerEvent(QTimerEvent *event) override
Handles animation timer updates.
Definition game_scene.cpp:765
PhasesToolbar * phasesToolbar
Toolbar showing game phases.
Definition game_scene.h:48
QList< qreal > calculateMinWidthByColumn() const
Computes the minimum width for each column based on player minimum widths.
Definition game_scene.cpp:431
void updateHoveredCard(CardItem *newCard)
Updates hovered card highlighting.
Definition game_scene.cpp:589
void clearViews()
Closes all zone views.
Definition game_scene.cpp:723
void deleteArrow(int playerId, int arrowId)
Definition game_scene.cpp:531
void onCardZoneChanged(CardItem *card, bool sameZone)
Definition game_scene.cpp:545
QTransform getViewTransform() const
Definition game_scene.cpp:742
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:626
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:600
void clearArrowsForPlayer(int playerId)
Definition game_scene.cpp:562
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:698
void playSelectedFaceDown(AbstractCardItem *card)
Definition game_scene.cpp:154
void unregisterAnimationItem(IAnimatedItem *item)
Unregisters an item from animation updates.
Definition game_scene.cpp:801
static const int playerAreaSpacing
Space between player areas.
Definition game_scene.h:46
void registerAnimationItem(IAnimatedItem *item)
Registers an item for animation updates with the shared scene timer.
Definition game_scene.cpp:779
bool needsReArrange
Pending rearrange requested during a pass.
Definition game_scene.h:61
QSize viewSize
Current view size.
Definition game_scene.h:53
void stopRubberBand()
Definition game_scene.cpp:830
void clearArrowsForPlayerLocally(int playerId)
Definition game_scene.cpp:569
QHash< QObject *, QMetaObject::Connection > animationItemConnections
destroyed->removeAnimatedItem handles per animated item
Definition game_scene.h:58
void closeMostRecentZoneView()
Closes the most recently added zone view.
Definition game_scene.cpp:733
bool event(QEvent *event) override
Handles hover updates.
Definition game_scene.cpp:754
void endCardHover(CardItem *card)
Deactivates hover state and restores the card to its clip container.
Definition game_scene.cpp:608
~GameScene() override
Destructor, cleans up timer and zone views.
Definition game_scene.cpp:45
int playerRotation
Rotation offset for player layout.
Definition game_scene.h:59
QPointer< CardItem > hoveredCard
Currently hovered card.
Definition game_scene.h:54
void retranslateUi()
Updates UI text for all zone views.
Definition game_scene.cpp:83
void processViewSizeChange(const QSize &newSize)
Handles view resize and redistributes player positions.
Definition game_scene.cpp:294
QList< QList< PlayerGraphicsItem * > > playersByColumn
Players organized by column.
Definition game_scene.h:50
void addArrow(QSharedPointer< ArrowData > data)
Directly modifies the scene.
Definition game_scene.cpp:492
QMap< int, PlayerGraphicsItem * > getPlayers() const
Definition game_scene.h:106
void adjustPlayerRotation(int rotationAdjustment)
Adjusts the global rotation offset for player layout.
Definition game_scene.cpp:241
QBasicTimer * animationTimer
Timer for scene animations.
Definition game_scene.h:55
QTransform getViewportTransform() const
Definition game_scene.cpp:747
void addPlayer(PlayerLogic *player)
Adds a player to the scene and stores their graphics item.
Definition game_scene.cpp:182
PlayerGraphicsItem * viewForPlayer(int playerId)
Definition game_scene.h:111
QMap< int, PlayerGraphicsItem * > playerViews
ID lookup for player graphics items.
Definition game_scene.h:49
void resizeColumnsAndPlayers(const QList< qreal > &minWidthByColumn, qreal newWidth)
Resizes columns and distributes extra width to players.
Definition game_scene.cpp:470
void hideSelected(AbstractCardItem *card)
Definition game_scene.cpp:165
void playSelected(AbstractCardItem *card)
Definition game_scene.cpp:143
void removeAnimatedItem(QObject *item)
Removes a destroyed item from the animation set.
Definition game_scene.cpp:809
ArrowRegistry arrowRegistry
ID registry for arrow graphics items.
Definition game_scene.h:51
QList< ZoneViewWidget * > zoneViews
Active zone view widgets.
Definition game_scene.h:52
void sigResizeRubberBand(const QPointF &cursorPoint, int selectedCount)
bool rearranging
Guard against re-entrant rearrange.
Definition game_scene.h:60
void rearrange()
Recomputes the layout of players and the scene size.
Definition game_scene.cpp:258
void sigStartRubberBand(const QPointF &selectionOrigin)
GameScene(PhasesToolbar *_phasesToolbar, QObject *parent=nullptr)
Constructs the GameScene.
Definition game_scene.cpp:34
void updateHover(const QPointF &scenePos)
Updates which card is currently hovered based on scene coordinates.
Definition game_scene.cpp:580
void onCardSelectionChanged(AbstractCardItem *card, bool selected)
Definition game_scene.cpp:102
Definition animated_item.h:15
Definition phases_toolbar.h:66
Definition player_graphics_item.h:26
Definition player_logic.h:65
Definition view_zone_widget.h:51
static QString cardList(const QList< CommanderSpellbookCardResult > &cards, int max=5)
Definition commander_spellbook_bracket_explainer.cpp:3
Q_LOGGING_CATEGORY(GameSceneLog, "game_scene")
Definition serverinfo_card.proto:5