From 893df56fe05fb0e865fda77477c37200bbab018b Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 6 Sep 2015 15:41:27 +0300 Subject: Use cached image for drawing text in progressbar. --- src/gui/widgets/progressbar.cpp | 79 ++++++++++++++++++++++++++++------------- src/gui/widgets/progressbar.h | 9 +++-- 2 files changed, 61 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/gui/widgets/progressbar.cpp b/src/gui/widgets/progressbar.cpp index 59951c960..294a8f610 100644 --- a/src/gui/widgets/progressbar.cpp +++ b/src/gui/widgets/progressbar.cpp @@ -47,6 +47,7 @@ ProgressBar::ProgressBar(const Widget2 *const widget, Widget(widget), WidgetListener(), mFillRect(), + mTextChunk(), mSkin(nullptr), mProgress(progress), mProgressToGo(progress), @@ -58,7 +59,8 @@ ProgressBar::ProgressBar(const Widget2 *const widget, mFillPadding(3), mFillImage(false), mSmoothProgress(true), - mSmoothColorChange(true) + mSmoothColorChange(true), + mTextChanged(true) { mBackgroundColor = Theme::getProgressColor( backColor >= ProgressColorId::PROG_HP @@ -106,6 +108,7 @@ ProgressBar::~ProgressBar() } Theme::unloadRect(mFillRect); delete2(mVertexes); + mTextChunk.deleteImage(); } void ProgressBar::logic() @@ -204,20 +207,25 @@ void ProgressBar::draw(Graphics *graphics) // The label if (!mText.empty()) { - const Color oldColor = graphics->getColor(); - Font *const font = gui->getFont(); - const int textX = mDimension.width / 2; - const int textY = (mDimension.height - font->getHeight()) / 2; - - font->drawString(graphics, - mForegroundColor, - mForegroundColor2, - mText, - textX - font->getWidth(mText) / 2, - textY); + if (mTextChanged) + { + mTextChunk.textFont = font; + mTextChunk.deleteImage(); + mTextChunk.text = mText; + mTextChunk.color = mForegroundColor; + mTextChunk.color2 = mForegroundColor2; + font->generate(mTextChunk); + mTextChanged = false; + } - graphics->setColor(oldColor); + const Image *const image = mTextChunk.img; + if (image) + { + const int textX = (mDimension.width - font->getWidth(mText)) / 2; + const int textY = (mDimension.height - font->getHeight()) / 2; + graphics->drawImage(image, textX, textY); + } } BLOCK_END("ProgressBar::draw") } @@ -270,20 +278,25 @@ void ProgressBar::safeDraw(Graphics *graphics) // The label if (!mText.empty()) { - const Color oldColor = graphics->getColor(); - Font *const font = gui->getFont(); - const int textX = mDimension.width / 2; - const int textY = (mDimension.height - font->getHeight()) / 2; - - font->drawString(graphics, - mForegroundColor, - mForegroundColor2, - mText, - textX - font->getWidth(mText) / 2, - textY); + if (mTextChanged) + { + mTextChunk.textFont = font; + mTextChunk.deleteImage(); + mTextChunk.text = mText; + mTextChunk.color = mForegroundColor; + mTextChunk.color2 = mForegroundColor2; + font->generate(mTextChunk); + mTextChanged = false; + } - graphics->setColor(oldColor); + const Image *const image = mTextChunk.img; + if (image) + { + const int textX = (mDimension.width - font->getWidth(mText)) / 2; + const int textY = (mDimension.height - font->getHeight()) / 2; + graphics->drawImage(image, textX, textY); + } } BLOCK_END("ProgressBar::safeDraw") } @@ -331,6 +344,7 @@ void ProgressBar::setColor(const Color &color1, const Color &color2) { mForegroundColor = color1; mForegroundColor2 = color2; + mTextChanged = true; } void ProgressBar::widgetResized(const Event &event A_UNUSED) @@ -342,3 +356,18 @@ void ProgressBar::widgetMoved(const Event &event A_UNUSED) { mRedraw = true; } + +void ProgressBar::setText(const std::string &str) +{ + if (mText != str) + { + mText = str; + mTextChanged = true; + } +} + +void ProgressBar::widgetHidden(const Event &event A_UNUSED) +{ + mTextChanged = true; + mTextChunk.deleteImage(); +} diff --git a/src/gui/widgets/progressbar.h b/src/gui/widgets/progressbar.h index f9a5f1e62..120569795 100644 --- a/src/gui/widgets/progressbar.h +++ b/src/gui/widgets/progressbar.h @@ -23,6 +23,8 @@ #ifndef GUI_WIDGETS_PROGRESSBAR_H #define GUI_WIDGETS_PROGRESSBAR_H +#include "gui/fonts/textchunk.h" + #include "gui/widgets/widget.h" #include "listeners/widgetlistener.h" @@ -108,8 +110,7 @@ class ProgressBar final : public Widget, /** * Sets the text shown on the progress bar. */ - void setText(const std::string &str) - { mText = str; } + void setText(const std::string &str); /** * Returns the text shown on the progress bar. @@ -133,11 +134,14 @@ class ProgressBar final : public Widget, void widgetMoved(const Event &event) override final; + void widgetHidden(const Event &event) override final; + void setPadding(unsigned int padding) { mPadding = padding; } private: ImageRect mFillRect; + TextChunk mTextChunk; Skin *mSkin; float mProgress; float mProgressToGo; @@ -156,6 +160,7 @@ class ProgressBar final : public Widget, bool mFillImage; bool mSmoothProgress; bool mSmoothColorChange; + bool mTextChanged; }; #endif // GUI_WIDGETS_PROGRESSBAR_H -- cgit v1.2.3-70-g09d2