From 5d77daebf3afac9437f5afb13fb64eb0a654aa5b Mon Sep 17 00:00:00 2001 From: implicitfield <114500360+implicitfield@users.noreply.github.com> Date: Mon, 17 Oct 2022 20:03:09 +0300 Subject: [PATCH] 2048: Convert to GML --- Userland/Games/2048/CMakeLists.txt | 8 +++ Userland/Games/2048/GameSizeDialog.cpp | 63 +++++++++-------------- Userland/Games/2048/GameSizeDialog.gml | 70 ++++++++++++++++++++++++++ Userland/Games/2048/GameWindow.gml | 17 +++++++ Userland/Games/2048/main.cpp | 11 ++-- 5 files changed, 125 insertions(+), 44 deletions(-) create mode 100644 Userland/Games/2048/GameSizeDialog.gml create mode 100644 Userland/Games/2048/GameWindow.gml diff --git a/Userland/Games/2048/CMakeLists.txt b/Userland/Games/2048/CMakeLists.txt index b92d95ed1f..d710503bb5 100644 --- a/Userland/Games/2048/CMakeLists.txt +++ b/Userland/Games/2048/CMakeLists.txt @@ -4,6 +4,9 @@ serenity_component( TARGETS 2048 ) +compile_gml(GameSizeDialog.gml GameSizeDialogGML.h game_size_dialog_gml) +compile_gml(GameWindow.gml GameWindowGML.h game_window_gml) + set(SOURCES BoardView.cpp Game.cpp @@ -11,5 +14,10 @@ set(SOURCES main.cpp ) +set(GENERATED_SOURCES + GameSizeDialogGML.h + GameWindowGML.h +) + serenity_app(2048 ICON app-2048) target_link_libraries(2048 LibConfig LibGUI LibMain LibDesktop) diff --git a/Userland/Games/2048/GameSizeDialog.cpp b/Userland/Games/2048/GameSizeDialog.cpp index 0f925ccbab..eb920c03a0 100644 --- a/Userland/Games/2048/GameSizeDialog.cpp +++ b/Userland/Games/2048/GameSizeDialog.cpp @@ -12,6 +12,7 @@ #include #include #include +#include GameSizeDialog::GameSizeDialog(GUI::Window* parent, size_t board_size, size_t target, bool evil_ai) : GUI::Dialog(parent) @@ -25,59 +26,43 @@ GameSizeDialog::GameSizeDialog(GUI::Window* parent, size_t board_size, size_t ta set_resizable(false); auto& main_widget = set_main_widget(); - main_widget.set_fill_with_background_color(true); + if (!main_widget.load_from_gml(game_size_dialog_gml)) + VERIFY_NOT_REACHED(); - auto& layout = main_widget.set_layout(); - layout.set_margins(4); + auto board_size_spinbox = main_widget.find_descendant_of_type_named("board_size_spinbox"); + board_size_spinbox->set_value(m_board_size); - auto& board_size_box = main_widget.add(); - auto& input_layout = board_size_box.set_layout(); - input_layout.set_spacing(4); + auto tile_value_label = main_widget.find_descendant_of_type_named("tile_value_label"); + tile_value_label->set_text(String::number(target_tile())); + auto target_spinbox = main_widget.find_descendant_of_type_named("target_spinbox"); + target_spinbox->set_max(Game::max_power_for_board(m_board_size)); + target_spinbox->set_value(m_target_tile_power); - board_size_box.add("Board size").set_text_alignment(Gfx::TextAlignment::CenterLeft); - auto& spinbox = board_size_box.add(); - - auto& target_box = main_widget.add(); - auto& target_layout = target_box.set_layout(); - target_layout.set_spacing(4); - spinbox.set_min(2); - spinbox.set_value(m_board_size); - - target_box.add("Target tile").set_text_alignment(Gfx::TextAlignment::CenterLeft); - auto& tile_value_label = target_box.add(String::number(target_tile())); - tile_value_label.set_text_alignment(Gfx::TextAlignment::CenterRight); - auto& target_spinbox = target_box.add(); - target_spinbox.set_max(Game::max_power_for_board(m_board_size)); - target_spinbox.set_min(3); - target_spinbox.set_value(m_target_tile_power); - - spinbox.on_change = [&](auto value) { + board_size_spinbox->on_change = [this, target_spinbox](auto value) { m_board_size = value; - target_spinbox.set_max(Game::max_power_for_board(m_board_size)); + target_spinbox->set_max(Game::max_power_for_board(m_board_size)); }; - target_spinbox.on_change = [&](auto value) { + target_spinbox->on_change = [this, tile_value_label](auto value) { m_target_tile_power = value; - tile_value_label.set_text(String::number(target_tile())); + tile_value_label->set_text(String::number(target_tile())); }; - auto& evil_ai_checkbox = main_widget.add("Evil AI"); - evil_ai_checkbox.set_checked(m_evil_ai); - evil_ai_checkbox.on_checked = [this](auto checked) { m_evil_ai = checked; }; + auto evil_ai_checkbox = main_widget.find_descendant_of_type_named("evil_ai_checkbox"); + evil_ai_checkbox->set_checked(m_evil_ai); + evil_ai_checkbox->on_checked = [this](auto checked) { m_evil_ai = checked; }; - auto& temp_checkbox = main_widget.add("Temporarily apply changes"); - temp_checkbox.set_checked(m_temporary); - temp_checkbox.on_checked = [this](auto checked) { m_temporary = checked; }; + auto temporary_checkbox = main_widget.find_descendant_of_type_named("temporary_checkbox"); + temporary_checkbox->set_checked(m_temporary); + temporary_checkbox->on_checked = [this](auto checked) { m_temporary = checked; }; - auto& buttonbox = main_widget.add(); - auto& button_layout = buttonbox.set_layout(); - button_layout.set_spacing(10); - - buttonbox.add("Cancel").on_click = [this](auto) { + auto cancel_button = main_widget.find_descendant_of_type_named("cancel_button"); + cancel_button->on_click = [this](auto) { done(ExecResult::Cancel); }; - buttonbox.add("OK").on_click = [this](auto) { + auto ok_button = main_widget.find_descendant_of_type_named("ok_button"); + ok_button->on_click = [this](auto) { done(ExecResult::OK); }; } diff --git a/Userland/Games/2048/GameSizeDialog.gml b/Userland/Games/2048/GameSizeDialog.gml new file mode 100644 index 0000000000..0ca9494915 --- /dev/null +++ b/Userland/Games/2048/GameSizeDialog.gml @@ -0,0 +1,70 @@ +@GUI::Frame { + fill_with_background_color: true + layout: @GUI::VerticalBoxLayout { + margins: [4] + } + + @GUI::Widget { + layout: @GUI::HorizontalBoxLayout { + spacing: 4 + } + + @GUI::Label { + text: "Board size" + text_alignment: "CenterLeft" + } + + @GUI::SpinBox { + name: "board_size_spinbox" + max: 100 + min: 2 + } + } + + @GUI::Widget { + layout: @GUI::HorizontalBoxLayout { + spacing: 4 + } + + @GUI::Label { + text: "Target tile" + text_alignment: "CenterLeft" + } + + @GUI::Label { + name: "tile_value_label" + text_alignment: "CenterRight" + } + + @GUI::SpinBox { + name: "target_spinbox" + min: 3 + } + } + + @GUI::CheckBox { + name: "evil_ai_checkbox" + text: "Evil AI" + } + + @GUI::CheckBox { + name: "temporary_checkbox" + text: "Temporarily apply changes" + } + + @GUI::Widget { + layout: @GUI::HorizontalBoxLayout { + spacing: 10 + } + + @GUI::Button { + name: "cancel_button" + text: "Cancel" + } + + @GUI::Button { + name: "ok_button" + text: "OK" + } + } +} diff --git a/Userland/Games/2048/GameWindow.gml b/Userland/Games/2048/GameWindow.gml new file mode 100644 index 0000000000..a928ebbb5d --- /dev/null +++ b/Userland/Games/2048/GameWindow.gml @@ -0,0 +1,17 @@ +@GUI::Frame { + fill_with_background_color: true + layout: @GUI::VerticalBoxLayout {} + + @GUI::Widget { + layout: @GUI::VerticalBoxLayout {} + + @GUI::Widget { + name: "board_view_container" + layout: @GUI::VerticalBoxLayout {} + } + + @GUI::Statusbar { + name: "statusbar" + } + } +} diff --git a/Userland/Games/2048/main.cpp b/Userland/Games/2048/main.cpp index 239aafe99f..db3c35b3e4 100644 --- a/Userland/Games/2048/main.cpp +++ b/Userland/Games/2048/main.cpp @@ -8,6 +8,7 @@ #include "Game.h" #include "GameSizeDialog.h" #include +#include #include #include #include @@ -65,15 +66,15 @@ ErrorOr serenity_main(Main::Arguments arguments) window->set_title("2048"); window->resize(315, 336); - auto main_widget = TRY(window->try_set_main_widget()); - (void)TRY(main_widget->try_set_layout()); - main_widget->set_fill_with_background_color(true); + auto& main_widget = window->set_main_widget(); + if (!main_widget.load_from_gml(game_window_gml)) + VERIFY_NOT_REACHED(); Game game { board_size, target_tile, evil_ai }; - auto board_view = TRY(main_widget->try_add(&game.board())); + auto board_view = TRY(main_widget.find_descendant_of_type_named("board_view_container")->try_add(&game.board())); board_view->set_focus(true); - auto statusbar = TRY(main_widget->try_add()); + auto statusbar = main_widget.find_descendant_of_type_named("statusbar"); app->on_action_enter = [&](GUI::Action& action) { auto text = action.status_tip();