fix crashy behavior in TextFieldWidget

This commit is contained in:
Chris Forbes
2011-05-16 21:54:03 +12:00
committed by Paul Chote
parent a96b1ce01c
commit aa772db9a7
5 changed files with 31 additions and 34 deletions

View File

@@ -8,21 +8,20 @@
*/ */
#endregion #endregion
using System;
using System.Drawing;
using OpenRA.Graphics;
namespace OpenRA.Widgets namespace OpenRA.Widgets
{ {
public class PasswordFieldWidget : TextFieldWidget public class PasswordFieldWidget : TextFieldWidget
{ {
public PasswordFieldWidget() : base() {} public PasswordFieldWidget() : base() {}
protected PasswordFieldWidget(PasswordFieldWidget widget) : base(widget) {} protected PasswordFieldWidget(PasswordFieldWidget widget) : base(widget) {}
// TODO: Mouse interaction is wrong with this.
public override void DrawInner() public override void DrawInner()
{ {
DrawWithString(new string('*', Text.Length)); DrawWithString(new string('*', Text.Length));
} }
public override Widget Clone() { return new PasswordFieldWidget(this); } public override Widget Clone() { return new PasswordFieldWidget(this); }
} }
} }

View File

@@ -10,13 +10,19 @@
using System; using System;
using System.Drawing; using System.Drawing;
using OpenRA.Graphics; using OpenRA.Traits;
namespace OpenRA.Widgets namespace OpenRA.Widgets
{ {
public class TextFieldWidget : Widget public class TextFieldWidget : Widget
{ {
public string Text = ""; string text = "";
public string Text
{
get { return text; }
set { text = value ?? ""; CursorPosition = CursorPosition.Clamp(0, text.Length); }
}
public int MaxLength = 0; public int MaxLength = 0;
public bool Bold = false; public bool Bold = false;
public int VisualHeight = 1; public int VisualHeight = 1;
@@ -68,10 +74,8 @@ namespace OpenRA.Widgets
public int ClosestCursorPosition(int x) public int ClosestCursorPosition(int x)
{ {
if (Text == null)
return 0;
var font = (Bold) ? Game.Renderer.Fonts["Bold"] : Game.Renderer.Fonts["Regular"]; var font = (Bold) ? Game.Renderer.Fonts["Bold"] : Game.Renderer.Fonts["Regular"];
var textSize = font.Measure(Text); var textSize = font.Measure(Text);
var start = RenderOrigin.X + LeftMargin; var start = RenderOrigin.X + LeftMargin;
@@ -135,10 +139,8 @@ namespace OpenRA.Widgets
if (e.KeyName == "delete") if (e.KeyName == "delete")
{ {
if (Text.Length > 0 && CursorPosition < Text.Length) if (CursorPosition < Text.Length)
{
Text = Text.Remove(CursorPosition, 1); Text = Text.Remove(CursorPosition, 1);
}
return true; return true;
} }
@@ -148,16 +150,11 @@ namespace OpenRA.Widgets
public void TypeChar(KeyInput key) public void TypeChar(KeyInput key)
{ {
if (Text == null) if (key.KeyName == "backspace" && CursorPosition > 0)
Text = "";
if (key.KeyName == "backspace" && Text.Length > 0 && CursorPosition > 0)
{ {
Text = Text.Remove(CursorPosition - 1, 1);
CursorPosition--; CursorPosition--;
}
else if (key.KeyName == "delete" && Text.Length > 0 && CursorPosition < Text.Length - 1)
Text = Text.Remove(CursorPosition, 1); Text = Text.Remove(CursorPosition, 1);
}
else if (key.IsValidInput()) else if (key.IsValidInput())
{ {
@@ -172,6 +169,7 @@ namespace OpenRA.Widgets
protected int blinkCycle = 10; protected int blinkCycle = 10;
protected bool showCursor = true; protected bool showCursor = true;
public override void Tick() public override void Tick()
{ {
if (--blinkCycle <= 0) if (--blinkCycle <= 0)
@@ -185,14 +183,9 @@ namespace OpenRA.Widgets
public virtual void DrawWithString(string text) public virtual void DrawWithString(string text)
{ {
if (text == null) text = "";
var font = (Bold) ? Game.Renderer.Fonts["Bold"] : Game.Renderer.Fonts["Regular"]; var font = (Bold) ? Game.Renderer.Fonts["Bold"] : Game.Renderer.Fonts["Regular"];
var pos = RenderOrigin; var pos = RenderOrigin;
if (CursorPosition > text.Length)
CursorPosition = text.Length;
var textSize = font.Measure(text); var textSize = font.Measure(text);
var cursorPosition = font.Measure(text.Substring(0,CursorPosition)); var cursorPosition = font.Measure(text.Substring(0,CursorPosition));
@@ -208,7 +201,8 @@ namespace OpenRA.Widgets
if (Focused) if (Focused)
textPos += new int2(Bounds.Width - LeftMargin - RightMargin - textSize.X, 0); textPos += new int2(Bounds.Width - LeftMargin - RightMargin - textSize.X, 0);
Game.Renderer.EnableScissor(pos.X + LeftMargin, pos.Y, Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom); Game.Renderer.EnableScissor(pos.X + LeftMargin, pos.Y,
Bounds.Width - LeftMargin - RightMargin, Bounds.Bottom);
} }
font.DrawText(text, textPos, Color.White); font.DrawText(text, textPos, Color.White);

View File

@@ -46,6 +46,7 @@ namespace OpenRA.Mods.Cnc.Widgets
ChromeProvider.GetImage("checkbox", "checked"), ChromeProvider.GetImage("checkbox", "checked"),
new float2(rect.Left + 2, rect.Top + 2)); new float2(rect.Left + 2, rect.Top + 2));
} }
public override Widget Clone() { return new CncCheckboxWidget(this); } public override Widget Clone() { return new CncCheckboxWidget(this); }
} }
@@ -243,6 +244,7 @@ namespace OpenRA.Mods.Cnc.Widgets
{ {
Font = other.Font; Font = other.Font;
} }
public override Widget Clone() { return new CncDropDownButtonWidget(this); } public override Widget Clone() { return new CncDropDownButtonWidget(this); }
public override void DrawInner() public override void DrawInner()

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Mods.Cnc.Widgets
var files = Directory.GetFiles(replayDir, "*.rep").Reverse(); var files = Directory.GetFiles(replayDir, "*.rep").Reverse();
foreach (var replayFile in files) foreach (var replayFile in files)
AddReplay(rl, replayFile, template); AddReplay(rl, replayFile, template);
SelectReplay(files.FirstOrDefault()); SelectReplay(files.FirstOrDefault());
} }
@@ -59,9 +59,10 @@ namespace OpenRA.Mods.Cnc.Widgets
panel.GetWidget("REPLAY_INFO").IsVisible = () => currentSummary != null; panel.GetWidget("REPLAY_INFO").IsVisible = () => currentSummary != null;
} }
ReplaySummary currentSummary; ReplaySummary currentSummary;
Map currentMap; Map currentMap;
void SelectReplay(string filename) void SelectReplay(string filename)
{ {
if (filename == null) if (filename == null)
@@ -81,7 +82,7 @@ namespace OpenRA.Mods.Cnc.Widgets
var players = currentSummary.LobbyInfo.Slots.Count(s => currentSummary.LobbyInfo.ClientInSlot(s) != null || s.Bot != null); var players = currentSummary.LobbyInfo.Slots.Count(s => currentSummary.LobbyInfo.ClientInSlot(s) != null || s.Bot != null);
panel.GetWidget<LabelWidget>("PLAYERS").GetText = () => players.ToString(); panel.GetWidget<LabelWidget>("PLAYERS").GetText = () => players.ToString();
} }
catch(Exception e) catch (Exception e)
{ {
Log.Write("debug", "Exception while parsing replay: {0}", e.ToString()); Log.Write("debug", "Exception while parsing replay: {0}", e.ToString());
currentSummary = null; currentSummary = null;

View File

@@ -43,20 +43,21 @@ namespace OpenRA.Mods.Cnc.Widgets
{ {
{ "initialMap", map.Uid }, { "initialMap", map.Uid },
{ "onExit", () => {} }, { "onExit", () => {} },
{ "onSelect", new Action<Map>(m => map = m) } { "onSelect", (Action<Map>)(m => map = m) }
}); });
}; };
if (string.IsNullOrEmpty(Game.Settings.Server.Map) || !Game.modData.AvailableMaps.TryGetValue(Game.Settings.Server.Map, out map)) if (string.IsNullOrEmpty(Game.Settings.Server.Map) ||
!Game.modData.AvailableMaps.TryGetValue(Game.Settings.Server.Map, out map))
map = Game.modData.AvailableMaps.FirstOrDefault(m => m.Value.Selectable).Value; map = Game.modData.AvailableMaps.FirstOrDefault(m => m.Value.Selectable).Value;
panel.GetWidget<MapPreviewWidget>("MAP_PREVIEW").Map = () => map; panel.GetWidget<MapPreviewWidget>("MAP_PREVIEW").Map = () => map;
panel.GetWidget<LabelWidget>("MAP_NAME").GetText = () => map.Title; panel.GetWidget<LabelWidget>("MAP_NAME").GetText = () => map.Title;
panel.GetWidget<TextFieldWidget>("SERVER_NAME").Text = settings.Server.Name ?? ""; panel.GetWidget<TextFieldWidget>("SERVER_NAME").Text = settings.Server.Name ?? "";
panel.GetWidget<TextFieldWidget>("LISTEN_PORT").Text = settings.Server.ListenPort.ToString(); panel.GetWidget<TextFieldWidget>("LISTEN_PORT").Text = settings.Server.ListenPort.ToString();
advertiseOnline = Game.Settings.Server.AdvertiseOnline; advertiseOnline = Game.Settings.Server.AdvertiseOnline;
var externalPort = panel.GetWidget<CncTextFieldWidget>("EXTERNAL_PORT"); var externalPort = panel.GetWidget<CncTextFieldWidget>("EXTERNAL_PORT");
externalPort.Text = settings.Server.ExternalPort.ToString(); externalPort.Text = settings.Server.ExternalPort.ToString();
externalPort.IsDisabled = () => !advertiseOnline; externalPort.IsDisabled = () => !advertiseOnline;