Explorar o código

GamesSettings: Add a preview for the current card-game settings :^)

Sam Atkins %!s(int64=2) %!d(string=hai) anos
pai
achega
cde4552710

+ 1 - 1
Userland/Applications/GamesSettings/CMakeLists.txt

@@ -14,4 +14,4 @@ set(SOURCES
         )
         )
 
 
 serenity_app(GamesSettings ICON games)
 serenity_app(GamesSettings ICON games)
-target_link_libraries(GamesSettings LibGUI LibMain)
+target_link_libraries(GamesSettings LibGUI LibMain LibCards)

+ 30 - 2
Userland/Applications/GamesSettings/CardSettingsWidget.cpp

@@ -6,8 +6,10 @@
 
 
 #include "CardSettingsWidget.h"
 #include "CardSettingsWidget.h"
 #include <Applications/GamesSettings/CardSettingsWidgetGML.h>
 #include <Applications/GamesSettings/CardSettingsWidgetGML.h>
+#include <LibCards/CardPainter.h>
 #include <LibConfig/Client.h>
 #include <LibConfig/Client.h>
 #include <LibGUI/FileSystemModel.h>
 #include <LibGUI/FileSystemModel.h>
+#include <LibGfx/Palette.h>
 
 
 static constexpr StringView default_card_back_image_path = "/res/icons/cards/buggie-deck.png"sv;
 static constexpr StringView default_card_back_image_path = "/res/icons/cards/buggie-deck.png"sv;
 
 
@@ -15,10 +17,25 @@ CardSettingsWidget::CardSettingsWidget()
 {
 {
     load_from_gml(card_settings_widget_gml);
     load_from_gml(card_settings_widget_gml);
 
 
-    m_background_color_input = find_descendant_of_type_named<GUI::ColorInput>("cards_background_color");
     auto background_color = Gfx::Color::from_string(Config::read_string("Games"sv, "Cards"sv, "BackgroundColor"sv)).value_or(Gfx::Color::from_rgb(0x008000));
     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<GUI::Frame>("cards_preview");
+    set_cards_background_color(background_color);
+
+    m_preview_card_back = find_descendant_of_type_named<GUI::ImageWidget>("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<GUI::ImageWidget>("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<GUI::ImageWidget>("cards_preview_card_front_queen");
+    m_preview_card_front_queen->set_bitmap(Cards::CardPainter::the().card_front(Cards::Suit::Hearts, Cards::Rank::Queen));
+
+    m_background_color_input = find_descendant_of_type_named<GUI::ColorInput>("cards_background_color");
     m_background_color_input->set_color(background_color, GUI::AllowCallback::No);
     m_background_color_input->set_color(background_color, GUI::AllowCallback::No);
-    m_background_color_input->on_change = [&]() { set_modified(true); };
+    m_background_color_input->on_change = [&]() {
+        set_modified(true);
+        set_cards_background_color(m_background_color_input->color());
+    };
 
 
     m_card_back_image_view = find_descendant_of_type_named<GUI::IconView>("cards_back_image");
     m_card_back_image_view = find_descendant_of_type_named<GUI::IconView>("cards_back_image");
     m_card_back_image_view->set_model(GUI::FileSystemModel::create("/res/icons/cards"));
     m_card_back_image_view->set_model(GUI::FileSystemModel::create("/res/icons/cards"));
@@ -29,6 +46,8 @@ CardSettingsWidget::CardSettingsWidget()
         if (m_card_back_image_view->selection().is_empty())
         if (m_card_back_image_view->selection().is_empty())
             return;
             return;
         set_modified(true);
         set_modified(true);
+        Cards::CardPainter::the().set_background_image_path(card_back_image_path());
+        m_preview_card_back->update();
     };
     };
 }
 }
 
 
@@ -44,11 +63,20 @@ void CardSettingsWidget::reset_default_values()
     set_card_back_image_path(default_card_back_image_path);
     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(String const& path)
 bool CardSettingsWidget::set_card_back_image_path(String const& path)
 {
 {
     auto index = static_cast<GUI::FileSystemModel*>(m_card_back_image_view->model())->index(path, m_card_back_image_view->model_column());
     auto index = static_cast<GUI::FileSystemModel*>(m_card_back_image_view->model())->index(path, m_card_back_image_view->model_column());
     if (index.is_valid()) {
     if (index.is_valid()) {
         m_card_back_image_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
         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();
         return true;
         return true;
     }
     }
     return false;
     return false;

+ 27 - 0
Userland/Applications/GamesSettings/CardSettingsWidget.gml

@@ -4,6 +4,33 @@
         margins: [8]
         margins: [8]
     }
     }
 
 
+    @GUI::Frame {
+        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 {}
+    }
+
     @GUI::GroupBox {
     @GUI::GroupBox {
         title: "Background Color"
         title: "Background Color"
         max_height: "shrink"
         max_height: "shrink"

+ 8 - 0
Userland/Applications/GamesSettings/CardSettingsWidget.h

@@ -7,7 +7,9 @@
 #pragma once
 #pragma once
 
 
 #include <LibGUI/ColorInput.h>
 #include <LibGUI/ColorInput.h>
+#include <LibGUI/Frame.h>
 #include <LibGUI/IconView.h>
 #include <LibGUI/IconView.h>
+#include <LibGUI/ImageWidget.h>
 #include <LibGUI/SettingsWindow.h>
 #include <LibGUI/SettingsWindow.h>
 
 
 class CardSettingsWidget final : public GUI::SettingsWindow::Tab {
 class CardSettingsWidget final : public GUI::SettingsWindow::Tab {
@@ -21,9 +23,15 @@ public:
 private:
 private:
     CardSettingsWidget();
     CardSettingsWidget();
 
 
+    void set_cards_background_color(Gfx::Color);
     bool set_card_back_image_path(String const&);
     bool set_card_back_image_path(String const&);
     String card_back_image_path() const;
     String card_back_image_path() const;
 
 
+    RefPtr<GUI::Frame> m_preview_frame;
+    RefPtr<GUI::ImageWidget> m_preview_card_back;
+    RefPtr<GUI::ImageWidget> m_preview_card_front_ace;
+    RefPtr<GUI::ImageWidget> m_preview_card_front_queen;
+
     RefPtr<GUI::ColorInput> m_background_color_input;
     RefPtr<GUI::ColorInput> m_background_color_input;
     RefPtr<GUI::IconView> m_card_back_image_view;
     RefPtr<GUI::IconView> m_card_back_image_view;
 };
 };