KumiScript/renderer/Text.cs

149 lines
4 KiB
C#
Raw Normal View History

namespace KumiScript.Renderer
{
public class Text : InterfaceElement
{
readonly string _text;
readonly ManagedFont _font;
string[]? _words;
int _progress;
int _animProg;
readonly int _charcount;
public Text(string text, ManagedFont font)
{
_text = text;
_font = font;
_progress = 0;
_animProg = -24;
_charcount = text.Length;
}
public override void Click()
{
return;
}
public override void Draw(int x, int y)
{
foreach (char c in _text)
{
Glyph g = _font.GetGlyph(c);
g.DrawAdvancePen(x, y, out x);
}
}
public virtual void DrawWrapping(int x, int y, int maxw, int spacing, int retw, char delim, out int penx, out int peny)
{
if (_words is null)
_words = _text.Split(delim);
int spacew = _font.GetGlyph(delim).GetWidth();
penx = x;
peny = y;
foreach (string word in _words)
{
int w = 0;
Glyph[] gs = new Glyph[word.Length];
for (int i = 0; i < word.Length; i++)
{
gs[i] = _font.GetGlyph(word[i]);
w += gs[i].GetWidth();
}
if (penx + w >= maxw)
{
penx = retw;
peny += spacing;
}
foreach (Glyph g in gs)
g.DrawAdvancePen(penx, peny, out penx);
penx += spacew;
}
return;
}
public virtual bool DrawWrappingAnimate(int x, int y, int maxw, int spacing, int retw, char delim, out int penx, out int peny)
{
int c = 2;
int dist;
_animProg += 24;
if (_words is null)
_words = _text.Split(delim);
int spacew = _font.GetGlyph(delim).GetWidth();
penx = x;
peny = y;
foreach (string word in _words)
{
int w = 0;
Glyph[] gs = new Glyph[word.Length];
for (int i = 0; i < word.Length; i++)
{
gs[i] = _font.GetGlyph(word[i]);
w += gs[i].GetWidth();
}
if (penx + w >= maxw)
{
penx = retw;
peny += spacing;
}
foreach (Glyph g in gs)
{
if (_animProg >= 60)
{
_animProg = 0;
_progress++;
}
dist = _progress - c;
if (dist > 0)
{
g.DrawAdvancePen(penx, peny, out penx);
c++;
}
else if (dist < -4)
{
return false;
}
else
{
g.DrawAdvancePenModAlpha(penx, peny, out penx, (byte) (255 + dist * 42));
c++;
}
}
penx += spacew;
}
return true;
}
public override int GetBitmapHeight()
{
throw new NotImplementedException();
}
public override int GetBitmapWidth()
{
throw new NotImplementedException();
}
public override void OnMouseout()
{
return;
}
public override void OnMouseover()
{
return;
}
public void ForceProgress()
{
_progress = _charcount;
return;
}
public override void Dispose()
{
return;
}
}
}