Fix text entities wrapping

This commit is contained in:
Atlante45 2015-06-12 15:41:37 +02:00
parent f1b24a1df0
commit 565bf8bcb2
2 changed files with 14 additions and 12 deletions

View file

@ -51,8 +51,9 @@ void RenderableTextEntityItem::render(RenderArgs* args) {
transformToTopLeft.setScale(scale); // Scale to have the correct line height transformToTopLeft.setScale(scale); // Scale to have the correct line height
batch.setModelTransform(transformToTopLeft); batch.setModelTransform(transformToTopLeft);
float leftMargin = 0.5f * _lineHeight, topMargin = 0.5f * _lineHeight; float leftMargin = 0.1f * _lineHeight, topMargin = 0.1f * _lineHeight;
glm::vec2 bounds = glm::vec2(dimensions.x - 2.0f * leftMargin, dimensions.y - 2.0f * topMargin); glm::vec2 bounds = glm::vec2(dimensions.x - 2.0f * leftMargin,
dimensions.y - 2.0f * topMargin);
_textRenderer->draw(batch, leftMargin / scale, -topMargin / scale, _text, textColor, bounds / scale); _textRenderer->draw(batch, leftMargin / scale, -topMargin / scale, _text, textColor, bounds / scale);
} }

View file

@ -101,7 +101,7 @@ struct QuadBuilder {
texMin); texMin);
} }
QuadBuilder(const Glyph3D& glyph, const glm::vec2& offset) : QuadBuilder(const Glyph3D& glyph, const glm::vec2& offset) :
QuadBuilder(offset + glyph.offset - glm::vec2(0.0f, glyph.size.y), glyph.size, QuadBuilder(offset + glm::vec2(glyph.offset.x, glyph.offset.y - glyph.size.y), glyph.size,
glyph.texOffset, glyph.texSize) {} glyph.texOffset, glyph.texSize) {}
}; };
@ -113,7 +113,7 @@ public:
void read(QIODevice& path); void read(QIODevice& path);
glm::vec2 computeExtent(const QString& str) const; glm::vec2 computeExtent(const QString& str) const;
float getRowHeight() const { return _rowHeight; } float getRowHeight() const { return _fontSize; }
// Render string to batch // Render string to batch
void drawString(gpu::Batch& batch, float x, float y, const QString& str, void drawString(gpu::Batch& batch, float x, float y, const QString& str,
@ -251,13 +251,14 @@ glm::vec2 Font3D::computeTokenExtent(const QString& token) const {
glm::vec2 Font3D::computeExtent(const QString& str) const { glm::vec2 Font3D::computeExtent(const QString& str) const {
glm::vec2 extent = glm::vec2(0.0f, 0.0f); glm::vec2 extent = glm::vec2(0.0f, 0.0f);
QStringList tokens = splitLines(str); QStringList lines{ splitLines(str) };
foreach(const QString& token, tokens) { if (!lines.empty()) {
glm::vec2 tokenExtent = computeTokenExtent(token); for(const auto& line : lines) {
glm::vec2 tokenExtent = computeTokenExtent(line);
extent.x = std::max(tokenExtent.x, extent.x); extent.x = std::max(tokenExtent.x, extent.x);
} }
extent.y = tokens.count() * _rowHeight; extent.y = lines.count() * _fontSize;
}
return extent; return extent;
} }
@ -393,7 +394,7 @@ void Font3D::drawString(gpu::Batch& batch, float x, float y, const QString& str,
} }
if (isNewLine || forceNewLine) { if (isNewLine || forceNewLine) {
// Character return, move the advance to a new line // Character return, move the advance to a new line
advance = glm::vec2(x, advance.y - _rowHeight); advance = glm::vec2(x, advance.y - _fontSize);
if (isNewLine) { if (isNewLine) {
// No need to draw anything, go directly to next token // No need to draw anything, go directly to next token
@ -413,7 +414,7 @@ void Font3D::drawString(gpu::Batch& batch, float x, float y, const QString& str,
for (auto c : token) { for (auto c : token) {
auto glyph = _glyphs[c]; auto glyph = _glyphs[c];
QuadBuilder qd(glyph, advance - glm::vec2(0.0f, _fontSize)); QuadBuilder qd(glyph, advance - glm::vec2(0.0f, _ascent));
_verticesBuffer->append(sizeof(QuadBuilder), (const gpu::Byte*)&qd); _verticesBuffer->append(sizeof(QuadBuilder), (const gpu::Byte*)&qd);
_numVertices += 4; _numVertices += 4;