really fix modifier keys

This commit is contained in:
Chris Forbes
2010-02-22 21:25:47 +13:00
parent c2ac8ad096
commit 28215387cb
3 changed files with 21 additions and 18 deletions

View File

@@ -29,17 +29,9 @@ namespace OpenRa
{ {
public class Controller : IHandleInput public class Controller : IHandleInput
{ {
public IOrderGenerator orderGenerator; public IOrderGenerator orderGenerator = new UnitOrderGenerator();
public Selection selection = new Selection(); public Selection selection = new Selection();
readonly Func<Modifiers> GetModifierKeys;
public Controller(Func<Modifiers> getModifierKeys)
{
GetModifierKeys = getModifierKeys;
CancelInputMode();
}
public void CancelInputMode() { orderGenerator = new UnitOrderGenerator(); } public void CancelInputMode() { orderGenerator = new UnitOrderGenerator(); }
public bool ToggleInputMode<T>() where T : IOrderGenerator, new() public bool ToggleInputMode<T>() where T : IOrderGenerator, new()
@@ -123,6 +115,7 @@ namespace OpenRa
} }
public float2 MousePosition { get { return dragEnd; } } public float2 MousePosition { get { return dragEnd; } }
Modifiers modifiers;
public string ChooseCursor( World world ) public string ChooseCursor( World world )
{ {
@@ -134,7 +127,7 @@ namespace OpenRa
{ {
Location = ( Game.CellSize * MousePosition - Game.viewport.Location ).ToInt2(), Location = ( Game.CellSize * MousePosition - Game.viewport.Location ).ToInt2(),
Button = MouseButton.Right, Button = MouseButton.Right,
Modifiers = GetModifierKeys(), Modifiers = modifiers
}; };
return orderGenerator.GetCursor( world, MousePosition.ToInt2(), mi ); return orderGenerator.GetCursor( world, MousePosition.ToInt2(), mi );
@@ -145,5 +138,7 @@ namespace OpenRa
throw new InvalidOperationException( "Desync in Controller.ChooseCursor" ); throw new InvalidOperationException( "Desync in Controller.ChooseCursor" );
} }
} }
public void SetModifiers(Modifiers mods) { modifiers = mods; }
} }
} }

View File

@@ -341,6 +341,11 @@ namespace OpenRa
throw new InvalidOperationException( "Desync in OnKeyPress" ); throw new InvalidOperationException( "Desync in OnKeyPress" );
} }
public static void HandleModifierKeys(Modifiers mods)
{
controller.SetModifiers(mods);
}
static Size GetResolution(Settings settings) static Size GetResolution(Settings settings)
{ {
var desktopResolution = Screen.PrimaryScreen.Bounds.Size; var desktopResolution = Screen.PrimaryScreen.Bounds.Size;
@@ -354,9 +359,6 @@ namespace OpenRa
desktopResolution.Height); desktopResolution.Height);
} }
// [DllImport("user32")]
// static extern int ShowCursor([MarshalAs(UnmanagedType.Bool)] bool visible);
public static void PreInit(Settings settings) public static void PreInit(Settings settings)
{ {
while (!Directory.Exists("mods")) while (!Directory.Exists("mods"))
@@ -383,7 +385,7 @@ namespace OpenRa
renderer = new Renderer(resolution, windowed); renderer = new Renderer(resolution, windowed);
resolution = renderer.Resolution; resolution = renderer.Resolution;
var controller = new Controller(() => (Modifiers)(int)0/*ModifierKeys*/); /* a bit of insane input routing */ var controller = new Controller(); /* a bit of insane input routing */
Game.Initialize(Game.Settings.Map, renderer, new int2(resolution), Game.Settings.Player, controller); Game.Initialize(Game.Settings.Map, renderer, new int2(resolution), Game.Settings.Player, controller);

View File

@@ -124,7 +124,6 @@ namespace OpenRa.GlRenderer
CheckGlError(); CheckGlError();
} }
Modifiers mods = 0;
MouseButtons lastButtonBits = (MouseButtons)0; MouseButtons lastButtonBits = (MouseButtons)0;
static MouseButtons MakeButton(byte b) static MouseButtons MakeButton(byte b)
@@ -135,10 +134,20 @@ namespace OpenRa.GlRenderer
: 0; : 0;
} }
static Modifiers MakeModifiers(int raw)
{
return ((raw & Sdl.KMOD_ALT) != 0 ? Modifiers.Alt : 0)
| ((raw & Sdl.KMOD_CTRL) != 0 ? Modifiers.Ctrl : 0)
| ((raw & Sdl.KMOD_SHIFT) != 0 ? Modifiers.Shift : 0);
}
public void Present() public void Present()
{ {
Sdl.SDL_GL_SwapBuffers(); Sdl.SDL_GL_SwapBuffers();
var mods = MakeModifiers(Sdl.SDL_GetModState());
Game.HandleModifierKeys(mods);
Sdl.SDL_Event e; Sdl.SDL_Event e;
while (Sdl.SDL_PollEvent(out e) != 0) while (Sdl.SDL_PollEvent(out e) != 0)
{ {
@@ -177,9 +186,6 @@ namespace OpenRa.GlRenderer
case Sdl.SDL_KEYDOWN: case Sdl.SDL_KEYDOWN:
{ {
mods = ( ( e.key.keysym.mod & Sdl.KMOD_ALT ) != 0 ? Modifiers.Alt : 0 )
| ( ( e.key.keysym.mod & Sdl.KMOD_CTRL ) != 0 ? Modifiers.Ctrl : 0 )
| ( ( e.key.keysym.mod & Sdl.KMOD_SHIFT ) != 0 ? Modifiers.Shift : 0 );
if( e.key.keysym.unicode != 0 ) if( e.key.keysym.unicode != 0 )
Game.HandleKeyPress( new KeyPressEventArgs( (char)e.key.keysym.unicode ), mods ); Game.HandleKeyPress( new KeyPressEventArgs( (char)e.key.keysym.unicode ), mods );