KumiScript/renderer/TextBox.cs

176 lines
No EOL
4.9 KiB
C#

using KumiScript.Loader;
using SDL2;
namespace KumiScript.Renderer
{
public class TextBox : InterfaceElement
{
SDLTexture _texture;
readonly SDLRenderer _renderer;
int _width;
int _height;
int _hpadding;
int _vpadding;
int _linespacing;
readonly bool _consecutive;
List<Text> _texts;
List<Text> _textsQueue;
TextBoxState _state;
private enum TextBoxState
{
StateRenderLine,
StateWaitEnter,
StateDone
}
public TextBox(string path, SDLRenderer renderer, bool consecutive)
{
_texture = new SDLTexture(path, renderer);
_renderer = renderer;
KSMetaParser parser = new KSMetaParser(string.Concat(path, ".ksmeta"));
_width = parser.GetAttribute("width");
_height = parser.GetAttribute("height");
_hpadding = parser.GetAttribute("hpadding");
_vpadding = parser.GetAttribute("vpadding");
_linespacing = parser.GetAttribute("linespacing");
_texts = new List<Text>(8);
_textsQueue = new List<Text>(8);
_consecutive = consecutive;
if (consecutive)
_state = TextBoxState.StateRenderLine;
else
_state = TextBoxState.StateDone;
}
public TextBox(string path, SDLRenderer renderer, int linespacing, bool consecutive)
{
_texture = new SDLTexture(path, renderer);
_renderer = renderer;
KSMetaParser parser = new KSMetaParser(string.Concat(path, ".ksmeta"));
_width = parser.GetAttribute("width");
_height = parser.GetAttribute("height");
_hpadding = parser.GetAttribute("hpadding");
_vpadding = parser.GetAttribute("vpadding");
_linespacing = linespacing;
_texts = new List<Text>(8);
_textsQueue = new List<Text>(8);
_consecutive = consecutive;
if (consecutive)
_state = TextBoxState.StateRenderLine;
else
_state = TextBoxState.StateDone;
}
public override void Click()
{
return;
}
public override void Dispose()
{
_texture.Dispose();
}
public override void Draw(int x, int y)
{
SDL.SDL_Rect destRect = new SDL.SDL_Rect();
destRect.x = x;
destRect.y = y;
destRect.w = _width;
destRect.h = _height;
SDL.SDL_Rect sourceRect = new SDL.SDL_Rect();
sourceRect.x = 0;
sourceRect.y = 0;
sourceRect.w = _width;
sourceRect.h = _height;
_texture.Draw(_renderer, sourceRect, destRect);
int retw = x + _hpadding;
int penx = retw;
int peny = y + _vpadding;
foreach (Text t in _texts)
{
t.DrawWrapping(penx, peny, _width - _hpadding, _linespacing, retw, ' ', out penx, out peny);
}
if (_state == TextBoxState.StateRenderLine)
{
Text t2 = _textsQueue.First();
bool done = t2.DrawWrappingAnimate(penx, peny, _width - _hpadding, _linespacing, retw, ' ', out penx, out peny);
if (done)
{
_texts.Add(t2);
_textsQueue.RemoveAt(0);
_state = _textsQueue.Any() ? TextBoxState.StateWaitEnter : TextBoxState.StateDone;
}
}
}
public void BreakLine()
{
AddText(new DummyText("", null));
}
public void AddText(Text t)
{
if (_consecutive)
_textsQueue.Add(t);
else
_texts.Add(t);
return;
}
public void UpdateState()
{
switch(_state)
{
case TextBoxState.StateRenderLine:
SkipLine();
return;
case TextBoxState.StateWaitEnter:
_state = TextBoxState.StateRenderLine;
return;
}
return;
}
private void SkipLine()
{
Text t = _textsQueue.First();
t.ForceProgress();
return;
}
public void ClearText()
{
_texts.Clear();
}
public bool IsDone()
{
return _state == TextBoxState.StateDone;
}
public override int GetBitmapHeight()
{
return _height;
}
public override int GetBitmapWidth()
{
return _width;
}
public override void OnMouseout()
{
return;
}
public override void OnMouseover()
{
return;
}
}
}