From c0756d8e55fe645e8a905004b07bbf92011e92b7 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 12 Jan 2023 15:07:47 -0500 Subject: [PATCH] GamesSettings: Paint the card preview frame using a CardGame subclass We currently paint the 3 previewed cards using ImageWidget. This works fine, but in order to preview a card hovering over a valid target card, it will be easier to use the already-existing CardGame paint logic. So this patch changes GamesSettings to display the preview as-is using a CardGame. --- .../GamesSettings/CardSettingsWidget.cpp | 73 ++++++++++++++----- .../GamesSettings/CardSettingsWidget.gml | 25 +------ .../GamesSettings/CardSettingsWidget.h | 9 +-- 3 files changed, 58 insertions(+), 49 deletions(-) diff --git a/Userland/Applications/GamesSettings/CardSettingsWidget.cpp b/Userland/Applications/GamesSettings/CardSettingsWidget.cpp index 4c92862ef0..80d23ad2b2 100644 --- a/Userland/Applications/GamesSettings/CardSettingsWidget.cpp +++ b/Userland/Applications/GamesSettings/CardSettingsWidget.cpp @@ -6,7 +6,10 @@ #include "CardSettingsWidget.h" #include +#include +#include #include +#include #include #include #include @@ -15,28 +18,63 @@ namespace GamesSettings { static constexpr StringView default_card_back_image_path = "/res/icons/cards/buggie-deck.png"sv; +class Preview final : public Cards::CardGame { + C_OBJECT_ABSTRACT(Preview) + +public: + static ErrorOr> try_create() + { + auto preview = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Preview())); + + Gfx::IntPoint point { 30, 30 }; + TRY(preview->add_stack(point, Cards::CardStack::Type::Stock)); + + point.translate_by(Cards::Card::width + 30, 0); + TRY(preview->add_stack(point, Cards::CardStack::Type::Normal)); + + point.translate_by(Cards::Card::width + 30, 0); + TRY(preview->add_stack(point, Cards::CardStack::Type::Normal)); + + for (size_t i = 0; i < Cards::Card::card_count; ++i) + preview->stack_at_location(0).push(TRY(Cards::Card::try_create(Cards::Suit::Diamonds, static_cast(i)))); + preview->stack_at_location(1).push(TRY(Cards::Card::try_create(Cards::Suit::Spades, Cards::Rank::Ace))); + preview->stack_at_location(2).push(TRY(Cards::Card::try_create(Cards::Suit::Hearts, Cards::Rank::Queen))); + + preview->stack_at_location(0).peek().set_upside_down(true); + return preview; + } + +private: + Preview() = default; + + virtual void paint_event(GUI::PaintEvent& event) override + { + Cards::CardGame::paint_event(event); + + GUI::Painter painter(*this); + painter.add_clip_rect(frame_inner_rect()); + painter.add_clip_rect(event.rect()); + + auto background_color = this->background_color(); + for (auto& stack : stacks()) + stack.paint(painter, background_color); + } +}; + CardSettingsWidget::CardSettingsWidget() { load_from_gml(card_settings_widget_gml).release_value_but_fixme_should_propagate_errors(); 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("cards_preview"); - set_cards_background_color(background_color); - - m_preview_card_back = find_descendant_of_type_named("cards_preview_card_back"); - m_preview_card_back->set_bitmap(Cards::CardPainter::the().card_back()); - - m_preview_card_front_ace = find_descendant_of_type_named("cards_preview_card_front_ace"); - m_preview_card_front_ace->set_bitmap(Cards::CardPainter::the().card_front(Cards::Suit::Spades, Cards::Rank::Ace)); - m_preview_card_front_queen = find_descendant_of_type_named("cards_preview_card_front_queen"); - m_preview_card_front_queen->set_bitmap(Cards::CardPainter::the().card_front(Cards::Suit::Hearts, Cards::Rank::Queen)); + m_preview_frame = find_descendant_of_type_named("cards_preview"); + m_preview_frame->set_background_color(background_color); m_background_color_input = find_descendant_of_type_named("cards_background_color"); m_background_color_input->set_color(background_color, GUI::AllowCallback::No); m_background_color_input->on_change = [&]() { set_modified(true); - set_cards_background_color(m_background_color_input->color()); + m_preview_frame->set_background_color(m_background_color_input->color()); }; m_card_back_image_view = find_descendant_of_type_named("cards_back_image"); @@ -51,7 +89,7 @@ CardSettingsWidget::CardSettingsWidget() m_last_selected_card_back = card_back_selection.first(); set_modified(true); Cards::CardPainter::the().set_background_image_path(card_back_image_path()); - m_preview_card_back->update(); + m_preview_frame->update(); }; m_last_selected_card_back = m_card_back_image_view->selection().first(); @@ -69,20 +107,13 @@ void CardSettingsWidget::reset_default_values() set_card_back_image_path(default_card_back_image_path); } -void CardSettingsWidget::set_cards_background_color(Gfx::Color color) -{ - auto new_palette = m_preview_frame->palette(); - new_palette.set_color(Gfx::ColorRole::Background, color); - m_preview_frame->set_palette(new_palette); -} - bool CardSettingsWidget::set_card_back_image_path(DeprecatedString const& path) { auto index = static_cast(m_card_back_image_view->model())->index(path, m_card_back_image_view->model_column()); if (index.is_valid()) { m_card_back_image_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set); Cards::CardPainter::the().set_background_image_path(path); - m_preview_card_back->update(); + m_preview_frame->update(); return true; } return false; @@ -98,3 +129,5 @@ DeprecatedString CardSettingsWidget::card_back_image_path() const } } + +REGISTER_WIDGET(GamesSettings, Preview); diff --git a/Userland/Applications/GamesSettings/CardSettingsWidget.gml b/Userland/Applications/GamesSettings/CardSettingsWidget.gml index e4a8788472..bd4c061748 100644 --- a/Userland/Applications/GamesSettings/CardSettingsWidget.gml +++ b/Userland/Applications/GamesSettings/CardSettingsWidget.gml @@ -4,31 +4,10 @@ margins: [8] } - @GUI::Frame { + @GamesSettings::Preview { name: "cards_preview" - max_height: "shrink" - background_color: "green" fill_with_background_color: true - layout: @GUI::HorizontalBoxLayout { - margins: [8] - spacing: 8 - } - - @GUI::Layout::Spacer {} - - @GUI::ImageWidget { - name: "cards_preview_card_back" - } - - @GUI::ImageWidget { - name: "cards_preview_card_front_ace" - } - - @GUI::ImageWidget { - name: "cards_preview_card_front_queen" - } - - @GUI::Layout::Spacer {} + fixed_height: 160 } @GUI::GroupBox { diff --git a/Userland/Applications/GamesSettings/CardSettingsWidget.h b/Userland/Applications/GamesSettings/CardSettingsWidget.h index a44bfe39ee..f0a6761733 100644 --- a/Userland/Applications/GamesSettings/CardSettingsWidget.h +++ b/Userland/Applications/GamesSettings/CardSettingsWidget.h @@ -15,6 +15,8 @@ namespace GamesSettings { +class Preview; + class CardSettingsWidget final : public GUI::SettingsWindow::Tab { C_OBJECT(CardSettingsWidget) public: @@ -26,15 +28,10 @@ public: private: CardSettingsWidget(); - void set_cards_background_color(Gfx::Color); bool set_card_back_image_path(DeprecatedString const&); DeprecatedString card_back_image_path() const; - RefPtr m_preview_frame; - RefPtr m_preview_card_back; - RefPtr m_preview_card_front_ace; - RefPtr m_preview_card_front_queen; - + RefPtr m_preview_frame; RefPtr m_background_color_input; RefPtr m_card_back_image_view;