Text:add and Text:addf now return index numbers which can be used as arguments to Text:getWidth and text:getHeight to determine the (pre-transformed) width and height of specific text within the Text object.

This commit is contained in:
Alex Szpakowski
2015-11-14 18:51:43 -04:00
parent 1d6cde226d
commit e9c75c342e
7 changed files with 89 additions and 56 deletions
+27 -14
View File
@@ -33,7 +33,6 @@ namespace opengl
Text::Text(Font *font, const std::vector<Font::ColoredString> &text)
: font(font)
, vbo(nullptr)
, text_info()
, vert_offset(0)
, texture_cache_id((uint32) -1)
{
@@ -82,7 +81,7 @@ void Text::uploadVertices(const std::vector<Font::GlyphVertex> &vertices, size_t
vbo = new_vbo;
}
if (vbo != nullptr)
if (vbo != nullptr && datasize > 0)
{
GLBuffer::Bind bind(*vbo);
vbodata = (uint8 *) vbo->map();
@@ -113,6 +112,8 @@ void Text::addTextData(const TextData &t)
std::vector<Font::GlyphVertex> vertices;
std::vector<Font::DrawCommand> new_commands;
Font::TextInfo text_info;
// We only have formatted text if the align mode is valid.
if (t.align == Font::ALIGN_MAX_ENUM)
new_commands = font->generateVertices(t.codepoints, vertices, 0.0f, Vector(0.0f, 0.0f), &text_info);
@@ -158,7 +159,9 @@ void Text::addTextData(const TextData &t)
}
vert_offset = voffset + vertices.size();
text_data.push_back(t);
text_data.back().text_info = text_info;
// Font::generateVertices can invalidate the font's texture cache.
if (font->getTextureCacheID() != texture_cache_id)
@@ -178,7 +181,7 @@ void Text::set(const std::vector<Font::ColoredString> &text, float wrap, Font::A
Font::ColoredCodepoints codepoints;
Font::getCodepointsFromString(text, codepoints);
addTextData({codepoints, wrap, align, false, false, Matrix3()});
addTextData({codepoints, wrap, align, {}, false, false, Matrix3()});
}
void Text::set()
@@ -186,22 +189,21 @@ void Text::set()
clear();
}
void Text::add(const std::vector<Font::ColoredString> &text, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
int Text::add(const std::vector<Font::ColoredString> &text, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
{
return addf(text, -1.0f, Font::ALIGN_MAX_ENUM, x, y, angle, sx, sy, ox, oy, kx, ky);
}
void Text::addf(const std::vector<Font::ColoredString> &text, float wrap, Font::AlignMode align, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
int Text::addf(const std::vector<Font::ColoredString> &text, float wrap, Font::AlignMode align, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
{
if (text.empty() || (text.size() == 1 && text[0].str.empty()))
return;
Font::ColoredCodepoints codepoints;
Font::getCodepointsFromString(text, codepoints);
Matrix3 m(x, y, angle, sx, sy, ox, oy, kx, ky);
addTextData({codepoints, wrap, align, true, true, m});
addTextData({codepoints, wrap, align, {}, true, true, m});
return (int) text_data.size() - 1;
}
void Text::clear()
@@ -209,7 +211,6 @@ void Text::clear()
text_data.clear();
draw_commands.clear();
texture_cache_id = font->getTextureCacheID();
text_info = {};
vert_offset = 0;
}
@@ -262,14 +263,26 @@ Font *Text::getFont() const
return font.get();
}
int Text::getWidth() const
int Text::getWidth(int index) const
{
return text_info.width;
if (index < 0)
index = std::max((int) text_data.size() - 1, 0);
if (index >= (int) text_data.size())
return 0;
return text_data[index].text_info.width;
}
int Text::getHeight() const
int Text::getHeight(int index) const
{
return text_info.height;
if (index < 0)
index = std::max((int) text_data.size() - 1, 0);
if (index >= (int) text_data.size())
return 0;
return text_data[index].text_info.height;
}
} // opengl
+5 -5
View File
@@ -45,8 +45,8 @@ public:
void set(const std::vector<Font::ColoredString> &text, float wrap, Font::AlignMode align);
void set();
void add(const std::vector<Font::ColoredString> &text, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
void addf(const std::vector<Font::ColoredString> &text, float wrap, Font::AlignMode align, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
int add(const std::vector<Font::ColoredString> &text, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
int addf(const std::vector<Font::ColoredString> &text, float wrap, Font::AlignMode align, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
void clear();
// Implements Drawable.
@@ -58,12 +58,12 @@ public:
/**
* Gets the width of the currently set text.
**/
int getWidth() const;
int getWidth(int index = 0) const;
/**
* Gets the height of the currently set text.
**/
int getHeight() const;
int getHeight(int index = 0) const;
private:
@@ -72,6 +72,7 @@ private:
Font::ColoredCodepoints codepoints;
float wrap;
Font::AlignMode align;
Font::TextInfo text_info;
bool use_matrix;
bool append_vertices;
Matrix3 matrix;
@@ -87,7 +88,6 @@ private:
std::vector<Font::DrawCommand> draw_commands;
std::vector<TextData> text_data;
Font::TextInfo text_info;
size_t vert_offset;
+14 -6
View File
@@ -144,8 +144,11 @@ int w_Text_add(lua_State *L)
float kx = (float) luaL_optnumber(L, 10, 0.0);
float ky = (float) luaL_optnumber(L, 11, 0.0);
luax_catchexcept(L, [&](){ t->add(text, x, y, a, sx, sy, ox, oy, kx, ky); });
return 0;
int index = 0;
luax_catchexcept(L, [&](){ index = t->add(text, x, y, a, sx, sy, ox, oy, kx, ky); });
lua_pushnumber(L, index + 1);
return 1;
}
int w_Text_addf(lua_State *L)
@@ -173,8 +176,11 @@ int w_Text_addf(lua_State *L)
float kx = (float) luaL_optnumber(L, 12, 0.0);
float ky = (float) luaL_optnumber(L, 13, 0.0);
luax_catchexcept(L, [&](){ t->addf(text, wrap, align, x, y, a, sx, sy, ox, oy, kx, ky); });
return 0;
int index = 0;
luax_catchexcept(L, [&](){ index = t->addf(text, wrap, align, x, y, a, sx, sy, ox, oy, kx, ky); });
lua_pushnumber(L, index + 1);
return 1;
}
int w_Text_clear(lua_State *L)
@@ -203,14 +209,16 @@ int w_Text_getFont(lua_State *L)
int w_Text_getWidth(lua_State *L)
{
Text *t = luax_checktext(L, 1);
lua_pushnumber(L, t->getWidth());
int index = (int) luaL_optnumber(L, 2, 0) - 1;
lua_pushnumber(L, t->getWidth(index));
return 1;
}
int w_Text_getHeight(lua_State *L)
{
Text *t = luax_checktext(L, 1);
lua_pushnumber(L, t->getHeight());
int index = (int) luaL_optnumber(L, 2, 0) - 1;
lua_pushnumber(L, t->getHeight(index));
return 1;
}