mirror of
https://github.com/RGBCube/serenity
synced 2025-06-02 20:08:12 +00:00
2048: TRY() all the things in serenity_main() :^)
This commit is contained in:
parent
e91cf53e67
commit
d56e4d5145
1 changed files with 34 additions and 31 deletions
|
@ -59,31 +59,31 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
window->set_title("2048");
|
window->set_title("2048");
|
||||||
window->resize(315, 336);
|
window->resize(315, 336);
|
||||||
|
|
||||||
auto& main_widget = window->set_main_widget<GUI::Widget>();
|
auto main_widget = TRY(window->try_set_main_widget<GUI::Widget>());
|
||||||
main_widget.set_layout<GUI::VerticalBoxLayout>();
|
TRY(main_widget->try_set_layout<GUI::VerticalBoxLayout>());
|
||||||
main_widget.set_fill_with_background_color(true);
|
main_widget->set_fill_with_background_color(true);
|
||||||
|
|
||||||
Game game { board_size, target_tile, evil_ai };
|
Game game { board_size, target_tile, evil_ai };
|
||||||
|
|
||||||
auto& board_view = main_widget.add<BoardView>(&game.board());
|
auto board_view = TRY(main_widget->try_add<BoardView>(&game.board()));
|
||||||
board_view.set_focus(true);
|
board_view->set_focus(true);
|
||||||
auto& statusbar = main_widget.add<GUI::Statusbar>();
|
auto statusbar = TRY(main_widget->try_add<GUI::Statusbar>());
|
||||||
|
|
||||||
app->on_action_enter = [&](GUI::Action& action) {
|
app->on_action_enter = [&](GUI::Action& action) {
|
||||||
auto text = action.status_tip();
|
auto text = action.status_tip();
|
||||||
if (text.is_empty())
|
if (text.is_empty())
|
||||||
text = Gfx::parse_ampersand_string(action.text());
|
text = Gfx::parse_ampersand_string(action.text());
|
||||||
statusbar.set_override_text(move(text));
|
statusbar->set_override_text(move(text));
|
||||||
};
|
};
|
||||||
|
|
||||||
app->on_action_leave = [&](GUI::Action&) {
|
app->on_action_leave = [&](GUI::Action&) {
|
||||||
statusbar.set_override_text({});
|
statusbar->set_override_text({});
|
||||||
};
|
};
|
||||||
|
|
||||||
auto update = [&]() {
|
auto update = [&]() {
|
||||||
board_view.set_board(&game.board());
|
board_view->set_board(&game.board());
|
||||||
board_view.update();
|
board_view->update();
|
||||||
statusbar.set_text(String::formatted("Score: {}", game.score()));
|
statusbar->set_text(String::formatted("Score: {}", game.score()));
|
||||||
};
|
};
|
||||||
|
|
||||||
update();
|
update();
|
||||||
|
@ -120,14 +120,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
game = Game(board_size, target_tile, evil_ai);
|
game = Game(board_size, target_tile, evil_ai);
|
||||||
|
|
||||||
// This ensures that the sizes are correct.
|
// This ensures that the sizes are correct.
|
||||||
board_view.set_board(nullptr);
|
board_view->set_board(nullptr);
|
||||||
board_view.set_board(&game.board());
|
board_view->set_board(&game.board());
|
||||||
|
|
||||||
update();
|
update();
|
||||||
window->update();
|
window->update();
|
||||||
};
|
};
|
||||||
|
|
||||||
board_view.on_move = [&](Game::Direction direction) {
|
board_view->on_move = [&](Game::Direction direction) {
|
||||||
undo_stack.append(game);
|
undo_stack.append(game);
|
||||||
auto outcome = game.attempt_move(direction);
|
auto outcome = game.attempt_move(direction);
|
||||||
switch (outcome) {
|
switch (outcome) {
|
||||||
|
@ -167,37 +167,40 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
auto& game_menu = window->add_menu("&Game");
|
auto game_menu = TRY(window->try_add_menu("&Game"));
|
||||||
|
|
||||||
game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
|
TRY(game_menu->try_add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
|
||||||
start_a_new_game();
|
start_a_new_game();
|
||||||
}));
|
})));
|
||||||
|
|
||||||
game_menu.add_action(GUI::CommonActions::make_undo_action([&](auto&) {
|
TRY(game_menu->try_add_action(GUI::CommonActions::make_undo_action([&](auto&) {
|
||||||
if (undo_stack.is_empty())
|
if (undo_stack.is_empty())
|
||||||
return;
|
return;
|
||||||
redo_stack.append(game);
|
redo_stack.append(game);
|
||||||
game = undo_stack.take_last();
|
game = undo_stack.take_last();
|
||||||
update();
|
update();
|
||||||
}));
|
})));
|
||||||
game_menu.add_action(GUI::CommonActions::make_redo_action([&](auto&) {
|
|
||||||
|
TRY(game_menu->try_add_action(GUI::CommonActions::make_redo_action([&](auto&) {
|
||||||
if (redo_stack.is_empty())
|
if (redo_stack.is_empty())
|
||||||
return;
|
return;
|
||||||
undo_stack.append(game);
|
undo_stack.append(game);
|
||||||
game = redo_stack.take_last();
|
game = redo_stack.take_last();
|
||||||
update();
|
update();
|
||||||
}));
|
})));
|
||||||
game_menu.add_separator();
|
|
||||||
game_menu.add_action(GUI::Action::create("&Settings...", [&](auto&) {
|
|
||||||
change_settings();
|
|
||||||
}));
|
|
||||||
game_menu.add_separator();
|
|
||||||
game_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
|
||||||
GUI::Application::the()->quit();
|
|
||||||
}));
|
|
||||||
|
|
||||||
auto& help_menu = window->add_menu("&Help");
|
TRY(game_menu->try_add_separator());
|
||||||
help_menu.add_action(GUI::CommonActions::make_about_action("2048", app_icon, window));
|
TRY(game_menu->try_add_action(GUI::Action::create("&Settings...", [&](auto&) {
|
||||||
|
change_settings();
|
||||||
|
})));
|
||||||
|
|
||||||
|
TRY(game_menu->try_add_separator());
|
||||||
|
TRY(game_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
||||||
|
GUI::Application::the()->quit();
|
||||||
|
})));
|
||||||
|
|
||||||
|
auto help_menu = TRY(window->try_add_menu("&Help"));
|
||||||
|
TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("2048", app_icon, window)));
|
||||||
|
|
||||||
window->show();
|
window->show();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue