From a07c178a0252201212308d90ca8fbbca010246ab Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 5 May 2021 14:06:28 -0400 Subject: [PATCH] Solitaire: Add statusbar segment to display elapsed time The timer begins after the new-game animation ends, and stops when either the game-over animation begins or the new-game animation is started again. --- Userland/Games/Solitaire/Game.cpp | 9 +++++++++ Userland/Games/Solitaire/Game.h | 2 ++ Userland/Games/Solitaire/Solitaire.gml | 1 + Userland/Games/Solitaire/main.cpp | 26 +++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Userland/Games/Solitaire/Game.cpp b/Userland/Games/Solitaire/Game.cpp index 152935a50b..c6bbeab553 100644 --- a/Userland/Games/Solitaire/Game.cpp +++ b/Userland/Games/Solitaire/Game.cpp @@ -76,6 +76,9 @@ void Game::start_game_over_animation() m_game_over_animation = true; start_timer(s_timer_interval_ms); + + if (on_game_end) + on_game_end(); } void Game::stop_game_over_animation() @@ -93,6 +96,9 @@ void Game::setup() { stop_game_over_animation(); + if (on_game_end) + on_game_end(); + for (auto& stack : m_stacks) stack.clear(); @@ -370,6 +376,9 @@ void Game::paint_event(GUI::PaintEvent& event) stack(Stock).push(m_new_deck.take_last()); m_new_game_animation = false; stop_timer(); + + if (on_game_start) + on_game_start(); } } } diff --git a/Userland/Games/Solitaire/Game.h b/Userland/Games/Solitaire/Game.h index 97167dbbcd..0a4902154f 100644 --- a/Userland/Games/Solitaire/Game.h +++ b/Userland/Games/Solitaire/Game.h @@ -22,6 +22,8 @@ public: void setup(); Function on_score_update; + Function on_game_start; + Function on_game_end; private: Game(); diff --git a/Userland/Games/Solitaire/Solitaire.gml b/Userland/Games/Solitaire/Solitaire.gml index bd9ace9971..18361997c8 100644 --- a/Userland/Games/Solitaire/Solitaire.gml +++ b/Userland/Games/Solitaire/Solitaire.gml @@ -12,5 +12,6 @@ @GUI::Statusbar { name: "statusbar" + label_count: 2 } } diff --git a/Userland/Games/Solitaire/main.cpp b/Userland/Games/Solitaire/main.cpp index b6c0610537..76d3679844 100644 --- a/Userland/Games/Solitaire/main.cpp +++ b/Userland/Games/Solitaire/main.cpp @@ -6,6 +6,7 @@ #include "Game.h" #include +#include #include #include #include @@ -46,6 +47,8 @@ int main(int argc, char** argv) game.set_focus(true); auto& statusbar = *widget.find_descendant_of_type_named("statusbar"); + statusbar.set_text(0, "Score: 0"); + statusbar.set_text(1, "Time: 00:00:00"); app->on_action_enter = [&](GUI::Action& action) { auto text = action.status_tip(); @@ -59,7 +62,28 @@ int main(int argc, char** argv) }; game.on_score_update = [&](uint32_t score) { - statusbar.set_text(String::formatted("Score: {}", score)); + statusbar.set_text(0, String::formatted("Score: {}", score)); + }; + + uint64_t seconds_elapsed = 0; + + auto timer = Core::Timer::create_repeating(1000, [&]() { + ++seconds_elapsed; + + uint64_t hours = seconds_elapsed / 3600; + uint64_t minutes = (seconds_elapsed / 60) % 60; + uint64_t seconds = seconds_elapsed % 60; + + statusbar.set_text(1, String::formatted("Time: {:02}:{:02}:{:02}", hours, minutes, seconds)); + }); + + game.on_game_start = [&]() { + seconds_elapsed = 0; + timer->start(); + }; + game.on_game_end = [&]() { + if (timer->is_active()) + timer->stop(); }; auto menubar = GUI::Menubar::construct();