fix text bounds and cursor

This commit is contained in:
2019-09-19 08:42:35 +02:00
parent 72fa400651
commit 4f9383aba7
2 changed files with 13 additions and 14 deletions

View File

@@ -102,8 +102,11 @@ std::vector<TextMesh::Token> TextMesh::tokenize(const std::string& s, const Font
float y = 0; float y = 0;
for (char c : p) for (char c : p)
{ {
stbtt_aligned_quad q; if (c >= 32 && c < 127) // visible ascii character
stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, c - f.start_char, &x, &y, &q, true); {
stbtt_aligned_quad q;
stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, c - f.start_char, &x, &y, &q, true);
}
} }
ret.emplace_back(p, x * f.scale); ret.emplace_back(p, x * f.scale);
} }
@@ -136,7 +139,7 @@ void TextMesh::update(kFont id, const std::string& text)
auto& f = FontManager::get(id); auto& f = FontManager::get(id);
float spacing = f.bounds.w - f.bounds.y; float spacing = f.bounds.w - f.bounds.y;
float avg_width = f.bounds.z - f.bounds.x; float avg_width = f.bounds.z - f.bounds.x;
cur_box = glm::vec4(0, 0, 5, spacing); cur_box = glm::vec4(0, f.bounds.y, 5, avg_width);
glm::vec2 bbmin(FLT_MAX); glm::vec2 bbmin(FLT_MAX);
glm::vec2 bbmax(-FLT_MAX); glm::vec2 bbmax(-FLT_MAX);
@@ -157,12 +160,6 @@ void TextMesh::update(kFont id, const std::string& text)
tokenize(text, f) : tokenize(text, f) :
std::vector<Token>{ Token(text, 0.f) }; std::vector<Token>{ Token(text, 0.f) };
if (parts.empty())
{
bbmin = { 0, -spacing * 0.5f };
bbmax = { 1, +spacing * 0.5f };
}
for (auto p : parts) for (auto p : parts)
{ {
if (max_width > 0 && (x + p.w) * f.scale > max_width) if (max_width > 0 && (x + p.w) * f.scale > max_width)
@@ -176,6 +173,7 @@ void TextMesh::update(kFont id, const std::string& text)
{ {
x = 0; x = 0;
y += spacing; y += spacing;
cur_box = glm::vec4(x, y + f.bounds.y, 5, avg_width);
continue; continue;
} }
stbtt_aligned_quad q; stbtt_aligned_quad q;
@@ -196,13 +194,14 @@ void TextMesh::update(kFont id, const std::string& text)
idx.push_back(n + 0); idx.push_back(n + 0);
idx.push_back(n + 2); idx.push_back(n + 2);
idx.push_back(n + 3); idx.push_back(n + 3);
bbmin = glm::min(bbmin, { q.x0 / f.scale, q.y0 / f.scale }); bbmin = glm::min(bbmin, xy(f.bounds));
bbmax = glm::max(bbmax, { q.x1 / f.scale, q.y1 / f.scale }); bbmax = glm::max(bbmax, { q.x1 / f.scale, y + f.bounds.w });
cur_box = glm::vec4(x, y, 5, spacing); cur_box = glm::vec4(q.x1 / f.scale + 2, y + f.bounds.y, 5, avg_width);
} }
} }
for (auto& vi : v) for (auto& vi : v)
vi -= glm::vec4(bbmin, 0, 0); vi -= glm::vec4(xy(f.bounds), 0, 0);
cur_box -= glm::vec4(xy(f.bounds), 0, 0);
bb = bbmax - bbmin; bb = bbmax - bbmin;
font_array_count = (int)idx.size(); font_array_count = (int)idx.size();
App::I->render_task([&] App::I->render_task([&]

View File

@@ -110,7 +110,7 @@ kEventResult NodeTextInput::handle_event(Event* e)
on_return(this); on_return(this);
} }
} }
else if (ke->m_char >= 32 && ke->m_char < (32 + 96)) else if (ke->m_char >= 32 && ke->m_char < 127) // visible ascii character
{ {
m_text += (char)ke->m_char; m_text += (char)ke->m_char;
set_text(m_text); set_text(m_text);