1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +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;
}
/// 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 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[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<std::string> temp;
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));
graphics->fonts[FONT_SMALL]->renderTextLeft(to, buildDateString(), Colors::WHITE, Point(txtpos[7].first,txtpos[7].second));
}
void CResDataBar::show(SDL_Surface * to)

View File

@@ -15,11 +15,12 @@
/// Current date is displayed too
class CResDataBar : public CIntObject
{
std::string buildDateString();
public:
std::shared_ptr<CPicture> background;
std::vector<std::pair<int,int> > txtpos;
std::string datetext;
void clickRight(tribool down, bool previousState) override;
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)
icon->setFrame(owner->getSiegeShooterIconID(), 1);
amount->setText(CSDL_Ext::makeNumberShort(unit->getCount(), 4));
amount->setText(vstd::formatMetric(unit->getCount(), 4));
if(stateIcon)
{

View File

@@ -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)

View File

@@ -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

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;
}
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 )
{
return isTransparent(srf, position.x, position.y);

View File

@@ -17,12 +17,12 @@ struct SDL_Rect;
struct SDL_Window;
struct SDL_Renderer;
struct SDL_Texture;
struct SDL_Surface;
struct SDL_Color;
VCMI_LIB_NAMESPACE_BEGIN
class Rect;
struct SDL_Surface;
struct SDL_Color;
VCMI_LIB_NAMESPACE_BEGIN
class Rect;
class Point;
VCMI_LIB_NAMESPACE_END
@@ -39,45 +39,25 @@ SDL_Rect toSDL(const Rect & rect);
/// creates Color using provided SDL_Color
ColorRGBA fromSDL(const SDL_Color & color);
/// 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);
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);
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<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); //creates new surface, with flags/format same as in screen surface
SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
template<int bpp>
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<int bpp>
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.

View File

@@ -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
{

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)
{
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<std::string> temp;
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));
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
{

View File

@@ -117,6 +117,8 @@ public:
class CMinorResDataBar : public CIntObject
{
std::shared_ptr<CPicture> background;
std::string buildDateString();
public:
void show(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>(
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)

View File

@@ -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"

View File

@@ -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);