From 5bf3e05f03eb55e393ade7495762a8ae0f7d71d3 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Wed, 16 Dec 2009 11:31:53 +1300 Subject: [PATCH] routing modifier keys where they need to be --- OpenRa.Game/Actor.cs | 4 +- OpenRa.Game/Chrome.cs | 2 +- OpenRa.Game/Controller.cs | 60 ++++++++++++----------- OpenRa.Game/Exts.cs | 2 +- OpenRa.Game/Game.cs | 8 ++-- OpenRa.Game/Graphics/Viewport.cs | 1 + OpenRa.Game/IOrderGenerator.cs | 2 +- OpenRa.Game/MainWindow.cs | 66 +++++++++++++++----------- OpenRa.Game/PlaceBuilding.cs | 4 +- OpenRa.Game/Traits/AttackBase.cs | 4 +- OpenRa.Game/Traits/Harvester.cs | 7 ++- OpenRa.Game/Traits/Helicopter.cs | 4 +- OpenRa.Game/Traits/McvDeploy.cs | 4 +- OpenRa.Game/Traits/Mobile.cs | 4 +- OpenRa.Game/Traits/ProductionQueue.cs | 2 +- OpenRa.Game/Traits/RallyPoint.cs | 4 +- OpenRa.Game/Traits/TraitsInterfaces.cs | 2 +- OpenRa.Game/UnitOrderGenerator.cs | 4 +- 18 files changed, 100 insertions(+), 84 deletions(-) diff --git a/OpenRa.Game/Actor.cs b/OpenRa.Game/Actor.cs index 5e001e4e91..46bb94cd05 100755 --- a/OpenRa.Game/Actor.cs +++ b/OpenRa.Game/Actor.cs @@ -77,7 +77,7 @@ namespace OpenRa.Game return traits.WithInterface().SelectMany( x => x.Render( this ) ); } - public Order Order( int2 xy, bool lmb ) + public Order Order( int2 xy, MouseInput mi ) { if (Owner != Game.LocalPlayer) return null; @@ -91,7 +91,7 @@ namespace OpenRa.Game underCursor = null; return traits.WithInterface() - .Select( x => x.IssueOrder( this, xy, lmb, underCursor ) ) + .Select( x => x.IssueOrder( this, xy, mi, underCursor ) ) .FirstOrDefault( x => x != null ); } diff --git a/OpenRa.Game/Chrome.cs b/OpenRa.Game/Chrome.cs index f1c65e197e..2199061a77 100644 --- a/OpenRa.Game/Chrome.cs +++ b/OpenRa.Game/Chrome.cs @@ -331,7 +331,7 @@ namespace OpenRa.Game return false; if (mi.Event == MouseInputEvent.Down) - action(mi.Button == MouseButtons.Left); + action(mi.Button == MouseButton.Left); return true; } diff --git a/OpenRa.Game/Controller.cs b/OpenRa.Game/Controller.cs index 954d71a098..e608c4f258 100644 --- a/OpenRa.Game/Controller.cs +++ b/OpenRa.Game/Controller.cs @@ -6,6 +6,7 @@ using OpenRa.Game.GameRules; using OpenRa.Game.Graphics; using OpenRa.Game.Traits; using IjwFramework.Collections; +using System; namespace OpenRa.Game { @@ -13,13 +14,20 @@ namespace OpenRa.Game { public IOrderGenerator orderGenerator; + readonly Func GetModifierKeys; + + public Controller(Func getModifierKeys) + { + GetModifierKeys = getModifierKeys; + } + List recentOrders = new List(); - void ApplyOrders(float2 xy, bool left) + void ApplyOrders(float2 xy, MouseInput mi) { var doVoice = null as Actor; if (orderGenerator != null) - foreach (var order in orderGenerator.Order(xy.ToInt2(), left)) + foreach (var order in orderGenerator.Order(xy.ToInt2(), mi)) { AddOrder( order ); if (order.Subject != null && order.Player == Game.LocalPlayer) @@ -34,18 +42,10 @@ namespace OpenRa.Game public List GetRecentOrders( bool imm ) { - if (imm) - { - var result = recentOrders.Where(o => o.IsImmediate).ToList(); - recentOrders.RemoveAll(o => o.IsImmediate); - return result; - } - else - { - var result = recentOrders.Where(o => !o.IsImmediate).ToList(); - recentOrders.RemoveAll(o => !o.IsImmediate); - return result; - } + Func p = o => o.IsImmediate ^ !imm; + var result = recentOrders.Where(p).ToList(); + recentOrders.RemoveAll(o => p(o)); // ffs. + return result; } float2 dragStart, dragEnd; @@ -53,32 +53,32 @@ namespace OpenRa.Game { var xy = Game.viewport.ViewToWorld(mi); - if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Down) + if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Down) { if (!(orderGenerator is PlaceBuilding)) dragStart = dragEnd = xy; - ApplyOrders(xy, true); + ApplyOrders(xy, mi); } - if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Move) + if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Move) dragEnd = xy; - if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Up) + if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Up) { if (!(orderGenerator is PlaceBuilding)) { var newSelection = Game.SelectActorsInBox(Game.CellSize * dragStart, Game.CellSize * xy); - CombineSelection(newSelection, mi.Modifiers.HasModifier(Keys.Shift), dragStart == xy); + CombineSelection(newSelection, mi.Modifiers.HasModifier(Modifiers.Shift), dragStart == xy); } dragStart = dragEnd = xy; } - if (mi.Button == MouseButtons.None && mi.Event == MouseInputEvent.Move) + if (mi.Button == MouseButton.None && mi.Event == MouseInputEvent.Move) dragStart = dragEnd = xy; - if (mi.Button == MouseButtons.Right && mi.Event == MouseInputEvent.Down) - ApplyOrders(xy, false); + if (mi.Button == MouseButton.Right && mi.Event == MouseInputEvent.Down) + ApplyOrders(xy, mi); return true; } @@ -116,9 +116,11 @@ namespace OpenRa.Game public Cursor ChooseCursor() { - var c = (orderGenerator is UnitOrderGenerator) ? orderGenerator.Order(dragEnd.ToInt2(), false) - .Where( o => o.Validate() ) - .Select(o => CursorForOrderString( o.OrderString, o.Subject, o.TargetLocation )) + var mods = GetModifierKeys(); + var c = (orderGenerator is UnitOrderGenerator) ? orderGenerator.Order(dragEnd.ToInt2(), + new MouseInput { Button = MouseButton.Right, Modifiers = mods }) + .Where(o => o.Validate()) + .Select(o => CursorForOrderString(o.OrderString, o.Subject, o.TargetLocation)) .FirstOrDefault(a => a != null) : null; return c ?? (Game.SelectActorsInBox(Game.CellSize * dragEnd, Game.CellSize * dragEnd).Any() ? Cursor.Select : Cursor.Default); @@ -149,10 +151,10 @@ namespace OpenRa.Game Cache> cache = new Cache>(_ => new List()); - public void DoControlGroup(int group, Keys keys) + public void DoControlGroup(int group, Modifiers mods) { var uog = orderGenerator as UnitOrderGenerator; - if (keys.HasModifier(Keys.Control)) + if (mods.HasModifier(Modifiers.Ctrl)) { if (uog == null || !uog.selection.Any()) return; @@ -162,14 +164,14 @@ namespace OpenRa.Game return; } - if (keys.HasModifier(Keys.Alt)) + if (mods.HasModifier(Modifiers.Alt)) { Game.viewport.Center(cache[group]); return; } if (uog == null) return; - CombineSelection(cache[group], keys.HasModifier(Keys.Shift), false); + CombineSelection(cache[group], mods.HasModifier(Modifiers.Shift), false); } } } diff --git a/OpenRa.Game/Exts.cs b/OpenRa.Game/Exts.cs index 187681e1d5..b66ad80010 100644 --- a/OpenRa.Game/Exts.cs +++ b/OpenRa.Game/Exts.cs @@ -12,7 +12,7 @@ namespace OpenRa.Game return string.Format(fmt, args); } - public static bool HasModifier(this Keys k, Keys mod) + public static bool HasModifier(this Modifiers k, Modifiers mod) { return (k & mod) == mod; } diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index 5aeb0b6dcf..9d0b87899a 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -8,6 +8,7 @@ using OpenRa.Game.GameRules; using OpenRa.Game.Graphics; using OpenRa.Game.Support; using OpenRa.Game.Traits; +using System.Windows.Forms; namespace OpenRa.Game { @@ -47,7 +48,8 @@ namespace OpenRa.Game public static bool skipMakeAnims = true; - public static void Initialize(string mapName, Renderer renderer, int2 clientSize, int localPlayer, bool useAftermath) + public static void Initialize(string mapName, Renderer renderer, int2 clientSize, + int localPlayer, bool useAftermath, Controller controller) { Rules.LoadRules(mapName, useAftermath); world = new World(); @@ -55,7 +57,7 @@ namespace OpenRa.Game for( int i = 0 ; i < 8 ; i++ ) { var a = new Actor( null, new int2( int.MaxValue, int.MaxValue ), null ); - players[ i ] = new Player( a, i, i, "Multi{0}".F( i ), Race.Soviet ); + players[ i ] = new Player( a, i, i, "Multi{0}".F( i ), Race.Allies ); a.Owner = players[ i ]; a.traits.Add( new Traits.ProductionQueue( a ) ); Game.world.Add( a ); @@ -65,7 +67,7 @@ namespace OpenRa.Game Rules.Map.InitOreDensity(); - controller = new Controller(); + Game.controller = controller; worldRenderer = new WorldRenderer( renderer ); SequenceProvider.Initialize(useAftermath); diff --git a/OpenRa.Game/Graphics/Viewport.cs b/OpenRa.Game/Graphics/Viewport.cs index cc028fd752..84c9ca5a2f 100644 --- a/OpenRa.Game/Graphics/Viewport.cs +++ b/OpenRa.Game/Graphics/Viewport.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Windows.Forms; namespace OpenRa.Game.Graphics { diff --git a/OpenRa.Game/IOrderGenerator.cs b/OpenRa.Game/IOrderGenerator.cs index 76f71e8121..0bdfc382ea 100644 --- a/OpenRa.Game/IOrderGenerator.cs +++ b/OpenRa.Game/IOrderGenerator.cs @@ -4,7 +4,7 @@ namespace OpenRa.Game { interface IOrderGenerator { - IEnumerable Order( int2 xy, bool lmb ); + IEnumerable Order( int2 xy, MouseInput mi ); void Tick(); void Render(); } diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 5d8214b7a0..6de41f5c97 100755 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -58,8 +58,10 @@ namespace OpenRa.Game renderer = new Renderer( this, GetResolution( settings ), windowed ); SheetBuilder.Initialize( renderer ); + var controller = new Controller( () => (Modifiers)(int)ModifierKeys ); /* a bit of insane input routing */ + Game.Initialize(settings.GetValue("map", "scm12ea.ini"), renderer, new int2(ClientSize), - settings.GetValue("player", 1), useAftermath); + settings.GetValue("player", 1), useAftermath, controller ); SequenceProvider.ForcePrecache(); @@ -79,18 +81,23 @@ namespace OpenRa.Game int2 lastPos; + void DispatchMouseInput(MouseInputEvent ev, MouseEventArgs e) + { + Game.viewport.DispatchMouseInput( + new MouseInput + { + Button = (MouseButton)(int)e.Button, + Event = ev, + Location = new int2(e.Location), + Modifiers = (Modifiers)(int)ModifierKeys, + }); + } + protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); lastPos = new int2(e.Location); - - Game.viewport.DispatchMouseInput(new MouseInput - { - Button = e.Button, - Event = MouseInputEvent.Down, - Location = new int2(e.Location), - Modifiers = ModifierKeys, - }); + DispatchMouseInput(MouseInputEvent.Down, e); } protected override void OnMouseMove(MouseEventArgs e) @@ -104,26 +111,13 @@ namespace OpenRa.Game lastPos = p; } - Game.viewport.DispatchMouseInput(new MouseInput - { - Button = e.Button, - Event = MouseInputEvent.Move, - Location = new int2(e.Location), - Modifiers = ModifierKeys, - }); + DispatchMouseInput(MouseInputEvent.Move, e); } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); - - Game.viewport.DispatchMouseInput(new MouseInput - { - Button = e.Button, - Event = MouseInputEvent.Up, - Location = new int2(e.Location), - Modifiers = ModifierKeys, - }); + DispatchMouseInput(MouseInputEvent.Up, e); } protected override void OnKeyDown(KeyEventArgs e) @@ -141,7 +135,7 @@ namespace OpenRa.Game if (!Game.chat.isChatting) if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) - Game.controller.DoControlGroup( (int)e.KeyCode - (int)Keys.D0, e.Modifiers ); + Game.controller.DoControlGroup( (int)e.KeyCode - (int)Keys.D0, (Modifiers)(int)e.Modifiers ); } protected override void OnKeyPress(KeyPressEventArgs e) @@ -155,12 +149,30 @@ namespace OpenRa.Game } } + [Flags] + enum MouseButton + { + None = (int)MouseButtons.None, + Left = (int)MouseButtons.Left, + Right = (int)MouseButtons.Right, + Middle = (int)MouseButtons.Middle, + } + + [Flags] + enum Modifiers + { + None = (int)Keys.None, + Shift = (int)Keys.Shift, + Alt = (int)Keys.Alt, + Ctrl = (int)Keys.Control, + } + struct MouseInput { public MouseInputEvent Event; public int2 Location; - public MouseButtons Button; - public Keys Modifiers; + public MouseButton Button; + public Modifiers Modifiers; } enum MouseInputEvent { Down, Move, Up }; diff --git a/OpenRa.Game/PlaceBuilding.cs b/OpenRa.Game/PlaceBuilding.cs index 309bb0dfda..f1a6e25840 100644 --- a/OpenRa.Game/PlaceBuilding.cs +++ b/OpenRa.Game/PlaceBuilding.cs @@ -14,9 +14,9 @@ namespace OpenRa.Game Building = (BuildingInfo)Rules.UnitInfo[ name ]; } - public IEnumerable Order(int2 xy, bool lmb) + public IEnumerable Order(int2 xy, MouseInput mi) { - if( lmb ) + if( mi.Button == MouseButton.Left ) { if( !Game.CanPlaceBuilding( Building, xy, null, true ) ) yield break; diff --git a/OpenRa.Game/Traits/AttackBase.cs b/OpenRa.Game/Traits/AttackBase.cs index 8543574d0a..6b3e6c387a 100644 --- a/OpenRa.Game/Traits/AttackBase.cs +++ b/OpenRa.Game/Traits/AttackBase.cs @@ -125,9 +125,9 @@ namespace OpenRa.Game.Traits return true; } - public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor) + public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor) { - if (lmb || underCursor == null) return null; + if (mi.Button == MouseButton.Left || underCursor == null) return null; if (underCursor.Owner == self.Owner) return null; if (!Combat.HasAnyValidWeapons(self, underCursor)) return null; return Order.Attack(self, underCursor); diff --git a/OpenRa.Game/Traits/Harvester.cs b/OpenRa.Game/Traits/Harvester.cs index 69edadd248..9426921f66 100644 --- a/OpenRa.Game/Traits/Harvester.cs +++ b/OpenRa.Game/Traits/Harvester.cs @@ -1,5 +1,4 @@ - -namespace OpenRa.Game.Traits +namespace OpenRa.Game.Traits { class Harvester : IOrder { @@ -25,9 +24,9 @@ namespace OpenRa.Game.Traits gemsCarried = 0; } - public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor) + public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor) { - if (lmb) return null; + if (mi.Button == MouseButton.Left) return null; if (underCursor != null && underCursor.Owner == self.Owner diff --git a/OpenRa.Game/Traits/Helicopter.cs b/OpenRa.Game/Traits/Helicopter.cs index 36a0bfa69d..f1438fb148 100644 --- a/OpenRa.Game/Traits/Helicopter.cs +++ b/OpenRa.Game/Traits/Helicopter.cs @@ -16,9 +16,9 @@ namespace OpenRa.Game.Traits targetLocation = self.Location; } - public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor) + public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor) { - if (lmb) return null; + if (mi.Button == MouseButton.Left) return null; if (underCursor == null) return Order.Move(self, xy); diff --git a/OpenRa.Game/Traits/McvDeploy.cs b/OpenRa.Game/Traits/McvDeploy.cs index 2858bdcf96..dfef831c25 100644 --- a/OpenRa.Game/Traits/McvDeploy.cs +++ b/OpenRa.Game/Traits/McvDeploy.cs @@ -6,9 +6,9 @@ namespace OpenRa.Game.Traits { public McvDeploy(Actor self) { } - public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor) + public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor) { - if (lmb) return null; + if (mi.Button == MouseButton.Left) return null; if( xy != self.Location ) return null; return Order.DeployMcv(self); diff --git a/OpenRa.Game/Traits/Mobile.cs b/OpenRa.Game/Traits/Mobile.cs index 0a0da91524..8b242b842c 100644 --- a/OpenRa.Game/Traits/Mobile.cs +++ b/OpenRa.Game/Traits/Mobile.cs @@ -28,9 +28,9 @@ namespace OpenRa.Game.Traits Game.UnitInfluence.Add(self, this); } - public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor) + public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor) { - if (lmb) return null; + if (mi.Button == MouseButton.Left) return null; if (underCursor != null) return null; if (xy == toCell) return null; return Order.Move(self, xy); diff --git a/OpenRa.Game/Traits/ProductionQueue.cs b/OpenRa.Game/Traits/ProductionQueue.cs index 18e35d35f0..5e122f645b 100755 --- a/OpenRa.Game/Traits/ProductionQueue.cs +++ b/OpenRa.Game/Traits/ProductionQueue.cs @@ -23,7 +23,7 @@ namespace OpenRa.Game.Traits p.Value.Tick( self.Owner ); } - public Order IssueOrder( Actor self, int2 xy, bool lmb, Actor underCursor ) + public Order IssueOrder( Actor self, int2 xy, MouseInput mi, Actor underCursor ) { // production isn't done by clicks in the world; the chrome handles it. return null; diff --git a/OpenRa.Game/Traits/RallyPoint.cs b/OpenRa.Game/Traits/RallyPoint.cs index 3d2a046e37..e55daad437 100644 --- a/OpenRa.Game/Traits/RallyPoint.cs +++ b/OpenRa.Game/Traits/RallyPoint.cs @@ -24,9 +24,9 @@ namespace OpenRa.Game.Traits anim.Image, Util.CenterOfCell(rallyPoint)); } - public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor) + public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor) { - if (lmb || underCursor != null) return null; + if (mi.Button == MouseButton.Left || underCursor != null) return null; return Order.SetRallyPoint(self, xy); } diff --git a/OpenRa.Game/Traits/TraitsInterfaces.cs b/OpenRa.Game/Traits/TraitsInterfaces.cs index 504578fefa..13540c39c6 100644 --- a/OpenRa.Game/Traits/TraitsInterfaces.cs +++ b/OpenRa.Game/Traits/TraitsInterfaces.cs @@ -13,7 +13,7 @@ namespace OpenRa.Game.Traits interface INotifyBuildComplete { void BuildingComplete (Actor self); } interface IOrder { - Order IssueOrder( Actor self, int2 xy, bool lmb, Actor underCursor ); + Order IssueOrder( Actor self, int2 xy, MouseInput mi, Actor underCursor ); void ResolveOrder( Actor self, Order order ); } interface IProducer { bool Produce( Actor self, UnitInfo producee ); } diff --git a/OpenRa.Game/UnitOrderGenerator.cs b/OpenRa.Game/UnitOrderGenerator.cs index fe11c77ebd..8885899db3 100755 --- a/OpenRa.Game/UnitOrderGenerator.cs +++ b/OpenRa.Game/UnitOrderGenerator.cs @@ -13,11 +13,11 @@ namespace OpenRa.Game selection = selected.ToList(); } - public IEnumerable Order( int2 xy, bool lmb ) + public IEnumerable Order( int2 xy, MouseInput mi ) { foreach( var unit in selection ) { - var ret = unit.Order( xy, lmb ); + var ret = unit.Order( xy, mi ); if( ret != null ) yield return ret; }