Skip to content

Commit

Permalink
sprite font typewriter animation
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij committed Jun 5, 2022
1 parent 1312549 commit 93cb6b1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
7 changes: 5 additions & 2 deletions Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ void Editor::Initialize()

void EditorLoadingScreen::Load()
{
font = wi::SpriteFont("Loading...", wi::font::Params(0, 0, 36, wi::font::WIFALIGN_CENTER, wi::font::WIFALIGN_CENTER));
font = wi::SpriteFont("Loading...", wi::font::Params(0, 0, 36, wi::font::WIFALIGN_LEFT, wi::font::WIFALIGN_CENTER));
font.anim.typewriter.time = 2;
font.anim.typewriter.looped = true;
font.anim.typewriter.character_start = 7;
AddFont(&font);

sprite = wi::Sprite("images/logo_small.png");
Expand All @@ -62,7 +65,7 @@ void EditorLoadingScreen::Load()
}
void EditorLoadingScreen::Update(float dt)
{
font.params.posX = GetLogicalWidth()*0.5f;
font.params.posX = GetLogicalWidth()*0.5f - font.TextWidth() * 0.5f;
font.params.posY = GetLogicalHeight()*0.5f;
sprite.params.pos = XMFLOAT3(GetLogicalWidth()*0.5f, GetLogicalHeight()*0.5f - font.TextHeight(), 0);

Expand Down
54 changes: 42 additions & 12 deletions WickedEngine/wiFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,24 +514,22 @@ namespace wi::font
canvas = current_canvas;
}

Cursor Draw(const char* text, const Params& params, CommandList cmd)
Cursor Draw(const char* text, size_t text_length, const Params& params, CommandList cmd)
{
size_t text_length = strlen(text);
if (text_length == 0)
{
return Cursor();
}
return Draw_internal(text, text_length, params, cmd);
}
Cursor Draw(const wchar_t* text, const Params& params, CommandList cmd)
Cursor Draw(const wchar_t* text, size_t text_length, const Params& params, CommandList cmd)
{
size_t text_length = wcslen(text);
if (text_length == 0)
{
return Cursor();
}
return Draw_internal(text, text_length, params, cmd);
}
Cursor Draw(const char* text, const Params& params, CommandList cmd)
{
return Draw_internal(text, strlen(text), params, cmd);
}
Cursor Draw(const wchar_t* text, const Params& params, CommandList cmd)
{
return Draw_internal(text, wcslen(text), params, cmd);
}
Cursor Draw(const std::string& text, const Params& params, CommandList cmd)
{
return Draw_internal(text.c_str(), text.length(), params, cmd);
Expand All @@ -541,6 +539,22 @@ namespace wi::font
return Draw_internal(text.c_str(), text.length(), params, cmd);
}

XMFLOAT2 TextSize(const char* text, size_t text_length, const Params& params)
{
if (text_length == 0)
{
return XMFLOAT2(0, 0);
}
return ParseText(text, text_length, params).cursor.size;
}
XMFLOAT2 TextSize(const wchar_t* text, size_t text_length, const Params& params)
{
if (text_length == 0)
{
return XMFLOAT2(0, 0);
}
return ParseText(text, text_length, params).cursor.size;
}
XMFLOAT2 TextSize(const char* text, const Params& params)
{
size_t text_length = strlen(text);
Expand Down Expand Up @@ -576,6 +590,14 @@ namespace wi::font
return ParseText(text.c_str(), text.length(), params).cursor.size;
}

float TextWidth(const char* text, size_t text_length, const Params& params)
{
return TextSize(text, text_length, params).x;
}
float TextWidth(const wchar_t* text, size_t text_length, const Params& params)
{
return TextSize(text, text_length, params).x;
}
float TextWidth(const char* text, const Params& params)
{
return TextSize(text, params).x;
Expand All @@ -593,6 +615,14 @@ namespace wi::font
return TextSize(text, params).x;
}

float TextHeight(const char* text, size_t text_length, const Params& params)
{
return TextSize(text, text_length, params).y;
}
float TextHeight(const wchar_t* text, size_t text_length, const Params& params)
{
return TextSize(text, text_length, params).y;
}
float TextHeight(const char* text, const Params& params)
{
return TextSize(text, params).y;
Expand Down
8 changes: 8 additions & 0 deletions WickedEngine/wiFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,34 @@ namespace wi::font
// Draw text with specified parameters and return cursor for last word
// The next Draw() can continue from where this left off by using the return value of this function
// in wi::font::Params::cursor
Cursor Draw(const char* text, size_t text_length, const Params& params, wi::graphics::CommandList cmd);
Cursor Draw(const wchar_t* text, size_t text_length, const Params& params, wi::graphics::CommandList cmd);
Cursor Draw(const char* text, const Params& params, wi::graphics::CommandList cmd);
Cursor Draw(const wchar_t* text, const Params& params, wi::graphics::CommandList cmd);
Cursor Draw(const std::string& text, const Params& params, wi::graphics::CommandList cmd);
Cursor Draw(const std::wstring& text, const Params& params, wi::graphics::CommandList cmd);

// Computes the text's size measurements in logical canvas coordinates
XMFLOAT2 TextSize(const char* text, size_t text_length, const Params& params);
XMFLOAT2 TextSize(const wchar_t* text, size_t text_length, const Params& params);
XMFLOAT2 TextSize(const char* text, const Params& params);
XMFLOAT2 TextSize(const wchar_t* text, const Params& params);
XMFLOAT2 TextSize(const std::string& text, const Params& params);
XMFLOAT2 TextSize(const std::wstring& text, const Params& params);

// Computes the text's width in logical canvas coordinates
// Avoid calling TextWidth() and TextHeight() both, instead use TextSize() if you need both measurements!
float TextWidth(const char* text, size_t text_length, const Params& params);
float TextWidth(const wchar_t* text, size_t text_length, const Params& params);
float TextWidth(const char* text, const Params& params);
float TextWidth(const wchar_t* text, const Params& params);
float TextWidth(const std::string& text, const Params& params);
float TextWidth(const std::wstring& text, const Params& params);

// Computes the text's height in logical canvas coordinates
// Avoid calling TextWidth() and TextHeight() both, instead use TextSize() if you need both measurements!
float TextHeight(const char* text, size_t text_length, const Params& params);
float TextHeight(const wchar_t* text, size_t text_length, const Params& params);
float TextHeight(const char* text, const Params& params);
float TextHeight(const wchar_t* text, const Params& params);
float TextHeight(const std::string& text, const Params& params);
Expand Down
19 changes: 18 additions & 1 deletion WickedEngine/wiSpriteFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,30 @@ namespace wi
{
if (IsDisableUpdate())
return;

if (anim.typewriter.time > 0)
{
anim.typewriter.elapsed += dt;
if (anim.typewriter.looped && anim.typewriter.elapsed > anim.typewriter.time)
{
anim.typewriter.reset();
}
}
}

void SpriteFont::Draw(CommandList cmd) const
{
if (IsHidden())
return;
wi::font::Draw(text, params, cmd);

size_t text_length = text.length();

if (anim.typewriter.time > 0)
{
text_length = std::min(text_length, size_t(wi::math::Lerp(float(std::min(text_length, anim.typewriter.character_start)), float(text_length + 1), anim.typewriter.elapsed / anim.typewriter.time)));
}

wi::font::Draw(text.c_str(), text_length, params, cmd);
}

XMFLOAT2 SpriteFont::TextSize() const
Expand Down
17 changes: 17 additions & 0 deletions WickedEngine/wiSpriteFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,22 @@ namespace wi

std::string GetTextA() const;
const std::wstring& GetText() const;

struct Animation
{
struct Typewriter
{
float time = 0; // time to fully type the text in seconds (0: disable)
bool looped = false; // if true, typing starts over when finished
size_t character_start = 0; // starting character for the animation

float elapsed = 0; // internal use; you don't need to initialize

void reset()
{
elapsed = 0;
}
} typewriter;
} anim;
};
}
2 changes: 1 addition & 1 deletion WickedEngine/wiVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace wi::version
// minor features, major updates, breaking compatibility changes
const int minor = 60;
// minor bug fixes, alterations, refactors, updates
const int revision = 83;
const int revision = 84;

const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);

Expand Down

0 comments on commit 93cb6b1

Please sign in to comment.