mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:42:44 +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:
		
							parent
							
								
									ee1a4a06e0
								
							
						
					
					
						commit
						a07c178a02
					
				
					 4 changed files with 37 additions and 1 deletions
				
			
		|  | @ -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(); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  |  | |||
|  | @ -22,6 +22,8 @@ public: | |||
|     void setup(); | ||||
| 
 | ||||
|     Function<void(uint32_t)> on_score_update; | ||||
|     Function<void()> on_game_start; | ||||
|     Function<void()> on_game_end; | ||||
| 
 | ||||
| private: | ||||
|     Game(); | ||||
|  |  | |||
|  | @ -12,5 +12,6 @@ | |||
| 
 | ||||
|     @GUI::Statusbar { | ||||
|         name: "statusbar" | ||||
|         label_count: 2 | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -6,6 +6,7 @@ | |||
| 
 | ||||
| #include "Game.h" | ||||
| #include <Games/Solitaire/SolitaireGML.h> | ||||
| #include <LibCore/Timer.h> | ||||
| #include <LibGUI/Action.h> | ||||
| #include <LibGUI/Application.h> | ||||
| #include <LibGUI/Icon.h> | ||||
|  | @ -46,6 +47,8 @@ int main(int argc, char** argv) | |||
|     game.set_focus(true); | ||||
| 
 | ||||
|     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) { | ||||
|         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(); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Timothy Flynn
						Timothy Flynn