From aca4d2ebbb3ba55e7e63c7da28ee9b03a83ccbd4 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Mon, 23 May 2011 19:57:40 +1200 Subject: [PATCH] fixed 823 -- mouse interaction with password fields uses the metrics of the mask character, not the actual content --- OpenRA.Game/Widgets/PasswordFieldWidget.cs | 12 ++------ OpenRA.Game/Widgets/TextFieldWidget.cs | 32 ++++++++++------------ 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/OpenRA.Game/Widgets/PasswordFieldWidget.cs b/OpenRA.Game/Widgets/PasswordFieldWidget.cs index a1f47b85ae..b6c7468520 100644 --- a/OpenRA.Game/Widgets/PasswordFieldWidget.cs +++ b/OpenRA.Game/Widgets/PasswordFieldWidget.cs @@ -12,16 +12,10 @@ namespace OpenRA.Widgets { public class PasswordFieldWidget : TextFieldWidget { - public PasswordFieldWidget() : base() {} - protected PasswordFieldWidget(PasswordFieldWidget widget) : base(widget) {} - - // TODO: Mouse interaction is wrong with this. - - public override void DrawInner() - { - DrawWithString(new string('*', Text.Length)); - } + public PasswordFieldWidget() : base() { } + protected PasswordFieldWidget(PasswordFieldWidget widget) : base(widget) { } + protected override string GetApparentText() { return new string('*', Text.Length); } public override Widget Clone() { return new PasswordFieldWidget(this); } } } \ No newline at end of file diff --git a/OpenRA.Game/Widgets/TextFieldWidget.cs b/OpenRA.Game/Widgets/TextFieldWidget.cs index 9561c8623c..9baae3ace4 100644 --- a/OpenRA.Game/Widgets/TextFieldWidget.cs +++ b/OpenRA.Game/Widgets/TextFieldWidget.cs @@ -78,12 +78,14 @@ namespace OpenRA.Widgets CursorPosition = ClosestCursorPosition(mi.Location.X); return true; } - + + protected virtual string GetApparentText() { return text; } public int ClosestCursorPosition(int x) { + var apparentText = GetApparentText(); var font = Game.Renderer.Fonts[Font]; - var textSize = font.Measure(Text); + var textSize = font.Measure(apparentText); var start = RenderOrigin.X + LeftMargin; if (textSize.X > Bounds.Width - LeftMargin - RightMargin && Focused) @@ -91,9 +93,9 @@ namespace OpenRA.Widgets int minIndex = -1; int minValue = int.MaxValue; - for (int i = 0; i <= Text.Length; i++) + for (int i = 0; i <= apparentText.Length; i++) { - var dist = Math.Abs(start + font.Measure(Text.Substring(0,i)).X - x); + var dist = Math.Abs(start + font.Measure(apparentText.Substring(0, i)).X - x); if (dist > minValue) break; minValue = dist; @@ -190,15 +192,16 @@ namespace OpenRA.Widgets base.Tick(); } - - public virtual void DrawWithString(string text) + + public override void DrawInner() { + var apparentText = GetApparentText(); var font = Game.Renderer.Fonts[Font]; var pos = RenderOrigin; - var textSize = font.Measure(text); - var cursorPosition = font.Measure(text.Substring(0,CursorPosition)); - + var textSize = font.Measure(apparentText); + var cursorPosition = font.Measure(apparentText.Substring(0, CursorPosition)); + var disabled = IsDisabled(); var state = disabled ? "textfield-disabled" : Focused ? "textfield-focused" : @@ -217,12 +220,12 @@ namespace OpenRA.Widgets if (Focused) textPos += new int2(Bounds.Width - LeftMargin - RightMargin - textSize.X, 0); - Game.Renderer.EnableScissor(pos.X + LeftMargin, pos.Y, + Game.Renderer.EnableScissor(pos.X + LeftMargin, pos.Y, Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom); } - + var color = disabled ? DisabledColor : TextColor; - font.DrawText(text, textPos, color); + font.DrawText(apparentText, textPos, color); if (showCursor && Focused) font.DrawText("|", new float2(textPos.X + cursorPosition.X - 2, textPos.Y), Color.White); @@ -230,11 +233,6 @@ namespace OpenRA.Widgets if (textSize.X > Bounds.Width - LeftMargin - RightMargin) Game.Renderer.DisableScissor(); } - - public override void DrawInner() - { - DrawWithString(Text); - } public override Widget Clone() { return new TextFieldWidget(this); } }