1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-22 18:35:18 +00:00

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.
This commit is contained in:
Timothy Flynn 2021-05-05 14:06:28 -04:00 committed by Andreas Kling
parent ee1a4a06e0
commit a07c178a02
4 changed files with 37 additions and 1 deletions

View file

@ -76,6 +76,9 @@ void Game::start_game_over_animation()
m_game_over_animation = true; m_game_over_animation = true;
start_timer(s_timer_interval_ms); start_timer(s_timer_interval_ms);
if (on_game_end)
on_game_end();
} }
void Game::stop_game_over_animation() void Game::stop_game_over_animation()
@ -93,6 +96,9 @@ void Game::setup()
{ {
stop_game_over_animation(); stop_game_over_animation();
if (on_game_end)
on_game_end();
for (auto& stack : m_stacks) for (auto& stack : m_stacks)
stack.clear(); stack.clear();
@ -370,6 +376,9 @@ void Game::paint_event(GUI::PaintEvent& event)
stack(Stock).push(m_new_deck.take_last()); stack(Stock).push(m_new_deck.take_last());
m_new_game_animation = false; m_new_game_animation = false;
stop_timer(); stop_timer();
if (on_game_start)
on_game_start();
} }
} }
} }

View file

@ -22,6 +22,8 @@ public:
void setup(); void setup();
Function<void(uint32_t)> on_score_update; Function<void(uint32_t)> on_score_update;
Function<void()> on_game_start;
Function<void()> on_game_end;
private: private:
Game(); Game();

View file

@ -12,5 +12,6 @@
@GUI::Statusbar { @GUI::Statusbar {
name: "statusbar" name: "statusbar"
label_count: 2
} }
} }

View file

@ -6,6 +6,7 @@
#include "Game.h" #include "Game.h"
#include <Games/Solitaire/SolitaireGML.h> #include <Games/Solitaire/SolitaireGML.h>
#include <LibCore/Timer.h>
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/Icon.h> #include <LibGUI/Icon.h>
@ -46,6 +47,8 @@ int main(int argc, char** argv)
game.set_focus(true); game.set_focus(true);
auto& statusbar = *widget.find_descendant_of_type_named<GUI::Statusbar>("statusbar"); auto& statusbar = *widget.find_descendant_of_type_named<GUI::Statusbar>("statusbar");
statusbar.set_text(0, "Score: 0");
statusbar.set_text(1, "Time: 00:00:00");
app->on_action_enter = [&](GUI::Action& action) { app->on_action_enter = [&](GUI::Action& action) {
auto text = action.status_tip(); auto text = action.status_tip();
@ -59,7 +62,28 @@ int main(int argc, char** argv)
}; };
game.on_score_update = [&](uint32_t score) { 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(); auto menubar = GUI::Menubar::construct();