From 322aea1d69a47e8080bcd0552473a5a5d66c5e1a Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 5 Oct 2013 20:30:26 +0300 Subject: add dynamic labels string cutting. using it in char creation dialog. --- src/gui/widgets/label.cpp | 34 ++++++++++++++++++++++++++++++++++ src/gui/widgets/label.h | 2 ++ src/gui/windows/charcreatedialog.cpp | 7 ++++--- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/gui/widgets/label.cpp b/src/gui/widgets/label.cpp index 29bd39af9..d57268bdd 100644 --- a/src/gui/widgets/label.cpp +++ b/src/gui/widgets/label.cpp @@ -127,3 +127,37 @@ void Label::setForegroundColorAll(const gcn::Color &color1, mForegroundColor = color1; mForegroundColor2 = color2; } + +void Label::resizeTo(const int sz) +{ + const gcn::Font *const font = getFont(); + const int pad2 = 2 * mPadding; + setHeight(font->getHeight() + pad2); + + if (font->getWidth(mCaption) + pad2 > sz) + { + const int dots = font->getWidth("..."); + if (dots > sz) + { + setWidth(sz); + return; + } + const size_t szChars = mCaption.size(); + for (size_t f = 1; f < szChars - 1; f ++) + { + const std::string text = mCaption.substr(0, szChars - f); + const int width = font->getWidth(text) + dots + pad2; + if (width <= sz) + { + setCaption(text + "..."); + setWidth(width); + return; + } + } + setWidth(sz); + } + else + { + setWidth(font->getWidth(mCaption) + pad2); + } +} diff --git a/src/gui/widgets/label.h b/src/gui/widgets/label.h index 31d75b6ff..42f07d1c2 100644 --- a/src/gui/widgets/label.h +++ b/src/gui/widgets/label.h @@ -68,6 +68,8 @@ class Label final : public gcn::Label, public Widget2 void setForegroundColorAll(const gcn::Color &color1, const gcn::Color &color2); + void resizeTo(const int sz); + static Skin *mSkin; static int mInstances; diff --git a/src/gui/windows/charcreatedialog.cpp b/src/gui/windows/charcreatedialog.cpp index 7c09b0dfb..f6cd22720 100644 --- a/src/gui/windows/charcreatedialog.cpp +++ b/src/gui/windows/charcreatedialog.cpp @@ -587,7 +587,7 @@ void CharCreateDialog::updateHair() } const ItemInfo &item = ItemDB::get(-mHairStyle); mHairStyleNameLabel->setCaption(item.getName()); - mHairStyleNameLabel->adjustSize(); + mHairStyleNameLabel->resizeTo(150); if (ColorDB::getHairSize()) mHairColor %= ColorDB::getHairSize(); @@ -602,6 +602,7 @@ void CharCreateDialog::updateHair() } mHairColorNameLabel->setCaption(ColorDB::getHairColorName(mHairColor)); mHairColorNameLabel->adjustSize(); + mHairColorNameLabel->resizeTo(150); mPlayer->setSprite(Net::getCharServerHandler()->hairSprite(), mHairStyle * -1, item.getDyeColorsString(mHairColor)); @@ -638,12 +639,12 @@ void CharCreateDialog::updateLook() if (mRaceNameLabel) { mRaceNameLabel->setCaption(item.getName()); - mRaceNameLabel->adjustSize(); + mRaceNameLabel->resizeTo(150); } if (mLookNameLabel) { mLookNameLabel->setCaption(item.getColorName(mLook)); - mLookNameLabel->adjustSize(); + mLookNameLabel->resizeTo(150); } } -- cgit v1.2.3-70-g09d2