From 6365f6b85766fda91e9fbb6230e9054ce95bcfcf Mon Sep 17 00:00:00 2001 From: Stojce Slavkovski Date: Sat, 7 Jun 2014 22:24:35 +0200 Subject: [PATCH] Extend TextOverlay to accept font size Fix TextRenderer color rendering --- interface/src/ui/TextRenderer.cpp | 8 ++++---- interface/src/ui/TextRenderer.h | 7 ++++++- interface/src/ui/overlays/TextOverlay.cpp | 17 +++++++++++++---- interface/src/ui/overlays/TextOverlay.h | 4 +++- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/interface/src/ui/TextRenderer.cpp b/interface/src/ui/TextRenderer.cpp index 2743e3e572..18279d3914 100644 --- a/interface/src/ui/TextRenderer.cpp +++ b/interface/src/ui/TextRenderer.cpp @@ -26,9 +26,9 @@ Glyph::Glyph(int textureID, const QPoint& location, const QRect& bounds, int wid } TextRenderer::TextRenderer(const char* family, int pointSize, int weight, - bool italic, EffectType effectType, int effectThickness) + bool italic, EffectType effectType, int effectThickness, QColor color) : _font(family, pointSize, weight, italic), _metrics(_font), _effectType(effectType), - _effectThickness(effectThickness), _x(IMAGE_SIZE), _y(IMAGE_SIZE), _rowHeight(0) { + _effectThickness(effectThickness), _x(IMAGE_SIZE), _y(IMAGE_SIZE), _rowHeight(0), _color(color) { _font.setKerning(false); } @@ -157,7 +157,7 @@ const Glyph& TextRenderer::getGlyph(char c) { // render the glyph into an image and copy it into the texture QImage image(bounds.width(), bounds.height(), QImage::Format_ARGB32); if (c == SOLID_BLOCK_CHAR) { - image.fill(QColor(255, 255, 255)); + image.fill(_color); } else { image.fill(0); @@ -180,7 +180,7 @@ const Glyph& TextRenderer::getGlyph(char c) { painter.setRenderHint(QPainter::Antialiasing); painter.drawPath(path); } - painter.setPen(QColor(255, 255, 255)); + painter.setPen(_color); painter.drawText(-bounds.x(), -bounds.y(), ch); } glTexSubImage2D(GL_TEXTURE_2D, 0, _x, _y, bounds.width(), bounds.height(), GL_RGBA, GL_UNSIGNED_BYTE, image.constBits()); diff --git a/interface/src/ui/TextRenderer.h b/interface/src/ui/TextRenderer.h index 813f15a5ac..2daba79c8f 100644 --- a/interface/src/ui/TextRenderer.h +++ b/interface/src/ui/TextRenderer.h @@ -12,6 +12,7 @@ #ifndef hifi_TextRenderer_h #define hifi_TextRenderer_h +#include #include #include #include @@ -41,7 +42,8 @@ public: enum EffectType { NO_EFFECT, SHADOW_EFFECT, OUTLINE_EFFECT }; TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false, - EffectType effect = NO_EFFECT, int effectThickness = 1); + EffectType effect = NO_EFFECT, int effectThickness = 1, + QColor color = QColor(255, 255, 255)); ~TextRenderer(); const QFontMetrics& metrics() const { return _metrics; } @@ -85,6 +87,9 @@ private: // the list of all texture ids for which we're responsible QVector _allTextureIDs; + + // text color + QColor _color; }; class Glyph { diff --git a/interface/src/ui/overlays/TextOverlay.cpp b/interface/src/ui/overlays/TextOverlay.cpp index e26c772b06..797d0be1a2 100644 --- a/interface/src/ui/overlays/TextOverlay.cpp +++ b/interface/src/ui/overlays/TextOverlay.cpp @@ -19,7 +19,8 @@ TextOverlay::TextOverlay() : _leftMargin(DEFAULT_MARGIN), - _topMargin(DEFAULT_MARGIN) + _topMargin(DEFAULT_MARGIN), + _fontSize(DEFAULT_FONTSIZE) { } @@ -32,7 +33,7 @@ void TextOverlay::render() { } const float MAX_COLOR = 255; - glColor4f(_color.red / MAX_COLOR, _color.green / MAX_COLOR, _color.blue / MAX_COLOR, _alpha); + glColor4f(0 / MAX_COLOR, 0 / MAX_COLOR, 0 / MAX_COLOR, _alpha); glBegin(GL_QUADS); glVertex2f(_bounds.left(), _bounds.top()); @@ -43,8 +44,9 @@ void TextOverlay::render() { //TextRenderer(const char* family, int pointSize = -1, int weight = -1, bool italic = false, // EffectType effect = NO_EFFECT, int effectThickness = 1); - - TextRenderer textRenderer(SANS_FONT_FAMILY, 11, 50); + TextRenderer textRenderer(SANS_FONT_FAMILY, _fontSize, 50, false, TextRenderer::NO_EFFECT, 1, + QColor(_color.red, _color.green, _color.blue)); + const int leftAdjust = -1; // required to make text render relative to left edge of bounds const int topAdjust = -2; // required to make text render relative to top edge of bounds int x = _bounds.left() + _leftMargin + leftAdjust; @@ -67,6 +69,13 @@ void TextOverlay::render() { void TextOverlay::setProperties(const QScriptValue& properties) { Overlay2D::setProperties(properties); + + QScriptValue font = properties.property("font"); + if (font.isObject()) { + if (font.property("size").isValid()) { + setFontSize(font.property("size").toInt32()); + } + } QScriptValue text = properties.property("text"); if (text.isValid()) { diff --git a/interface/src/ui/overlays/TextOverlay.h b/interface/src/ui/overlays/TextOverlay.h index fc04966d07..78a037762e 100644 --- a/interface/src/ui/overlays/TextOverlay.h +++ b/interface/src/ui/overlays/TextOverlay.h @@ -29,6 +29,7 @@ #include "Overlay2D.h" const int DEFAULT_MARGIN = 10; +const int DEFAULT_FONTSIZE = 11; class TextOverlay : public Overlay2D { Q_OBJECT @@ -47,6 +48,7 @@ public: void setText(const QString& text) { _text = text; } void setLeftMargin(int margin) { _leftMargin = margin; } void setTopMargin(int margin) { _topMargin = margin; } + void setFontSize(int fontSize) { _fontSize = fontSize; } virtual void setProperties(const QScriptValue& properties); @@ -55,7 +57,7 @@ private: QString _text; int _leftMargin; int _topMargin; - + int _fontSize; };