Turn GameState into QML singleton
This commit is contained in:
parent
f82b176f55
commit
3acfe3e093
14
Main.qml
14
Main.qml
|
@ -39,7 +39,7 @@ ApplicationWindow {
|
||||||
model: 4 // Each of the 4 suits
|
model: 4 // Each of the 4 suits
|
||||||
CardModel {
|
CardModel {
|
||||||
required property int index; // passed from repeater
|
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
|
isFaceDown: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,10 +61,10 @@ ApplicationWindow {
|
||||||
spacing: -60
|
spacing: -60
|
||||||
|
|
||||||
Repeater {
|
Repeater {
|
||||||
model: Math.min(gameState.throwawayPile.length, 3)
|
model: Math.min(GameState.throwawayPile.length, 3)
|
||||||
delegate: CardModel {
|
delegate: CardModel {
|
||||||
required property int index // passed from repeater
|
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
|
isFaceDown: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ ApplicationWindow {
|
||||||
|
|
||||||
// Draw pile (only the top card is shown)
|
// Draw pile (only the top card is shown)
|
||||||
CardModel {
|
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
|
isFaceDown: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,17 +88,17 @@ ApplicationWindow {
|
||||||
anchors.topMargin: 50
|
anchors.topMargin: 50
|
||||||
|
|
||||||
Repeater {
|
Repeater {
|
||||||
model: gameState.columns.length
|
model: GameState.columns.length
|
||||||
// Make a column for each slot
|
// Make a column for each slot
|
||||||
delegate: Column {
|
delegate: Column {
|
||||||
required property int index // passed from repeater
|
required property int index // passed from repeater
|
||||||
spacing: -80 // Overlap
|
spacing: -80 // Overlap
|
||||||
|
|
||||||
Repeater {
|
Repeater {
|
||||||
model: gameState.columns[parent.index].length
|
model: GameState.columns[parent.index].length
|
||||||
delegate: CardModel {
|
delegate: CardModel {
|
||||||
required property int index
|
required property int index
|
||||||
property ColumnSlot col: gameState.columns[parent.index][index]
|
property ColumnSlot col: GameState.columns[parent.index][index]
|
||||||
|
|
||||||
card: col.card
|
card: col.card
|
||||||
isFaceDown: !col.revealed
|
isFaceDown: !col.revealed
|
||||||
|
|
|
@ -14,6 +14,8 @@ GameState::GameState(QObject *parent)
|
||||||
for (int i = 0; i < 7; ++i) {
|
for (int i = 0; i < 7; ++i) {
|
||||||
m_columns.append(QList<ColumnSlot*>());
|
m_columns.append(QList<ColumnSlot*>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dealCards();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameState::dealCards()
|
void GameState::dealCards()
|
||||||
|
|
|
@ -10,7 +10,7 @@ class GameState : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
QML_ELEMENT
|
QML_ELEMENT
|
||||||
QML_UNCREATABLE("Use C++ logic to instantiate")
|
QML_SINGLETON
|
||||||
Q_PROPERTY(QList<PlayingCard*> drawPile READ drawPile NOTIFY drawPileChanged)
|
Q_PROPERTY(QList<PlayingCard*> drawPile READ drawPile NOTIFY drawPileChanged)
|
||||||
Q_PROPERTY(QList<PlayingCard*> throwawayPile READ throwawayPile NOTIFY throwawayPileChanged)
|
Q_PROPERTY(QList<PlayingCard*> throwawayPile READ throwawayPile NOTIFY throwawayPileChanged)
|
||||||
Q_PROPERTY(QList<QList<ColumnSlot*>> columns READ columns NOTIFY columnsChanged)
|
Q_PROPERTY(QList<QList<ColumnSlot*>> columns READ columns NOTIFY columnsChanged)
|
||||||
|
|
12
main.cpp
12
main.cpp
|
@ -15,15 +15,11 @@ int main(int argc, char *argv[])
|
||||||
[]() { QCoreApplication::exit(-1); },
|
[]() { QCoreApplication::exit(-1); },
|
||||||
Qt::QueuedConnection);
|
Qt::QueuedConnection);
|
||||||
|
|
||||||
GameState gameState;
|
auto gameState = engine.singletonInstance<GameState*>("Solitare", "GameState");
|
||||||
gameState.dealCards();
|
gameState->drawNextCard();
|
||||||
gameState.drawNextCard();
|
gameState->drawNextCard();
|
||||||
gameState.drawNextCard();
|
gameState->drawNextCard();
|
||||||
gameState.drawNextCard();
|
|
||||||
|
|
||||||
engine.rootContext()->setContextProperty("gameState", &gameState);
|
|
||||||
|
|
||||||
engine.loadFromModule("Solitare", "Main");
|
engine.loadFromModule("Solitare", "Main");
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue