1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-02-09 13:24:01 +00:00

Add Lua function text_getStringWidth()

This commit is contained in:
fgenesis 2015-03-08 20:43:41 +01:00
parent dee156cf7a
commit b70de7f94b
6 changed files with 53 additions and 1 deletions

View file

@ -8651,7 +8651,7 @@ luaFunc(avatar_updatePosition)
luaFunc(avatar_toggleMovement)
{
dsq->game->avatar->toggleMovement((bool)lua_tointeger(L, 1));
dsq->game->avatar->toggleMovement(getBool(L));
luaReturnNil();
}
@ -9047,6 +9047,12 @@ luaFunc(text_getHeight)
luaReturnNum(txt ? txt->getHeight() : 0.0f);
}
luaFunc(text_getStringWidth)
{
BaseText *txt = getText(L);
luaReturnNum(txt ? txt->getStringWidth(getString(L, 2)) : 0.0f);
}
luaFunc(loadShader)
{
int handle = 0;
@ -10120,6 +10126,7 @@ static const struct {
luaRegister(text_setWidth),
luaRegister(text_setAlign),
luaRegister(text_getHeight),
luaRegister(text_getStringWidth),
luaRegister(loadShader),
luaRegister(createShader),

View file

@ -14,6 +14,7 @@ public:
virtual void setFontSize(int sz) = 0;
virtual void setAlign(Align a) = 0;
virtual float getHeight() = 0;
virtual float getStringWidth(const std::string& text) = 0;
};

View file

@ -425,6 +425,30 @@ int BitmapText::getNumLines()
return lines.size();
}
float BitmapText::getStringWidth(const std::string& text)
{
std::string tmp;
int maxsize = 0;
tmp.reserve(text.length());
for (size_t i = 0; i < text.size(); i++)
{
if(text[i] == '\n')
{
std::pair<int, int> dim;
bmpFont->font.GetStringSize(tmp, &dim);
maxsize = std::max(maxsize, dim.first);
tmp.resize(0);
}
else
tmp += text[i];
}
std::pair<int, int> dim;
bmpFont->font.GetStringSize(tmp, &dim);
maxsize = std::max(maxsize, dim.first);
return maxsize * bmpFont->scale;
}
/*
BitmapText::BitmapText() : RenderObject()
{

View file

@ -75,6 +75,7 @@ public:
virtual float getHeight();
void unloadDevice();
void reloadDevice();
virtual float getStringWidth(const std::string& text);
int getNumLines();
protected:

View file

@ -52,6 +52,24 @@ float DebugFont::getHeight()
return fontDrawSize * lines.size() * 1.5f; // vspc in render()
}
float DebugFont::getStringWidth(const std::string& text)
{
int maxchars = 0;
int c = 0;
for (size_t i = 0; i < text.size(); i++)
{
if(text[i] == '\n')
{
maxchars = std::max(maxchars, c);
c = 0;
}
else
++c;
}
maxchars = std::max(maxchars, c);
return float(fontDrawSize * maxchars);
}
void DebugFont::formatText()
{
std::string text;

View file

@ -34,6 +34,7 @@ public:
int getNumLines() { return lines.size(); }
virtual void setAlign(Align align);
virtual float getHeight();
virtual float getStringWidth(const std::string& text);
protected:
int fontDrawSize, textWidth;
void formatText();