From c23959e3c40e97f9c1aeb9eabc52131b6fa68f2a Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Sun, 14 Oct 2012 01:36:25 +0300
Subject: Add new parameters to buttons theming.

New parameters:
    padding - padding over button text.
    spacing - space between text and image.
---
 data/graphics/gui/button.xml             |  3 ++
 data/graphics/gui/button_disabled.xml    |  3 ++
 data/graphics/gui/button_highlighted.xml |  3 ++
 data/graphics/gui/button_pressed.xml     |  3 ++
 src/gui/theme.h                          |  4 +-
 src/gui/widgets/button.cpp               | 74 ++++++++++++++++++++------------
 src/gui/widgets/button.h                 |  5 ++-
 7 files changed, 64 insertions(+), 31 deletions(-)

diff --git a/data/graphics/gui/button.xml b/data/graphics/gui/button.xml
index 899030820..95ac8d7f2 100644
--- a/data/graphics/gui/button.xml
+++ b/data/graphics/gui/button.xml
@@ -1,5 +1,8 @@
 <skinset name="Default" image="window.png">
 	<widget type="Window" xpos="41" ypos="0">
+        <option name="padding" value="4" />
+        <option name="spacing" value="2" />
+
 		<!-- Top Row -->
 		<part type="top-left-corner" xpos="0" ypos="0" width="10" height="5" />
 		<part type="top-edge" xpos="9" ypos="0" width="8" height="5" />
diff --git a/data/graphics/gui/button_disabled.xml b/data/graphics/gui/button_disabled.xml
index 8194bf4d5..0aaa0cb7a 100644
--- a/data/graphics/gui/button_disabled.xml
+++ b/data/graphics/gui/button_disabled.xml
@@ -1,5 +1,8 @@
 <skinset name="Default" image="window.png">
 	<widget type="Window" xpos="41" ypos="75">
+        <option name="padding" value="4" />
+        <option name="spacing" value="2" />
+
 		<!-- Top Row -->
 		<part type="top-left-corner" xpos="0" ypos="0" width="10" height="5" />
 		<part type="top-edge" xpos="9" ypos="0" width="8" height="5" />
diff --git a/data/graphics/gui/button_highlighted.xml b/data/graphics/gui/button_highlighted.xml
index 356cee5ce..13aa8cb62 100644
--- a/data/graphics/gui/button_highlighted.xml
+++ b/data/graphics/gui/button_highlighted.xml
@@ -1,5 +1,8 @@
 <skinset name="Default" image="window.png">
 	<widget type="Window" xpos="41" ypos="25">
+        <option name="padding" value="4" />
+        <option name="spacing" value="2" />
+
 		<!-- Top Row -->
 		<part type="top-left-corner" xpos="0" ypos="0" width="10" height="5" />
 		<part type="top-edge" xpos="9" ypos="0" width="8" height="5" />
diff --git a/data/graphics/gui/button_pressed.xml b/data/graphics/gui/button_pressed.xml
index bd9de9e28..c5e400aec 100644
--- a/data/graphics/gui/button_pressed.xml
+++ b/data/graphics/gui/button_pressed.xml
@@ -1,5 +1,8 @@
 <skinset name="Default" image="window.png">
 	<widget type="Window" xpos="41" ypos="50">
+        <option name="padding" value="4" />
+        <option name="spacing" value="2" />
+
 		<!-- Top Row -->
 		<part type="top-left-corner" xpos="0" ypos="0" width="10" height="5" />
 		<part type="top-edge" xpos="9" ypos="0" width="8" height="5" />
diff --git a/src/gui/theme.h b/src/gui/theme.h
index cbda2ee4d..4d99649da 100644
--- a/src/gui/theme.h
+++ b/src/gui/theme.h
@@ -68,7 +68,7 @@ class Skin final
         /**
          * Returns the background skin.
          */
-        const ImageRect &getBorder() const
+        ImageRect &getBorder() const
         { return *mBorder; }
 
         /**
@@ -104,7 +104,7 @@ class Skin final
         int getTitlePadding() const
         { return mTitlePadding; }
 
-        int getOption(std::string name)
+        int getOption(const std::string &name) const
         {
             if (mOptions->find(name) != mOptions->end())
                 return (*mOptions)[name];
diff --git a/src/gui/widgets/button.cpp b/src/gui/widgets/button.cpp
index ce2dfd74a..4cba0b535 100644
--- a/src/gui/widgets/button.cpp
+++ b/src/gui/widgets/button.cpp
@@ -62,7 +62,7 @@ static std::string const data[BUTTON_COUNT] =
     "button_disabled.xml"
 };
 
-ImageRect Button::button[BUTTON_COUNT];
+Skin *Button::button[BUTTON_COUNT];
 
 Button::Button() :
     gcn::Button(),
@@ -184,7 +184,7 @@ void Button::init()
         {
             for (int mode = 0; mode < BUTTON_COUNT; mode ++)
             {
-                Theme::instance()->loadRect(button[mode],
+                button[mode] = Theme::instance()->load(
                     data[mode], "button.xml");
             }
         }
@@ -205,9 +205,9 @@ Button::~Button()
 
     if (mInstances == 0 && Theme::instance())
     {
-        const Theme *const theme = Theme::instance();
+        Theme *const theme = Theme::instance();
         for (int mode = 0; mode < BUTTON_COUNT; mode ++)
-            theme->unloadRect(button[mode]);
+            theme->unload(button[mode]);
     }
     delete mVertexes;
     mVertexes = nullptr;
@@ -256,16 +256,19 @@ void Button::updateAlpha()
     if (mAlpha != alpha)
     {
         mAlpha = alpha;
-        for (int a = 0; a < 9; a++)
+        for (int mode = 0; mode < BUTTON_COUNT; mode ++)
         {
-            if (button[BUTTON_DISABLED].grid[a])
-                button[BUTTON_DISABLED].grid[a]->setAlpha(mAlpha);
-            if (button[BUTTON_PRESSED].grid[a])
-                button[BUTTON_PRESSED].grid[a]->setAlpha(mAlpha);
-            if (button[BUTTON_HIGHLIGHTED].grid[a])
-                button[BUTTON_HIGHLIGHTED].grid[a]->setAlpha(mAlpha);
-            if (button[BUTTON_STANDARD].grid[a])
-                button[BUTTON_STANDARD].grid[a]->setAlpha(mAlpha);
+            for (int a = 0; a < 9; a ++)
+            {
+                Skin *skin = button[mode];
+                if (skin)
+                {
+                    ImageRect &rect = skin->getBorder();
+                    Image *image = rect.grid[a];
+                    if (image)
+                        image->setAlpha(mAlpha);
+                }
+            }
         }
     }
 }
@@ -283,6 +286,10 @@ void Button::draw(gcn::Graphics *graphics)
     else
         mode = BUTTON_STANDARD;
 
+    const Skin *const skin = button[mode];
+    if (!skin)
+        return;
+
     updateAlpha();
 
     Graphics *const g2 = static_cast<Graphics *const>(graphics);
@@ -318,13 +325,17 @@ void Button::draw(gcn::Graphics *graphics)
     {
         mRedraw = false;
         mMode = mode;
-        g2->calcWindow(mVertexes, 0, 0, getWidth(), getHeight(), button[mode]);
+        g2->calcWindow(mVertexes, 0, 0, getWidth(), getHeight(),
+            skin->getBorder());
     }
 
-    g2->drawImageRect2(mVertexes, button[mode]);
+    g2->drawImageRect2(mVertexes, skin->getBorder());
 
 //    g2->drawImageRect(0, 0, getWidth(), getHeight(), button[mode]);
 
+    const int padding = skin->getPadding();
+    const int spacing = skin->getOption("spacing");
+
     switch (mode)
     {
         case BUTTON_DISABLED:
@@ -356,21 +367,21 @@ void Button::draw(gcn::Graphics *graphics)
         case gcn::Graphics::LEFT:
             if (mImages)
             {
-                imageX = 4;
-                textX = 4 + mImageWidth + 2;
+                imageX = padding;
+                textX = padding + mImageWidth + spacing;
             }
             else
             {
-                textX = 4;
+                textX = padding;
             }
             break;
         case gcn::Graphics::CENTER:
             if (mImages)
             {
                 const int width = getFont()->getWidth(mCaption)
-                    + mImageWidth + 2;
+                    + mImageWidth + spacing;
                 imageX = getWidth() / 2 - width / 2;
-                textX = imageX + mImageWidth + 2;
+                textX = imageX + mImageWidth + spacing;
             }
             else
             {
@@ -378,8 +389,8 @@ void Button::draw(gcn::Graphics *graphics)
             }
             break;
         case gcn::Graphics::RIGHT:
-            textX = getWidth() - 4;
-            imageX = textX - getFont()->getWidth(mCaption) - 2;
+            textX = getWidth() - padding;
+            imageX = textX - getFont()->getWidth(mCaption) - spacing;
             break;
     }
 
@@ -434,19 +445,28 @@ void Button::widgetMoved(const gcn::Event &event A_UNUSED)
 void Button::adjustSize()
 {
     const gcn::Font *const font = getFont();
+    const Skin *const skin = button[BUTTON_STANDARD];
+    if (!skin)
+        return;
+    const int padding = skin->getPadding();
+
     if (mImages)
     {
-        setWidth(font->getWidth(mCaption)
-            + mImageWidth + 2 + 2 * mSpacing);
+        const int spacing = skin->getOption("spacing");
+        const int width = font->getWidth(mCaption);
+        if (width)
+            setWidth(width + mImageWidth + spacing + 2 * padding);
+        else
+            setWidth(mImageWidth + 2 * padding);
         int height = font->getHeight();
         if (height < mImageHeight)
             height = mImageHeight;
-        setHeight(height + 2 * mSpacing);
+        setHeight(height + 2 * padding);
     }
     else
     {
-        setWidth(font->getWidth(mCaption) + 2 * mSpacing);
-        setHeight(font->getHeight() + 2 * mSpacing);
+        setWidth(font->getWidth(mCaption) + 2 * padding);
+        setHeight(font->getHeight() + 2 * padding);
     }
 }
 
diff --git a/src/gui/widgets/button.h b/src/gui/widgets/button.h
index d7e7ebf89..84aedb078 100644
--- a/src/gui/widgets/button.h
+++ b/src/gui/widgets/button.h
@@ -31,6 +31,7 @@ class GraphicsVertexes;
 class Image;
 class ImageSet;
 class ImageRect;
+class Skin;
 
 const std::string BUTTON_PLAY = "buttonplay.png";
 
@@ -131,8 +132,8 @@ class Button final : public gcn::Button, public gcn::WidgetListener
     private:
         void init();
 
-        static ImageRect button[4];    /**< Button state graphics */
-        static int mInstances;         /**< Number of button instances */
+        static Skin *button[4];    /**< Button state graphics */
+        static int mInstances;     /**< Number of button instances */
         static float mAlpha;
 
         std::string mDescription;
-- 
cgit v1.2.3-70-g09d2