From 740beea5ce3b23b46472b3d2dd00686f72922545 Mon Sep 17 00:00:00 2001 From: Martin Frederic Date: Sat, 2 Apr 2022 16:52:58 +0200 Subject: [PATCH] Pong: Add 'New Game' action This declares Game::reset() public and lets the menu action invoke it. --- Userland/Games/Pong/Game.h | 3 ++- Userland/Games/Pong/main.cpp | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Userland/Games/Pong/Game.h b/Userland/Games/Pong/Game.h index ef7a793688c..ebde7607244 100644 --- a/Userland/Games/Pong/Game.h +++ b/Userland/Games/Pong/Game.h @@ -28,6 +28,8 @@ public: virtual ~Game() override = default; + void reset(); + private: Game(); @@ -37,7 +39,6 @@ private: virtual void timer_event(Core::TimerEvent&) override; virtual void track_mouse_move(Gfx::IntPoint const&) override; - void reset(); void reset_ball(int serve_to_player); void reset_paddles(); void tick(); diff --git a/Userland/Games/Pong/main.cpp b/Userland/Games/Pong/main.cpp index fb3accfc3fb..fc9fd1c8e4a 100644 --- a/Userland/Games/Pong/main.cpp +++ b/Userland/Games/Pong/main.cpp @@ -37,10 +37,17 @@ ErrorOr serenity_main(Main::Arguments arguments) window->set_icon(app_icon.bitmap_for_size(16)); window->set_title("Pong"); window->set_double_buffering_enabled(false); - (void)TRY(window->try_set_main_widget()); + auto game = TRY(window->try_set_main_widget()); window->set_resizable(false); auto game_menu = TRY(window->try_add_menu("&Game")); + + TRY(game_menu->try_add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/reload.png")), [&](auto&) { + game->reset(); + }))); + + TRY(game_menu->try_add_separator()); + TRY(game_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) { GUI::Application::the()->quit(); })));