Turn GameState into QML singleton

This commit is contained in:
ItsDrike 2024-12-03 15:32:30 +01:00
parent f82b176f55
commit 3acfe3e093
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0
4 changed files with 14 additions and 16 deletions

View file

@ -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

View file

@ -14,6 +14,8 @@ GameState::GameState(QObject *parent)
for (int i = 0; i < 7; ++i) {
m_columns.append(QList<ColumnSlot*>());
}
dealCards();
}
void GameState::dealCards()

View file

@ -10,7 +10,7 @@ class GameState : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("Use C++ logic to instantiate")
QML_SINGLETON
Q_PROPERTY(QList<PlayingCard*> drawPile READ drawPile NOTIFY drawPileChanged)
Q_PROPERTY(QList<PlayingCard*> throwawayPile READ throwawayPile NOTIFY throwawayPileChanged)
Q_PROPERTY(QList<QList<ColumnSlot*>> columns READ columns NOTIFY columnsChanged)

View file

@ -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<GameState*>("Solitare", "GameState");
gameState->drawNextCard();
gameState->drawNextCard();
gameState->drawNextCard();
engine.loadFromModule("Solitare", "Main");
return app.exec();
}