MaxLength on textfields

This commit is contained in:
Paul Chote
2010-07-10 23:50:42 +12:00
parent 62995ffad8
commit 3296cc2ec1
2 changed files with 26 additions and 17 deletions

View File

@@ -26,6 +26,7 @@ namespace OpenRA.Widgets
class TextFieldWidget : Widget
{
public string Text = "";
public int MaxLength = 0;
public bool Bold = false;
public int VisualHeight = 1;
public Func<bool> OnEnterKey = () => {return false;};
@@ -41,6 +42,9 @@ namespace OpenRA.Widgets
:base(widget)
{
Text = (widget as TextFieldWidget).Text;
MaxLength = (widget as TextFieldWidget).MaxLength;
Bold = (widget as TextFieldWidget).Bold;
VisualHeight = (widget as TextFieldWidget).VisualHeight;
}
public override bool HandleInput(MouseInput mi)
@@ -103,7 +107,13 @@ namespace OpenRA.Widgets
Text = Text.Remove(Text.Length - 1);
}
else if (!char.IsControl(c))
{
Console.WriteLine("{0} {1}",MaxLength, Text.Length);
if (MaxLength > 0 && Text.Length >= MaxLength)
return;
Text += c;
}
}
int blinkCycle = 10;
@@ -122,9 +132,8 @@ namespace OpenRA.Widgets
{
int margin = 5;
var font = (Bold) ? Game.chrome.renderer.BoldFont : Game.chrome.renderer.RegularFont;
var text = Text + ((showCursor && Chrome.selectedWidget == this) ? "|" : "");
var textSize = font.Measure(Text);
var cursor = (showCursor && Chrome.selectedWidget == this) ? "|" : "";
var textSize = font.Measure(Text + "|");
var pos = DrawPosition();
WidgetUtils.DrawPanel("dialog3",
@@ -140,7 +149,7 @@ namespace OpenRA.Widgets
Game.chrome.renderer.Device.EnableScissor(pos.X + margin, pos.Y, Bounds.Width - 2*margin, Bounds.Bottom);
}
font.DrawText(text, textPos, Color.White);
font.DrawText(Text + cursor, textPos, Color.White);
if (textSize.X > Bounds.Width - 2*margin)
{