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

GamesSettings: Port GamesSettings to GML compilation

Co-Authored-By: Tim Schumacher <timschumi@gmx.de>
This commit is contained in:
tetektoza 2023-09-28 17:06:49 +02:00 committed by Tim Schumacher
parent 935aaab757
commit e26548989a
10 changed files with 281 additions and 233 deletions

View file

@ -4,19 +4,16 @@ serenity_component(
TARGETS GamesSettings
)
stringify_gml(CardSettingsWidget.gml CardSettingsWidgetGML.h card_settings_widget_gml)
stringify_gml(ChessSettingsWidget.gml ChessSettingsWidgetGML.h chess_settings_widget_gml)
compile_gml(CardSettingsWidget.gml CardSettingsWidgetGML.cpp)
compile_gml(ChessSettingsWidget.gml ChessSettingsWidgetGML.cpp)
set(SOURCES
main.cpp
CardSettingsWidget.cpp
CardSettingsWidgetGML.cpp
ChessSettingsWidgetGML.cpp
ChessSettingsWidget.cpp
)
set(GENERATED_SOURCES
CardSettingsWidgetGML.h
ChessSettingsWidgetGML.h
)
serenity_app(GamesSettings ICON games)
target_link_libraries(GamesSettings PRIVATE LibConfig LibCore LibGfx LibGUI LibMain LibCards LibChess)

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibCards/Card.h>
#include <LibCards/CardGame.h>
#include <LibCards/CardPainter.h>
#include <LibCards/CardStack.h>
#include <LibConfig/Client.h>
#include <LibGUI/FileSystemModel.h>
#include <LibGfx/Palette.h>
namespace GamesSettings {
class CardGamePreview final : public Cards::CardGame {
C_OBJECT_ABSTRACT(CardGamePreview)
public:
static ErrorOr<NonnullRefPtr<CardGamePreview>> try_create();
private:
CardGamePreview() = default;
virtual void paint_event(GUI::PaintEvent& event) override;
};
}

View file

@ -5,7 +5,6 @@
*/
#include "CardSettingsWidget.h"
#include <Applications/GamesSettings/CardSettingsWidgetGML.h>
#include <LibCards/Card.h>
#include <LibCards/CardGame.h>
#include <LibCards/CardPainter.h>
@ -21,12 +20,8 @@ namespace GamesSettings {
static constexpr StringView default_card_back_image_path = "/res/graphics/cards/backs/Red.png"sv;
static constexpr StringView default_card_front_image_set = "Classic"sv;
class CardGamePreview final : public Cards::CardGame {
C_OBJECT_ABSTRACT(CardGamePreview)
public:
static ErrorOr<NonnullRefPtr<CardGamePreview>> try_create()
{
ErrorOr<NonnullRefPtr<CardGamePreview>> CardGamePreview::try_create()
{
auto preview = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) CardGamePreview()));
Gfx::IntPoint point { 25, 24 };
@ -51,13 +46,10 @@ public:
preview->stack_at_location(2).set_highlighted(true);
return preview;
}
}
private:
CardGamePreview() = default;
virtual void paint_event(GUI::PaintEvent& event) override
{
void CardGamePreview::paint_event(GUI::PaintEvent& event)
{
Cards::CardGame::paint_event(event);
GUI::Painter painter(*this);
@ -67,20 +59,17 @@ private:
auto background_color = this->background_color();
for (auto& stack : stacks())
stack->paint(painter, background_color);
}
};
}
ErrorOr<NonnullRefPtr<CardSettingsWidget>> CardSettingsWidget::try_create()
ErrorOr<NonnullRefPtr<CardSettingsWidget>> CardSettingsWidget::create()
{
auto card_settings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) CardSettingsWidget));
auto card_settings_widget = TRY(try_create());
TRY(card_settings_widget->initialize());
return card_settings_widget;
}
ErrorOr<void> CardSettingsWidget::initialize()
{
TRY(load_from_gml(card_settings_widget_gml));
auto background_color = Gfx::Color::from_string(Config::read_string("Games"sv, "Cards"sv, "BackgroundColor"sv)).value_or(Gfx::Color::from_rgb(0x008000));
m_preview_frame = find_descendant_of_type_named<CardGamePreview>("cards_preview");
@ -179,5 +168,3 @@ String CardSettingsWidget::card_front_images_set_name() const
}
}
REGISTER_WIDGET(GamesSettings, CardGamePreview);

View file

@ -1,4 +1,4 @@
@GUI::Frame {
@GamesSettings::CardSettingsWidget {
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
margins: [8]
@ -19,7 +19,7 @@
@GUI::ColorInput {
name: "cards_background_color"
has_alpha_channel: false
color_has_alpha_channel: false
}
}
@ -42,7 +42,7 @@
@GUI::ComboBox {
name: "cards_front_image_set"
model_only: true
only_allow_values_from_model: true
}
}

View file

@ -6,6 +6,7 @@
#pragma once
#include "CardGamePreview.h"
#include <LibGUI/ColorInput.h>
#include <LibGUI/ComboBox.h>
#include <LibGUI/Frame.h>
@ -16,12 +17,11 @@
namespace GamesSettings {
class CardGamePreview;
class CardSettingsWidget final : public GUI::SettingsWindow::Tab {
C_OBJECT_ABSTRACT(CardSettingsWidget)
public:
static ErrorOr<NonnullRefPtr<CardSettingsWidget>> try_create();
static ErrorOr<NonnullRefPtr<CardSettingsWidget>> create();
virtual ~CardSettingsWidget() override = default;
virtual void apply_settings() override;

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibChess/Chess.h>
#include <LibConfig/Client.h>
#include <LibCore/Directory.h>
#include <LibGUI/CheckBox.h>
#include <LibGUI/ComboBox.h>
#include <LibGUI/Frame.h>
#include <LibGUI/ItemListModel.h>
#include <LibGUI/Painter.h>
namespace GamesSettings {
class ChessGamePreview final : public GUI::Frame {
C_OBJECT_ABSTRACT(ChessGamePreview)
public:
static ErrorOr<NonnullRefPtr<ChessGamePreview>> try_create();
virtual ~ChessGamePreview() = default;
void set_piece_set_name(String piece_set_name);
void set_dark_square_color(Gfx::Color dark_square_color);
void set_light_square_color(Gfx::Color light_square_color);
void set_show_coordinates(bool show_coordinates);
private:
ChessGamePreview();
virtual void paint_event(GUI::PaintEvent& event) override;
HashMap<Chess::Piece, RefPtr<Gfx::Bitmap>> m_piece_images;
bool m_any_piece_images_are_missing { false };
Gfx::Color m_dark_square_color;
Gfx::Color m_light_square_color;
bool m_show_coordinates { true };
String m_piece_set_name;
};
}

View file

@ -5,7 +5,7 @@
*/
#include "ChessSettingsWidget.h"
#include <Applications/GamesSettings/ChessSettingsWidgetGML.h>
#include "ChessGamePreview.h"
#include <LibChess/Chess.h>
#include <LibConfig/Client.h>
#include <LibCore/Directory.h>
@ -75,20 +75,20 @@ private:
}
};
class ChessGamePreview final : public GUI::Frame {
C_OBJECT_ABSTRACT(ChessGamePreview)
ChessGamePreview::ChessGamePreview()
: m_dark_square_color { s_board_themes[0].dark_square_color }
, m_light_square_color { s_board_themes[0].light_square_color }
{
}
public:
static ErrorOr<NonnullRefPtr<ChessGamePreview>> try_create()
{
ErrorOr<NonnullRefPtr<ChessGamePreview>> ChessGamePreview::try_create()
{
auto preview = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ChessGamePreview()));
return preview;
}
}
virtual ~ChessGamePreview() = default;
void set_piece_set_name(String piece_set_name)
{
void ChessGamePreview::set_piece_set_name(String piece_set_name)
{
if (m_piece_set_name == piece_set_name)
return;
@ -120,40 +120,37 @@ public:
load_piece_image(Chess::Color::Black, Chess::Type::King, "black-king.png"sv);
update();
}
}
void set_dark_square_color(Gfx::Color dark_square_color)
{
void ChessGamePreview::set_dark_square_color(Gfx::Color dark_square_color)
{
if (m_dark_square_color == dark_square_color)
return;
m_dark_square_color = dark_square_color;
update();
}
}
void set_light_square_color(Gfx::Color light_square_color)
{
void ChessGamePreview::set_light_square_color(Gfx::Color light_square_color)
{
if (m_light_square_color == light_square_color)
return;
m_light_square_color = light_square_color;
update();
}
}
void set_show_coordinates(bool show_coordinates)
{
void ChessGamePreview::set_show_coordinates(bool show_coordinates)
{
if (m_show_coordinates == show_coordinates)
return;
m_show_coordinates = show_coordinates;
update();
}
}
private:
ChessGamePreview() = default;
virtual void paint_event(GUI::PaintEvent& event) override
{
void ChessGamePreview::paint_event(GUI::PaintEvent& event)
{
GUI::Frame::paint_event(event);
GUI::Painter painter(*this);
@ -232,28 +229,17 @@ private:
painter.fill_rect(warning_rect, palette().base());
painter.draw_text(warning_rect.shrunken(4, 4), "Warning: This set is missing images for some pieces!"sv, coordinate_font, Gfx::TextAlignment::CenterLeft, palette().base_text());
}
}
}
HashMap<Chess::Piece, RefPtr<Gfx::Bitmap>> m_piece_images;
bool m_any_piece_images_are_missing { false };
Gfx::Color m_dark_square_color { s_board_themes[0].dark_square_color };
Gfx::Color m_light_square_color { s_board_themes[0].light_square_color };
bool m_show_coordinates { true };
String m_piece_set_name;
};
ErrorOr<NonnullRefPtr<ChessSettingsWidget>> ChessSettingsWidget::try_create()
ErrorOr<NonnullRefPtr<ChessSettingsWidget>> ChessSettingsWidget::create()
{
auto chess_settings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ChessSettingsWidget));
auto chess_settings_widget = TRY(try_create());
TRY(chess_settings_widget->initialize());
return chess_settings_widget;
}
ErrorOr<void> ChessSettingsWidget::initialize()
{
TRY(load_from_gml(chess_settings_widget_gml));
auto piece_set_name = Config::read_string("Games"sv, "Chess"sv, "PieceSet"sv, "Classic"sv);
auto board_theme = get_board_theme(Config::read_string("Games"sv, "Chess"sv, "BoardTheme"sv, "Beige"sv));
auto show_coordinates = Config::read_bool("Games"sv, "Chess"sv, "ShowCoordinates"sv, true);
@ -328,5 +314,3 @@ void ChessSettingsWidget::reset_default_values()
}
}
REGISTER_WIDGET(GamesSettings, ChessGamePreview);

View file

@ -1,4 +1,4 @@
@GUI::Frame {
@GamesSettings::ChessSettingsWidget {
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
margins: [8]
@ -29,7 +29,7 @@
@GUI::ComboBox {
name: "piece_set"
model_only: true
only_allow_values_from_model: true
}
}
@ -45,7 +45,7 @@
@GUI::ComboBox {
name: "board_theme"
model_only: true
only_allow_values_from_model: true
}
}

View file

@ -6,6 +6,7 @@
#pragma once
#include "ChessGamePreview.h"
#include <AK/Array.h>
#include <AK/StringView.h>
#include <LibGUI/SettingsWindow.h>
@ -13,12 +14,11 @@
namespace GamesSettings {
class ChessGamePreview;
class ChessSettingsWidget final : public GUI::SettingsWindow::Tab {
C_OBJECT_ABSTRACT(ChessSettingsWidget)
public:
static ErrorOr<NonnullRefPtr<ChessSettingsWidget>> try_create();
static ErrorOr<NonnullRefPtr<ChessSettingsWidget>> create();
virtual ~ChessSettingsWidget() override = default;
virtual void apply_settings() override;
@ -30,7 +30,7 @@ private:
Vector<DeprecatedString> m_piece_sets;
RefPtr<ChessGamePreview> m_preview;
RefPtr<GamesSettings::ChessGamePreview> m_preview;
RefPtr<GUI::ComboBox> m_piece_set_combobox;
RefPtr<GUI::ComboBox> m_board_theme_combobox;
RefPtr<GUI::CheckBox> m_show_coordinates_checkbox;

View file

@ -35,8 +35,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto window = TRY(GUI::SettingsWindow::create("Games Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes));
window->set_icon(app_icon.bitmap_for_size(16));
(void)TRY(window->add_tab<GamesSettings::CardSettingsWidget>("Cards"_string, "cards"sv));
(void)TRY(window->add_tab<GamesSettings::ChessSettingsWidget>("Chess"_string, "chess"sv));
auto widget_cards = TRY(GamesSettings::CardSettingsWidget::create());
auto widget_chess = TRY(GamesSettings::ChessSettingsWidget::create());
(void)TRY(window->add_tab(widget_cards, "Cards"_string, "cards"sv));
(void)TRY(window->add_tab(widget_chess, "Chess"_string, "chess"sv));
window->set_active_tab(selected_tab);
window->show();