Skip to content

Commit

Permalink
Fix bogus handling of special keyboard characters everywhere else. Te…
Browse files Browse the repository at this point in the history
…xtfields now only accept valid characters, support right-delete.
  • Loading branch information
pchote committed Mar 17, 2011
1 parent 7d6d488 commit eb69b69
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 41 deletions.
9 changes: 8 additions & 1 deletion OpenRA.FileFormats/Exts.cs
Expand Up @@ -110,7 +110,14 @@ public static bool Contains(this RectangleF r, int2 p)
public static bool HasModifier(this Modifiers k, Modifiers mod)
{
return (k & mod) == mod;
}
}

public static bool IsValidInput(this KeyInput key)
{
return char.IsLetter(key.UnicodeChar) || char.IsDigit(key.UnicodeChar) ||
char.IsSymbol(key.UnicodeChar) || char.IsSeparator(key.UnicodeChar) ||
char.IsPunctuation(key.UnicodeChar);
}

public static V GetOrAdd<K, V>(this Dictionary<K, V> d, K k)
where V : new()
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.FileFormats/Graphics/IInputHandler.cs
Expand Up @@ -63,7 +63,7 @@ public enum KeyInputEvent { Down, Up };
public struct KeyInput
{
public KeyInputEvent Event;
public char KeyChar;
public char UnicodeChar;
public string KeyName;
public Modifiers Modifiers;
public int VirtKey;
Expand Down
8 changes: 4 additions & 4 deletions OpenRA.Game/Widgets/ChatEntryWidget.cs
Expand Up @@ -56,7 +56,7 @@ public override bool HandleKeyPressInner(KeyInput e)
{
if (e.Event == KeyInputEvent.Up) return false;

if (e.KeyChar == '\r')
if (e.KeyName == "return" || e.KeyName == "enter" )
{
if (composing)
{
Expand Down Expand Up @@ -94,15 +94,15 @@ public override bool HandleKeyPressInner(KeyInput e)

if (composing)
{
if (e.KeyChar == '\b' || e.KeyChar == 0x7f)
if (e.KeyName == "backspace")
{
if (content.Length > 0)
content = content.Remove(content.Length - 1);
return true;
}
else if (!char.IsControl(e.KeyChar))
else if (e.IsValidInput())
{
content += e.KeyChar;
content += e.UnicodeChar.ToString();
return true;
}

Expand Down
34 changes: 16 additions & 18 deletions OpenRA.Game/Widgets/TextFieldWidget.cs
Expand Up @@ -95,10 +95,10 @@ public override bool HandleKeyPressInner(KeyInput e)
if (!Focused)
return false;

if (e.KeyChar == '\r' && OnEnterKey())
if ((e.KeyName == "return" || e.KeyName == "enter") && OnEnterKey())
return true;

if (e.KeyChar == '\t' && OnTabKey())
if (e.KeyName == "tab" && OnTabKey())
return true;

if (e.KeyName == "left")
Expand Down Expand Up @@ -126,31 +126,29 @@ public override bool HandleKeyPressInner(KeyInput e)
return true;
}

TypeChar(e.KeyChar);
TypeChar(e);
return true;
}

public void TypeChar(char c)
public void TypeChar(KeyInput key)
{
// backspace
if (c == '\b' || c == 0x7f)
if (Text == null)
Text = "";

if (key.KeyName == "backspace" && Text.Length > 0 && CursorPosition > 0)
{
if (Text.Length > 0 && CursorPosition > 0)
{
Text = Text.Remove(CursorPosition - 1, 1);

CursorPosition--;
}
Text = Text.Remove(CursorPosition - 1, 1);
CursorPosition--;
}
else if (!char.IsControl(c))
{
if (Text == null)
Text = "";

else if (key.KeyName == "delete" && Text.Length > 0 && CursorPosition < Text.Length - 1)
Text = Text.Remove(CursorPosition, 1);

else if (key.IsValidInput())
{
if (MaxLength > 0 && Text.Length >= MaxLength)
return;

Text = Text.Insert(CursorPosition, c.ToString());
Text = Text.Insert(CursorPosition, key.UnicodeChar.ToString());

CursorPosition++;
}
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Widgets/VqaPlayerWidget.cs
Expand Up @@ -108,7 +108,7 @@ public override bool HandleKeyPressInner(KeyInput e)
{
if (e.Event == KeyInputEvent.Down)
{
if (e.KeyChar == 27) // Escape
if (e.KeyName == "escape")
{
Stop();
return true;
Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs
Expand Up @@ -154,7 +154,7 @@ public override bool HandleKeyPressInner(KeyInput e)
return true;
}

return DoBuildingHotkey(Char.ToLowerInvariant(e.KeyChar), world);
return DoBuildingHotkey(e.KeyName, world);
}

// TODO: BuildPaletteWidget doesn't support delegate methods for mouse input
Expand Down Expand Up @@ -499,12 +499,12 @@ void DrawProductionTooltip(World world, string unit, int2 pos)
p.ToInt2(), Color.White);
}

bool DoBuildingHotkey(char c, World world)
bool DoBuildingHotkey(string key, World world)
{
if (!paletteOpen) return false;
if (CurrentQueue == null) return false;

var toBuild = CurrentQueue.BuildableItems().FirstOrDefault(b => b.Traits.Get<BuildableInfo>().Hotkey == c.ToString());
var toBuild = CurrentQueue.BuildableItems().FirstOrDefault(b => b.Traits.Get<BuildableInfo>().Hotkey == key);

if ( toBuild != null )
{
Expand Down
19 changes: 10 additions & 9 deletions OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs
Expand Up @@ -12,10 +12,11 @@ public class WorldCommandWidget : Widget
{
public World World { get { return OrderManager.world; } }

public char AttackMoveKey = 'a';
public char StopKey = 's';
public char ScatterKey = 'x';
public char DeployKey = 'f';
public string AttackMoveKey = "a";
public string StopKey = "s";
public string ScatterKey = "x";
public string DeployKey = "f";
public string BaseCycleKey = "backspace";
public readonly OrderManager OrderManager;

[ObjectCreator.UseCtor]
Expand All @@ -40,22 +41,22 @@ bool ProcessInput(KeyInput e)
{
if (e.Modifiers == Modifiers.None)
{
if (e.KeyChar == '\b' || e.KeyChar == (char)127)
if (e.KeyName == BaseCycleKey)
return CycleBases();

if (!World.Selection.Actors.Any())
return false;

if (e.KeyChar == AttackMoveKey)
if (e.KeyName == AttackMoveKey)
return PerformAttackMove();

if (e.KeyChar == StopKey)
if (e.KeyName == StopKey)
return PerformStop();

if (e.KeyChar == ScatterKey)
if (e.KeyName == ScatterKey)
return PerformScatter();

if (e.KeyChar == DeployKey)
if (e.KeyName == DeployKey)
return PerformDeploy();
}

Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Renderer.Cg/GraphicsDevice.cs
Expand Up @@ -243,7 +243,7 @@ public void Present( IInputHandler inputHandler )
{
Event = KeyInputEvent.Down,
Modifiers = mods,
KeyChar = (char)e.key.keysym.unicode,
UnicodeChar = (char)e.key.keysym.unicode,
KeyName = Sdl.SDL_GetKeyName( e.key.keysym.sym ),
VirtKey = e.key.keysym.sym
};
Expand All @@ -258,7 +258,7 @@ public void Present( IInputHandler inputHandler )
{
Event = KeyInputEvent.Up,
Modifiers = mods,
KeyChar = (char)e.key.keysym.unicode,
UnicodeChar = (char)e.key.keysym.unicode,
KeyName = Sdl.SDL_GetKeyName( e.key.keysym.sym ),
VirtKey = e.key.keysym.sym
};
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Renderer.Gl/GraphicsDevice.cs
Expand Up @@ -238,7 +238,7 @@ public void Present( IInputHandler inputHandler )
{
Event = KeyInputEvent.Down,
Modifiers = mods,
KeyChar = (char)e.key.keysym.unicode,
UnicodeChar = (char)e.key.keysym.unicode,
KeyName = Sdl.SDL_GetKeyName( e.key.keysym.sym ),
VirtKey = e.key.keysym.sym
};
Expand All @@ -253,7 +253,7 @@ public void Present( IInputHandler inputHandler )
{
Event = KeyInputEvent.Up,
Modifiers = mods,
KeyChar = (char)e.key.keysym.unicode,
UnicodeChar = (char)e.key.keysym.unicode,
KeyName = Sdl.SDL_GetKeyName( e.key.keysym.sym ),
VirtKey = e.key.keysym.sym
};
Expand Down

0 comments on commit eb69b69

Please sign in to comment.