1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-15 20:03:15 +02:00

Removed text utilities from SDL_Extensions

This commit is contained in:
Ivan Savenko
2023-02-03 18:55:25 +02:00
parent 7c58a46279
commit 34dcb4127a
14 changed files with 94 additions and 92 deletions

View File

@@ -725,6 +725,27 @@ namespace vstd
return a + (b - a) * f; 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<typename IntType>
std::string formatMetric(IntType number, int maxLength)
{
IntType max = pow(10, maxLength);
if (std::abs(number) < max)
return boost::lexical_cast<std::string>(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 boost::math::round;
} }
using vstd::operator-=; using vstd::operator-=;

View File

@@ -45,8 +45,6 @@ CResDataBar::CResDataBar(const std::string & defname, int x, int y, int offx, in
txtpos[i].second = pos.y + offy; txtpos[i].second = pos.y + offy;
} }
txtpos[7].first = txtpos[6].first + datedist; 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); addUsedEvents(RCLICK);
} }
@@ -69,12 +67,23 @@ CResDataBar::CResDataBar()
txtpos[i].second = pos.y + ADVOPT.resOffsetY; txtpos[i].second = pos.y + ADVOPT.resOffsetY;
} }
txtpos[7].first = txtpos[6].first + ADVOPT.resDateDist; 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; 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) void CResDataBar::draw(SDL_Surface * to)
{ {
//TODO: all this should be labels, but they require proper text update on change //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)); graphics->fonts[FONT_SMALL]->renderTextLeft(to, text, Colors::WHITE, Point(txtpos[i].first,txtpos[i].second));
} }
std::vector<std::string> temp; graphics->fonts[FONT_SMALL]->renderTextLeft(to, buildDateString(), Colors::WHITE, Point(txtpos[7].first,txtpos[7].second));
temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
temp.push_back(boost::lexical_cast<std::string>(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));
} }
void CResDataBar::show(SDL_Surface * to) void CResDataBar::show(SDL_Surface * to)

View File

@@ -15,11 +15,12 @@
/// Current date is displayed too /// Current date is displayed too
class CResDataBar : public CIntObject class CResDataBar : public CIntObject
{ {
std::string buildDateString();
public: public:
std::shared_ptr<CPicture> background; std::shared_ptr<CPicture> background;
std::vector<std::pair<int,int> > txtpos; std::vector<std::pair<int,int> > txtpos;
std::string datetext;
void clickRight(tribool down, bool previousState) override; void clickRight(tribool down, bool previousState) override;
CResDataBar(); CResDataBar();

View File

@@ -789,7 +789,7 @@ void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn)
if (unit->unitType()->idNumber == CreatureID::ARROW_TOWERS) if (unit->unitType()->idNumber == CreatureID::ARROW_TOWERS)
icon->setFrame(owner->getSiegeShooterIconID(), 1); icon->setFrame(owner->getSiegeShooterIconID(), 1);
amount->setText(CSDL_Ext::makeNumberShort(unit->getCount(), 4)); amount->setText(vstd::formatMetric(unit->getCount(), 4));
if(stateIcon) if(stateIcon)
{ {

View File

@@ -316,7 +316,7 @@ void BattleStacksController::showStackAmountBox(Canvas & canvas, const CStack *
//blitting amount //blitting amount
Point textPos = stackAnimation[stack->ID]->pos.topLeft() + amountBG->dimensions()/2 + Point(xAdd, yAdd); 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) void BattleStacksController::showStack(Canvas & canvas, const CStack * stack)

View File

@@ -742,7 +742,7 @@ const Point & CGuiHandler::getCursorPosition() const
Point CGuiHandler::screenDimensions() const Point CGuiHandler::screenDimensions() const
{ {
return return Point(screen->w, screen->h); return Point(screen->w, screen->h);
} }
bool CGuiHandler::isMouseButtonPressed() const bool CGuiHandler::isMouseButtonPressed() const

View File

@@ -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; return (uint8_t *)srf->pixels + y * srf->pitch + x * srf->format->BytesPerPixel;
} }
std::string CSDL_Ext::processStr(std::string str, std::vector<std::string> & tor)
{
for (size_t i=0; (i<tor.size())&&(boost::find_first(str,"%s")); ++i)
{
boost::replace_first(str,"%s",tor[i]);
}
return str;
}
bool CSDL_Ext::isTransparent( SDL_Surface * srf, const Point & position ) bool CSDL_Ext::isTransparent( SDL_Surface * srf, const Point & position )
{ {
return isTransparent(srf, position.x, position.y); return isTransparent(srf, position.x, position.y);

View File

@@ -45,26 +45,6 @@ SDL_Color toSDL(const ColorRGBA & color);
void setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors); void setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors);
void setAlpha(SDL_Surface * bg, int value); void setAlpha(SDL_Surface * bg, int value);
template<typename IntType>
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<std::string>(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<std::string>(number) + *iter;
}
Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy); 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 (*TColorPutter)(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B);
@@ -111,7 +91,7 @@ typedef void (*TColorPutterAlpha)(uint8_t *&ptr, const uint8_t & R, const uint8_
void drawBorder(SDL_Surface * sur, const Rect &r, 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 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 void setPlayerColor(SDL_Surface * sur, PlayerColor player); //sets correct color of flags; -1 for neutral
std::string processStr(std::string str, std::vector<std::string> & 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, 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 * 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 SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface

View File

@@ -374,7 +374,7 @@ void CGarrisonSlot::update()
creatureImage->setFrame(creature->getIconIndex()); creatureImage->setFrame(creature->getIconIndex());
stackCount->enable(); stackCount->enable();
stackCount->setText(CSDL_Ext::makeNumberShort(myStack->count, 4)); stackCount->setText(vstd::formatMetric(myStack->count, 4));
} }
else else
{ {

View File

@@ -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) void CMinorResDataBar::showAll(SDL_Surface * to)
{ {
CIntObject::showAll(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)); graphics->fonts[FONT_SMALL]->renderTextCenter(to, text, Colors::WHITE, Point(pos.x + 50 + 76 * i, pos.y + pos.h/2));
} }
std::vector<std::string> temp; graphics->fonts[FONT_SMALL]->renderTextCenter(to, buildDateString(), Colors::WHITE, Point(pos.x+545+(pos.w-545)/2,pos.y+pos.h/2));
temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
temp.push_back(boost::lexical_cast<std::string>(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));
} }
CMinorResDataBar::CMinorResDataBar() CMinorResDataBar::CMinorResDataBar()
@@ -248,7 +251,7 @@ void CArmyTooltip::init(const InfoAboutArmy &army)
std::string subtitle; std::string subtitle;
if(army.army.isDetailed) if(army.army.isDetailed)
{ {
subtitle = CSDL_Ext::makeNumberShort(slot.second.count, 4); subtitle = vstd::formatMetric(slot.second.count, 4);
} }
else else
{ {

View File

@@ -117,6 +117,8 @@ public:
class CMinorResDataBar : public CIntObject class CMinorResDataBar : public CIntObject
{ {
std::shared_ptr<CPicture> background; std::shared_ptr<CPicture> background;
std::string buildDateString();
public: public:
void show(SDL_Surface * to) override; void show(SDL_Surface * to) override;
void showAll(SDL_Surface * to) override; void showAll(SDL_Surface * to) override;

View File

@@ -582,7 +582,7 @@ CStackWindow::MainSection::MainSection(CStackWindow * owner, int yOffset, bool s
} }
expLabel = std::make_shared<CLabel>( expLabel = std::make_shared<CLabel>(
pos.x + 21, pos.y + 52, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, pos.x + 21, pos.y + 52, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
CSDL_Ext::makeNumberShort<TExpType>(stack->experience, 6)); vstd::formatMetric(stack->experience, 6));
} }
if(showArt) if(showArt)

View File

@@ -18,6 +18,7 @@
#include "../widgets/Buttons.h" #include "../widgets/Buttons.h"
#include "../widgets/CComponent.h" #include "../widgets/CComponent.h"
#include "../widgets/TextControls.h" #include "../widgets/TextControls.h"
#include "../gui/CGuiHandler.h"
#include "../render/CAnimation.h" #include "../render/CAnimation.h"
#include "../render/IImage.h" #include "../render/IImage.h"
#include "../renderSDL/SDL_Extensions.h" #include "../renderSDL/SDL_Extensions.h"

View File

@@ -1281,8 +1281,8 @@ void CExchangeWindow::updateWidgets()
secSkillIcons[leftRight][m]->setFrame(2 + id * 3 + level); secSkillIcons[leftRight][m]->setFrame(2 + id * 3 + level);
} }
expValues[leftRight]->setText(CSDL_Ext::makeNumberShort(hero->exp)); expValues[leftRight]->setText(vstd::formatMetric(hero->exp, 3));
manaValues[leftRight]->setText(CSDL_Ext::makeNumberShort(hero->mana)); manaValues[leftRight]->setText(vstd::formatMetric(hero->mana, 3));
morale[leftRight]->set(hero); morale[leftRight]->set(hero);
luck[leftRight]->set(hero); luck[leftRight]->set(hero);