From b70de7f94bf20111d42eafaebf53138fa4b6e789 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Sun, 8 Mar 2015 20:43:41 +0100 Subject: [PATCH] Add Lua function text_getStringWidth() --- Aquaria/ScriptInterface.cpp | 9 ++++++++- BBGE/BaseText.h | 1 + BBGE/BitmapFont.cpp | 24 ++++++++++++++++++++++++ BBGE/BitmapFont.h | 1 + BBGE/DebugFont.cpp | 18 ++++++++++++++++++ BBGE/DebugFont.h | 1 + 6 files changed, 53 insertions(+), 1 deletion(-) diff --git a/Aquaria/ScriptInterface.cpp b/Aquaria/ScriptInterface.cpp index 232e143..9fd5af3 100644 --- a/Aquaria/ScriptInterface.cpp +++ b/Aquaria/ScriptInterface.cpp @@ -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), diff --git a/BBGE/BaseText.h b/BBGE/BaseText.h index 203bf18..ec84314 100644 --- a/BBGE/BaseText.h +++ b/BBGE/BaseText.h @@ -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; }; diff --git a/BBGE/BitmapFont.cpp b/BBGE/BitmapFont.cpp index 9bf0fc2..e7dc664 100644 --- a/BBGE/BitmapFont.cpp +++ b/BBGE/BitmapFont.cpp @@ -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 dim; + bmpFont->font.GetStringSize(tmp, &dim); + maxsize = std::max(maxsize, dim.first); + tmp.resize(0); + } + else + tmp += text[i]; + } + std::pair dim; + bmpFont->font.GetStringSize(tmp, &dim); + maxsize = std::max(maxsize, dim.first); + + return maxsize * bmpFont->scale; +} + /* BitmapText::BitmapText() : RenderObject() { diff --git a/BBGE/BitmapFont.h b/BBGE/BitmapFont.h index 613faf9..cc9f62e 100644 --- a/BBGE/BitmapFont.h +++ b/BBGE/BitmapFont.h @@ -75,6 +75,7 @@ public: virtual float getHeight(); void unloadDevice(); void reloadDevice(); + virtual float getStringWidth(const std::string& text); int getNumLines(); protected: diff --git a/BBGE/DebugFont.cpp b/BBGE/DebugFont.cpp index f1ffc69..0e253d8 100644 --- a/BBGE/DebugFont.cpp +++ b/BBGE/DebugFont.cpp @@ -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; diff --git a/BBGE/DebugFont.h b/BBGE/DebugFont.h index 2a1d5bc..d68d3e9 100644 --- a/BBGE/DebugFont.h +++ b/BBGE/DebugFont.h @@ -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();