From 7de044e127fa79ddb2552bd3310b42a2004a4aff Mon Sep 17 00:00:00 2001 From: Odizinne Date: Sun, 23 Feb 2025 10:07:31 +0100 Subject: [PATCH] refactor --- CMakeLists.txt | 14 ++--- qml/{AboutPage.qml => AboutPopup.qml} | 0 qml/GameGrid.qml | 36 +++++------- qml/GameState.qml | 7 +++ ...aderboardPage.qml => LeaderboardPopup.qml} | 0 qml/{LoadWindow.qml => LoadPopup.qml} | 0 qml/Main.qml | 16 +++--- qml/{GameOverPopup.qml => PostgamePopup.qml} | 55 ++++++------------- qml/{SaveWindow.qml => SavePopup.qml} | 0 qml/{SettingsPage.qml => SettingsWindow.qml} | 0 qml/{WelcomePage.qml => WelcomePopup.qml} | 0 11 files changed, 52 insertions(+), 76 deletions(-) rename qml/{AboutPage.qml => AboutPopup.qml} (100%) rename qml/{LeaderboardPage.qml => LeaderboardPopup.qml} (100%) rename qml/{LoadWindow.qml => LoadPopup.qml} (100%) rename qml/{GameOverPopup.qml => PostgamePopup.qml} (59%) rename qml/{SaveWindow.qml => SavePopup.qml} (100%) rename qml/{SettingsPage.qml => SettingsWindow.qml} (100%) rename qml/{WelcomePage.qml => WelcomePopup.qml} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index cbf2be8..504e061 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,16 +32,16 @@ set(HEADERS ) set(QML_FILES qml/Cell.qml - qml/SettingsPage.qml - qml/GameOverPopup.qml - qml/AboutPage.qml - qml/LoadWindow.qml - qml/SaveWindow.qml + qml/SettingsWindow.qml + qml/PostgamePopup.qml + qml/AboutPopup.qml + qml/LoadPopup.qml + qml/SavePopup.qml qml/ErrorWindow.qml qml/TopBar.qml - qml/LeaderboardPage.qml + qml/LeaderboardPopup.qml qml/TempScrollBar.qml - qml/WelcomePage.qml + qml/WelcomePopup.qml qml/Main.qml qml/InfoIcon.qml qml/GameGrid.qml diff --git a/qml/AboutPage.qml b/qml/AboutPopup.qml similarity index 100% rename from qml/AboutPage.qml rename to qml/AboutPopup.qml diff --git a/qml/GameGrid.qml b/qml/GameGrid.qml index c15bf8f..37c971d 100644 --- a/qml/GameGrid.qml +++ b/qml/GameGrid.qml @@ -13,7 +13,6 @@ GridView { property bool initialAnimationPlayed: false property int cellsCreated: 0 required property var leaderboardWindow - required property var gameOverPopup GameAudio { id: audioEngine @@ -109,13 +108,11 @@ GridView { if (GameState.mines.includes(currentIndex)) { cell.isBombClicked = true GameState.gameOver = true + GameState.gameWon = false GameTimer.stop() revealAllMines() audioEngine.playLoose() - gameOverPopup.gameOverLabelText = "Game over" - gameOverPopup.gameOverLabelColor = "#d12844" - gameOverPopup.newRecordVisible = false - gameOverPopup.visible = true + GameState.displayPostGame = true return } @@ -223,6 +220,7 @@ GridView { function checkWin() { if (GameState.revealedCount === GameState.gridSizeX * GameState.gridSizeY - GameState.mineCount && !GameState.gameOver) { GameState.gameOver = true + GameState.gameWon = true GameTimer.stop() let leaderboardData = GameCore.loadGameState("leaderboard.json") @@ -255,9 +253,7 @@ GridView { leaderboard[timeField] = formattedTime; leaderboard[centisecondsField] = centiseconds; leaderboardWindow[timeField] = formattedTime; - gameOverPopup.newRecordVisible = true - } else { - gameOverPopup.newRecordVisible = false + GameState.displayNewRecord = true } } @@ -271,22 +267,22 @@ GridView { if (difficulty === 'easy') { if (!SteamIntegration.isAchievementUnlocked("ACH_NO_HINT_EASY")) { SteamIntegration.unlockAchievement("ACH_NO_HINT_EASY"); - gameOverPopup.notificationText = qsTr("New flag unlocked!") - gameOverPopup.notificationVisible = true; + GameState.notificationText = qsTr("New flag unlocked!") + GameState.displayNotification = true; GameState.flag1Unlocked = true; } } else if (difficulty === 'medium') { if (!SteamIntegration.isAchievementUnlocked("ACH_NO_HINT_MEDIUM")) { SteamIntegration.unlockAchievement("ACH_NO_HINT_MEDIUM"); - gameOverPopup.notificationText = qsTr("New flag unlocked!") - gameOverPopup.notificationVisible = true; + GameState.notificationText = qsTr("New flag unlocked!") + GameState.displayNotification = true; GameState.flag2Unlocked = true; } } else if (difficulty === 'hard') { if (!SteamIntegration.isAchievementUnlocked("ACH_NO_HINT_HARD")) { SteamIntegration.unlockAchievement("ACH_NO_HINT_HARD"); - gameOverPopup.notificationText = qsTr("New flag unlocked!") - gameOverPopup.notificationVisible = true; + GameState.notificationText = qsTr("New flag unlocked!") + GameState.displayNotification = true; GameState.flag3Unlocked = true; } } @@ -295,14 +291,14 @@ GridView { if (difficulty === 'easy') { if (Math.floor(GameTimer.centiseconds / 100) < 15 && !SteamIntegration.isAchievementUnlocked("ACH_SPEED_DEMON")) { SteamIntegration.unlockAchievement("ACH_SPEED_DEMON"); - gameOverPopup.notificationText = qsTr("New grid animation unlocked!") - gameOverPopup.notificationVisible = true + GameState.notificationText = qsTr("New grid animation unlocked!") + GameState.displayNotification = true GameState.anim2Unlocked = true } if (GameState.currentHintCount >= 20 && !SteamIntegration.isAchievementUnlocked("ACH_HINT_MASTER")) { SteamIntegration.unlockAchievement("ACH_HINT_MASTER"); - gameOverPopup.notificationText = qsTr("New grid animation unlocked!") - gameOverPopup.notificationVisible = true + GameState.notificationText = qsTr("New grid animation unlocked!") + GameState.displayNotification = true GameState.anim1Unlocked = true } } @@ -311,9 +307,7 @@ GridView { } } - gameOverPopup.gameOverLabelText = qsTr("Victory") - gameOverPopup.gameOverLabelColor = "#28d13c" - gameOverPopup.visible = true + GameState.displayPostGame = true audioEngine.playWin() } else { audioEngine.playClick() diff --git a/qml/GameState.qml b/qml/GameState.qml index d23931a..e574136 100644 --- a/qml/GameState.qml +++ b/qml/GameState.qml @@ -64,4 +64,11 @@ QtObject { } return null; } + property bool gameWon: false + property string postgameText: gameWon ? qsTr("Victory") : qsTr("Game over") + property string postgameColor: gameWon ? "#28d13c" : "#d12844" + property string notificationText: "" + property bool displayNotification: false + property bool displayNewRecord: false + property bool displayPostGame: false } diff --git a/qml/LeaderboardPage.qml b/qml/LeaderboardPopup.qml similarity index 100% rename from qml/LeaderboardPage.qml rename to qml/LeaderboardPopup.qml diff --git a/qml/LoadWindow.qml b/qml/LoadPopup.qml similarity index 100% rename from qml/LoadWindow.qml rename to qml/LoadPopup.qml diff --git a/qml/Main.qml b/qml/Main.qml index a6d12c1..20ee72d 100644 --- a/qml/Main.qml +++ b/qml/Main.qml @@ -166,7 +166,7 @@ ApplicationWindow { anchors.fill: parent active: GameCore.showWelcome sourceComponent: Component { - WelcomePage { + WelcomePopup { } } } @@ -176,17 +176,16 @@ ApplicationWindow { anchors.fill: parent active: !SteamIntegration.initialized sourceComponent: Component { - AboutPage { + AboutPopup { } } } - GameOverPopup { - id: gameOverPopup + PostgamePopup { grid: grid } - SettingsPage { + SettingsWindow { id: settingsWindow grid: grid rootWidth: root.width @@ -195,15 +194,15 @@ ApplicationWindow { rootY: root.y } - LoadWindow { + LoadPopup { id: loadWindow } - SaveWindow { + SavePopup { id: saveWindow } - LeaderboardPage { + LeaderboardPopup { id: leaderboardWindow } @@ -238,7 +237,6 @@ ApplicationWindow { GameGrid { id: grid leaderboardWindow: leaderboardWindow - gameOverPopup: gameOverPopup Component.onCompleted: SaveManager.setGrid(grid) delegate: Cell { root: root diff --git a/qml/GameOverPopup.qml b/qml/PostgamePopup.qml similarity index 59% rename from qml/GameOverPopup.qml rename to qml/PostgamePopup.qml index 369aace..e7d8027 100644 --- a/qml/GameOverPopup.qml +++ b/qml/PostgamePopup.qml @@ -6,16 +6,11 @@ Popup { anchors.centerIn: parent id: control required property var grid - visible: false + visible: GameState.displayPostGame modal: true closePolicy: Popup.NoAutoClose width: 300 property int buttonWidth: Math.max(retryButton.implicitWidth, closeButton.implicitWidth) - property string notificationText - property bool notificationVisible: false - property string gameOverLabelText: "Game Over" - property string gameOverLabelColor: "#d12844" - property bool newRecordVisible: false Shortcut { sequence: "Return" @@ -29,48 +24,27 @@ Popup { Shortcut { sequence: "Esc" enabled: control.visible - onActivated: control.visible = false - } - - enter: Transition { - NumberAnimation { - property: "opacity" - from: 0.0 - to: 1.0 - duration: 200 - easing.type: Easing.InOutQuad - } - } - - exit: Transition { - NumberAnimation { - property: "opacity" - from: 1.0 - to: 0.0 - duration: 200 - easing.type: Easing.InOutQuad - } + onActivated: retryButton.clicked() } GridLayout { - id: popupLayout anchors.fill: parent columns: 2 rowSpacing: 15 Label { - id: gameOverLabel Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter - text: control.gameOverLabelText - color: control.gameOverLabelColor + text: GameState.postgameText + color: GameState.postgameColor Layout.columnSpan: 2 font.family: GameConstants.numberFont.name font.pixelSize: 16 } Label { + id: notificationLabel Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter - text: control.notificationText - visible: control.notificationVisible + text: GameState.notificationText + visible: GameState.displayNotification font.pixelSize: 13 font.bold: true Layout.columnSpan: 2 @@ -78,9 +52,10 @@ Popup { } Label { + id: recordLabel Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter text: qsTr("New record saved") - visible: control.newRecordVisible + visible: GameState.displayNewRecord Layout.columnSpan: 2 font.pixelSize: 13 } @@ -90,10 +65,11 @@ Popup { text: qsTr("Retry") Layout.fillWidth: true Layout.preferredWidth: control.buttonWidth - onClicked: { - control.visible = false - control.notificationVisible = false + onClicked: { control.grid.initGame() + GameState.displayNewRecord = false + GameState.displayNotification = false + GameState.displayPostGame = false } } @@ -103,8 +79,9 @@ Popup { Layout.fillWidth: true Layout.preferredWidth: control.buttonWidth onClicked: { - control.visible = false - control.notificationVisible = false + GameState.displayNewRecord = false + GameState.displayNotification = false + GameState.displayPostGame = false } } } diff --git a/qml/SaveWindow.qml b/qml/SavePopup.qml similarity index 100% rename from qml/SaveWindow.qml rename to qml/SavePopup.qml diff --git a/qml/SettingsPage.qml b/qml/SettingsWindow.qml similarity index 100% rename from qml/SettingsPage.qml rename to qml/SettingsWindow.qml diff --git a/qml/WelcomePage.qml b/qml/WelcomePopup.qml similarity index 100% rename from qml/WelcomePage.qml rename to qml/WelcomePopup.qml