Textfield cursor support and forward-delete (rewritten by pchote).

This commit is contained in:
geckosoft
2010-11-22 21:39:33 +01:00
committed by Paul Chote
parent 5e6f325df1
commit 8085bcb232

View File

@@ -23,6 +23,7 @@ namespace OpenRA.Widgets
public Func<bool> OnEnterKey = () => false; public Func<bool> OnEnterKey = () => false;
public Func<bool> OnTabKey = () => false; public Func<bool> OnTabKey = () => false;
public Action OnLoseFocus = () => { }; public Action OnLoseFocus = () => { };
public int CursorPosition { get; protected set; }
public TextFieldWidget() : base() {} public TextFieldWidget() : base() {}
protected TextFieldWidget(TextFieldWidget widget) protected TextFieldWidget(TextFieldWidget widget)
@@ -72,23 +73,55 @@ namespace OpenRA.Widgets
if (e.KeyChar == '\t' && OnTabKey()) if (e.KeyChar == '\t' && OnTabKey())
return true; return true;
if (e.KeyName == "left")
{
if (CursorPosition > 0)
CursorPosition--;
return true;
}
if (e.KeyName == "right")
{
if (CursorPosition <= Text.Length-1)
CursorPosition++;
return true;
}
if (e.KeyName == "delete")
{
if (Text.Length > 0 && CursorPosition < Text.Length)
{
Text = Text.Remove(CursorPosition, 1);
}
return true;
}
TypeChar(e.KeyChar); TypeChar(e.KeyChar);
return true; return true;
} }
public void TypeChar(char c) public void TypeChar(char c)
{ {
// backspace
if (c == '\b' || c == 0x7f) if (c == '\b' || c == 0x7f)
{ {
if (Text.Length > 0) if (Text.Length > 0 && CursorPosition > 0)
Text = Text.Remove(Text.Length - 1); {
Text = Text.Remove(CursorPosition - 1, 1);
CursorPosition--;
}
} }
else if (!char.IsControl(c)) else if (!char.IsControl(c))
{ {
if (MaxLength > 0 && Text.Length >= MaxLength) if (MaxLength > 0 && Text.Length >= MaxLength)
return; return;
Text += c; Text = Text.Insert(CursorPosition, c.ToString());
CursorPosition++;
} }
} }
@@ -101,17 +134,24 @@ namespace OpenRA.Widgets
blinkCycle = 20; blinkCycle = 20;
showCursor ^= true; showCursor ^= true;
} }
base.Tick(); base.Tick();
} }
public virtual void DrawWithString(string text) public virtual void DrawWithString(string text)
{ {
if (text == null) text = "";
int margin = 5; int margin = 5;
var font = (Bold) ? Game.Renderer.BoldFont : Game.Renderer.RegularFont; var font = (Bold) ? Game.Renderer.BoldFont : Game.Renderer.RegularFont;
var cursor = (showCursor && Focused) ? "|" : "";
var textSize = font.Measure(text + "|");
var pos = RenderOrigin; var pos = RenderOrigin;
if (CursorPosition > text.Length)
CursorPosition = text.Length;
var textSize = font.Measure(text);
var cursorPosition = font.Measure(text.Substring(0,CursorPosition));
WidgetUtils.DrawPanel("dialog3", WidgetUtils.DrawPanel("dialog3",
new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height)); new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height));
@@ -127,7 +167,10 @@ namespace OpenRA.Widgets
Game.Renderer.EnableScissor(pos.X + margin, pos.Y, Bounds.Width - 2 * margin, Bounds.Bottom); Game.Renderer.EnableScissor(pos.X + margin, pos.Y, Bounds.Width - 2 * margin, Bounds.Bottom);
} }
font.DrawText(text + cursor, textPos, Color.White); font.DrawText(text, textPos, Color.White);
if (showCursor && Focused)
font.DrawText("|", new float2(textPos.X + cursorPosition.X - 2, textPos.Y), Color.White);
if (textSize.X > Bounds.Width - 2 * margin) if (textSize.X > Bounds.Width - 2 * margin)
Game.Renderer.DisableScissor(); Game.Renderer.DisableScissor();