diff options
author | Andrei Karas <akaras@inbox.ru> | 2015-05-24 15:45:13 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2015-05-24 15:45:13 +0300 |
commit | 351e16d2f290a5b30981b7beae87d5d8a1a30662 (patch) | |
tree | ba64ab4843a87678cfff6d45e13d18648d132a11 | |
parent | 07cef0090ecb3f9b904afbaeaf7b2e2449f34794 (diff) | |
download | manaplus-351e16d2f290a5b30981b7beae87d5d8a1a30662.tar.gz manaplus-351e16d2f290a5b30981b7beae87d5d8a1a30662.tar.bz2 manaplus-351e16d2f290a5b30981b7beae87d5d8a1a30662.tar.xz manaplus-351e16d2f290a5b30981b7beae87d5d8a1a30662.zip |
Improve checkbox draw speed.
Use local text image and improve a bit calculations in draw.
-rw-r--r-- | src/gui/widgets/checkbox.cpp | 49 | ||||
-rw-r--r-- | src/gui/widgets/checkbox.h | 18 |
2 files changed, 60 insertions, 7 deletions
diff --git a/src/gui/widgets/checkbox.cpp b/src/gui/widgets/checkbox.cpp index 8bcb583c7..e2141058c 100644 --- a/src/gui/widgets/checkbox.cpp +++ b/src/gui/widgets/checkbox.cpp @@ -91,14 +91,18 @@ CheckBox::CheckBox(const Widget2 *const widget, Widget(widget), ToolTipListener(), KeyListener(), + WidgetListener(), mSelected(selected), mCaption(), + mTextChunk(), mPadding(0), mImagePadding(0), mImageSize(9), mSpacing(2), + mTextX(0 + 9 + 2), mHasMouse(false), - mDrawBox(true) + mDrawBox(true), + mTextChanged(true) { setCaption(caption); mAllowLogic = false; @@ -133,12 +137,16 @@ CheckBox::CheckBox(const Widget2 *const widget, mImageSize = mSkin->getOption("imageSize"); mSpacing = mSkin->getOption("spacing"); mDrawBox = mSkin->getOption("drawBox", 1); + mTextX = mPadding + mImageSize + mSpacing; } adjustSize(); } CheckBox::~CheckBox() { + if (mWindow) + mWindow->removeWidgetListener(this); + if (gui) gui->removeDragged(this); @@ -157,9 +165,22 @@ void CheckBox::draw(Graphics *const graphics) drawBox(graphics); Font *const font = getFont(); - graphics->setColorAll(mForegroundColor, mForegroundColor2); - font->drawString(graphics, mCaption, mPadding + mImageSize + mSpacing, - mPadding); + + if (mTextChanged) + { + mTextChunk.textFont = font; + mTextChunk.deleteImage(); + mTextChunk.text = mCaption; + mTextChunk.color = mForegroundColor; + mTextChunk.color2 = mForegroundColor2; + font->generate(mTextChunk); + mTextChanged = false; + } + + const Image *const image = mTextChunk.img; + if (image) + graphics->drawImage(image, mTextX, mPadding); + BLOCK_END("CheckBox::draw") } @@ -276,3 +297,23 @@ void CheckBox::toggleSelected() mSelected = !mSelected; distributeActionEvent(); } + +void CheckBox::setCaption(const std::string& caption) +{ + if (caption != mCaption) + mTextChanged = true; + mCaption = caption; +} + +void CheckBox::setParent(Widget *widget) +{ + if (mWindow) + mWindow->addWidgetListener(this); + Widget::setParent(widget); +} + +void CheckBox::widgetHidden(const Event &event A_UNUSED) +{ + mTextChanged = true; + mTextChunk.deleteImage(); +} diff --git a/src/gui/widgets/checkbox.h b/src/gui/widgets/checkbox.h index d3ed8b12f..0aa3a6faf 100644 --- a/src/gui/widgets/checkbox.h +++ b/src/gui/widgets/checkbox.h @@ -69,6 +69,10 @@ #include "listeners/keylistener.h" #include "listeners/tooltiplistener.h" +#include "listeners/widgetlistener.h" + +#include "gui/fonts/textchunk.h" + #include "gui/widgets/widget.h" #include "localconsts.h" @@ -82,7 +86,8 @@ class Skin; */ class CheckBox final : public Widget, public ToolTipListener, - public KeyListener + public KeyListener, + public WidgetListener { public: /** @@ -165,13 +170,16 @@ class CheckBox final : public Widget, * @param caption The caption of the check box. * @see getCaption, adjustSize */ - void setCaption(const std::string& caption) - { mCaption = caption; } + void setCaption(const std::string& caption); void mouseClicked(MouseEvent& event) override final; void mouseDragged(MouseEvent& event) override final; + void setParent(Widget *widget) override final; + + void widgetHidden(const Event &event) override final; + private: void toggleSelected(); @@ -185,12 +193,16 @@ class CheckBox final : public Widget, */ std::string mCaption; + TextChunk mTextChunk; + int mPadding; int mImagePadding; int mImageSize; int mSpacing; + int mTextX; bool mHasMouse; bool mDrawBox; + bool mTextChanged; static int instances; static Skin *mSkin; |