From 34dcb4127a5d66c890e3da74b77e150a75c88bea Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Fri, 3 Feb 2023 18:55:25 +0200 Subject: [PATCH] Removed text utilities from SDL_Extensions --- Global.h | 21 ++++++ client/adventureMap/CResDataBar.cpp | 25 ++++--- client/adventureMap/CResDataBar.h | 3 +- client/battle/BattleInterfaceClasses.cpp | 2 +- client/battle/BattleStacksController.cpp | 2 +- client/gui/CGuiHandler.cpp | 2 +- client/renderSDL/SDL_Extensions.cpp | 9 --- client/renderSDL/SDL_Extensions.h | 86 +++++++++--------------- client/widgets/CGarrisonInt.cpp | 2 +- client/widgets/MiscWidgets.cpp | 25 ++++--- client/widgets/MiscWidgets.h | 2 + client/windows/CCreatureWindow.cpp | 2 +- client/windows/CMessage.cpp | 1 + client/windows/GUIClasses.cpp | 4 +- 14 files changed, 94 insertions(+), 92 deletions(-) diff --git a/Global.h b/Global.h index 39ac92848..b1494ba82 100644 --- a/Global.h +++ b/Global.h @@ -725,6 +725,27 @@ namespace vstd return a + (b - a) * f; } + /// converts number into string using metric system prefixes, e.g. 'k' or 'M' to keep resulting strings within specified size + template + std::string formatMetric(IntType number, int maxLength) + { + IntType max = pow(10, maxLength); + if (std::abs(number) < max) + return boost::lexical_cast(number); + + std::string symbols = " kMGTPE"; + auto iter = symbols.begin(); + + while (number >= max) + { + number /= 1000; + iter++; + + assert(iter != symbols.end());//should be enough even for int64 + } + return std::to_string(number) + *iter; + } + using boost::math::round; } using vstd::operator-=; diff --git a/client/adventureMap/CResDataBar.cpp b/client/adventureMap/CResDataBar.cpp index f2d030fff..64e45939c 100644 --- a/client/adventureMap/CResDataBar.cpp +++ b/client/adventureMap/CResDataBar.cpp @@ -45,8 +45,6 @@ CResDataBar::CResDataBar(const std::string & defname, int x, int y, int offx, in txtpos[i].second = pos.y + offy; } txtpos[7].first = txtpos[6].first + datedist; - datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63] - + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s"; addUsedEvents(RCLICK); } @@ -69,12 +67,23 @@ CResDataBar::CResDataBar() txtpos[i].second = pos.y + ADVOPT.resOffsetY; } txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist; - datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63] - + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s"; + } CResDataBar::~CResDataBar() = default; +std::string CResDataBar::buildDateString() +{ + std::string pattern = "%s: %d, %s: %d, %s: %d"; + + auto formatted = boost::format(pattern) + % CGI->generaltexth->allTexts[62] % LOCPLINT->cb->getDate(Date::MONTH) + % CGI->generaltexth->allTexts[63] % LOCPLINT->cb->getDate(Date::WEEK) + % CGI->generaltexth->allTexts[64] % LOCPLINT->cb->getDate(Date::DAY_OF_WEEK); + + return boost::str(formatted); +} + void CResDataBar::draw(SDL_Surface * to) { //TODO: all this should be labels, but they require proper text update on change @@ -84,13 +93,7 @@ void CResDataBar::draw(SDL_Surface * to) graphics->fonts[FONT_SMALL]->renderTextLeft(to, text, Colors::WHITE, Point(txtpos[i].first,txtpos[i].second)); } - std::vector temp; - - temp.push_back(boost::lexical_cast(LOCPLINT->cb->getDate(Date::MONTH))); - temp.push_back(boost::lexical_cast(LOCPLINT->cb->getDate(Date::WEEK))); - temp.push_back(boost::lexical_cast(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK))); - - graphics->fonts[FONT_SMALL]->renderTextLeft(to, CSDL_Ext::processStr(datetext,temp), Colors::WHITE, Point(txtpos[7].first,txtpos[7].second)); + graphics->fonts[FONT_SMALL]->renderTextLeft(to, buildDateString(), Colors::WHITE, Point(txtpos[7].first,txtpos[7].second)); } void CResDataBar::show(SDL_Surface * to) diff --git a/client/adventureMap/CResDataBar.h b/client/adventureMap/CResDataBar.h index 98598451e..821618c8a 100644 --- a/client/adventureMap/CResDataBar.h +++ b/client/adventureMap/CResDataBar.h @@ -15,11 +15,12 @@ /// Current date is displayed too class CResDataBar : public CIntObject { + std::string buildDateString(); + public: std::shared_ptr background; std::vector > txtpos; - std::string datetext; void clickRight(tribool down, bool previousState) override; CResDataBar(); diff --git a/client/battle/BattleInterfaceClasses.cpp b/client/battle/BattleInterfaceClasses.cpp index 988ea70c0..5ff49d0dc 100644 --- a/client/battle/BattleInterfaceClasses.cpp +++ b/client/battle/BattleInterfaceClasses.cpp @@ -789,7 +789,7 @@ void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn) if (unit->unitType()->idNumber == CreatureID::ARROW_TOWERS) icon->setFrame(owner->getSiegeShooterIconID(), 1); - amount->setText(CSDL_Ext::makeNumberShort(unit->getCount(), 4)); + amount->setText(vstd::formatMetric(unit->getCount(), 4)); if(stateIcon) { diff --git a/client/battle/BattleStacksController.cpp b/client/battle/BattleStacksController.cpp index b203c99b7..75c3acb8f 100644 --- a/client/battle/BattleStacksController.cpp +++ b/client/battle/BattleStacksController.cpp @@ -316,7 +316,7 @@ void BattleStacksController::showStackAmountBox(Canvas & canvas, const CStack * //blitting amount Point textPos = stackAnimation[stack->ID]->pos.topLeft() + amountBG->dimensions()/2 + Point(xAdd, yAdd); - canvas.drawText(textPos, EFonts::FONT_TINY, Colors::WHITE, ETextAlignment::CENTER, CSDL_Ext::makeNumberShort(stack->getCount(), 4)); + canvas.drawText(textPos, EFonts::FONT_TINY, Colors::WHITE, ETextAlignment::CENTER, vstd::formatMetric(stack->getCount(), 4)); } void BattleStacksController::showStack(Canvas & canvas, const CStack * stack) diff --git a/client/gui/CGuiHandler.cpp b/client/gui/CGuiHandler.cpp index b34ce76b4..3aae7b87a 100644 --- a/client/gui/CGuiHandler.cpp +++ b/client/gui/CGuiHandler.cpp @@ -742,7 +742,7 @@ const Point & CGuiHandler::getCursorPosition() const Point CGuiHandler::screenDimensions() const { - return return Point(screen->w, screen->h); + return Point(screen->w, screen->h); } bool CGuiHandler::isMouseButtonPressed() const diff --git a/client/renderSDL/SDL_Extensions.cpp b/client/renderSDL/SDL_Extensions.cpp index 4a44d7f83..e0996cdd1 100644 --- a/client/renderSDL/SDL_Extensions.cpp +++ b/client/renderSDL/SDL_Extensions.cpp @@ -558,15 +558,6 @@ uint8_t * CSDL_Ext::getPxPtr(const SDL_Surface * const &srf, const int x, const return (uint8_t *)srf->pixels + y * srf->pitch + x * srf->format->BytesPerPixel; } -std::string CSDL_Ext::processStr(std::string str, std::vector & tor) -{ - for (size_t i=0; (i -std::string makeNumberShort(IntType number, IntType maxLength = 3) //the output is a string containing at most 5 characters [4 if positive] (eg. intead 10000 it gives 10k) -{ - IntType max = pow(10, maxLength); - if (std::abs(number) < max) - return boost::lexical_cast(number); - - std::string symbols = " kMGTPE"; - auto iter = symbols.begin(); - - while (number >= max) - { - number /= 1000; - iter++; - - assert(iter != symbols.end());//should be enough even for int64 - } - return boost::lexical_cast(number) + *iter; -} - -Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy); - -typedef void (*TColorPutter)(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B); -typedef void (*TColorPutterAlpha)(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A); - - void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst); - void blitAt(SDL_Surface * src, const Rect & pos, SDL_Surface * dst); - - void setClipRect(SDL_Surface * src, const Rect & other); - void getClipRect(SDL_Surface * src, Rect & other); - - void blitSurface(SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dest); - void blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest); +/// creates SDL_Color using provided Color +SDL_Color toSDL(const ColorRGBA & color); + +void setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors); +void setAlpha(SDL_Surface * bg, int value); + +Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy); + +typedef void (*TColorPutter)(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B); +typedef void (*TColorPutterAlpha)(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A); + + void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst); + void blitAt(SDL_Surface * src, const Rect & pos, SDL_Surface * dst); + + void setClipRect(SDL_Surface * src, const Rect & other); + void getClipRect(SDL_Surface * src, Rect & other); + + void blitSurface(SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dest); + void blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest); void fillSurface(SDL_Surface *dst, const SDL_Color & color); void fillRect(SDL_Surface *dst, const Rect & dstrect, const SDL_Color & color); @@ -109,14 +89,14 @@ typedef void (*TColorPutterAlpha)(uint8_t *&ptr, const uint8_t & R, const uint8_ void drawLine(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2); void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const SDL_Color &color, int depth = 1); void drawBorder(SDL_Surface * sur, const Rect &r, const SDL_Color &color, int depth = 1); - void drawDashedBorder(SDL_Surface * sur, const Rect &r, const SDL_Color &color); - void setPlayerColor(SDL_Surface * sur, PlayerColor player); //sets correct color of flags; -1 for neutral - std::string processStr(std::string str, std::vector & tor); //replaces %s in string - SDL_Surface * newSurface(int w, int h, SDL_Surface * mod); //creates new surface, with flags/format same as in surface given - SDL_Surface * newSurface(int w, int h); //creates new surface, with flags/format same as in screen surface - SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface - template - SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value + void drawDashedBorder(SDL_Surface * sur, const Rect &r, const SDL_Color &color); + void setPlayerColor(SDL_Surface * sur, PlayerColor player); //sets correct color of flags; -1 for neutral + + SDL_Surface * newSurface(int w, int h, SDL_Surface * mod); //creates new surface, with flags/format same as in surface given + SDL_Surface * newSurface(int w, int h); //creates new surface, with flags/format same as in screen surface + SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface + template + SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis //scale surface to required size. diff --git a/client/widgets/CGarrisonInt.cpp b/client/widgets/CGarrisonInt.cpp index a3672eb09..6a0b7a178 100644 --- a/client/widgets/CGarrisonInt.cpp +++ b/client/widgets/CGarrisonInt.cpp @@ -374,7 +374,7 @@ void CGarrisonSlot::update() creatureImage->setFrame(creature->getIconIndex()); stackCount->enable(); - stackCount->setText(CSDL_Ext::makeNumberShort(myStack->count, 4)); + stackCount->setText(vstd::formatMetric(myStack->count, 4)); } else { diff --git a/client/widgets/MiscWidgets.cpp b/client/widgets/MiscWidgets.cpp index e82109ce3..57a440c38 100644 --- a/client/widgets/MiscWidgets.cpp +++ b/client/widgets/MiscWidgets.cpp @@ -182,6 +182,18 @@ void CMinorResDataBar::show(SDL_Surface * to) { } +std::string CMinorResDataBar::buildDateString() +{ + std::string pattern = "%s: %d, %s: %d, %s: %d"; + + auto formatted = boost::format(pattern) + % CGI->generaltexth->allTexts[62] % LOCPLINT->cb->getDate(Date::MONTH) + % CGI->generaltexth->allTexts[63] % LOCPLINT->cb->getDate(Date::WEEK) + % CGI->generaltexth->allTexts[64] % LOCPLINT->cb->getDate(Date::DAY_OF_WEEK); + + return boost::str(formatted); +} + void CMinorResDataBar::showAll(SDL_Surface * to) { CIntObject::showAll(to); @@ -192,16 +204,7 @@ void CMinorResDataBar::showAll(SDL_Surface * to) graphics->fonts[FONT_SMALL]->renderTextCenter(to, text, Colors::WHITE, Point(pos.x + 50 + 76 * i, pos.y + pos.h/2)); } - std::vector temp; - - temp.push_back(boost::lexical_cast(LOCPLINT->cb->getDate(Date::MONTH))); - temp.push_back(boost::lexical_cast(LOCPLINT->cb->getDate(Date::WEEK))); - temp.push_back(boost::lexical_cast(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK))); - - std::string datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63] - + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s"; - - graphics->fonts[FONT_SMALL]->renderTextCenter(to, CSDL_Ext::processStr(datetext,temp), Colors::WHITE, Point(pos.x+545+(pos.w-545)/2,pos.y+pos.h/2)); + graphics->fonts[FONT_SMALL]->renderTextCenter(to, buildDateString(), Colors::WHITE, Point(pos.x+545+(pos.w-545)/2,pos.y+pos.h/2)); } CMinorResDataBar::CMinorResDataBar() @@ -248,7 +251,7 @@ void CArmyTooltip::init(const InfoAboutArmy &army) std::string subtitle; if(army.army.isDetailed) { - subtitle = CSDL_Ext::makeNumberShort(slot.second.count, 4); + subtitle = vstd::formatMetric(slot.second.count, 4); } else { diff --git a/client/widgets/MiscWidgets.h b/client/widgets/MiscWidgets.h index f97a41bbf..686403346 100644 --- a/client/widgets/MiscWidgets.h +++ b/client/widgets/MiscWidgets.h @@ -117,6 +117,8 @@ public: class CMinorResDataBar : public CIntObject { std::shared_ptr background; + + std::string buildDateString(); public: void show(SDL_Surface * to) override; void showAll(SDL_Surface * to) override; diff --git a/client/windows/CCreatureWindow.cpp b/client/windows/CCreatureWindow.cpp index 8109bdc52..87c7063fc 100644 --- a/client/windows/CCreatureWindow.cpp +++ b/client/windows/CCreatureWindow.cpp @@ -582,7 +582,7 @@ CStackWindow::MainSection::MainSection(CStackWindow * owner, int yOffset, bool s } expLabel = std::make_shared( pos.x + 21, pos.y + 52, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, - CSDL_Ext::makeNumberShort(stack->experience, 6)); + vstd::formatMetric(stack->experience, 6)); } if(showArt) diff --git a/client/windows/CMessage.cpp b/client/windows/CMessage.cpp index dd51e9b03..27b7c825a 100644 --- a/client/windows/CMessage.cpp +++ b/client/windows/CMessage.cpp @@ -18,6 +18,7 @@ #include "../widgets/Buttons.h" #include "../widgets/CComponent.h" #include "../widgets/TextControls.h" +#include "../gui/CGuiHandler.h" #include "../render/CAnimation.h" #include "../render/IImage.h" #include "../renderSDL/SDL_Extensions.h" diff --git a/client/windows/GUIClasses.cpp b/client/windows/GUIClasses.cpp index a78359e6c..1f8b46e1c 100644 --- a/client/windows/GUIClasses.cpp +++ b/client/windows/GUIClasses.cpp @@ -1281,8 +1281,8 @@ void CExchangeWindow::updateWidgets() secSkillIcons[leftRight][m]->setFrame(2 + id * 3 + level); } - expValues[leftRight]->setText(CSDL_Ext::makeNumberShort(hero->exp)); - manaValues[leftRight]->setText(CSDL_Ext::makeNumberShort(hero->mana)); + expValues[leftRight]->setText(vstd::formatMetric(hero->exp, 3)); + manaValues[leftRight]->setText(vstd::formatMetric(hero->mana, 3)); morale[leftRight]->set(hero); luck[leftRight]->set(hero);