From 744d393dfcfdec1fd57e85c17114d9100ba6e389 Mon Sep 17 00:00:00 2001
From: Joshua Goins <josh@redstrate.com>
Date: Wed, 11 Jan 2023 15:28:15 -0500
Subject: [PATCH 1/2] Add an option for themes to change the background stretch
 modified

This commit adds a new optional theme key called
"BackgroundLocation" that themes can use to control the location of
the background when resized. The currently supported modes are:
- Stretch: default behavior
- TopLeft: the background is pinned to the top left
- Center: the background is centered
---
 src/GraphicsInterface.h | 13 +++++++++++--
 src/KBlocksGraphics.cpp | 18 ++++++++++++++----
 src/KBlocksGraphics.h   |  2 +-
 src/KBlocksScene.cpp    | 22 +++++++++++++++++++++-
 src/KBlocksScene.h      |  2 ++
 5 files changed, 49 insertions(+), 8 deletions(-)

diff --git a/src/GraphicsInterface.h b/src/GraphicsInterface.h
index 6df89ea..d106cbc 100644
--- a/src/GraphicsInterface.h
+++ b/src/GraphicsInterface.h
@@ -12,6 +12,12 @@ class QString;
 class QSvgRenderer;
 class KgTheme;
 
+enum class BackgroundLocation {
+    Stretch,
+    TopLeft,
+    Center
+};
+
 class GraphicsInterface
 {
 public:
@@ -24,13 +30,14 @@ public:
           m_PlayArea_NumberOfBlocks_X{},
           m_PlayArea_NumberOfBlocks_Y{},
           m_PreviewArea_CenterPoint_X{},
-          m_PreviewArea_CenterPoint_Y{}
+          m_PreviewArea_CenterPoint_Y{},
+          m_BackgroundLocation{}
     {}
     virtual ~GraphicsInterface() = default;
 
 public:
     virtual bool loadTheme(const KgTheme *theme) = 0;
-    virtual void readThemeValues() = 0;
+    virtual void readThemeValues(const KgTheme *theme) = 0;
     virtual void adjustForSize(const QSize &) = 0;
     virtual QSvgRenderer *renderer() const = 0;
 
@@ -43,6 +50,8 @@ public:
     int m_PlayArea_NumberOfBlocks_Y;
     int m_PreviewArea_CenterPoint_X;
     int m_PreviewArea_CenterPoint_Y;
+
+    BackgroundLocation m_BackgroundLocation;
 };
 
 #endif // GRAPHICSINTERFACE_H
diff --git a/src/KBlocksGraphics.cpp b/src/KBlocksGraphics.cpp
index a1391c0..d64b7bd 100644
--- a/src/KBlocksGraphics.cpp
+++ b/src/KBlocksGraphics.cpp
@@ -15,7 +15,7 @@
 KBlocksGraphics::KBlocksGraphics(const KgTheme *theme)
 {
     m_renderer = new QSvgRenderer(theme->graphicsPath());
-    readThemeValues();
+    readThemeValues(theme);
 }
 
 KBlocksGraphics::~KBlocksGraphics()
@@ -32,7 +32,8 @@ bool KBlocksGraphics::loadTheme(const KgTheme *theme)
     }
     //clear the cache or pixmaps from the old theme will be returned
     //QPixmapCache::clear();
-    readThemeValues();
+    readThemeValues(theme);
+
     return true;
 }
 
@@ -40,7 +41,7 @@ void KBlocksGraphics::adjustForSize(const QSize &newsize)
 {
     Q_UNUSED(newsize)
     //Reset our values
-    readThemeValues();
+    //readThemeValues();
 
     return;
 
@@ -69,7 +70,7 @@ void KBlocksGraphics::adjustForSize(const QSize &newsize)
     */
 }
 
-void KBlocksGraphics::readThemeValues()
+void KBlocksGraphics::readThemeValues(const KgTheme *theme)
 {
     //Extract values from SVG elements
     QRectF bounds;
@@ -86,5 +87,14 @@ void KBlocksGraphics::readThemeValues()
     bounds = m_renderer->boundsOnElement(QStringLiteral("NEXTPIECE_AREA"));
     m_PreviewArea_CenterPoint_X = bounds.center().x();
     m_PreviewArea_CenterPoint_Y = bounds.center().y();
+
+    const QString backgroundLocation = theme->customData(QStringLiteral("BackgroundLocation"), QStringLiteral("Stretch"));
+    if(backgroundLocation == QStringLiteral("Stretch")) {
+        m_BackgroundLocation = BackgroundLocation::Stretch;
+    } else if(backgroundLocation == QStringLiteral("TopLeft")) {
+        m_BackgroundLocation = BackgroundLocation::TopLeft;
+    } else if(backgroundLocation == QStringLiteral("Center")) {
+        m_BackgroundLocation = BackgroundLocation::Center;
+    }
 }
 
diff --git a/src/KBlocksGraphics.h b/src/KBlocksGraphics.h
index 9a7b501..47fadfd 100644
--- a/src/KBlocksGraphics.h
+++ b/src/KBlocksGraphics.h
@@ -22,7 +22,7 @@ public:
 
 public:
     bool loadTheme(const KgTheme *theme) override;
-    void readThemeValues() override;
+    void readThemeValues(const KgTheme *theme) override;
     void adjustForSize(const QSize &newsize) override;
     QSvgRenderer *renderer() const override
     {
diff --git a/src/KBlocksScene.cpp b/src/KBlocksScene.cpp
index 904162c..15548be 100644
--- a/src/KBlocksScene.cpp
+++ b/src/KBlocksScene.cpp
@@ -262,6 +262,8 @@ void KBlocksScene::updateDimensions()
         maGroupList[i]->setPos(left, top);
         maGroupList[i]->refreshPosition();
     }
+
+    mBackgroundSize = mpGrafx->renderer()->boundsOnElement(QStringLiteral("BACKGROUND")).size();
 }
 
 void KBlocksScene::greetPlayer()
@@ -376,7 +378,25 @@ void KBlocksScene::playDropSound()
 void KBlocksScene::drawBackground(QPainter *painter, const QRectF &rect)
 {
     if (mpGrafx->renderer()->isValid()) {
-        mpGrafx->renderer()->render(painter, QStringLiteral("BACKGROUND"), rect);
+        // QtSvgRenderer only supports KeepAspectRatio, so we have to adjust the
+        // bounds instead.
+        const QSizeF newSize = mBackgroundSize.scaled(rect.size(), Qt::KeepAspectRatioByExpanding);
+
+        QRectF adjustedRect = rect;
+
+        switch(mpGrafx->m_BackgroundLocation) {
+            case BackgroundLocation::Stretch:
+                break;
+            case BackgroundLocation::TopLeft:
+                adjustedRect.setSize(newSize);
+                break;
+            case BackgroundLocation::Center:
+                adjustedRect.setSize(newSize);
+                adjustedRect.moveCenter(rect.center());
+                break;
+        }
+
+        mpGrafx->renderer()->render(painter, QStringLiteral("BACKGROUND"), adjustedRect);
     }
 }
 
diff --git a/src/KBlocksScene.h b/src/KBlocksScene.h
index bb30b69..06370c1 100644
--- a/src/KBlocksScene.h
+++ b/src/KBlocksScene.h
@@ -104,6 +104,8 @@ private:
     KBlocksItemGroup **maGroupList = nullptr;
     KBlocksScore **maGameScoreList = nullptr;
 
+    QSizeF mBackgroundSize;
+
     KGamePopupItem *mMessageBox = nullptr;
 
     int mUpdateInterval;
-- 
GitLab


From 62fdf35c79bbb457f94a50068db184340edc4af0 Mon Sep 17 00:00:00 2001
From: Joshua Goins <josh@redstrate.com>
Date: Wed, 1 Mar 2023 19:24:52 -0500
Subject: [PATCH 2/2] Remove GraphicsInterface::adjustForSize

This isn't called by anything right now
---
 src/GraphicsInterface.h    |  1 -
 src/KBlocksGraphics.cpp    | 33 ---------------------------------
 src/KBlocksGraphics.h      |  1 -
 src/Testing/MockGraphics.h |  3 +--
 4 files changed, 1 insertion(+), 37 deletions(-)

diff --git a/src/GraphicsInterface.h b/src/GraphicsInterface.h
index d106cbc..5c76d33 100644
--- a/src/GraphicsInterface.h
+++ b/src/GraphicsInterface.h
@@ -38,7 +38,6 @@ public:
 public:
     virtual bool loadTheme(const KgTheme *theme) = 0;
     virtual void readThemeValues(const KgTheme *theme) = 0;
-    virtual void adjustForSize(const QSize &) = 0;
     virtual QSvgRenderer *renderer() const = 0;
 
     int m_Block_Size;
diff --git a/src/KBlocksGraphics.cpp b/src/KBlocksGraphics.cpp
index d64b7bd..6b1fec7 100644
--- a/src/KBlocksGraphics.cpp
+++ b/src/KBlocksGraphics.cpp
@@ -37,39 +37,6 @@ bool KBlocksGraphics::loadTheme(const KgTheme *theme)
     return true;
 }
 
-void KBlocksGraphics::adjustForSize(const QSize &newsize)
-{
-    Q_UNUSED(newsize)
-    //Reset our values
-    //readThemeValues();
-
-    return;
-
-    /*
-    double aspectratio;
-    double nw = newsize.width();
-    double nh = newsize.height();
-
-    double origw = m_View_Size_Width;
-    double origh = m_View_Size_Height;
-
-    if ((origw/origh)>(nw/nh)) {
-        //space will be left on height, use width as limit
-        aspectratio = nw/origw;
-    } else {
-        aspectratio = nh/origh;
-    }
-    //qCDebug(KBGraphics) << aspectratio;
-    m_Block_Size = (int) (aspectratio*(qreal)m_Block_Size);
-    m_View_Size_Width = (int) (aspectratio*(double)m_View_Size_Width);
-    m_View_Size_Height = (int) (aspectratio*(double)m_View_Size_Height);
-    m_PlayArea_OffsetPoint_X = (int) (aspectratio*(qreal)m_PlayArea_OffsetPoint_X);
-    m_PlayArea_OffsetPoint_Y = (int) (aspectratio*(qreal)m_PlayArea_OffsetPoint_Y);
-    m_PreviewArea_CenterPoint_X = (int) (aspectratio*(qreal)m_PreviewArea_CenterPoint_X);
-    m_PreviewArea_CenterPoint_Y = (int) (aspectratio*(qreal)m_PreviewArea_CenterPoint_Y);
-    */
-}
-
 void KBlocksGraphics::readThemeValues(const KgTheme *theme)
 {
     //Extract values from SVG elements
diff --git a/src/KBlocksGraphics.h b/src/KBlocksGraphics.h
index 47fadfd..924db51 100644
--- a/src/KBlocksGraphics.h
+++ b/src/KBlocksGraphics.h
@@ -23,7 +23,6 @@ public:
 public:
     bool loadTheme(const KgTheme *theme) override;
     void readThemeValues(const KgTheme *theme) override;
-    void adjustForSize(const QSize &newsize) override;
     QSvgRenderer *renderer() const override
     {
         return m_renderer;
diff --git a/src/Testing/MockGraphics.h b/src/Testing/MockGraphics.h
index b3f3681..235c163 100644
--- a/src/Testing/MockGraphics.h
+++ b/src/Testing/MockGraphics.h
@@ -28,8 +28,7 @@ public:
 
 public:
     bool loadTheme(const KgTheme *) override { return false; }
-    void readThemeValues() override {}
-    void adjustForSize(const QSize &) override {}
+    void readThemeValues(const KgTheme *) override {}
     QSvgRenderer *renderer() const override { return mRenderer.get(); }
 
 private:
-- 
GitLab