diff --git a/OpenRA.Game/Chrome.cs b/OpenRA.Game/Chrome.cs index 0b4eadf18d..2af06961c8 100644 --- a/OpenRA.Game/Chrome.cs +++ b/OpenRA.Game/Chrome.cs @@ -18,9 +18,6 @@ */ #endregion -using System; -using System.Collections.Generic; -using System.Drawing; using System.Linq; using OpenRA.FileFormats; using OpenRA.Graphics; @@ -83,12 +80,12 @@ namespace OpenRA } - public bool HandleKeyPress(System.Windows.Forms.KeyPressEventArgs e, Modifiers modifiers) + public bool HandleKeyPress(KeyInput e) { if (Widget.SelectedWidget != null) - return Widget.SelectedWidget.HandleKeyPressOuter(e, modifiers); + return Widget.SelectedWidget.HandleKeyPressOuter(e); - if (rootWidget.HandleKeyPressOuter(e, modifiers)) + if (rootWidget.HandleKeyPressOuter(e)) return true; return false; } diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index 7c9847cb2e..84e5a915d1 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -498,18 +498,18 @@ namespace OpenRA { ')', '0' }, }; - public static void HandleKeyPress(KeyPressEventArgs e, Modifiers modifiers) + public static void HandleKeyPress(KeyInput e) { int sync = world.SyncHash(); - if (chrome.HandleKeyPress(e, modifiers)) + if (chrome.HandleKeyPress(e)) return; var c = RemapKeys.ContainsKey(e.KeyChar) ? RemapKeys[e.KeyChar] : e.KeyChar; if (c >= '0' && c <= '9') Game.controller.selection.DoControlGroup(world, - c - '0', modifiers); + c - '0', e.Modifiers); if (c == 08) Game.controller.GotoNextBase(); diff --git a/OpenRA.Game/MainWindow.cs b/OpenRA.Game/MainWindow.cs index c3ed7f0033..86188340e3 100755 --- a/OpenRA.Game/MainWindow.cs +++ b/OpenRA.Game/MainWindow.cs @@ -23,6 +23,16 @@ using System.Windows.Forms; namespace OpenRA { + public struct MouseInput + { + public MouseInputEvent Event; + public int2 Location; + public MouseButton Button; + public Modifiers Modifiers; + } + + public enum MouseInputEvent { Down, Move, Up }; + [Flags] public enum MouseButton { @@ -41,13 +51,11 @@ namespace OpenRA Ctrl = (int)Keys.Control, } - public struct MouseInput + public struct KeyInput { - public MouseInputEvent Event; - public int2 Location; - public MouseButton Button; + public char KeyChar; + public string KeyName; public Modifiers Modifiers; + public int VirtKey; } - - public enum MouseInputEvent { Down, Move, Up }; } diff --git a/OpenRA.Game/Widgets/BuildPaletteWidget.cs b/OpenRA.Game/Widgets/BuildPaletteWidget.cs index 68c8140084..b7a9fe2f18 100644 --- a/OpenRA.Game/Widgets/BuildPaletteWidget.cs +++ b/OpenRA.Game/Widgets/BuildPaletteWidget.cs @@ -26,7 +26,6 @@ using OpenRA.FileFormats; using OpenRA.Graphics; using OpenRA.Orders; using OpenRA.Traits; -using System.Windows.Forms; namespace OpenRA.Widgets { @@ -142,12 +141,12 @@ namespace OpenRA.Widgets paletteOpen = true; currentTab = produces; } - - public override bool HandleKeyPress (KeyPressEventArgs e, Modifiers modifiers) + + public override bool HandleKeyPress(KeyInput e) { if (e.KeyChar == 09) - TabChange((Control.ModifierKeys & Keys.Shift) == Keys.Shift); - + TabChange(e.Modifiers.HasModifier(Modifiers.Shift)); + DoBuildingHotkey(Char.ToLowerInvariant(e.KeyChar), Game.world); return true; } diff --git a/OpenRA.Game/Widgets/ChatEntryWidget.cs b/OpenRA.Game/Widgets/ChatEntryWidget.cs index 4c6acd8dc1..fd10d17080 100644 --- a/OpenRA.Game/Widgets/ChatEntryWidget.cs +++ b/OpenRA.Game/Widgets/ChatEntryWidget.cs @@ -19,7 +19,6 @@ #endregion using System.Drawing; -using System.Windows.Forms; namespace OpenRA.Widgets { @@ -56,13 +55,13 @@ namespace OpenRA.Widgets public override bool HandleInput(MouseInput mi) { return false; } - public override bool HandleKeyPress(KeyPressEventArgs e, Modifiers modifiers) + public override bool HandleKeyPress(KeyInput e) { if (e.KeyChar == '\r') { if (composing) { - if (modifiers.HasModifier(Modifiers.Shift)) + if (e.Modifiers.HasModifier(Modifiers.Shift)) { teamChat ^= true; return true; @@ -82,7 +81,7 @@ namespace OpenRA.Widgets { TakeFocus(new MouseInput()); composing = true; - teamChat ^= modifiers.HasModifier(Modifiers.Shift); + teamChat ^= e.Modifiers.HasModifier(Modifiers.Shift); return true; } } @@ -104,7 +103,7 @@ namespace OpenRA.Widgets return false; } - return base.HandleKeyPress(e, modifiers); + return base.HandleKeyPress(e); } } } diff --git a/OpenRA.Game/Widgets/TextFieldWidget.cs b/OpenRA.Game/Widgets/TextFieldWidget.cs index da1fdd17c4..96b9611ff3 100644 --- a/OpenRA.Game/Widgets/TextFieldWidget.cs +++ b/OpenRA.Game/Widgets/TextFieldWidget.cs @@ -71,7 +71,7 @@ namespace OpenRA.Widgets return true; } - public override bool HandleKeyPress(System.Windows.Forms.KeyPressEventArgs e, Modifiers modifiers) + public override bool HandleKeyPress(KeyInput e) { // Only take input if we are focused if (!Focused) diff --git a/OpenRA.Game/Widgets/Widget.cs b/OpenRA.Game/Widgets/Widget.cs index 25e3cf9c6b..2853cecde2 100644 --- a/OpenRA.Game/Widgets/Widget.cs +++ b/OpenRA.Game/Widgets/Widget.cs @@ -22,7 +22,6 @@ using System; using System.Collections.Generic; using System.Drawing; using System.Linq; -using System.Windows.Forms; using OpenRA.FileFormats; namespace OpenRA.Widgets @@ -52,7 +51,7 @@ namespace OpenRA.Widgets public Func OnMouseDown = mi => false; public Func OnMouseUp = mi => false; public Func OnMouseMove = mi => false; - public Func OnKeyPress = (e, modifiers) => false; + public Func OnKeyPress = e => false; public Func IsVisible; @@ -211,22 +210,22 @@ namespace OpenRA.Widgets } - public virtual bool HandleKeyPress(System.Windows.Forms.KeyPressEventArgs e, Modifiers modifiers) { return false; } - public virtual bool HandleKeyPressOuter(System.Windows.Forms.KeyPressEventArgs e, Modifiers modifiers) + public virtual bool HandleKeyPress(KeyInput e) { return false; } + public virtual bool HandleKeyPressOuter(KeyInput e) { if (!IsVisible()) return false; // Can any of our children handle this? foreach (var child in Children) - if (child.HandleKeyPressOuter(e, modifiers)) + if (child.HandleKeyPressOuter(e)) return true; // Do any widgety behavior (enter text etc) - var handled = HandleKeyPress(e,modifiers); + var handled = HandleKeyPress(e); // Apply any special logic added by delegates; they return true if they caught the input - if (OnKeyPress(e,modifiers)) return true; + if (OnKeyPress(e)) return true; return handled; } diff --git a/OpenRA.Gl/GraphicsDevice.cs b/OpenRA.Gl/GraphicsDevice.cs index 28351cf544..d52396b150 100644 --- a/OpenRA.Gl/GraphicsDevice.cs +++ b/OpenRA.Gl/GraphicsDevice.cs @@ -157,6 +157,29 @@ namespace OpenRA.GlRenderer | ((raw & Sdl.KMOD_SHIFT) != 0 ? Modifiers.Shift : 0); } + bool HandleSpecialKey(KeyInput k) + { + switch (k.VirtKey) + { + case Sdl.SDLK_F13: + var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + + Path.DirectorySeparatorChar + DateTime.UtcNow.ToString("OpenRA-yyyy-MM-ddThhmmssZ") + ".bmp"; + Sdl.SDL_SaveBMP(surf, path); + return true; + + case Sdl.SDLK_F4: + if (k.Modifiers.HasModifier(Modifiers.Alt)) + { + OpenRA.Game.Exit(); + return true; + } + return false; + + default: + return false; + } + } + public void Present() { Sdl.SDL_GL_SwapBuffers(); @@ -202,34 +225,17 @@ namespace OpenRA.GlRenderer case Sdl.SDL_KEYDOWN: { - bool handled = true; - switch (e.key.keysym.sym) + bool handled = true; + + var keyEvent = new KeyInput { - case Sdl.SDLK_UP: Game.HandleArrowKeyScroll("up", true); break; - case Sdl.SDLK_LEFT: Game.HandleArrowKeyScroll("left", true); break; - case Sdl.SDLK_DOWN: Game.HandleArrowKeyScroll("down", true); break; - case Sdl.SDLK_RIGHT: Game.HandleArrowKeyScroll("right", true); break; - - case Sdl.SDLK_F13: - string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + Path.DirectorySeparatorChar + DateTime.UtcNow.ToString("OpenRA-yyyy-MM-ddThhmmssZ")+".bmp"; - Sdl.SDL_SaveBMP(surf,path); - break; - default: - handled = false; - break; - } + Modifiers = mods, + KeyChar = (char) e.key.keysym.unicode, + KeyName = Sdl.SDL_GetKeyName( e.key.keysym.sym ) + }; - if (e.key.keysym.unicode != 0 && !handled) - Game.HandleKeyPress(new KeyPressEventArgs((char)e.key.keysym.unicode), mods); - - else if (mods != 0) - { - var keyName = Sdl.SDL_GetKeyName(e.key.keysym.sym); - if (keyName.Length == 1) - Game.HandleKeyPress(new KeyPressEventArgs(keyName[0]), mods); - else if (keyName == "f4" && ((mods & Modifiers.Alt) != 0)) - OpenRA.Game.Exit(); - } + if (!HandleSpecialKey(keyEvent)) + Game.HandleKeyPress(keyEvent); } break; case Sdl.SDL_KEYUP: