From aab6fec68b2f1b5a953bd0386846b7bd29c59aaf Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 20 Oct 2013 18:16:33 +1300 Subject: [PATCH] Remove VirtKey and KeyName. --- OpenRA.FileFormats/Graphics/IInputHandler.cs | 4 --- OpenRA.Game/Widgets/ButtonWidget.cs | 2 +- OpenRA.Game/Widgets/ChatEntryWidget.cs | 6 ++-- OpenRA.Game/Widgets/TextFieldWidget.cs | 18 +++++----- .../Widgets/ViewportControllerWidget.cs | 10 +++--- OpenRA.Game/Widgets/VqaPlayerWidget.cs | 2 +- .../WorldInteractionControllerWidget.cs | 7 ++-- .../Widgets/ProductionTabsWidget.cs | 2 +- OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs | 8 ++--- .../Widgets/Logic/IngameChatLogic.cs | 3 +- OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs | 19 +++++----- .../MultiTapDetection.cs | 8 ++--- OpenRA.Renderer.SdlCommon/SdlInput.cs | 35 ++++++------------- 13 files changed, 54 insertions(+), 70 deletions(-) diff --git a/OpenRA.FileFormats/Graphics/IInputHandler.cs b/OpenRA.FileFormats/Graphics/IInputHandler.cs index a6bdfe10cb..b4ab513100 100755 --- a/OpenRA.FileFormats/Graphics/IInputHandler.cs +++ b/OpenRA.FileFormats/Graphics/IInputHandler.cs @@ -70,9 +70,5 @@ namespace OpenRA public Modifiers Modifiers; public int MultiTapCount; public char UnicodeChar; - - // Deprecated - public string KeyName; - public int VirtKey; } } diff --git a/OpenRA.Game/Widgets/ButtonWidget.cs b/OpenRA.Game/Widgets/ButtonWidget.cs index 364e45474a..3166571476 100644 --- a/OpenRA.Game/Widgets/ButtonWidget.cs +++ b/OpenRA.Game/Widgets/ButtonWidget.cs @@ -91,7 +91,7 @@ namespace OpenRA.Widgets 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; if (!IsDisabled()) diff --git a/OpenRA.Game/Widgets/ChatEntryWidget.cs b/OpenRA.Game/Widgets/ChatEntryWidget.cs index 0d1ce7bdf8..19c1f0e196 100755 --- a/OpenRA.Game/Widgets/ChatEntryWidget.cs +++ b/OpenRA.Game/Widgets/ChatEntryWidget.cs @@ -49,7 +49,7 @@ namespace OpenRA.Widgets { 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) { @@ -79,14 +79,14 @@ namespace OpenRA.Widgets if (composing) { - if (e.KeyName == "escape") + if (e.Key == Keycode.ESCAPE) { composing = false; content = ""; YieldKeyboardFocus(); return true; } - else if (e.KeyName == "backspace") + else if (e.Key == Keycode.BACKSPACE) { if (content.Length > 0) content = content.Remove(content.Length - 1); diff --git a/OpenRA.Game/Widgets/TextFieldWidget.cs b/OpenRA.Game/Widgets/TextFieldWidget.cs index cb386340a3..7afcbad503 100644 --- a/OpenRA.Game/Widgets/TextFieldWidget.cs +++ b/OpenRA.Game/Widgets/TextFieldWidget.cs @@ -110,16 +110,16 @@ namespace OpenRA.Widgets if (!HasKeyboardFocus) return false; - if ((e.KeyName == "return" || e.KeyName == "enter") && OnEnterKey()) + if ((e.Key == Keycode.RETURN || e.Key == Keycode.KP_ENTER) && OnEnterKey()) return true; - if (e.KeyName == "tab" && OnTabKey()) + if (e.Key == Keycode.TAB && OnTabKey()) return true; - if (e.KeyName == "escape" && OnEscKey()) + if (e.Key == Keycode.ESCAPE && OnEscKey()) return true; - if (e.KeyName == "left") + if (e.Key == Keycode.LEFT) { if (CursorPosition > 0) CursorPosition--; @@ -127,7 +127,7 @@ namespace OpenRA.Widgets return true; } - if (e.KeyName == "right") + if (e.Key == Keycode.RIGHT) { if (CursorPosition <= Text.Length-1) CursorPosition++; @@ -135,19 +135,19 @@ namespace OpenRA.Widgets return true; } - if (e.KeyName == "home") + if (e.Key == Keycode.HOME) { CursorPosition = 0; return true; } - if (e.KeyName == "end") + if (e.Key == Keycode.END) { CursorPosition = Text.Length; return true; } - if (e.KeyName == "delete") + if (e.Key == Keycode.DELETE) { if (CursorPosition < Text.Length) Text = Text.Remove(CursorPosition, 1); @@ -160,7 +160,7 @@ namespace OpenRA.Widgets public void TypeChar(KeyInput key) { - if (key.KeyName == "backspace" && CursorPosition > 0) + if (key.Key == Keycode.BACKSPACE && CursorPosition > 0) { CursorPosition--; Text = Text.Remove(CursorPosition, 1); diff --git a/OpenRA.Game/Widgets/ViewportControllerWidget.cs b/OpenRA.Game/Widgets/ViewportControllerWidget.cs index b6dd8ddfd6..db920cb00b 100644 --- a/OpenRA.Game/Widgets/ViewportControllerWidget.cs +++ b/OpenRA.Game/Widgets/ViewportControllerWidget.cs @@ -167,12 +167,12 @@ namespace OpenRA.Widgets 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 "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 "right": keyboardDirections = keyboardDirections.Set(ScrollDirection.Right, e.Event == KeyInputEvent.Down); return true; + case Keycode.UP: keyboardDirections = keyboardDirections.Set(ScrollDirection.Up, e.Event == KeyInputEvent.Down); return true; + case Keycode.DOWN: keyboardDirections = keyboardDirections.Set(ScrollDirection.Down, e.Event == KeyInputEvent.Down); return true; + case Keycode.LEFT: keyboardDirections = keyboardDirections.Set(ScrollDirection.Left, e.Event == KeyInputEvent.Down); return true; + case Keycode.RIGHT: keyboardDirections = keyboardDirections.Set(ScrollDirection.Right, e.Event == KeyInputEvent.Down); return true; } return false; diff --git a/OpenRA.Game/Widgets/VqaPlayerWidget.cs b/OpenRA.Game/Widgets/VqaPlayerWidget.cs index 6c6933981f..418ff1eb4f 100644 --- a/OpenRA.Game/Widgets/VqaPlayerWidget.cs +++ b/OpenRA.Game/Widgets/VqaPlayerWidget.cs @@ -108,7 +108,7 @@ namespace OpenRA.Widgets { if (e.Event == KeyInputEvent.Down) { - if (e.KeyName == "escape") + if (e.Key == Keycode.ESCAPE) { Stop(); return true; diff --git a/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs b/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs index 12ac02159c..c60daa2fbe 100644 --- a/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs +++ b/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs @@ -169,14 +169,15 @@ namespace OpenRA.Widgets { 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; } // 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); } return false; diff --git a/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs b/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs index 41c5be3040..3c9621e821 100755 --- a/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs @@ -275,7 +275,7 @@ namespace OpenRA.Mods.Cnc.Widgets public override bool HandleKeyPress(KeyInput e) { 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); SelectNextTab(e.Modifiers.HasModifier(Modifiers.Shift)); diff --git a/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs b/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs index a18a7fb18d..558ba35d55 100755 --- a/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs +++ b/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs @@ -147,12 +147,12 @@ namespace OpenRA.Mods.RA.Widgets public override bool HandleKeyPress(KeyInput e) { 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)); return true; } - return DoBuildingHotkey(e.KeyName, world); + return DoBuildingHotkey(e, world); } public override bool HandleMouseInput(MouseInput mi) @@ -495,12 +495,12 @@ namespace OpenRA.Mods.RA.Widgets p.ToInt2(), Color.White); } - bool DoBuildingHotkey(string key, World world) + bool DoBuildingHotkey(KeyInput e, World world) { if (!paletteOpen) return false; if (CurrentQueue == null) return false; - var toBuild = CurrentQueue.BuildableItems().FirstOrDefault(b => b.Traits.Get().Hotkey == key); + var toBuild = CurrentQueue.BuildableItems().FirstOrDefault(b => b.Traits.Get().Hotkey == KeycodeExts.DisplayString(e.Key)); if (toBuild != null) { diff --git a/OpenRA.Mods.RA/Widgets/Logic/IngameChatLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/IngameChatLogic.cs index 8587827343..7c40bc2382 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/IngameChatLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/IngameChatLogic.cs @@ -70,9 +70,8 @@ namespace OpenRA.Mods.RA.Widgets.Logic chatPanel.OnKeyPress = (e) => { 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 toggle = Game.Settings.Game.TeamChatToggle ; TeamChat = (!toggle && shift) || ( toggle && (TeamChat ^ shift) ); diff --git a/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs b/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs index 0e301778cd..e4188c9a1a 100644 --- a/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs +++ b/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs @@ -47,35 +47,36 @@ namespace OpenRA.Mods.RA.Widgets { 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(); - if (e.KeyName == Game.Settings.Keys.ToLastEventKey) + if (KeycodeExts.DisplayString(e.Key) == ks.ToLastEventKey) return ToLastEvent(); - if (e.KeyName == Game.Settings.Keys.ToSelectionKey) + if (KeycodeExts.DisplayString(e.Key) == ks.ToSelectionKey) return ToSelection(); // Put all functions that aren't unit-specific before this line! if (!world.Selection.Actors.Any()) return false; - if (e.KeyName == Game.Settings.Keys.AttackMoveKey) + if (KeycodeExts.DisplayString(e.Key) == ks.AttackMoveKey) return PerformAttackMove(); - if (e.KeyName == Game.Settings.Keys.StopKey) + if (KeycodeExts.DisplayString(e.Key) == ks.StopKey) return PerformStop(); - if (e.KeyName == Game.Settings.Keys.ScatterKey) + if (KeycodeExts.DisplayString(e.Key) == ks.ScatterKey) return PerformScatter(); - if (e.KeyName == Game.Settings.Keys.DeployKey) + if (KeycodeExts.DisplayString(e.Key) == ks.DeployKey) return PerformDeploy(); - if (e.KeyName == Game.Settings.Keys.StanceCycleKey) + if (KeycodeExts.DisplayString(e.Key) == ks.StanceCycleKey) return PerformStanceCycle(); - if (e.KeyName == Game.Settings.Keys.GuardKey) + if (KeycodeExts.DisplayString(e.Key) == ks.GuardKey) return PerformGuard(); } diff --git a/OpenRA.Renderer.SdlCommon/MultiTapDetection.cs b/OpenRA.Renderer.SdlCommon/MultiTapDetection.cs index 33bc647f28..45d9312bdf 100644 --- a/OpenRA.Renderer.SdlCommon/MultiTapDetection.cs +++ b/OpenRA.Renderer.SdlCommon/MultiTapDetection.cs @@ -14,8 +14,8 @@ using OpenRA.FileFormats; public static class MultiTapDetection { - static Cache keyHistoryCache = - new Cache(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1))); + static Cache keyHistoryCache = + new Cache(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1))); static Cache clickHistoryCache = new Cache(_ => new TapHistory(DateTime.Now - TimeSpan.FromSeconds(1))); @@ -29,12 +29,12 @@ public static class MultiTapDetection return clickHistoryCache[button].LastTapCount(); } - public static int DetectFromKeyboard(string key) + public static int DetectFromKeyboard(Keycode key) { return keyHistoryCache[key].GetTapCount(int2.Zero); } - public static int InfoFromKeyboard(string key) + public static int InfoFromKeyboard(Keycode key) { return keyHistoryCache[key].LastTapCount(); } diff --git a/OpenRA.Renderer.SdlCommon/SdlInput.cs b/OpenRA.Renderer.SdlCommon/SdlInput.cs index 431f2bb291..80badd2568 100644 --- a/OpenRA.Renderer.SdlCommon/SdlInput.cs +++ b/OpenRA.Renderer.SdlCommon/SdlInput.cs @@ -102,18 +102,23 @@ namespace OpenRA.Renderer.SdlCommon } 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 { - Event = KeyInputEvent.Down, - Key = (Keycode)e.key.keysym.sym, + Event = type, + Key = keyCode, Modifiers = mods, UnicodeChar = (char)e.key.keysym.unicode, - MultiTapCount = MultiTapDetection.DetectFromKeyboard(keyName), - KeyName = Sdl.SDL_GetKeyName(e.key.keysym.sym), - VirtKey = e.key.keysym.sym + MultiTapCount = tapCount }; // Special case workaround for windows users @@ -127,24 +132,6 @@ namespace OpenRA.Renderer.SdlCommon 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; - } } }