Support for input validation on TextFieldWidgets

See OnTextEdited(), IsValid(), TextColorInvalid (with the default in metrics.yaml).
This commit is contained in:
Pavlos Touboulidis
2014-05-01 19:35:11 +03:00
parent ce8c42b552
commit 713141cf4d
6 changed files with 17 additions and 1 deletions

View File

@@ -31,12 +31,15 @@ namespace OpenRA.Widgets
public Func<bool> OnTabKey = () => false; public Func<bool> OnTabKey = () => false;
public Func<bool> OnEscKey = () => false; public Func<bool> OnEscKey = () => false;
public Action OnLoseFocus = () => { }; public Action OnLoseFocus = () => { };
public Action OnTextEdited = () => { };
public int CursorPosition { get; set; } public int CursorPosition { get; set; }
public Func<bool> IsDisabled = () => false; public Func<bool> IsDisabled = () => false;
public Func<bool> IsValid = () => true;
public string Font = ChromeMetrics.Get<string>("TextfieldFont"); public string Font = ChromeMetrics.Get<string>("TextfieldFont");
public Color TextColor = ChromeMetrics.Get<Color>("TextfieldColor"); public Color TextColor = ChromeMetrics.Get<Color>("TextfieldColor");
public Color TextColorDisabled = ChromeMetrics.Get<Color>("TextfieldColorDisabled"); public Color TextColorDisabled = ChromeMetrics.Get<Color>("TextfieldColorDisabled");
public Color TextColorInvalid = ChromeMetrics.Get<Color>("TextfieldColorInvalid");
public TextFieldWidget() {} public TextFieldWidget() {}
protected TextFieldWidget(TextFieldWidget widget) protected TextFieldWidget(TextFieldWidget widget)
@@ -47,6 +50,7 @@ namespace OpenRA.Widgets
Font = widget.Font; Font = widget.Font;
TextColor = widget.TextColor; TextColor = widget.TextColor;
TextColorDisabled = widget.TextColorDisabled; TextColorDisabled = widget.TextColorDisabled;
TextColorInvalid = widget.TextColorInvalid;
VisualHeight = widget.VisualHeight; VisualHeight = widget.VisualHeight;
} }
@@ -148,7 +152,10 @@ namespace OpenRA.Widgets
if (e.Key == Keycode.DELETE) if (e.Key == Keycode.DELETE)
{ {
if (CursorPosition < Text.Length) if (CursorPosition < Text.Length)
{
Text = Text.Remove(CursorPosition, 1); Text = Text.Remove(CursorPosition, 1);
OnTextEdited();
}
return true; return true;
} }
@@ -156,6 +163,7 @@ namespace OpenRA.Widgets
{ {
CursorPosition--; CursorPosition--;
Text = Text.Remove(CursorPosition, 1); Text = Text.Remove(CursorPosition, 1);
OnTextEdited();
} }
return true; return true;
@@ -171,6 +179,7 @@ namespace OpenRA.Widgets
Text = Text.Insert(CursorPosition, text); Text = Text.Insert(CursorPosition, text);
CursorPosition++; CursorPosition++;
OnTextEdited();
return true; return true;
} }
@@ -228,7 +237,9 @@ namespace OpenRA.Widgets
Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom)); Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom));
} }
var color = disabled ? TextColorDisabled : TextColor; var color = disabled ? TextColorDisabled
: IsValid() ? TextColor
: TextColorInvalid;
font.DrawText(apparentText, textPos, color); font.DrawText(apparentText, textPos, color);
if (showCursor && HasKeyboardFocus) if (showCursor && HasKeyboardFocus)

View File

@@ -14,6 +14,7 @@ Metrics:
TextfieldFont: Regular TextfieldFont: Regular
TextfieldColor: 255,255,255 TextfieldColor: 255,255,255
TextfieldColorDisabled: 128,128,128 TextfieldColorDisabled: 128,128,128
TextfieldColorInvalid: 255,192,192
TextFont: Regular TextFont: Regular
TextColor: 255,255,255 TextColor: 255,255,255
TextContrast: false TextContrast: false

View File

@@ -14,6 +14,7 @@ Metrics:
TextfieldFont: Regular TextfieldFont: Regular
TextfieldColor: 255,255,255 TextfieldColor: 255,255,255
TextfieldColorDisabled: 128,128,128 TextfieldColorDisabled: 128,128,128
TextfieldColorInvalid: 255,192,192
TextFont: Regular TextFont: Regular
TextColor: 255,255,255 TextColor: 255,255,255
TextContrast: false TextContrast: false

View File

@@ -14,6 +14,7 @@ Metrics:
TextfieldFont: Regular TextfieldFont: Regular
TextfieldColor: 255,255,255 TextfieldColor: 255,255,255
TextfieldColorDisabled: 128,128,128 TextfieldColorDisabled: 128,128,128
TextfieldColorInvalid: 255,192,192
TextFont: Regular TextFont: Regular
TextColor: 255,255,255 TextColor: 255,255,255
TextContrast: false TextContrast: false

View File

@@ -14,6 +14,7 @@ Metrics:
TextfieldFont: Regular TextfieldFont: Regular
TextfieldColor: 255,255,255 TextfieldColor: 255,255,255
TextfieldColorDisabled: 128,128,128 TextfieldColorDisabled: 128,128,128
TextfieldColorInvalid: 255,192,192
TextFont: Regular TextFont: Regular
TextColor: 255,255,255 TextColor: 255,255,255
TextContrast: false TextContrast: false

View File

@@ -14,6 +14,7 @@ Metrics:
TextfieldFont: Regular TextfieldFont: Regular
TextfieldColor: 255,255,255 TextfieldColor: 255,255,255
TextfieldColorDisabled: 128,128,128 TextfieldColorDisabled: 128,128,128
TextfieldColorInvalid: 255,192,192
TextFont: Regular TextFont: Regular
TextColor: 255,255,255 TextColor: 255,255,255
TextContrast: false TextContrast: false