From 0bcc82648c49db05980e879a13baba2c76264bd4 Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Mon, 25 May 2015 17:55:41 +0300
Subject: Add safeDraw method into progressbar.

---
 src/gui/widgets/progressbar.cpp | 199 ++++++++++++++++++++++++----------------
 src/gui/widgets/progressbar.h   |   7 +-
 2 files changed, 123 insertions(+), 83 deletions(-)

diff --git a/src/gui/widgets/progressbar.cpp b/src/gui/widgets/progressbar.cpp
index 9acb8386c..da11c779d 100644
--- a/src/gui/widgets/progressbar.cpp
+++ b/src/gui/widgets/progressbar.cpp
@@ -151,105 +151,103 @@ void ProgressBar::updateAlpha()
 void ProgressBar::draw(Graphics *graphics)
 {
     BLOCK_START("ProgressBar::draw")
+    if (!mSkin)
+    {
+        BLOCK_END("ProgressBar::draw")
+        return;
+    }
+
     updateAlpha();
     mBackgroundColor.a = static_cast<int>(mAlpha * 255);
-    render(graphics);
-    BLOCK_END("ProgressBar::draw")
-}
-
-void ProgressBar::setProgress(const float progress)
-{
-    const float p = std::min(1.0F, std::max(0.0F, progress));
-    mProgressToGo = p;
-    mRedraw = true;
-
-    if (!mSmoothProgress)
-        mProgress = p;
 
-    if (mProgressPalette >= 0)
+    if (mRedraw || graphics->getRedraw())
     {
-        mBackgroundColorToGo = Theme::getProgressColor(
-            mProgressPalette, progress);
+        mRedraw = false;
+        mVertexes->clear();
+        graphics->calcWindow(mVertexes, 0, 0,
+            mDimension.width, mDimension.height, mSkin->getBorder());
+        if (mFillImage)
+        {
+            const unsigned int pad = 2 * mFillPadding;
+            const int maxWidth = mDimension.width - pad;
+            int width = static_cast<int>(mProgress
+                * static_cast<float>(maxWidth));
+            if (width > 0)
+            {
+                if (width > maxWidth)
+                    width = maxWidth;
+                graphics->calcWindow(mVertexes, mFillPadding, mFillPadding,
+                    width, mDimension.height - pad, mFillRect);
+            }
+        }
+        graphics->finalize(mVertexes);
     }
-}
 
-void ProgressBar::setProgressPalette(const int progressPalette)
-{
-    const int oldPalette = mProgressPalette;
-    mProgressPalette = progressPalette;
-    mRedraw = true;
+    graphics->drawTileCollection(mVertexes);
 
-    if (mProgressPalette != oldPalette && mProgressPalette >= 0)
+    // The bar
+    if (!mFillImage && mProgress > 0)
     {
-        mBackgroundColorToGo = Theme::getProgressColor(
-            mProgressPalette, mProgressToGo);
+        graphics->setColor(mBackgroundColor);
+        const unsigned int pad = 2 * mFillPadding;
+        const int maxWidth = mDimension.width - pad;
+        int width = static_cast<int>(mProgress * static_cast<float>(maxWidth));
+        if (width > 0)
+        {
+            if (width > maxWidth)
+                width = maxWidth;
+            graphics->fillRectangle(Rect(mFillPadding, mFillPadding,
+                width, mDimension.height - pad));
+        }
     }
-}
 
-void ProgressBar::setBackgroundColor(const Color &color)
-{
-    mRedraw = true;
-    mBackgroundColorToGo = color;
+    // The label
+    if (!mText.empty())
+    {
+        const Color oldColor = graphics->getColor();
 
-    if (!mSmoothColorChange)
-        mBackgroundColor = color;
-}
+        Font *const font = gui->getFont();
+        const int textX = mDimension.width / 2;
+        const int textY = (mDimension.height - font->getHeight()) / 2;
 
-void ProgressBar::setColor(const Color &color1, const Color &color2)
-{
-    mForegroundColor = color1;
-    mForegroundColor2 = color2;
+        font->drawString(graphics,
+            mForegroundColor,
+            mForegroundColor2,
+            mText,
+            textX - font->getWidth(mText) / 2,
+            textY);
+
+        graphics->setColor(oldColor);
+    }
+    BLOCK_END("ProgressBar::draw")
 }
 
-void ProgressBar::render(Graphics *graphics)
+void ProgressBar::safeDraw(Graphics *graphics)
 {
+    BLOCK_START("ProgressBar::safeDraw")
     if (!mSkin)
+    {
+        BLOCK_END("ProgressBar::safeDraw")
         return;
+    }
 
-    if (isBatchDrawRenders(openGLMode))
-    {
-        if (mRedraw || graphics->getRedraw())
-        {
-            mRedraw = false;
-            mVertexes->clear();
-            graphics->calcWindow(mVertexes, 0, 0,
-                mDimension.width, mDimension.height, mSkin->getBorder());
-            if (mFillImage)
-            {
-                const unsigned int pad = 2 * mFillPadding;
-                const int maxWidth = mDimension.width - pad;
-                int width = static_cast<int>(mProgress
-                    * static_cast<float>(maxWidth));
-                if (width > 0)
-                {
-                    if (width > maxWidth)
-                        width = maxWidth;
-                    graphics->calcWindow(mVertexes, mFillPadding, mFillPadding,
-                        width, mDimension.height - pad, mFillRect);
-                }
-            }
-            graphics->finalize(mVertexes);
-        }
+    updateAlpha();
+    mBackgroundColor.a = static_cast<int>(mAlpha * 255);
 
-        graphics->drawTileCollection(mVertexes);
-    }
-    else
+    graphics->drawImageRect(0, 0, mDimension.width, mDimension.height,
+        mSkin->getBorder());
+    if (mFillImage)
     {
-        graphics->drawImageRect(0, 0, mDimension.width, mDimension.height,
-            mSkin->getBorder());
-        if (mFillImage)
+        const unsigned int pad = 2 * mFillPadding;
+        const int maxWidth = mDimension.width - pad;
+        int width = static_cast<int>(mProgress
+            * static_cast<float>(maxWidth));
+        if (width > 0)
         {
-            const unsigned int pad = 2 * mFillPadding;
-            const int maxWidth = mDimension.width - pad;
-            int width = static_cast<int>(mProgress
-                * static_cast<float>(maxWidth));
-            if (width > 0)
-            {
-                if (width > maxWidth)
-                    width = maxWidth;
-                graphics->drawImageRect(mFillPadding, mFillPadding,
-                    width, mDimension.height - pad, mFillRect);
-            }
+            if (width > maxWidth)
+                width = maxWidth;
+            graphics->drawImageRect(mFillPadding, mFillPadding,
+                width, mDimension.height - pad, mFillRect);
         }
     }
 
@@ -287,6 +285,51 @@ void ProgressBar::render(Graphics *graphics)
 
         graphics->setColor(oldColor);
     }
+    BLOCK_END("ProgressBar::safeDraw")
+}
+
+void ProgressBar::setProgress(const float progress)
+{
+    const float p = std::min(1.0F, std::max(0.0F, progress));
+    mProgressToGo = p;
+    mRedraw = true;
+
+    if (!mSmoothProgress)
+        mProgress = p;
+
+    if (mProgressPalette >= 0)
+    {
+        mBackgroundColorToGo = Theme::getProgressColor(
+            mProgressPalette, progress);
+    }
+}
+
+void ProgressBar::setProgressPalette(const int progressPalette)
+{
+    const int oldPalette = mProgressPalette;
+    mProgressPalette = progressPalette;
+    mRedraw = true;
+
+    if (mProgressPalette != oldPalette && mProgressPalette >= 0)
+    {
+        mBackgroundColorToGo = Theme::getProgressColor(
+            mProgressPalette, mProgressToGo);
+    }
+}
+
+void ProgressBar::setBackgroundColor(const Color &color)
+{
+    mRedraw = true;
+    mBackgroundColorToGo = color;
+
+    if (!mSmoothColorChange)
+        mBackgroundColor = color;
+}
+
+void ProgressBar::setColor(const Color &color1, const Color &color2)
+{
+    mForegroundColor = color1;
+    mForegroundColor2 = color2;
 }
 
 void ProgressBar::widgetResized(const Event &event A_UNUSED)
diff --git a/src/gui/widgets/progressbar.h b/src/gui/widgets/progressbar.h
index 2a379a998..053710ae8 100644
--- a/src/gui/widgets/progressbar.h
+++ b/src/gui/widgets/progressbar.h
@@ -73,6 +73,8 @@ class ProgressBar final : public Widget,
          */
         void draw(Graphics *graphics) override final;
 
+        void safeDraw(Graphics *graphics) override final;
+
         /**
          * Sets the current progress.
          */
@@ -127,11 +129,6 @@ class ProgressBar final : public Widget,
         void setSmoothColorChange(bool smoothColorChange)
         { mSmoothColorChange = smoothColorChange; }
 
-        /**
-         * Renders a progressbar with the given properties.
-         */
-        void render(Graphics *graphics);
-
         void widgetResized(const Event &event) override final;
 
         void widgetMoved(const Event &event) override final;
-- 
cgit v1.2.3-70-g09d2