From 60add2c149c9c61bfbede5ae92cfe216927aca8a Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 27 Dec 2012 14:42:15 +0300 Subject: Improve a bit draw speed in other controls. --- src/gui/viewport.cpp | 16 +++++++---- src/gui/widgets/button.cpp | 38 ++++++++++++++++--------- src/gui/widgets/checkbox.cpp | 5 ++-- src/gui/widgets/dropdown.cpp | 5 ++-- src/gui/widgets/label.cpp | 11 +++---- src/gui/widgets/radiobutton.cpp | 5 ++-- src/gui/widgets/textbox.cpp | 29 +++++++++++++++++++ src/gui/widgets/textbox.h | 2 ++ src/gui/widgets/textfield.cpp | 5 ++-- src/gui/widgets/window.cpp | 18 ++++++++++-- src/guichan/include/guichan/widgets/textbox.hpp | 2 +- src/guichan/widgets/textbox.cpp | 26 ++--------------- 12 files changed, 103 insertions(+), 59 deletions(-) diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp index df5fe4353..ada563b8d 100644 --- a/src/gui/viewport.cpp +++ b/src/gui/viewport.cpp @@ -49,6 +49,8 @@ #include "resources/resourcemanager.h" +#include + #include "debug.h" extern volatile int tick_time; @@ -358,6 +360,8 @@ void Viewport::_drawPath(Graphics *const graphics, const Path &path, { graphics->setColor(color); + gcn::Font *const font = getFont(); + #ifdef MANASERV_SUPPORT if (Net::getNetworkType() != ServerInfo::MANASERV) #endif @@ -372,8 +376,9 @@ void Viewport::_drawPath(Graphics *const graphics, const Path &path, graphics->fillRectangle(gcn::Rectangle(squareX, squareY, 8, 8)); if (mMap) { - graphics->drawText(toString(cnt), - squareX + 4, squareY + 12, gcn::Graphics::CENTER); + const std::string str = toString(cnt); + font->drawString(graphics, str, squareX + 4 + - font->getWidth(str) / 2, squareY + 12); } cnt ++; } @@ -391,9 +396,10 @@ void Viewport::_drawPath(Graphics *const graphics, const Path &path, 8, 8)); if (mMap) { - graphics->drawText( - toString(mMap->getMetaTile(i->x / 32, i->y / 32)->Gcost), - squareX + 4, squareY + 12, gcn::Graphics::CENTER); + const std::string str = toString(mMap->getMetaTile( + i->x / 32, i->y / 32)->Gcost); + font->drawString(graphics, str, + squareX + 4 - font->getWidth(text) / 2, squareY + 12); } } } diff --git a/src/gui/widgets/button.cpp b/src/gui/widgets/button.cpp index a6ecfa7db..4606cca57 100644 --- a/src/gui/widgets/button.cpp +++ b/src/gui/widgets/button.cpp @@ -345,19 +345,23 @@ void Button::draw(gcn::Graphics *graphics) break; } - int textX = 0; - const int textY = getHeight() / 2 - getFont()->getHeight() / 2; int imageX = 0; int imageY = 0; + int textX = 0; + int textY = getHeight() / 2 - getFont()->getHeight() / 2; if (mImages) imageY = getHeight() / 2 - mImageHeight / 2; // need move calculation from draw!!! - switch (getAlignment()) + gcn::Font *const font = getFont(); + graphics->setFont(font); + + switch (mAlignment) { default: case gcn::Graphics::LEFT: + { if (mImages) { imageX = padding; @@ -368,27 +372,31 @@ void Button::draw(gcn::Graphics *graphics) textX = padding; } break; + } case gcn::Graphics::CENTER: + { + const int width = font->getWidth(mCaption); if (mImages) { - const int width = getFont()->getWidth(mCaption) - + mImageWidth + spacing; + const int width = width + mImageWidth + spacing; imageX = getWidth() / 2 - width / 2; - textX = imageX + mImageWidth + spacing; + textX = imageX + mImageWidth + spacing - width / 2; } else { - textX = getWidth() / 2; + textX = (getWidth() - width) / 2; } break; + } case gcn::Graphics::RIGHT: - textX = getWidth() - padding; - imageX = textX - getFont()->getWidth(mCaption) - spacing; + { + const int width = font->getWidth(mCaption); + textX = getWidth() - width - padding; + imageX = textX - width - spacing; break; + } } - graphics->setFont(getFont()); - if (openGLMode != 2) { if (recalc) @@ -427,9 +435,11 @@ void Button::draw(gcn::Graphics *graphics) } if (isPressed()) - g2->drawText(getCaption(), textX + 1, textY + 1, getAlignment()); - else - g2->drawText(getCaption(), textX, textY, getAlignment()); + { + textX ++; + textY ++; + } + font->drawString(g2, mCaption, textX, textY); BLOCK_END("Button::draw") } diff --git a/src/gui/widgets/checkbox.cpp b/src/gui/widgets/checkbox.cpp index 786c77fd2..75c3960db 100644 --- a/src/gui/widgets/checkbox.cpp +++ b/src/gui/widgets/checkbox.cpp @@ -94,10 +94,11 @@ void CheckBox::draw(gcn::Graphics* graphics) BLOCK_START("CheckBox::draw") drawBox(graphics); - graphics->setFont(getFont()); + gcn::Font *const font = getFont(); + graphics->setFont(font); graphics->setColor(mForegroundColor); - graphics->drawText(getCaption(), mPadding + mImageSize + mSpacing, + font->drawString(graphics, mCaption, mPadding + mImageSize + mSpacing, mPadding); BLOCK_END("CheckBox::draw") } diff --git a/src/gui/widgets/dropdown.cpp b/src/gui/widgets/dropdown.cpp index 8c8f6843f..ec564146e 100644 --- a/src/gui/widgets/dropdown.cpp +++ b/src/gui/widgets/dropdown.cpp @@ -227,9 +227,10 @@ void DropDown::draw(gcn::Graphics* graphics) if (mPopup->getListModel() && mPopup->getSelected() >= 0) { - graphics->setFont(getFont()); + gcn::Font *const font = getFont(); + graphics->setFont(font); graphics->setColor(mForegroundColor); - graphics->drawText(mPopup->getListModel()->getElementAt( + font->drawString(graphics, mPopup->getListModel()->getElementAt( mPopup->getSelected()), mPadding, mPadding); } diff --git a/src/gui/widgets/label.cpp b/src/gui/widgets/label.cpp index 2d565a571..9efa375d7 100644 --- a/src/gui/widgets/label.cpp +++ b/src/gui/widgets/label.cpp @@ -72,27 +72,28 @@ void Label::draw(gcn::Graphics* graphics) BLOCK_START("Label::draw") int textX; const int textY = getHeight() / 2 - getFont()->getHeight() / 2; + gcn::Font *const font = getFont(); + graphics->setFont(font); - switch (getAlignment()) + switch (mAlignment) { case Graphics::LEFT: default: textX = mPadding; break; case Graphics::CENTER: - textX = getWidth() / 2; + textX = (getWidth() - font->getWidth(mCaption)) / 2; break; case Graphics::RIGHT: if (getWidth() > mPadding) - textX = getWidth() - mPadding; + textX = getWidth() - mPadding - font->getWidth(mCaption); else textX = 0; break; } - graphics->setFont(getFont()); graphics->setColor(mForegroundColor); - graphics->drawText(getCaption(), textX, textY, getAlignment()); + font->drawString(graphics, mCaption, textX, textY); BLOCK_END("Label::draw") } diff --git a/src/gui/widgets/radiobutton.cpp b/src/gui/widgets/radiobutton.cpp index 1c56f787e..b2f38db4a 100644 --- a/src/gui/widgets/radiobutton.cpp +++ b/src/gui/widgets/radiobutton.cpp @@ -152,10 +152,11 @@ void RadioButton::draw(gcn::Graphics* graphics) BLOCK_START("RadioButton::draw") drawBox(graphics); - graphics->setFont(getFont()); + gcn::Font *const font = getFont(); + graphics->setFont(font); graphics->setColor(mForegroundColor); - graphics->drawText(getCaption(), mPadding + mImageSize + mSpacing, + font->drawString(graphics, mCaption, mPadding + mImageSize + mSpacing, mPadding); BLOCK_END("RadioButton::draw") } diff --git a/src/gui/widgets/textbox.cpp b/src/gui/widgets/textbox.cpp index 51933708b..976d7f772 100644 --- a/src/gui/widgets/textbox.cpp +++ b/src/gui/widgets/textbox.cpp @@ -338,3 +338,32 @@ void TextBox::keyPressed(gcn::KeyEvent& keyEvent) keyEvent.consume(); } + +void TextBox::draw(gcn::Graphics* graphics) +{ + BLOCK_START("TextBox::draw") + if (mOpaque) + { + graphics->setColor(mBackgroundColor); + graphics->fillRectangle(gcn::Rectangle(0, 0, getWidth(), getHeight())); + } + + if (isFocused() && isEditable()) + { + drawCaret(graphics, getFont()->getWidth( + mTextRows[mCaretRow].substr(0, mCaretColumn)), + mCaretRow * getFont()->getHeight()); + } + + graphics->setColor(mForegroundColor); + gcn::Font *const font = getFont(); + graphics->setFont(font); + const int fontHeight = font->getHeight(); + + for (size_t i = 0, sz = mTextRows.size(); i < sz; i++) + { + // Move the text one pixel so we can have a caret before a letter. + font->drawString(graphics, mTextRows[i], 1, i * fontHeight); + } + BLOCK_END("TextBox::draw") +} diff --git a/src/gui/widgets/textbox.h b/src/gui/widgets/textbox.h index 51402fad7..5271f742e 100644 --- a/src/gui/widgets/textbox.h +++ b/src/gui/widgets/textbox.h @@ -60,6 +60,8 @@ class TextBox final : public gcn::TextBox, void keyPressed(gcn::KeyEvent& keyEvent) override; + void draw(gcn::Graphics* graphics); + private: int mMinWidth; }; diff --git a/src/gui/widgets/textfield.cpp b/src/gui/widgets/textfield.cpp index fc9c73a68..02ad7699e 100644 --- a/src/gui/widgets/textfield.cpp +++ b/src/gui/widgets/textfield.cpp @@ -130,8 +130,9 @@ void TextField::draw(gcn::Graphics *graphics) } graphics->setColor(mForegroundColor); - graphics->setFont(getFont()); - graphics->drawText(mText, mPadding - mXScroll, mPadding); + gcn::Font *const font = getFont(); + graphics->setFont(font); + font->drawString(graphics, mText, mPadding - mXScroll, mPadding); BLOCK_END("TextField::draw") } diff --git a/src/gui/widgets/window.cpp b/src/gui/widgets/window.cpp index 52042f019..ffeac0cb1 100644 --- a/src/gui/widgets/window.cpp +++ b/src/gui/widgets/window.cpp @@ -36,6 +36,7 @@ #include #include +#include #include "debug.h" @@ -267,8 +268,21 @@ void Window::draw(gcn::Graphics *graphics) { g->setColor(mForegroundColor); g->setFont(mCaptionFont); - g->drawText(getCaption(), mCaptionOffsetX, mCaptionOffsetY, - static_cast(mCaptionAlign)); + int x; + switch (static_cast(mCaptionAlign)) + { + case Graphics::LEFT: + default: + x = mCaptionOffsetX; + break; + case Graphics::CENTER: + x = mCaptionOffsetX - mCaptionFont->getWidth(mCaption) / 2; + break; + case Graphics::RIGHT: + x = mCaptionOffsetX - mCaptionFont->getWidth(mCaption); + break; + } + mCaptionFont->drawString(g, mCaption, x, mCaptionOffsetY); } if (update) diff --git a/src/guichan/include/guichan/widgets/textbox.hpp b/src/guichan/include/guichan/widgets/textbox.hpp index e220bf46a..062ab9c8a 100644 --- a/src/guichan/include/guichan/widgets/textbox.hpp +++ b/src/guichan/include/guichan/widgets/textbox.hpp @@ -230,7 +230,7 @@ namespace gcn // Inherited from Widget - virtual void draw(Graphics* graphics); +// virtual void draw(Graphics* graphics); virtual void fontChanged(); diff --git a/src/guichan/widgets/textbox.cpp b/src/guichan/widgets/textbox.cpp index 66441894b..e743ef64f 100644 --- a/src/guichan/widgets/textbox.cpp +++ b/src/guichan/widgets/textbox.cpp @@ -118,33 +118,11 @@ namespace gcn adjustSize(); } +/* void TextBox::draw(Graphics* graphics) { - BLOCK_START("TextBox::draw") - if (mOpaque) - { - graphics->setColor(mBackgroundColor); - graphics->fillRectangle(Rectangle(0, 0, getWidth(), getHeight())); - } - - if (isFocused() && isEditable()) - { - drawCaret(graphics, getFont()->getWidth( - mTextRows[mCaretRow].substr(0, mCaretColumn)), - mCaretRow * getFont()->getHeight()); - } - - graphics->setColor(mForegroundColor); - graphics->setFont(getFont()); - - for (size_t i = 0, sz = mTextRows.size(); i < sz; i++) - { - // Move the text one pixel so we can have a caret before a letter. - graphics->drawText(mTextRows[i], 1, - static_cast(i * getFont()->getHeight())); - } - BLOCK_END("TextBox::draw") } +*/ void TextBox::drawCaret(Graphics* graphics, int x, int y) { -- cgit v1.2.3-60-g2f50