From d32461a9403a7fdbea885dc98de7043f60146fde Mon Sep 17 00:00:00 2001 From: Bob Date: Thu, 21 Jan 2010 12:37:19 +1300 Subject: [PATCH] moved some input stuff from MainWindow to Game --- OpenRa.Game/Game.cs | 58 +++++++++++++++++++++++++++++++++++++++ OpenRa.Game/MainWindow.cs | 57 ++++---------------------------------- 2 files changed, 63 insertions(+), 52 deletions(-) diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index 43820d20d3..293342084b 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -9,6 +9,7 @@ using OpenRa.Graphics; using OpenRa.Orders; using OpenRa.Support; using OpenRa.Traits; +using System.Windows.Forms; namespace OpenRa { @@ -231,5 +232,62 @@ namespace OpenRa taken.Add(sp); return sp; } + + internal static void DispatchMouseInput(MouseInputEvent ev, MouseEventArgs e, Keys ModifierKeys) + { + int sync = Game.world.SyncHash(); + + Game.viewport.DispatchMouseInput( + new MouseInput + { + Button = (MouseButton)(int)e.Button, + Event = ev, + Location = new int2(e.Location), + Modifiers = (Modifiers)(int)ModifierKeys, + }); + + if( sync != Game.world.SyncHash() ) + throw new InvalidOperationException( "Desync in DispatchMouseInput" ); + } + + internal static void HandleKeyDown( KeyEventArgs e ) + { + int sync = Game.world.SyncHash(); + + /* hack hack hack */ + if( e.KeyCode == Keys.F8 && !Game.orderManager.GameStarted ) + { + Game.controller.AddOrder( + new Order( "ToggleReady", Game.world.LocalPlayer.PlayerActor, null, int2.Zero, "" ) { IsImmediate = true } ); + } + + /* temporary hack: DO NOT LEAVE IN */ + if( e.KeyCode == Keys.F2 ) + Game.world.LocalPlayer = Game.world.players[ ( Game.world.LocalPlayer.Index + 1 ) % 4 ]; + if( e.KeyCode == Keys.F3 ) + Game.controller.orderGenerator = new SellOrderGenerator(); + if( e.KeyCode == Keys.F4 ) + Game.controller.orderGenerator = new RepairOrderGenerator(); + + if( !Game.chat.isChatting ) + if( e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ) + Game.controller.DoControlGroup( (int)e.KeyCode - (int)Keys.D0, (Modifiers)(int)e.Modifiers ); + + if( sync != Game.world.SyncHash() ) + throw new InvalidOperationException( "Desync in OnKeyDown" ); + } + + internal static void HandleKeyPress( KeyPressEventArgs e ) + { + int sync = Game.world.SyncHash(); + + if( e.KeyChar == '\r' ) + Game.chat.Toggle(); + else if( Game.chat.isChatting ) + Game.chat.TypeChar( e.KeyChar ); + + if( sync != Game.world.SyncHash() ) + throw new InvalidOperationException( "Desync in OnKeyPress" ); + } } } diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index ab8652ddea..30f8d9c5fc 100755 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -90,28 +90,11 @@ namespace OpenRa int2 lastPos; - void DispatchMouseInput(MouseInputEvent ev, MouseEventArgs e) - { - int sync = Game.world.SyncHash(); - - Game.viewport.DispatchMouseInput( - new MouseInput - { - Button = (MouseButton)(int)e.Button, - Event = ev, - Location = new int2(e.Location), - Modifiers = (Modifiers)(int)ModifierKeys, - }); - - if( sync != Game.world.SyncHash() ) - throw new InvalidOperationException( "Desync in DispatchMouseInput" ); - } - protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); lastPos = new int2(e.Location); - DispatchMouseInput(MouseInputEvent.Down, e); + Game.DispatchMouseInput(MouseInputEvent.Down, e, ModifierKeys); } protected override void OnMouseMove(MouseEventArgs e) @@ -125,57 +108,27 @@ namespace OpenRa lastPos = p; } - DispatchMouseInput(MouseInputEvent.Move, e); + Game.DispatchMouseInput(MouseInputEvent.Move, e, ModifierKeys); } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); - DispatchMouseInput(MouseInputEvent.Up, e); + Game.DispatchMouseInput(MouseInputEvent.Up, e, ModifierKeys); } protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); - int sync = Game.world.SyncHash(); - - /* hack hack hack */ - if (e.KeyCode == Keys.F8 && !Game.orderManager.GameStarted) - { - Game.controller.AddOrder( - new Order( "ToggleReady", Game.world.LocalPlayer.PlayerActor, null, int2.Zero, "") { IsImmediate = true }); - } - - /* temporary hack: DO NOT LEAVE IN */ - if (e.KeyCode == Keys.F2) - Game.world.LocalPlayer = Game.world.players[(Game.world.LocalPlayer.Index + 1) % 4]; - if (e.KeyCode == Keys.F3) - Game.controller.orderGenerator = new SellOrderGenerator(); - if (e.KeyCode == Keys.F4) - Game.controller.orderGenerator = new RepairOrderGenerator(); - - if (!Game.chat.isChatting) - if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) - Game.controller.DoControlGroup( (int)e.KeyCode - (int)Keys.D0, (Modifiers)(int)e.Modifiers ); - - if( sync != Game.world.SyncHash() ) - throw new InvalidOperationException( "Desync in OnKeyDown" ); + Game.HandleKeyDown( e ); } protected override void OnKeyPress(KeyPressEventArgs e) { base.OnKeyPress(e); - int sync = Game.world.SyncHash(); - - if (e.KeyChar == '\r') - Game.chat.Toggle(); - else if (Game.chat.isChatting) - Game.chat.TypeChar(e.KeyChar); - - if( sync != Game.world.SyncHash() ) - throw new InvalidOperationException( "Desync in OnKeyPress" ); + Game.HandleKeyPress( e ); } }