Remove VirtKey and KeyName.

This commit is contained in:
Paul Chote
2013-10-20 18:16:33 +13:00
parent e5f93ec39e
commit aab6fec68b
13 changed files with 54 additions and 70 deletions

View File

@@ -70,9 +70,5 @@ namespace OpenRA
public Modifiers Modifiers; public Modifiers Modifiers;
public int MultiTapCount; public int MultiTapCount;
public char UnicodeChar; public char UnicodeChar;
// Deprecated
public string KeyName;
public int VirtKey;
} }
} }

View File

@@ -91,7 +91,7 @@ namespace OpenRA.Widgets
public override bool HandleKeyPress(KeyInput e) public override bool HandleKeyPress(KeyInput e)
{ {
if (e.KeyName != Key || e.Event != KeyInputEvent.Down) if (KeycodeExts.DisplayString(e.Key) != Key || e.Event != KeyInputEvent.Down)
return false; return false;
if (!IsDisabled()) if (!IsDisabled())

View File

@@ -49,7 +49,7 @@ namespace OpenRA.Widgets
{ {
if (e.Event == KeyInputEvent.Up) return false; if (e.Event == KeyInputEvent.Up) return false;
if (e.KeyName == "return" || e.KeyName == "enter" ) if (e.Key == Keycode.RETURN || e.Key == Keycode.KP_ENTER)
{ {
if (composing) if (composing)
{ {
@@ -79,14 +79,14 @@ namespace OpenRA.Widgets
if (composing) if (composing)
{ {
if (e.KeyName == "escape") if (e.Key == Keycode.ESCAPE)
{ {
composing = false; composing = false;
content = ""; content = "";
YieldKeyboardFocus(); YieldKeyboardFocus();
return true; return true;
} }
else if (e.KeyName == "backspace") else if (e.Key == Keycode.BACKSPACE)
{ {
if (content.Length > 0) if (content.Length > 0)
content = content.Remove(content.Length - 1); content = content.Remove(content.Length - 1);

View File

@@ -110,16 +110,16 @@ namespace OpenRA.Widgets
if (!HasKeyboardFocus) if (!HasKeyboardFocus)
return false; return false;
if ((e.KeyName == "return" || e.KeyName == "enter") && OnEnterKey()) if ((e.Key == Keycode.RETURN || e.Key == Keycode.KP_ENTER) && OnEnterKey())
return true; return true;
if (e.KeyName == "tab" && OnTabKey()) if (e.Key == Keycode.TAB && OnTabKey())
return true; return true;
if (e.KeyName == "escape" && OnEscKey()) if (e.Key == Keycode.ESCAPE && OnEscKey())
return true; return true;
if (e.KeyName == "left") if (e.Key == Keycode.LEFT)
{ {
if (CursorPosition > 0) if (CursorPosition > 0)
CursorPosition--; CursorPosition--;
@@ -127,7 +127,7 @@ namespace OpenRA.Widgets
return true; return true;
} }
if (e.KeyName == "right") if (e.Key == Keycode.RIGHT)
{ {
if (CursorPosition <= Text.Length-1) if (CursorPosition <= Text.Length-1)
CursorPosition++; CursorPosition++;
@@ -135,19 +135,19 @@ namespace OpenRA.Widgets
return true; return true;
} }
if (e.KeyName == "home") if (e.Key == Keycode.HOME)
{ {
CursorPosition = 0; CursorPosition = 0;
return true; return true;
} }
if (e.KeyName == "end") if (e.Key == Keycode.END)
{ {
CursorPosition = Text.Length; CursorPosition = Text.Length;
return true; return true;
} }
if (e.KeyName == "delete") if (e.Key == Keycode.DELETE)
{ {
if (CursorPosition < Text.Length) if (CursorPosition < Text.Length)
Text = Text.Remove(CursorPosition, 1); Text = Text.Remove(CursorPosition, 1);
@@ -160,7 +160,7 @@ namespace OpenRA.Widgets
public void TypeChar(KeyInput key) public void TypeChar(KeyInput key)
{ {
if (key.KeyName == "backspace" && CursorPosition > 0) if (key.Key == Keycode.BACKSPACE && CursorPosition > 0)
{ {
CursorPosition--; CursorPosition--;
Text = Text.Remove(CursorPosition, 1); Text = Text.Remove(CursorPosition, 1);

View File

@@ -167,12 +167,12 @@ namespace OpenRA.Widgets
public override bool HandleKeyPress(KeyInput e) public override bool HandleKeyPress(KeyInput e)
{ {
switch (e.KeyName) switch (e.Key)
{ {
case "up": keyboardDirections = keyboardDirections.Set(ScrollDirection.Up, e.Event == KeyInputEvent.Down); return true; case Keycode.UP: keyboardDirections = keyboardDirections.Set(ScrollDirection.Up, e.Event == KeyInputEvent.Down); return true;
case "down": keyboardDirections = keyboardDirections.Set(ScrollDirection.Down, e.Event == KeyInputEvent.Down); return true; case Keycode.DOWN: keyboardDirections = keyboardDirections.Set(ScrollDirection.Down, e.Event == KeyInputEvent.Down); return true;
case "left": keyboardDirections = keyboardDirections.Set(ScrollDirection.Left, e.Event == KeyInputEvent.Down); return true; case Keycode.LEFT: keyboardDirections = keyboardDirections.Set(ScrollDirection.Left, e.Event == KeyInputEvent.Down); return true;
case "right": keyboardDirections = keyboardDirections.Set(ScrollDirection.Right, e.Event == KeyInputEvent.Down); return true; case Keycode.RIGHT: keyboardDirections = keyboardDirections.Set(ScrollDirection.Right, e.Event == KeyInputEvent.Down); return true;
} }
return false; return false;

View File

@@ -108,7 +108,7 @@ namespace OpenRA.Widgets
{ {
if (e.Event == KeyInputEvent.Down) if (e.Event == KeyInputEvent.Down)
{ {
if (e.KeyName == "escape") if (e.Key == Keycode.ESCAPE)
{ {
Stop(); Stop();
return true; return true;

View File

@@ -169,14 +169,15 @@ namespace OpenRA.Widgets
{ {
if (e.Event == KeyInputEvent.Down) if (e.Event == KeyInputEvent.Down)
{ {
if (e.KeyName.Length == 1 && char.IsDigit(e.KeyName[0])) if (e.Key >= Keycode.NUMBER_0 && e.Key <= Keycode.NUMBER_9)
{ {
world.Selection.DoControlGroup(world, worldRenderer, e.KeyName[0] - '0', e.Modifiers, e.MultiTapCount); var group = (int)e.Key - (int)Keycode.NUMBER_0;
world.Selection.DoControlGroup(world, worldRenderer, group, e.Modifiers, e.MultiTapCount);
return true; return true;
} }
// Disable pausing for spectators // Disable pausing for spectators
else if (e.KeyName == Game.Settings.Keys.PauseKey && world.LocalPlayer != null) else if (KeycodeExts.DisplayString(e.Key) == Game.Settings.Keys.PauseKey && world.LocalPlayer != null)
world.SetPauseState(!world.Paused); world.SetPauseState(!world.Paused);
} }
return false; return false;

View File

@@ -275,7 +275,7 @@ namespace OpenRA.Mods.Cnc.Widgets
public override bool HandleKeyPress(KeyInput e) public override bool HandleKeyPress(KeyInput e)
{ {
if (e.Event != KeyInputEvent.Down) return false; if (e.Event != KeyInputEvent.Down) return false;
if (e.KeyName == Game.Settings.Keys.CycleTabsKey) if (KeycodeExts.DisplayString(e.Key) == Game.Settings.Keys.CycleTabsKey)
{ {
Sound.PlayNotification(null, "Sounds", "ClickSound", null); Sound.PlayNotification(null, "Sounds", "ClickSound", null);
SelectNextTab(e.Modifiers.HasModifier(Modifiers.Shift)); SelectNextTab(e.Modifiers.HasModifier(Modifiers.Shift));

View File

@@ -147,12 +147,12 @@ namespace OpenRA.Mods.RA.Widgets
public override bool HandleKeyPress(KeyInput e) public override bool HandleKeyPress(KeyInput e)
{ {
if (e.Event == KeyInputEvent.Up) return false; if (e.Event == KeyInputEvent.Up) return false;
if (e.KeyName == Game.Settings.Keys.CycleTabsKey) if (KeycodeExts.DisplayString(e.Key) == Game.Settings.Keys.CycleTabsKey)
{ {
TabChange(e.Modifiers.HasModifier(Modifiers.Shift)); TabChange(e.Modifiers.HasModifier(Modifiers.Shift));
return true; return true;
} }
return DoBuildingHotkey(e.KeyName, world); return DoBuildingHotkey(e, world);
} }
public override bool HandleMouseInput(MouseInput mi) public override bool HandleMouseInput(MouseInput mi)
@@ -495,12 +495,12 @@ namespace OpenRA.Mods.RA.Widgets
p.ToInt2(), Color.White); p.ToInt2(), Color.White);
} }
bool DoBuildingHotkey(string key, World world) bool DoBuildingHotkey(KeyInput e, World world)
{ {
if (!paletteOpen) return false; if (!paletteOpen) return false;
if (CurrentQueue == null) return false; if (CurrentQueue == null) return false;
var toBuild = CurrentQueue.BuildableItems().FirstOrDefault(b => b.Traits.Get<BuildableInfo>().Hotkey == key); var toBuild = CurrentQueue.BuildableItems().FirstOrDefault(b => b.Traits.Get<BuildableInfo>().Hotkey == KeycodeExts.DisplayString(e.Key));
if (toBuild != null) if (toBuild != null)
{ {

View File

@@ -70,9 +70,8 @@ namespace OpenRA.Mods.RA.Widgets.Logic
chatPanel.OnKeyPress = (e) => chatPanel.OnKeyPress = (e) =>
{ {
if (e.Event == KeyInputEvent.Up) return false; if (e.Event == KeyInputEvent.Up) return false;
if (!IsOpen && (e.KeyName == "enter" || e.KeyName == "return") ) if (!IsOpen && (e.Key == Keycode.RETURN || e.Key == Keycode.KP_ENTER))
{ {
var shift = e.Modifiers.HasModifier(Modifiers.Shift); var shift = e.Modifiers.HasModifier(Modifiers.Shift);
var toggle = Game.Settings.Game.TeamChatToggle ; var toggle = Game.Settings.Game.TeamChatToggle ;
TeamChat = (!toggle && shift) || ( toggle && (TeamChat ^ shift) ); TeamChat = (!toggle && shift) || ( toggle && (TeamChat ^ shift) );

View File

@@ -47,35 +47,36 @@ namespace OpenRA.Mods.RA.Widgets
{ {
if (e.Modifiers == Modifiers.None && e.Event == KeyInputEvent.Down) if (e.Modifiers == Modifiers.None && e.Event == KeyInputEvent.Down)
{ {
if (e.KeyName == Game.Settings.Keys.CycleBaseKey) var ks = Game.Settings.Keys;
if (KeycodeExts.DisplayString(e.Key) == ks.CycleBaseKey)
return CycleBases(); return CycleBases();
if (e.KeyName == Game.Settings.Keys.ToLastEventKey) if (KeycodeExts.DisplayString(e.Key) == ks.ToLastEventKey)
return ToLastEvent(); return ToLastEvent();
if (e.KeyName == Game.Settings.Keys.ToSelectionKey) if (KeycodeExts.DisplayString(e.Key) == ks.ToSelectionKey)
return ToSelection(); return ToSelection();
// Put all functions that aren't unit-specific before this line! // Put all functions that aren't unit-specific before this line!
if (!world.Selection.Actors.Any()) if (!world.Selection.Actors.Any())
return false; return false;
if (e.KeyName == Game.Settings.Keys.AttackMoveKey) if (KeycodeExts.DisplayString(e.Key) == ks.AttackMoveKey)
return PerformAttackMove(); return PerformAttackMove();
if (e.KeyName == Game.Settings.Keys.StopKey) if (KeycodeExts.DisplayString(e.Key) == ks.StopKey)
return PerformStop(); return PerformStop();
if (e.KeyName == Game.Settings.Keys.ScatterKey) if (KeycodeExts.DisplayString(e.Key) == ks.ScatterKey)
return PerformScatter(); return PerformScatter();
if (e.KeyName == Game.Settings.Keys.DeployKey) if (KeycodeExts.DisplayString(e.Key) == ks.DeployKey)
return PerformDeploy(); return PerformDeploy();
if (e.KeyName == Game.Settings.Keys.StanceCycleKey) if (KeycodeExts.DisplayString(e.Key) == ks.StanceCycleKey)
return PerformStanceCycle(); return PerformStanceCycle();
if (e.KeyName == Game.Settings.Keys.GuardKey) if (KeycodeExts.DisplayString(e.Key) == ks.GuardKey)
return PerformGuard(); return PerformGuard();
} }

View File

@@ -14,8 +14,8 @@ using OpenRA.FileFormats;
public static class MultiTapDetection public static class MultiTapDetection
{ {
static Cache<string, TapHistory> keyHistoryCache = static Cache<Keycode, TapHistory> keyHistoryCache =
new Cache<string, TapHistory>(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1))); new Cache<Keycode, TapHistory>(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1)));
static Cache<byte, TapHistory> clickHistoryCache = static Cache<byte, TapHistory> clickHistoryCache =
new Cache<byte, TapHistory>(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1))); new Cache<byte, TapHistory>(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1)));
@@ -29,12 +29,12 @@ public static class MultiTapDetection
return clickHistoryCache[button].LastTapCount(); return clickHistoryCache[button].LastTapCount();
} }
public static int DetectFromKeyboard(string key) public static int DetectFromKeyboard(Keycode key)
{ {
return keyHistoryCache[key].GetTapCount(int2.Zero); return keyHistoryCache[key].GetTapCount(int2.Zero);
} }
public static int InfoFromKeyboard(string key) public static int InfoFromKeyboard(Keycode key)
{ {
return keyHistoryCache[key].LastTapCount(); return keyHistoryCache[key].LastTapCount();
} }

View File

@@ -102,18 +102,23 @@ namespace OpenRA.Renderer.SdlCommon
} }
case Sdl.SDL_KEYDOWN: case Sdl.SDL_KEYDOWN:
case Sdl.SDL_KEYUP:
{ {
var keyName = Sdl.SDL_GetKeyName(e.key.keysym.sym); var keyCode = (Keycode)e.key.keysym.sym;
var type = e.type == Sdl.SDL_KEYDOWN ?
KeyInputEvent.Down : KeyInputEvent.Up;
var tapCount = e.type == Sdl.SDL_KEYDOWN ?
MultiTapDetection.DetectFromKeyboard(keyCode) :
MultiTapDetection.InfoFromKeyboard(keyCode);
var keyEvent = new KeyInput var keyEvent = new KeyInput
{ {
Event = KeyInputEvent.Down, Event = type,
Key = (Keycode)e.key.keysym.sym, Key = keyCode,
Modifiers = mods, Modifiers = mods,
UnicodeChar = (char)e.key.keysym.unicode, UnicodeChar = (char)e.key.keysym.unicode,
MultiTapCount = MultiTapDetection.DetectFromKeyboard(keyName), MultiTapCount = tapCount
KeyName = Sdl.SDL_GetKeyName(e.key.keysym.sym),
VirtKey = e.key.keysym.sym
}; };
// Special case workaround for windows users // Special case workaround for windows users
@@ -127,24 +132,6 @@ namespace OpenRA.Renderer.SdlCommon
break; break;
} }
case Sdl.SDL_KEYUP:
{
var keyName = Sdl.SDL_GetKeyName(e.key.keysym.sym);
var keyEvent = new KeyInput
{
Event = KeyInputEvent.Up,
Key = (Keycode)e.key.keysym.sym,
Modifiers = mods,
UnicodeChar = (char)e.key.keysym.unicode,
MultiTapCount = MultiTapDetection.InfoFromKeyboard(keyName),
KeyName = Sdl.SDL_GetKeyName(e.key.keysym.sym),
VirtKey = e.key.keysym.sym
};
inputHandler.OnKeyInput(keyEvent);
break;
}
} }
} }