1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:07:34 +00:00

2048: Convert to GML

This commit is contained in:
implicitfield 2022-10-17 20:03:09 +03:00 committed by Linus Groh
parent a6ba82fc49
commit 5d77daebf3
5 changed files with 125 additions and 44 deletions

View file

@ -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)

View file

@ -12,6 +12,7 @@
#include <LibGUI/CheckBox.h>
#include <LibGUI/Label.h>
#include <LibGUI/SpinBox.h>
#include <Userland/Games/2048/GameSizeDialogGML.h>
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<GUI::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<GUI::VerticalBoxLayout>();
layout.set_margins(4);
auto board_size_spinbox = main_widget.find_descendant_of_type_named<GUI::SpinBox>("board_size_spinbox");
board_size_spinbox->set_value(m_board_size);
auto& board_size_box = main_widget.add<GUI::Widget>();
auto& input_layout = board_size_box.set_layout<GUI::HorizontalBoxLayout>();
input_layout.set_spacing(4);
auto tile_value_label = main_widget.find_descendant_of_type_named<GUI::Label>("tile_value_label");
tile_value_label->set_text(String::number(target_tile()));
auto target_spinbox = main_widget.find_descendant_of_type_named<GUI::SpinBox>("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<GUI::Label>("Board size").set_text_alignment(Gfx::TextAlignment::CenterLeft);
auto& spinbox = board_size_box.add<GUI::SpinBox>();
auto& target_box = main_widget.add<GUI::Widget>();
auto& target_layout = target_box.set_layout<GUI::HorizontalBoxLayout>();
target_layout.set_spacing(4);
spinbox.set_min(2);
spinbox.set_value(m_board_size);
target_box.add<GUI::Label>("Target tile").set_text_alignment(Gfx::TextAlignment::CenterLeft);
auto& tile_value_label = target_box.add<GUI::Label>(String::number(target_tile()));
tile_value_label.set_text_alignment(Gfx::TextAlignment::CenterRight);
auto& target_spinbox = target_box.add<GUI::SpinBox>();
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<GUI::CheckBox>("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<GUI::CheckBox>("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<GUI::CheckBox>("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<GUI::CheckBox>("temporary_checkbox");
temporary_checkbox->set_checked(m_temporary);
temporary_checkbox->on_checked = [this](auto checked) { m_temporary = checked; };
auto& buttonbox = main_widget.add<GUI::Widget>();
auto& button_layout = buttonbox.set_layout<GUI::HorizontalBoxLayout>();
button_layout.set_spacing(10);
buttonbox.add<GUI::Button>("Cancel").on_click = [this](auto) {
auto cancel_button = main_widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
cancel_button->on_click = [this](auto) {
done(ExecResult::Cancel);
};
buttonbox.add<GUI::Button>("OK").on_click = [this](auto) {
auto ok_button = main_widget.find_descendant_of_type_named<GUI::Button>("ok_button");
ok_button->on_click = [this](auto) {
done(ExecResult::OK);
};
}

View file

@ -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"
}
}
}

View file

@ -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"
}
}
}

View file

@ -8,6 +8,7 @@
#include "Game.h"
#include "GameSizeDialog.h"
#include <AK/URL.h>
#include <Games/2048/GameWindowGML.h>
#include <LibConfig/Client.h>
#include <LibCore/System.h>
#include <LibDesktop/Launcher.h>
@ -65,15 +66,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->set_title("2048");
window->resize(315, 336);
auto main_widget = TRY(window->try_set_main_widget<GUI::Widget>());
(void)TRY(main_widget->try_set_layout<GUI::VerticalBoxLayout>());
main_widget->set_fill_with_background_color(true);
auto& main_widget = window->set_main_widget<GUI::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<BoardView>(&game.board()));
auto board_view = TRY(main_widget.find_descendant_of_type_named<GUI::Widget>("board_view_container")->try_add<BoardView>(&game.board()));
board_view->set_focus(true);
auto statusbar = TRY(main_widget->try_add<GUI::Statusbar>());
auto statusbar = main_widget.find_descendant_of_type_named<GUI::Statusbar>("statusbar");
app->on_action_enter = [&](GUI::Action& action) {
auto text = action.status_tip();