Fix bogus handling of special keyboard characters everywhere else. Textfields now only accept valid characters, support right-delete.

This commit is contained in:
Paul Chote
2011-03-18 10:48:12 +13:00
parent 7d6d488176
commit eb69b697b1
9 changed files with 47 additions and 41 deletions

View File

@@ -110,7 +110,14 @@ namespace OpenRA
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()

View File

@@ -63,7 +63,7 @@ namespace OpenRA
public struct KeyInput
{
public KeyInputEvent Event;
public char KeyChar;
public char UnicodeChar;
public string KeyName;
public Modifiers Modifiers;
public int VirtKey;

View File

@@ -56,7 +56,7 @@ namespace OpenRA.Widgets
{
if (e.Event == KeyInputEvent.Up) return false;
if (e.KeyChar == '\r')
if (e.KeyName == "return" || e.KeyName == "enter" )
{
if (composing)
{
@@ -94,15 +94,15 @@ namespace OpenRA.Widgets
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;
}

View File

@@ -95,10 +95,10 @@ namespace OpenRA.Widgets
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")
@@ -126,31 +126,29 @@ namespace OpenRA.Widgets
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++;
}

View File

@@ -108,7 +108,7 @@ namespace OpenRA.Widgets
{
if (e.Event == KeyInputEvent.Down)
{
if (e.KeyChar == 27) // Escape
if (e.KeyName == "escape")
{
Stop();
return true;

View File

@@ -154,7 +154,7 @@ namespace OpenRA.Mods.RA.Widgets
return true;
}
return DoBuildingHotkey(Char.ToLowerInvariant(e.KeyChar), world);
return DoBuildingHotkey(e.KeyName, world);
}
// TODO: BuildPaletteWidget doesn't support delegate methods for mouse input
@@ -499,12 +499,12 @@ namespace OpenRA.Mods.RA.Widgets
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 )
{

View File

@@ -12,10 +12,11 @@ namespace OpenRA.Mods.RA.Widgets
{
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]
@@ -40,22 +41,22 @@ namespace OpenRA.Mods.RA.Widgets
{
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();
}

View File

@@ -243,7 +243,7 @@ namespace OpenRA.Renderer.Cg
{
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
};
@@ -258,7 +258,7 @@ namespace OpenRA.Renderer.Cg
{
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
};

View File

@@ -238,7 +238,7 @@ namespace OpenRA.Renderer.Glsl
{
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
};
@@ -253,7 +253,7 @@ namespace OpenRA.Renderer.Glsl
{
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
};