From 713141cf4d2eb2f9e7980d12fd39642f43e5b103 Mon Sep 17 00:00:00 2001 From: Pavlos Touboulidis Date: Thu, 1 May 2014 19:35:11 +0300 Subject: [PATCH] Support for input validation on TextFieldWidgets See OnTextEdited(), IsValid(), TextColorInvalid (with the default in metrics.yaml). --- OpenRA.Game/Widgets/TextFieldWidget.cs | 13 ++++++++++++- mods/cnc/metrics.yaml | 1 + mods/d2k/metrics.yaml | 1 + mods/modchooser/metrics.yaml | 1 + mods/ra/metrics.yaml | 1 + mods/ts/metrics.yaml | 1 + 6 files changed, 17 insertions(+), 1 deletion(-) diff --git a/OpenRA.Game/Widgets/TextFieldWidget.cs b/OpenRA.Game/Widgets/TextFieldWidget.cs index e7b6474d3a..c5cd7b0bde 100644 --- a/OpenRA.Game/Widgets/TextFieldWidget.cs +++ b/OpenRA.Game/Widgets/TextFieldWidget.cs @@ -31,12 +31,15 @@ namespace OpenRA.Widgets public Func OnTabKey = () => false; public Func OnEscKey = () => false; public Action OnLoseFocus = () => { }; + public Action OnTextEdited = () => { }; public int CursorPosition { get; set; } public Func IsDisabled = () => false; + public Func IsValid = () => true; public string Font = ChromeMetrics.Get("TextfieldFont"); public Color TextColor = ChromeMetrics.Get("TextfieldColor"); public Color TextColorDisabled = ChromeMetrics.Get("TextfieldColorDisabled"); + public Color TextColorInvalid = ChromeMetrics.Get("TextfieldColorInvalid"); public TextFieldWidget() {} protected TextFieldWidget(TextFieldWidget widget) @@ -47,6 +50,7 @@ namespace OpenRA.Widgets Font = widget.Font; TextColor = widget.TextColor; TextColorDisabled = widget.TextColorDisabled; + TextColorInvalid = widget.TextColorInvalid; VisualHeight = widget.VisualHeight; } @@ -148,7 +152,10 @@ namespace OpenRA.Widgets if (e.Key == Keycode.DELETE) { if (CursorPosition < Text.Length) + { Text = Text.Remove(CursorPosition, 1); + OnTextEdited(); + } return true; } @@ -156,6 +163,7 @@ namespace OpenRA.Widgets { CursorPosition--; Text = Text.Remove(CursorPosition, 1); + OnTextEdited(); } return true; @@ -171,6 +179,7 @@ namespace OpenRA.Widgets Text = Text.Insert(CursorPosition, text); CursorPosition++; + OnTextEdited(); return true; } @@ -228,7 +237,9 @@ namespace OpenRA.Widgets Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom)); } - var color = disabled ? TextColorDisabled : TextColor; + var color = disabled ? TextColorDisabled + : IsValid() ? TextColor + : TextColorInvalid; font.DrawText(apparentText, textPos, color); if (showCursor && HasKeyboardFocus) diff --git a/mods/cnc/metrics.yaml b/mods/cnc/metrics.yaml index 4b71e6239c..e32eeabe8d 100644 --- a/mods/cnc/metrics.yaml +++ b/mods/cnc/metrics.yaml @@ -14,6 +14,7 @@ Metrics: TextfieldFont: Regular TextfieldColor: 255,255,255 TextfieldColorDisabled: 128,128,128 + TextfieldColorInvalid: 255,192,192 TextFont: Regular TextColor: 255,255,255 TextContrast: false diff --git a/mods/d2k/metrics.yaml b/mods/d2k/metrics.yaml index 137dc82c50..24241acadb 100644 --- a/mods/d2k/metrics.yaml +++ b/mods/d2k/metrics.yaml @@ -14,6 +14,7 @@ Metrics: TextfieldFont: Regular TextfieldColor: 255,255,255 TextfieldColorDisabled: 128,128,128 + TextfieldColorInvalid: 255,192,192 TextFont: Regular TextColor: 255,255,255 TextContrast: false diff --git a/mods/modchooser/metrics.yaml b/mods/modchooser/metrics.yaml index dcbaf3bfa1..a4f6800caa 100644 --- a/mods/modchooser/metrics.yaml +++ b/mods/modchooser/metrics.yaml @@ -14,6 +14,7 @@ Metrics: TextfieldFont: Regular TextfieldColor: 255,255,255 TextfieldColorDisabled: 128,128,128 + TextfieldColorInvalid: 255,192,192 TextFont: Regular TextColor: 255,255,255 TextContrast: false diff --git a/mods/ra/metrics.yaml b/mods/ra/metrics.yaml index c4b902869c..8a2e4c7c39 100644 --- a/mods/ra/metrics.yaml +++ b/mods/ra/metrics.yaml @@ -14,6 +14,7 @@ Metrics: TextfieldFont: Regular TextfieldColor: 255,255,255 TextfieldColorDisabled: 128,128,128 + TextfieldColorInvalid: 255,192,192 TextFont: Regular TextColor: 255,255,255 TextContrast: false diff --git a/mods/ts/metrics.yaml b/mods/ts/metrics.yaml index 5402e155fb..33b2c9673d 100644 --- a/mods/ts/metrics.yaml +++ b/mods/ts/metrics.yaml @@ -14,6 +14,7 @@ Metrics: TextfieldFont: Regular TextfieldColor: 255,255,255 TextfieldColorDisabled: 128,128,128 + TextfieldColorInvalid: 255,192,192 TextFont: Regular TextColor: 255,255,255 TextContrast: false