summaryrefslogtreecommitdiff
path: root/Userland/Games
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-07-21 21:21:03 +0200
committerAndreas Kling <kling@serenityos.org>2021-07-21 21:24:26 +0200
commit687a12d7fb480cf3586c97e30cdb4f67d7e72da5 (patch)
tree18075eda4d084c161e935c4d386f6b9f6790a9d1 /Userland/Games
parenta4fdb7f0296379affbf1083a7875d40e1624a067 (diff)
downloadserenity-687a12d7fb480cf3586c97e30cdb4f67d7e72da5.zip
Userland: Add GUI::Window::add_menu() and use it everywhere
Applications previously had to create a GUI::Menubar object, add menus to it, and then call GUI::Window::set_menubar(). This patch introduces GUI::Window::add_menu() which creates the menubar automatically and adds items to it. Application code becomes slightly simpler as a result. :^)
Diffstat (limited to 'Userland/Games')
-rw-r--r--Userland/Games/2048/main.cpp8
-rw-r--r--Userland/Games/Breakout/main.cpp8
-rw-r--r--Userland/Games/Chess/main.cpp11
-rw-r--r--Userland/Games/FlappyBug/main.cpp7
-rw-r--r--Userland/Games/GameOfLife/main.cpp7
-rw-r--r--Userland/Games/Hearts/main.cpp6
-rw-r--r--Userland/Games/Minesweeper/main.cpp10
-rw-r--r--Userland/Games/Pong/main.cpp8
-rw-r--r--Userland/Games/Snake/main.cpp8
-rw-r--r--Userland/Games/Solitaire/main.cpp6
-rw-r--r--Userland/Games/Spider/main.cpp7
11 files changed, 25 insertions, 61 deletions
diff --git a/Userland/Games/2048/main.cpp b/Userland/Games/2048/main.cpp
index 9ba195712b..22a5d5fd94 100644
--- a/Userland/Games/2048/main.cpp
+++ b/Userland/Games/2048/main.cpp
@@ -181,9 +181,7 @@ int main(int argc, char** argv)
}
};
- auto menubar = GUI::Menubar::construct();
-
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
start_a_new_game();
@@ -212,11 +210,9 @@ int main(int argc, char** argv)
GUI::Application::the()->quit();
}));
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("2048", app_icon, window));
- window->set_menubar(move(menubar));
-
window->show();
window->set_icon(app_icon.bitmap_for_size(16));
diff --git a/Userland/Games/Breakout/main.cpp b/Userland/Games/Breakout/main.cpp
index cd1f6cd6f8..f65882a687 100644
--- a/Userland/Games/Breakout/main.cpp
+++ b/Userland/Games/Breakout/main.cpp
@@ -47,9 +47,7 @@ int main(int argc, char** argv)
auto& game = window->set_main_widget<Breakout::Game>();
window->show();
- auto menubar = GUI::Menubar::construct();
-
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::Action::create_checkable("&Pause", { {}, Key_P }, [&](auto& action) {
game.set_paused(action.is_checked());
}));
@@ -60,10 +58,8 @@ int main(int argc, char** argv)
GUI::Application::the()->quit();
}));
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Breakout", app_icon, window));
- window->set_menubar(move(menubar));
-
return app->exec();
}
diff --git a/Userland/Games/Chess/main.cpp b/Userland/Games/Chess/main.cpp
index c72ca51a03..e4679f75ab 100644
--- a/Userland/Games/Chess/main.cpp
+++ b/Userland/Games/Chess/main.cpp
@@ -76,8 +76,7 @@ int main(int argc, char** argv)
widget.set_coordinates(config->read_bool_entry("Style", "Coordinates", true));
widget.set_show_available_moves(config->read_bool_entry("Style", "ShowAvailableMoves", true));
- auto menubar = GUI::Menubar::construct();
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::Action::create("&Resign", { Mod_None, Key_F3 }, [&](auto&) {
widget.resign();
@@ -131,7 +130,7 @@ int main(int argc, char** argv)
GUI::Application::the()->quit();
}));
- auto& style_menu = menubar->add_menu("&Style");
+ auto& style_menu = window->add_menu("&Style");
GUI::ActionGroup piece_set_action_group;
piece_set_action_group.set_exclusive(true);
auto& piece_set_menu = style_menu.add_submenu("&Piece Set");
@@ -189,7 +188,7 @@ int main(int argc, char** argv)
show_available_moves_action->set_checked(widget.show_available_moves());
style_menu.add_action(show_available_moves_action);
- auto& engine_menu = menubar->add_menu("&Engine");
+ auto& engine_menu = window->add_menu("&Engine");
GUI::ActionGroup engines_action_group;
engines_action_group.set_exclusive(true);
@@ -210,11 +209,9 @@ int main(int argc, char** argv)
engine_submenu.add_action(*action);
}
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Chess", app_icon, window));
- window->set_menubar(move(menubar));
-
window->show();
widget.reset();
diff --git a/Userland/Games/FlappyBug/main.cpp b/Userland/Games/FlappyBug/main.cpp
index d1135b5c3f..a8209337c8 100644
--- a/Userland/Games/FlappyBug/main.cpp
+++ b/Userland/Games/FlappyBug/main.cpp
@@ -68,17 +68,14 @@ int main(int argc, char** argv)
return high_score;
};
- auto menubar = GUI::Menubar::construct();
-
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
GUI::Application::the()->quit();
}));
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Flappy Bug", app_icon, window));
- window->set_menubar(move(menubar));
window->show();
return app->exec();
diff --git a/Userland/Games/GameOfLife/main.cpp b/Userland/Games/GameOfLife/main.cpp
index 4041ab1967..e24660a70d 100644
--- a/Userland/Games/GameOfLife/main.cpp
+++ b/Userland/Games/GameOfLife/main.cpp
@@ -110,8 +110,7 @@ int main(int argc, char** argv)
});
main_toolbar.add_action(rotate_pattern_action);
- auto menubar = GUI::Menubar::construct();
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(clear_board_action);
game_menu.add_action(randomize_cells_action);
@@ -123,11 +122,9 @@ int main(int argc, char** argv)
GUI::Application::the()->quit();
}));
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Game Of Life", app_icon, window));
- window->set_menubar(move(menubar));
-
board_widget.on_running_state_change = [&]() {
if (board_widget.is_running()) {
statusbar.set_text("Running...");
diff --git a/Userland/Games/Hearts/main.cpp b/Userland/Games/Hearts/main.cpp
index 4744ca2691..8a020d4b69 100644
--- a/Userland/Games/Hearts/main.cpp
+++ b/Userland/Games/Hearts/main.cpp
@@ -94,8 +94,7 @@ int main(int argc, char** argv)
GUI::MessageBox::show(window, "Settings have been successfully saved and will take effect in the next game.", "Settings Changed Successfully", GUI::MessageBox::Type::Information);
};
- auto menubar = GUI::Menubar::construct();
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
game.setup(player_name);
@@ -107,12 +106,11 @@ int main(int argc, char** argv)
game_menu.add_separator();
game_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Hearts", app_icon, window));
window->set_resizable(false);
window->resize(Hearts::Game::width, Hearts::Game::height + statusbar.max_height());
- window->set_menubar(move(menubar));
window->set_icon(app_icon.bitmap_for_size(16));
window->show();
game.setup(player_name);
diff --git a/Userland/Games/Minesweeper/main.cpp b/Userland/Games/Minesweeper/main.cpp
index 9a4a70c4e2..2df11e6c61 100644
--- a/Userland/Games/Minesweeper/main.cpp
+++ b/Userland/Games/Minesweeper/main.cpp
@@ -103,9 +103,7 @@ int main(int argc, char** argv)
window->resize(size);
});
- auto menubar = GUI::Menubar::construct();
-
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
field.reset();
@@ -125,7 +123,7 @@ int main(int argc, char** argv)
GUI::Application::the()->quit();
}));
- auto& difficulty_menu = menubar->add_menu("&Difficulty");
+ auto& difficulty_menu = window->add_menu("&Difficulty");
difficulty_menu.add_action(GUI::Action::create("&Beginner", { Mod_Ctrl, Key_B }, [&](auto&) {
field.set_field_size(9, 9, 10);
}));
@@ -139,11 +137,9 @@ int main(int argc, char** argv)
field.set_field_size(32, 60, 350);
}));
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Minesweeper", app_icon, window));
- window->set_menubar(move(menubar));
-
window->show();
window->set_icon(app_icon.bitmap_for_size(16));
diff --git a/Userland/Games/Pong/main.cpp b/Userland/Games/Pong/main.cpp
index 7ad75f83b6..5c2a8d5095 100644
--- a/Userland/Games/Pong/main.cpp
+++ b/Userland/Games/Pong/main.cpp
@@ -55,17 +55,13 @@ int main(int argc, char** argv)
window->set_resizable(false);
window->show();
- auto menubar = GUI::Menubar::construct();
-
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
GUI::Application::the()->quit();
}));
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Pong", app_icon, window));
- window->set_menubar(move(menubar));
-
return app->exec();
}
diff --git a/Userland/Games/Snake/main.cpp b/Userland/Games/Snake/main.cpp
index c4e0a60089..1ddae48033 100644
--- a/Userland/Games/Snake/main.cpp
+++ b/Userland/Games/Snake/main.cpp
@@ -58,9 +58,7 @@ int main(int argc, char** argv)
auto& game = window->set_main_widget<SnakeGame>();
- auto menubar = GUI::Menubar::construct();
-
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
game.reset();
@@ -70,11 +68,9 @@ int main(int argc, char** argv)
GUI::Application::the()->quit();
}));
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Snake", app_icon, window));
- window->set_menubar(move(menubar));
-
window->show();
window->set_icon(app_icon.bitmap_for_size(16));
diff --git a/Userland/Games/Solitaire/main.cpp b/Userland/Games/Solitaire/main.cpp
index a64963369b..c879c225ed 100644
--- a/Userland/Games/Solitaire/main.cpp
+++ b/Userland/Games/Solitaire/main.cpp
@@ -171,8 +171,7 @@ int main(int argc, char** argv)
three_card_draw_action->set_status_tip("Draw three cards at a time");
draw_setting_actions.add_action(three_card_draw_action);
- auto menubar = GUI::Menubar::construct();
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
game.setup(mode);
@@ -189,12 +188,11 @@ int main(int argc, char** argv)
game_menu.add_separator();
game_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Solitaire", app_icon, window));
window->set_resizable(false);
window->resize(Solitaire::Game::width, Solitaire::Game::height + statusbar.max_height());
- window->set_menubar(move(menubar));
window->set_icon(app_icon.bitmap_for_size(16));
window->show();
diff --git a/Userland/Games/Spider/main.cpp b/Userland/Games/Spider/main.cpp
index 1914ee4f23..aa80c7e5fa 100644
--- a/Userland/Games/Spider/main.cpp
+++ b/Userland/Games/Spider/main.cpp
@@ -163,9 +163,7 @@ int main(int argc, char** argv)
two_suit_action->set_checked(mode == Spider::Mode::TwoSuit);
suit_actions.add_action(two_suit_action);
- auto menubar = GUI::Menubar::construct();
-
- auto& game_menu = menubar->add_menu("&Game");
+ auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
game.setup(mode);
}));
@@ -175,12 +173,11 @@ int main(int argc, char** argv)
game_menu.add_separator();
game_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
- auto& help_menu = menubar->add_menu("&Help");
+ auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Spider", app_icon, window));
window->set_resizable(false);
window->resize(Spider::Game::width, Spider::Game::height + statusbar.max_height());
- window->set_menubar(move(menubar));
window->set_icon(app_icon.bitmap_for_size(16));
window->show();