From d4e3ad4a4e5818be1ceaa9e913b31b8ae010cd67 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 18 Apr 2015 12:40:10 +0000 Subject: [PATCH] text: new features and minor refactoring. Text objects now support character spacing (as a fraction of the character width) and text scaling. Also, the alignment enum is now safe. --- src/font.cpp | 4 +++- src/font.h | 2 +- src/text.cpp | 58 +++++++++++++++++++++++++++++++++++----------------- src/text.h | 28 +++++++++++++++++-------- 4 files changed, 63 insertions(+), 29 deletions(-) diff --git a/src/font.cpp b/src/font.cpp index 4bebff56..f6932425 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -62,7 +62,7 @@ char const *Font::GetName() return data->m_name.C(); } -void Font::Print(vec3 pos, char const *str, vec2 scale) +void Font::Print(vec3 pos, char const *str, vec2 scale, float spacing) { float origin_x = pos.x; while (*str) @@ -87,6 +87,8 @@ void Font::Print(vec3 pos, char const *str, vec2 scale) pos.x += data->size.x * scale.x; break; } + + pos.x += data->size.x * scale.x * spacing; } } diff --git a/src/font.h b/src/font.h index 2ce22aa0..a07b5a8e 100644 --- a/src/font.h +++ b/src/font.h @@ -35,7 +35,7 @@ protected: public: /* New methods */ - void Print(vec3 pos, char const *str, vec2 scale = vec2(1.0f)); + void Print(vec3 pos, char const *str, vec2 scale = vec2(1.0f), float spacing = 0.0f); ivec2 GetSize() const; private: diff --git a/src/text.cpp b/src/text.cpp index 379dccbd..d4e5d0f5 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -28,9 +28,12 @@ class TextData friend class Text; private: - int font, align; + int m_font; + TextAlign m_align; String m_text; - vec3 pos; + vec3 m_pos; + vec2 m_scale; + float m_spacing; }; /* @@ -40,9 +43,12 @@ private: Text::Text(String const &text, char const *font) : data(new TextData()) { - data->font = Forge::Register(font); + data->m_font = Forge::Register(font); + data->m_align = TextAlign::Left; data->m_text = text; - data->pos = vec3(0, 0, 0); + data->m_pos = vec3(0, 0, 0); + data->m_scale = vec2(1.f); + data->m_spacing = 0.f; m_drawgroup = DRAWGROUP_HUD; } @@ -59,22 +65,32 @@ void Text::SetInt(int val) void Text::SetPos(vec3 pos) { - data->pos = pos; + data->m_pos = pos; } -vec3 Text::GetPos() +void Text::SetScale(vec2 scale) +{ + data->m_scale = scale; +} + +void Text::SetSpacing(float spacing) +{ + data->m_spacing = spacing; +} + +void Text::SetAlign(TextAlign align) { - return (vec3)data->pos; + data->m_align = align; } -void Text::SetAlign(int align) +vec3 Text::GetPos() { - data->align = align; + return (vec3)data->m_pos; } ivec2 Text::GetFontSize() { - Font *font = Forge::GetFont(data->font); + Font *font = Forge::GetFont(data->m_font); return font->GetSize(); } @@ -82,22 +98,26 @@ void Text::TickDraw(float seconds, Scene &scene) { Entity::TickDraw(seconds, scene); - int length = data->m_text.count(); - if (length) + if (auto length = data->m_text.count()) { - Font *font = Forge::GetFont(data->font); + Font *font = Forge::GetFont(data->m_font); vec3 delta(0.0f); - if (data->align == ALIGN_RIGHT) - delta.x -= length * font->GetSize().x; - else if (data->align == ALIGN_CENTER) - delta.x -= length * font->GetSize().x / 2; - font->Print(data->pos + delta, data->m_text.C()); + float text_width = length * font->GetSize().x + + (length - 1) * data->m_spacing; + + if (data->m_align == TextAlign::Right) + delta.x -= text_width * data->m_scale.x; + else if (data->m_align == TextAlign::Center) + delta.x -= 0.5f * text_width * data->m_scale.x; + + font->Print(data->m_pos + delta, data->m_text.C(), + data->m_scale, data->m_spacing); } } Text::~Text() { - Forge::Deregister(data->font); + Forge::Deregister(data->m_font); delete data; } diff --git a/src/text.h b/src/text.h index f3b2f796..76f4ad9f 100644 --- a/src/text.h +++ b/src/text.h @@ -22,26 +22,38 @@ namespace lol class TextData; +enum class TextAlign +{ + Left, + Right, + Center, +}; + class Text : public Entity { public: Text(String const &text, char const *font); virtual ~Text(); + /** Set the text that will be displayed */ void SetText(String const &text); void SetInt(int val); + + /** Set the position of the text object, in the 3D world. */ void SetPos(vec3 pos); + + /** Set the text scaling */ + void SetScale(vec2 scale); + + /** Set the spacing between characters, as a fraction of character width */ + void SetSpacing(float spacing); + + /** Set the alignment method */ + void SetAlign(TextAlign align); + vec3 GetPos(); - void SetAlign(int align); ivec2 GetFontSize(); - enum - { - ALIGN_LEFT, - ALIGN_RIGHT, - ALIGN_CENTER, - }; - protected: virtual void TickDraw(float seconds, Scene &scene);