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

View File

@@ -356,11 +356,11 @@ Container:
TextField@NAME: TextField@NAME:
Id:NAME Id:NAME
Text:Name Text:Name
Width:127 Width:139
Height:25 Height:25
X:0 X:0
Y:0 Y:0
Bold: true MaxLength:16
Button@COLOR: Button@COLOR:
Id:COLOR Id:COLOR
Width:65 Width:65
@@ -378,7 +378,7 @@ Container:
Id:FACTION Id:FACTION
Width:110 Width:110
Height:25 Height:25
X:256 X:244
Y:0 Y:0
Children: Children:
Image@FACTIONFLAG: Image@FACTIONFLAG:
@@ -399,7 +399,7 @@ Container:
Text:Team Text:Team
Width:25 Width:25
Height:25 Height:25
X:398 X:374
Y:0 Y:0
Checkbox@STATUS: Checkbox@STATUS:
Id:STATUS Id:STATUS
@@ -417,7 +417,7 @@ Container:
Label@NAME: Label@NAME:
Id:NAME Id:NAME
Text:Name Text:Name
Width:127 Width:139
Height:25 Height:25
X:0 X:0
Y:0 Y:0
@@ -431,7 +431,7 @@ Container:
Id:FACTION Id:FACTION
Width:110 Width:110
Height:25 Height:25
X:256 X:244
Y:0 Y:0
Children: Children:
Image@FACTIONFLAG: Image@FACTIONFLAG:
@@ -452,7 +452,7 @@ Container:
Text:Team Text:Team
Width:70 Width:70
Height:25 Height:25
X:375 X:351
Y:0 Y:0
Align:Center Align:Center
Bold: false Bold: false
@@ -467,7 +467,7 @@ Container:
Children: Children:
Label@LABEL_LOBBY_NAME: Label@LABEL_LOBBY_NAME:
Id:LABEL_LOBBY_NAME Id:LABEL_LOBBY_NAME
Width:127 Width:139
Height:25 Height:25
X:0 X:0
Y:0 Y:0
@@ -487,7 +487,7 @@ Container:
Id:LABEL_LOBBY_FACTION Id:LABEL_LOBBY_FACTION
Width:110 Width:110
Height:25 Height:25
X:256 X:244
Y:0 Y:0
Text:Faction Text:Faction
Align:Center Align:Center
@@ -496,7 +496,7 @@ Container:
Id:LABEL_LOBBY_TEAM Id:LABEL_LOBBY_TEAM
Width:70 Width:70
Height:25 Height:25
X:375 X:351
Y:0 Y:0
Text:Team Text:Team
Align:Center Align:Center
@@ -530,7 +530,7 @@ Container:
#rabeep1.aud for ra #rabeep1.aud for ra
Label@LABEL_CHATTYPE: Label@LABEL_CHATTYPE:
Id:LABEL_CHATTYPE Id:LABEL_CHATTYPE
Width:70 Width:65
Height:25 Height:25
X:0 X:0
Y:PARENT_BOTTOM - 75 Y:PARENT_BOTTOM - 75
@@ -539,9 +539,9 @@ Container:
TextField@CHAT_TEXTFIELD: TextField@CHAT_TEXTFIELD:
Id:CHAT_TEXTFIELD Id:CHAT_TEXTFIELD
Visible:true Visible:true
X:75 X:70
Y:PARENT_BOTTOM - 74 Y:PARENT_BOTTOM - 74
Width:705 Width:710
Height:25 Height:25
Button@DISCONNECT_BUTTON: Button@DISCONNECT_BUTTON:
Id:DISCONNECT_BUTTON Id:DISCONNECT_BUTTON