瀏覽代碼

LibCards: Make the card back image configurable

`CardPainter::set_background_image_path()` immediately repaints the card
back and inverted card back bitmaps if they exist, so users just need
to `update()` that part of the screen. This is handled automatically by
`CardGame`, but other users will have to do this manually.
Sam Atkins 3 年之前
父節點
當前提交
40b1428194

+ 11 - 3
Userland/Libraries/LibCards/CardGame.cpp

@@ -5,6 +5,7 @@
  */
  */
 
 
 #include "CardGame.h"
 #include "CardGame.h"
+#include <LibCards/CardPainter.h>
 #include <LibConfig/Client.h>
 #include <LibConfig/Client.h>
 #include <LibGfx/Palette.h>
 #include <LibGfx/Palette.h>
 
 
@@ -18,9 +19,16 @@ CardGame::CardGame()
 
 
 void CardGame::config_string_did_change(String const& domain, String const& group, String const& key, String const& value)
 void CardGame::config_string_did_change(String const& domain, String const& group, String const& key, String const& value)
 {
 {
-    if (domain == "Games" && group == "Cards" && key == "BackgroundColor") {
-        if (auto maybe_color = Gfx::Color::from_string(value); maybe_color.has_value()) {
-            set_background_color(maybe_color.value());
+    if (domain == "Games" && group == "Cards") {
+        if (key == "BackgroundColor") {
+            if (auto maybe_color = Gfx::Color::from_string(value); maybe_color.has_value())
+                set_background_color(maybe_color.value());
+            return;
+        }
+        if (key == "CardBackImage") {
+            CardPainter::the().set_background_image_path(value);
+            update();
+            return;
         }
         }
     }
     }
 }
 }

+ 19 - 1
Userland/Libraries/LibCards/CardPainter.cpp

@@ -7,6 +7,7 @@
  */
  */
 
 
 #include "CardPainter.h"
 #include "CardPainter.h"
+#include <LibConfig/Client.h>
 #include <LibGfx/Font/Font.h>
 #include <LibGfx/Font/Font.h>
 #include <LibGfx/Font/FontDatabase.h>
 #include <LibGfx/Font/FontDatabase.h>
 
 
@@ -18,6 +19,11 @@ CardPainter& CardPainter::the()
     return s_card_painter;
     return s_card_painter;
 }
 }
 
 
+CardPainter::CardPainter()
+{
+    m_background_image_path = Config::read_string("Games"sv, "Cards"sv, "CardBackImage"sv, "/res/icons/cards/buggie-deck.png"sv);
+}
+
 static constexpr Gfx::CharacterBitmap s_diamond {
 static constexpr Gfx::CharacterBitmap s_diamond {
     "    #    "
     "    #    "
     "   ###   "
     "   ###   "
@@ -122,6 +128,18 @@ NonnullRefPtr<Gfx::Bitmap> CardPainter::card_back_inverted()
     return *m_card_back_inverted;
     return *m_card_back_inverted;
 }
 }
 
 
+void CardPainter::set_background_image_path(String path)
+{
+    if (m_background_image_path == path)
+        return;
+
+    m_background_image_path = path;
+    if (!m_card_back.is_null())
+        paint_card_back(*m_card_back);
+    if (!m_card_back_inverted.is_null())
+        paint_inverted_card(*m_card_back_inverted, *m_card_back);
+}
+
 NonnullRefPtr<Gfx::Bitmap> CardPainter::create_card_bitmap()
 NonnullRefPtr<Gfx::Bitmap> CardPainter::create_card_bitmap()
 {
 {
     return Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { Card::width, Card::height }).release_value_but_fixme_should_propagate_errors();
     return Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { Card::width, Card::height }).release_value_but_fixme_should_propagate_errors();
@@ -177,7 +195,7 @@ void CardPainter::paint_card_back(Gfx::Bitmap& bitmap)
     auto paint_rect = bitmap.rect();
     auto paint_rect = bitmap.rect();
     painter.clear_rect(paint_rect, Gfx::Color::Transparent);
     painter.clear_rect(paint_rect, Gfx::Color::Transparent);
 
 
-    auto image = Gfx::Bitmap::try_load_from_file("/res/icons/cards/buggie-deck.png"sv).release_value_but_fixme_should_propagate_errors();
+    auto image = Gfx::Bitmap::try_load_from_file(m_background_image_path).release_value_but_fixme_should_propagate_errors();
 
 
     float aspect_ratio = image->width() / static_cast<float>(image->height());
     float aspect_ratio = image->width() / static_cast<float>(image->height());
     Gfx::IntSize target_size { static_cast<int>(aspect_ratio * (Card::height - 5)), Card::height - 5 };
     Gfx::IntSize target_size { static_cast<int>(aspect_ratio * (Card::height - 5)), Card::height - 5 };

+ 5 - 0
Userland/Libraries/LibCards/CardPainter.h

@@ -21,7 +21,10 @@ public:
     NonnullRefPtr<Gfx::Bitmap> card_front_inverted(Suit, Rank);
     NonnullRefPtr<Gfx::Bitmap> card_front_inverted(Suit, Rank);
     NonnullRefPtr<Gfx::Bitmap> card_back_inverted();
     NonnullRefPtr<Gfx::Bitmap> card_back_inverted();
 
 
+    void set_background_image_path(String path);
+
 private:
 private:
+    CardPainter();
     NonnullRefPtr<Gfx::Bitmap> create_card_bitmap();
     NonnullRefPtr<Gfx::Bitmap> create_card_bitmap();
     void paint_card_front(Gfx::Bitmap&, Suit, Rank);
     void paint_card_front(Gfx::Bitmap&, Suit, Rank);
     void paint_card_back(Gfx::Bitmap&);
     void paint_card_back(Gfx::Bitmap&);
@@ -31,6 +34,8 @@ private:
     Array<Array<RefPtr<Gfx::Bitmap>, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards_inverted;
     Array<Array<RefPtr<Gfx::Bitmap>, to_underlying(Rank::__Count)>, to_underlying(Suit::__Count)> m_cards_inverted;
     RefPtr<Gfx::Bitmap> m_card_back;
     RefPtr<Gfx::Bitmap> m_card_back;
     RefPtr<Gfx::Bitmap> m_card_back_inverted;
     RefPtr<Gfx::Bitmap> m_card_back_inverted;
+
+    String m_background_image_path;
 };
 };
 
 
 }
 }