Merge pull request #5113 from Atlante45/debug

Fix text entities wrapping
This commit is contained in:
Brad Hefta-Gaub 2015-06-12 11:23:31 -07:00
commit a5b28660ad
3 changed files with 19 additions and 19 deletions

View file

@ -47,12 +47,13 @@ void RenderableTextEntityItem::render(RenderArgs* args) {
glm::vec3 maxCorner = glm::vec3(dimensions.x, 0.0f, SLIGHTLY_BEHIND); glm::vec3 maxCorner = glm::vec3(dimensions.x, 0.0f, SLIGHTLY_BEHIND);
DependencyManager::get<DeferredLightingEffect>()->renderQuad(batch, minCorner, maxCorner, backgroundColor); DependencyManager::get<DeferredLightingEffect>()->renderQuad(batch, minCorner, maxCorner, backgroundColor);
float scale = _lineHeight / _textRenderer->getRowHeight(); float scale = _lineHeight / _textRenderer->getFontSize();
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 getFontSize() 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,
@ -139,7 +139,6 @@ private:
// Font characteristics // Font characteristics
QString _family; QString _family;
float _fontSize = 0.0f; float _fontSize = 0.0f;
float _rowHeight = 0.0f;
float _leading = 0.0f; float _leading = 0.0f;
float _ascent = 0.0f; float _ascent = 0.0f;
float _descent = 0.0f; float _descent = 0.0f;
@ -251,13 +250,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) {
extent.x = std::max(tokenExtent.x, extent.x); glm::vec2 tokenExtent = computeTokenExtent(line);
extent.x = std::max(tokenExtent.x, extent.x);
}
extent.y = lines.count() * _fontSize;
} }
extent.y = tokens.count() * _rowHeight;
return extent; return extent;
} }
@ -288,8 +288,7 @@ void Font3D::read(QIODevice& in) {
readStream(in, _descent); readStream(in, _descent);
readStream(in, _spaceWidth); readStream(in, _spaceWidth);
_fontSize = _ascent + _descent; _fontSize = _ascent + _descent;
_rowHeight = _fontSize + _leading;
// Read character count // Read character count
uint16_t count; uint16_t count;
readStream(in, count); readStream(in, count);
@ -393,7 +392,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 - _leading);
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 +412,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;
@ -477,9 +476,9 @@ glm::vec2 TextRenderer3D::computeExtent(const QString& str) const {
return glm::vec2(0.0f, 0.0f); return glm::vec2(0.0f, 0.0f);
} }
float TextRenderer3D::getRowHeight() const { float TextRenderer3D::getFontSize() const {
if (_font) { if (_font) {
return _font->getRowHeight(); return _font->getFontSize();
} }
return 0.0f; return 0.0f;
} }

View file

@ -50,7 +50,7 @@ public:
~TextRenderer3D(); ~TextRenderer3D();
glm::vec2 computeExtent(const QString& str) const; glm::vec2 computeExtent(const QString& str) const;
float getRowHeight() const; float getFontSize() const;
void draw(gpu::Batch& batch, float x, float y, const QString& str, const glm::vec4& color = glm::vec4(-1.0f), void draw(gpu::Batch& batch, float x, float y, const QString& str, const glm::vec4& color = glm::vec4(-1.0f),
const glm::vec2& bounds = glm::vec2(-1.0f)); const glm::vec2& bounds = glm::vec2(-1.0f));