mirror of
https://github.com/overte-org/overte.git
synced 2025-04-16 16:26:17 +02:00
Replace 2D and 3D text overlay textWidth() method with textSize()
This commit is contained in:
parent
55ec455f77
commit
136c3a2cce
8 changed files with 54 additions and 14 deletions
|
@ -432,19 +432,19 @@ bool Overlays::isLoaded(unsigned int id) {
|
|||
return thisOverlay->isLoaded();
|
||||
}
|
||||
|
||||
float Overlays::textWidth(unsigned int id, const QString& text) const {
|
||||
QSizeF Overlays::textSize(unsigned int id, const QString& text) const {
|
||||
Overlay* thisOverlay = _overlays2D[id];
|
||||
if (thisOverlay) {
|
||||
if (typeid(*thisOverlay) == typeid(TextOverlay)) {
|
||||
return static_cast<TextOverlay*>(thisOverlay)->textWidth(text);
|
||||
return static_cast<TextOverlay*>(thisOverlay)->textSize(text);
|
||||
}
|
||||
} else {
|
||||
thisOverlay = _overlays3D[id];
|
||||
if (thisOverlay) {
|
||||
if (typeid(*thisOverlay) == typeid(Text3DOverlay)) {
|
||||
return static_cast<Text3DOverlay*>(thisOverlay)->textWidth(text);
|
||||
return static_cast<Text3DOverlay*>(thisOverlay)->textSize(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0.0f;
|
||||
return QSizeF(0.0f, 0.0f);
|
||||
}
|
||||
|
|
|
@ -85,9 +85,9 @@ public slots:
|
|||
/// returns whether the overlay's assets are loaded or not
|
||||
bool isLoaded(unsigned int id);
|
||||
|
||||
/// returns the width of the given text in the specified overlay if it is a text overlay: in pixels if it is a 2D text
|
||||
/// returns the size of the given text in the specified overlay if it is a text overlay: in pixels if it is a 2D text
|
||||
/// overlay; in meters if it is a 3D text overlay
|
||||
float textWidth(unsigned int id, const QString& text) const;
|
||||
QSizeF textSize(unsigned int id, const QString& text) const;
|
||||
|
||||
private:
|
||||
QMap<unsigned int, Overlay*> _overlays2D;
|
||||
|
|
|
@ -236,11 +236,23 @@ Text3DOverlay* Text3DOverlay::createClone() const {
|
|||
return new Text3DOverlay(this);;
|
||||
}
|
||||
|
||||
float Text3DOverlay::textWidth(const QString& text) const {
|
||||
QSizeF Text3DOverlay::textSize(const QString& text) const {
|
||||
|
||||
QFont font(SANS_FONT_FAMILY, FIXED_FONT_POINT_SIZE); // Same font properties as render()
|
||||
QFontMetrics fontMetrics(font);
|
||||
float scaleFactor = _lineHeight * LINE_SCALE_RATIO / (float)FIXED_FONT_POINT_SIZE;
|
||||
return scaleFactor * (float)fontMetrics.width(qPrintable(text));
|
||||
const float TEXT_SCALE_ADJUST = 1.02f; // Experimentally detemined for the specified font
|
||||
const int TEXT_HEIGHT_ADJUST = -6;
|
||||
float scaleFactor = _lineHeight * TEXT_SCALE_ADJUST * LINE_SCALE_RATIO / (float)FIXED_FONT_POINT_SIZE;
|
||||
|
||||
QStringList lines = text.split(QRegExp("\r\n|\r|\n"));
|
||||
|
||||
float width = 0.0f;
|
||||
for (int i = 0; i < lines.count(); i += 1) {
|
||||
width = std::max(width, scaleFactor * (float)fontMetrics.width(qPrintable(lines[i])));
|
||||
}
|
||||
|
||||
float height = lines.count() * scaleFactor * (float)(fontMetrics.height() + TEXT_HEIGHT_ADJUST);
|
||||
|
||||
return QSizeF(width, height);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ public:
|
|||
virtual void setProperties(const QScriptValue& properties);
|
||||
virtual QScriptValue getProperty(const QString& property);
|
||||
|
||||
float textWidth(const QString& text) const; // Meters
|
||||
QSizeF textSize(const QString& test) const; // Meters
|
||||
|
||||
virtual Text3DOverlay* createClone() const;
|
||||
|
||||
|
|
|
@ -169,8 +169,20 @@ QScriptValue TextOverlay::getProperty(const QString& property) {
|
|||
return Overlay2D::getProperty(property);
|
||||
}
|
||||
|
||||
float TextOverlay::textWidth(const QString& text) const {
|
||||
QSizeF TextOverlay::textSize(const QString& text) const {
|
||||
|
||||
QFont font(SANS_FONT_FAMILY, _fontSize, DEFAULT_FONT_WEIGHT); // Same font properties as render()
|
||||
QFontMetrics fontMetrics(font);
|
||||
return fontMetrics.width(qPrintable(text));
|
||||
const int TEXT_HEIGHT_ADJUST = -2; // Experimentally determined for the specified font
|
||||
|
||||
QStringList lines = text.split(QRegExp("\r\n|\r|\n"));
|
||||
|
||||
int width = 0;
|
||||
for (int i = 0; i < lines.count(); i += 1) {
|
||||
width = std::max(width, fontMetrics.width(qPrintable(lines[i])));
|
||||
}
|
||||
|
||||
int height = lines.count() * (fontMetrics.height() + TEXT_HEIGHT_ADJUST);
|
||||
|
||||
return QSizeF(width, height);
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ public:
|
|||
virtual TextOverlay* createClone() const;
|
||||
virtual QScriptValue getProperty(const QString& property);
|
||||
|
||||
float textWidth(const QString& text) const; // Pixels
|
||||
QSizeF textSize(const QString& test) const; // Pixels
|
||||
|
||||
private:
|
||||
QString _text;
|
||||
|
|
|
@ -39,6 +39,7 @@ void registerMetaTypes(QScriptEngine* engine) {
|
|||
qScriptRegisterMetaType(engine, pickRayToScriptValue, pickRayFromScriptValue);
|
||||
qScriptRegisterMetaType(engine, collisionToScriptValue, collisionFromScriptValue);
|
||||
qScriptRegisterMetaType(engine, quuidToScriptValue, quuidFromScriptValue);
|
||||
qScriptRegisterMetaType(engine, qSizeFToScriptValue, qSizeFFromScriptValue);
|
||||
}
|
||||
|
||||
QScriptValue vec4toScriptValue(QScriptEngine* engine, const glm::vec4& vec4) {
|
||||
|
@ -206,3 +207,14 @@ void quuidFromScriptValue(const QScriptValue& object, QUuid& uuid) {
|
|||
uuid = fromString;
|
||||
}
|
||||
|
||||
QScriptValue qSizeFToScriptValue(QScriptEngine* engine, const QSizeF& qSizeF) {
|
||||
QScriptValue obj = engine->newObject();
|
||||
obj.setProperty("width", qSizeF.width());
|
||||
obj.setProperty("height", qSizeF.height());
|
||||
return obj;
|
||||
}
|
||||
|
||||
void qSizeFFromScriptValue(const QScriptValue& object, QSizeF& qSizeF) {
|
||||
qSizeF.setWidth(object.property("width").toVariant().toFloat());
|
||||
qSizeF.setHeight(object.property("height").toVariant().toFloat());
|
||||
}
|
||||
|
|
|
@ -81,4 +81,8 @@ void collisionFromScriptValue(const QScriptValue &object, Collision& collision);
|
|||
QScriptValue quuidToScriptValue(QScriptEngine* engine, const QUuid& uuid);
|
||||
void quuidFromScriptValue(const QScriptValue& object, QUuid& uuid);
|
||||
|
||||
//Q_DECLARE_METATYPE(QSizeF) // Don't need to to this becase it's arleady a meta type
|
||||
QScriptValue qSizeFToScriptValue(QScriptEngine* engine, const QSizeF& qSizeF);
|
||||
void qSizeFFromScriptValue(const QScriptValue& object, QSizeF& qSizeF);
|
||||
|
||||
#endif // hifi_RegisteredMetaTypes_h
|
||||
|
|
Loading…
Reference in a new issue