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(); }