diff --git a/Userland/Applications/GamesSettings/CardSettingsWidget.cpp b/Userland/Applications/GamesSettings/CardSettingsWidget.cpp index fd65186c01..87672b5ace 100644 --- a/Userland/Applications/GamesSettings/CardSettingsWidget.cpp +++ b/Userland/Applications/GamesSettings/CardSettingsWidget.cpp @@ -11,12 +11,15 @@ #include #include #include +#include #include +#include #include namespace GamesSettings { static constexpr StringView default_card_back_image_path = "/res/graphics/cards/backs/buggie-deck.png"sv; +static constexpr StringView default_card_front_image_set = "Classic"sv; class CardGamePreview final : public Cards::CardGame { C_OBJECT_ABSTRACT(CardGamePreview) @@ -90,6 +93,24 @@ ErrorOr CardSettingsWidget::initialize() m_preview_frame->set_background_color(m_background_color_input->color()); }; + m_card_front_images_combo_box = find_descendant_of_type_named("cards_front_image_set"); + m_card_front_sets.append("None"); + TRY(Core::Directory::for_each_entry("/res/graphics/cards/fronts/"sv, Core::DirIterator::SkipParentAndBaseDir, [&](auto const& entry, auto&) -> ErrorOr { + TRY(m_card_front_sets.try_append(entry.name)); + return IterationDecision::Continue; + })); + auto piece_set_model = GUI::ItemListModel::create(m_card_front_sets); + m_card_front_images_combo_box->set_model(piece_set_model); + auto card_front_set = Config::read_string("Games"sv, "Cards"sv, "CardFrontImages"sv, default_card_front_image_set); + if (card_front_set.is_empty()) + card_front_set = "None"; + m_card_front_images_combo_box->set_text(card_front_set, GUI::AllowCallback::No); + m_card_front_images_combo_box->on_change = [&](auto&, auto&) { + set_modified(true); + Cards::CardPainter::the().set_front_images_set_name(card_front_images_set_name()); + m_preview_frame->update(); + }; + m_card_back_image_view = find_descendant_of_type_named("cards_back_image"); m_card_back_image_view->set_model(GUI::FileSystemModel::create("/res/graphics/cards/backs")); m_card_back_image_view->set_model_column(GUI::FileSystemModel::Column::Name); @@ -113,6 +134,7 @@ ErrorOr CardSettingsWidget::initialize() void CardSettingsWidget::apply_settings() { Config::write_string("Games"sv, "Cards"sv, "BackgroundColor"sv, m_background_color_input->text()); + Config::write_string("Games"sv, "Cards"sv, "CardFrontImages"sv, card_front_images_set_name()); Config::write_string("Games"sv, "Cards"sv, "CardBackImage"sv, card_back_image_path()); } @@ -120,6 +142,11 @@ void CardSettingsWidget::reset_default_values() { m_background_color_input->set_color(Gfx::Color::from_rgb(0x008000)); set_card_back_image_path(default_card_back_image_path); + // FIXME: `set_text()` on a combobox doesn't trigger the `on_change` callback, but it probably should! + // Until then, we have to manually tell the preview to update. + m_card_front_images_combo_box->set_text(default_card_front_image_set); + Cards::CardPainter::the().set_front_images_set_name(card_front_images_set_name()); + m_preview_frame->update(); } bool CardSettingsWidget::set_card_back_image_path(StringView path) @@ -143,6 +170,14 @@ String CardSettingsWidget::card_back_image_path() const return String::from_deprecated_string(static_cast(m_card_back_image_view->model())->full_path(card_back_image_index)).release_value_but_fixme_should_propagate_errors(); } +String CardSettingsWidget::card_front_images_set_name() const +{ + auto selected_set_name = m_card_front_images_combo_box->text(); + if (selected_set_name == "None") + return {}; + return MUST(String::from_deprecated_string(selected_set_name)); +} + } REGISTER_WIDGET(GamesSettings, CardGamePreview); diff --git a/Userland/Applications/GamesSettings/CardSettingsWidget.gml b/Userland/Applications/GamesSettings/CardSettingsWidget.gml index 2bacb7f32e..0c8c470f4f 100644 --- a/Userland/Applications/GamesSettings/CardSettingsWidget.gml +++ b/Userland/Applications/GamesSettings/CardSettingsWidget.gml @@ -24,11 +24,33 @@ } @GUI::GroupBox { - title: "Card back" + title: "Cards" layout: @GUI::VerticalBoxLayout { margins: [8] } + @GUI::Widget { + max_height: "shrink" + layout: @GUI::HorizontalBoxLayout { + margins: [0] + } + + @GUI::Label { + text: "Card fronts:" + text_alignment: "CenterLeft" + } + + @GUI::ComboBox { + name: "cards_front_image_set" + model_only: true + } + } + + @GUI::Label { + text: "Card backs:" + text_alignment: "CenterLeft" + } + @GUI::IconView { name: "cards_back_image" } diff --git a/Userland/Applications/GamesSettings/CardSettingsWidget.h b/Userland/Applications/GamesSettings/CardSettingsWidget.h index f908bb633b..569edffca9 100644 --- a/Userland/Applications/GamesSettings/CardSettingsWidget.h +++ b/Userland/Applications/GamesSettings/CardSettingsWidget.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -32,9 +33,13 @@ private: bool set_card_back_image_path(StringView); String card_back_image_path() const; + String card_front_images_set_name() const; + + Vector m_card_front_sets; RefPtr m_preview_frame; RefPtr m_background_color_input; + RefPtr m_card_front_images_combo_box; RefPtr m_card_back_image_view; GUI::ModelIndex m_last_selected_card_back; diff --git a/Userland/Libraries/LibCards/CardGame.cpp b/Userland/Libraries/LibCards/CardGame.cpp index 378388eb88..826549e4ce 100644 --- a/Userland/Libraries/LibCards/CardGame.cpp +++ b/Userland/Libraries/LibCards/CardGame.cpp @@ -125,6 +125,11 @@ void CardGame::config_string_did_change(StringView domain, StringView group, Str update(); return; } + if (key == "CardFrontImages") { + CardPainter::the().set_front_images_set_name(String::from_utf8(value).release_value_but_fixme_should_propagate_errors()); + update(); + return; + } } }