瀏覽代碼

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 9 年之前
父節點
當前提交
d4e3ad4a4e
共有 4 個檔案被更改,包括 63 行新增29 行删除
  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 查看文件

@@ -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;
}
}



+ 1
- 1
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:


+ 39
- 19
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;
}



+ 20
- 8
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);



Loading…
取消
儲存