From 3acfe3e09393cd866cf0ad2fe373bf80a89c2406 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Tue, 3 Dec 2024 15:32:30 +0100 Subject: [PATCH] Turn GameState into QML singleton --- Main.qml | 14 +++++++------- gamestate.cpp | 2 ++ gamestate.h | 2 +- main.cpp | 12 ++++-------- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Main.qml b/Main.qml index 34d8be4..161b797 100644 --- a/Main.qml +++ b/Main.qml @@ -39,7 +39,7 @@ ApplicationWindow { model: 4 // Each of the 4 suits CardModel { required property int index; // passed from repeater - card: gameState.foundation[index].length > 0 ? gameState.foundation[index][0] : null + card: GameState.foundation[index].length > 0 ? GameState.foundation[index][0] : null isFaceDown: false } } @@ -61,10 +61,10 @@ ApplicationWindow { spacing: -60 Repeater { - model: Math.min(gameState.throwawayPile.length, 3) + model: Math.min(GameState.throwawayPile.length, 3) delegate: CardModel { required property int index // passed from repeater - card: gameState.throwawayPile[gameState.throwawayPile.length - 1 - index] + card: GameState.throwawayPile[GameState.throwawayPile.length - 1 - index] isFaceDown: false } } @@ -72,7 +72,7 @@ ApplicationWindow { // Draw pile (only the top card is shown) CardModel { - card: gameState.drawPile.length > 0 ? gameState.drawPile[gameState.drawPile.length - 1] : null + card: GameState.drawPile.length > 0 ? GameState.drawPile[GameState.drawPile.length - 1] : null isFaceDown: true } } @@ -88,17 +88,17 @@ ApplicationWindow { anchors.topMargin: 50 Repeater { - model: gameState.columns.length + model: GameState.columns.length // Make a column for each slot delegate: Column { required property int index // passed from repeater spacing: -80 // Overlap Repeater { - model: gameState.columns[parent.index].length + model: GameState.columns[parent.index].length delegate: CardModel { required property int index - property ColumnSlot col: gameState.columns[parent.index][index] + property ColumnSlot col: GameState.columns[parent.index][index] card: col.card isFaceDown: !col.revealed diff --git a/gamestate.cpp b/gamestate.cpp index fa40b5a..0e882cc 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -14,6 +14,8 @@ GameState::GameState(QObject *parent) for (int i = 0; i < 7; ++i) { m_columns.append(QList()); } + + dealCards(); } void GameState::dealCards() diff --git a/gamestate.h b/gamestate.h index 8465373..e34688b 100644 --- a/gamestate.h +++ b/gamestate.h @@ -10,7 +10,7 @@ class GameState : public QObject { Q_OBJECT QML_ELEMENT - QML_UNCREATABLE("Use C++ logic to instantiate") + QML_SINGLETON Q_PROPERTY(QList drawPile READ drawPile NOTIFY drawPileChanged) Q_PROPERTY(QList throwawayPile READ throwawayPile NOTIFY throwawayPileChanged) Q_PROPERTY(QList> columns READ columns NOTIFY columnsChanged) diff --git a/main.cpp b/main.cpp index 1867309..93350bf 100644 --- a/main.cpp +++ b/main.cpp @@ -15,15 +15,11 @@ int main(int argc, char *argv[]) []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); - GameState gameState; - gameState.dealCards(); - gameState.drawNextCard(); - gameState.drawNextCard(); - gameState.drawNextCard(); - - engine.rootContext()->setContextProperty("gameState", &gameState); + auto gameState = engine.singletonInstance("Solitare", "GameState"); + gameState->drawNextCard(); + gameState->drawNextCard(); + gameState->drawNextCard(); engine.loadFromModule("Solitare", "Main"); - return app.exec(); }