Quellcode durchsuchen

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.
undefined
Sam Hocevar vor 9 Jahren
Ursprung
Commit
d4e3ad4a4e
4 geänderte Dateien mit 63 neuen und 29 gelöschten Zeilen
  1. +3
    -1
      src/font.cpp
  2. +1
    -1
      src/font.h
  3. +39
    -19
      src/text.cpp
  4. +20
    -8
      src/text.h

+ 3
- 1
src/font.cpp Datei anzeigen

@@ -62,7 +62,7 @@ char const *Font::GetName()
return data->m_name.C(); 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; float origin_x = pos.x;
while (*str) while (*str)
@@ -87,6 +87,8 @@ void Font::Print(vec3 pos, char const *str, vec2 scale)
pos.x += data->size.x * scale.x; pos.x += data->size.x * scale.x;
break; break;
} }

pos.x += data->size.x * scale.x * spacing;
} }
} }




+ 1
- 1
src/font.h Datei anzeigen

@@ -35,7 +35,7 @@ protected:


public: public:
/* New methods */ /* 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; ivec2 GetSize() const;


private: private:


+ 39
- 19
src/text.cpp Datei anzeigen

@@ -28,9 +28,12 @@ class TextData
friend class Text; friend class Text;


private: private:
int font, align;
int m_font;
TextAlign m_align;
String m_text; 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) Text::Text(String const &text, char const *font)
: data(new TextData()) : data(new TextData())
{ {
data->font = Forge::Register(font);
data->m_font = Forge::Register(font);
data->m_align = TextAlign::Left;
data->m_text = text; 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; m_drawgroup = DRAWGROUP_HUD;
} }
@@ -59,22 +65,32 @@ void Text::SetInt(int val)


void Text::SetPos(vec3 pos) 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() ivec2 Text::GetFontSize()
{ {
Font *font = Forge::GetFont(data->font);
Font *font = Forge::GetFont(data->m_font);
return font->GetSize(); return font->GetSize();
} }


@@ -82,22 +98,26 @@ void Text::TickDraw(float seconds, Scene &scene)
{ {
Entity::TickDraw(seconds, 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); 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() Text::~Text()
{ {
Forge::Deregister(data->font);
Forge::Deregister(data->m_font);
delete data; delete data;
} }




+ 20
- 8
src/text.h Datei anzeigen

@@ -22,26 +22,38 @@ namespace lol


class TextData; class TextData;


enum class TextAlign
{
Left,
Right,
Center,
};

class Text : public Entity class Text : public Entity
{ {
public: public:
Text(String const &text, char const *font); Text(String const &text, char const *font);
virtual ~Text(); virtual ~Text();


/** Set the text that will be displayed */
void SetText(String const &text); void SetText(String const &text);
void SetInt(int val); void SetInt(int val);

/** Set the position of the text object, in the 3D world. */
void SetPos(vec3 pos); 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(); vec3 GetPos();
void SetAlign(int align);
ivec2 GetFontSize(); ivec2 GetFontSize();


enum
{
ALIGN_LEFT,
ALIGN_RIGHT,
ALIGN_CENTER,
};

protected: protected:
virtual void TickDraw(float seconds, Scene &scene); virtual void TickDraw(float seconds, Scene &scene);




Laden…
Abbrechen
Speichern