clean up low-level keyboard input

This commit is contained in:
Chris Forbes
2010-07-15 20:06:00 +12:00
parent bf50a2961e
commit 071c790097
8 changed files with 67 additions and 59 deletions

View File

@@ -18,9 +18,6 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Graphics; 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) 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 true;
return false; return false;
} }

View File

@@ -498,18 +498,18 @@ namespace OpenRA
{ ')', '0' }, { ')', '0' },
}; };
public static void HandleKeyPress(KeyPressEventArgs e, Modifiers modifiers) public static void HandleKeyPress(KeyInput e)
{ {
int sync = world.SyncHash(); int sync = world.SyncHash();
if (chrome.HandleKeyPress(e, modifiers)) if (chrome.HandleKeyPress(e))
return; return;
var c = RemapKeys.ContainsKey(e.KeyChar) ? RemapKeys[e.KeyChar] : e.KeyChar; var c = RemapKeys.ContainsKey(e.KeyChar) ? RemapKeys[e.KeyChar] : e.KeyChar;
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
Game.controller.selection.DoControlGroup(world, Game.controller.selection.DoControlGroup(world,
c - '0', modifiers); c - '0', e.Modifiers);
if (c == 08) if (c == 08)
Game.controller.GotoNextBase(); Game.controller.GotoNextBase();

View File

@@ -23,6 +23,16 @@ using System.Windows.Forms;
namespace OpenRA namespace OpenRA
{ {
public struct MouseInput
{
public MouseInputEvent Event;
public int2 Location;
public MouseButton Button;
public Modifiers Modifiers;
}
public enum MouseInputEvent { Down, Move, Up };
[Flags] [Flags]
public enum MouseButton public enum MouseButton
{ {
@@ -41,13 +51,11 @@ namespace OpenRA
Ctrl = (int)Keys.Control, Ctrl = (int)Keys.Control,
} }
public struct MouseInput public struct KeyInput
{ {
public MouseInputEvent Event; public char KeyChar;
public int2 Location; public string KeyName;
public MouseButton Button;
public Modifiers Modifiers; public Modifiers Modifiers;
public int VirtKey;
} }
public enum MouseInputEvent { Down, Move, Up };
} }

View File

@@ -26,7 +26,6 @@ using OpenRA.FileFormats;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Orders; using OpenRA.Orders;
using OpenRA.Traits; using OpenRA.Traits;
using System.Windows.Forms;
namespace OpenRA.Widgets namespace OpenRA.Widgets
{ {
@@ -142,12 +141,12 @@ namespace OpenRA.Widgets
paletteOpen = true; paletteOpen = true;
currentTab = produces; currentTab = produces;
} }
public override bool HandleKeyPress (KeyPressEventArgs e, Modifiers modifiers) public override bool HandleKeyPress(KeyInput e)
{ {
if (e.KeyChar == 09) if (e.KeyChar == 09)
TabChange((Control.ModifierKeys & Keys.Shift) == Keys.Shift); TabChange(e.Modifiers.HasModifier(Modifiers.Shift));
DoBuildingHotkey(Char.ToLowerInvariant(e.KeyChar), Game.world); DoBuildingHotkey(Char.ToLowerInvariant(e.KeyChar), Game.world);
return true; return true;
} }

View File

@@ -19,7 +19,6 @@
#endregion #endregion
using System.Drawing; using System.Drawing;
using System.Windows.Forms;
namespace OpenRA.Widgets namespace OpenRA.Widgets
{ {
@@ -56,13 +55,13 @@ namespace OpenRA.Widgets
public override bool HandleInput(MouseInput mi) { return false; } 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 (e.KeyChar == '\r')
{ {
if (composing) if (composing)
{ {
if (modifiers.HasModifier(Modifiers.Shift)) if (e.Modifiers.HasModifier(Modifiers.Shift))
{ {
teamChat ^= true; teamChat ^= true;
return true; return true;
@@ -82,7 +81,7 @@ namespace OpenRA.Widgets
{ {
TakeFocus(new MouseInput()); TakeFocus(new MouseInput());
composing = true; composing = true;
teamChat ^= modifiers.HasModifier(Modifiers.Shift); teamChat ^= e.Modifiers.HasModifier(Modifiers.Shift);
return true; return true;
} }
} }
@@ -104,7 +103,7 @@ namespace OpenRA.Widgets
return false; return false;
} }
return base.HandleKeyPress(e, modifiers); return base.HandleKeyPress(e);
} }
} }
} }

View File

@@ -71,7 +71,7 @@ namespace OpenRA.Widgets
return true; 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 // Only take input if we are focused
if (!Focused) if (!Focused)

View File

@@ -22,7 +22,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Windows.Forms;
using OpenRA.FileFormats; using OpenRA.FileFormats;
namespace OpenRA.Widgets namespace OpenRA.Widgets
@@ -52,7 +51,7 @@ namespace OpenRA.Widgets
public Func<MouseInput, bool> OnMouseDown = mi => false; public Func<MouseInput, bool> OnMouseDown = mi => false;
public Func<MouseInput, bool> OnMouseUp = mi => false; public Func<MouseInput, bool> OnMouseUp = mi => false;
public Func<MouseInput, bool> OnMouseMove = mi => false; public Func<MouseInput, bool> OnMouseMove = mi => false;
public Func<KeyPressEventArgs, Modifiers, bool> OnKeyPress = (e, modifiers) => false; public Func<KeyInput, bool> OnKeyPress = e => false;
public Func<bool> IsVisible; public Func<bool> IsVisible;
@@ -211,22 +210,22 @@ namespace OpenRA.Widgets
} }
public virtual bool HandleKeyPress(System.Windows.Forms.KeyPressEventArgs e, Modifiers modifiers) { return false; } public virtual bool HandleKeyPress(KeyInput e) { return false; }
public virtual bool HandleKeyPressOuter(System.Windows.Forms.KeyPressEventArgs e, Modifiers modifiers) public virtual bool HandleKeyPressOuter(KeyInput e)
{ {
if (!IsVisible()) if (!IsVisible())
return false; return false;
// Can any of our children handle this? // Can any of our children handle this?
foreach (var child in Children) foreach (var child in Children)
if (child.HandleKeyPressOuter(e, modifiers)) if (child.HandleKeyPressOuter(e))
return true; return true;
// Do any widgety behavior (enter text etc) // 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 // 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; return handled;
} }

View File

@@ -157,6 +157,29 @@ namespace OpenRA.GlRenderer
| ((raw & Sdl.KMOD_SHIFT) != 0 ? Modifiers.Shift : 0); | ((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() public void Present()
{ {
Sdl.SDL_GL_SwapBuffers(); Sdl.SDL_GL_SwapBuffers();
@@ -202,34 +225,17 @@ namespace OpenRA.GlRenderer
case Sdl.SDL_KEYDOWN: case Sdl.SDL_KEYDOWN:
{ {
bool handled = true; bool handled = true;
switch (e.key.keysym.sym)
var keyEvent = new KeyInput
{ {
case Sdl.SDLK_UP: Game.HandleArrowKeyScroll("up", true); break; Modifiers = mods,
case Sdl.SDLK_LEFT: Game.HandleArrowKeyScroll("left", true); break; KeyChar = (char) e.key.keysym.unicode,
case Sdl.SDLK_DOWN: Game.HandleArrowKeyScroll("down", true); break; KeyName = Sdl.SDL_GetKeyName( e.key.keysym.sym )
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;
}
if (e.key.keysym.unicode != 0 && !handled) if (!HandleSpecialKey(keyEvent))
Game.HandleKeyPress(new KeyPressEventArgs((char)e.key.keysym.unicode), mods); Game.HandleKeyPress(keyEvent);
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();
}
} break; } break;
case Sdl.SDL_KEYUP: case Sdl.SDL_KEYUP: