From 2248320af71d09dc68ccac4a8edd0e8b515f6b29 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 25 Jul 2010 17:45:53 +1200 Subject: [PATCH] Move Game.Controller.HandleInput into a widget; works but is hacky --- OpenRA.Game/Controller.cs | 64 +------------- OpenRA.Game/Graphics/Viewport.cs | 22 +---- OpenRA.Game/OpenRA.Game.csproj | 1 + .../Widgets/DefaultInputControllerWidget.cs | 87 +++++++++++++++++++ OpenRA.Game/Widgets/RadarBinWidget.cs | 4 +- OpenRA.Game/Widgets/Widget.cs | 4 +- mods/cnc/chrome/ingame.yaml | 5 ++ mods/cnc/mod.yaml | 2 +- mods/ra/chrome/ingame.yaml | 5 ++ mods/ra/mod.yaml | 2 +- 10 files changed, 110 insertions(+), 86 deletions(-) create mode 100644 OpenRA.Game/Widgets/DefaultInputControllerWidget.cs diff --git a/OpenRA.Game/Controller.cs b/OpenRA.Game/Controller.cs index 7563fe922c..28f34a362b 100644 --- a/OpenRA.Game/Controller.cs +++ b/OpenRA.Game/Controller.cs @@ -17,7 +17,7 @@ using OpenRA.Traits; namespace OpenRA { - public class Controller : IHandleInput + public class Controller { public IOrderGenerator orderGenerator = new UnitOrderGenerator(); public Selection selection = new Selection(); @@ -38,7 +38,7 @@ namespace OpenRA } } - void ApplyOrders(World world, float2 xy, MouseInput mi) + public void ApplyOrders(World world, float2 xy, MouseInput mi) { if (orderGenerator == null) return; @@ -61,40 +61,7 @@ namespace OpenRA } } - float2 dragStart, dragEnd; - public bool HandleInput(World world, MouseInput mi) - { - var xy = Game.viewport.ViewToWorld(mi); - - if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Down) - { - dragStart = dragEnd = xy; - ApplyOrders(world, xy, mi); - } - - if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Move) - dragEnd = xy; - - if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Up) - { - if (orderGenerator is UnitOrderGenerator) - { - var newSelection = world.SelectActorsInBox(Game.CellSize * dragStart, Game.CellSize * xy); - selection.Combine(world, newSelection, mi.Modifiers.HasModifier(Modifiers.Shift), dragStart == xy); - } - - dragStart = dragEnd = xy; - } - - if (mi.Button == MouseButton.None && mi.Event == MouseInputEvent.Move) - dragStart = dragEnd = xy; - - if (mi.Button == MouseButton.Right && mi.Event == MouseInputEvent.Down) - ApplyOrders(world, xy, mi); - - return true; - } - + public float2 dragStart, dragEnd; public Pair? SelectionBox { get @@ -107,31 +74,6 @@ namespace OpenRA public float2 MousePosition { get { return dragEnd; } } Modifiers modifiers; - public string ChooseCursor( World world ) - { - int sync = world.SyncHash(); - - try - { - if (!world.GameHasStarted) - return "default"; - - var mi = new MouseInput - { - Location = ( Game.CellSize * MousePosition - Game.viewport.Location ).ToInt2(), - Button = MouseButton.Right, - Modifiers = modifiers - }; - - return orderGenerator.GetCursor( world, MousePosition.ToInt2(), mi ); - } - finally - { - if( sync != world.SyncHash() ) - throw new InvalidOperationException( "Desync in Controller.ChooseCursor" ); - } - } - public void SetModifiers(Modifiers mods) { modifiers = mods; } public Modifiers GetModifiers() { return modifiers; } diff --git a/OpenRA.Game/Graphics/Viewport.cs b/OpenRA.Game/Graphics/Viewport.cs index 1f7330b76c..3b7adbe3d9 100644 --- a/OpenRA.Game/Graphics/Viewport.cs +++ b/OpenRA.Game/Graphics/Viewport.cs @@ -17,11 +17,6 @@ using OpenRA.Widgets; namespace OpenRA.Graphics { - interface IHandleInput - { - bool HandleInput(World world, MouseInput mi); - } - class Viewport { readonly float2 screenSize; @@ -41,8 +36,6 @@ namespace OpenRA.Graphics scrollPosition = scrollPosition + delta; } - public IEnumerable regions { get { return new IHandleInput[] { Widget.RootWidget, Game.controller }; } } - public Viewport(float2 screenSize, int2 mapStart, int2 mapEnd, Renderer renderer) { this.screenSize = screenSize; @@ -68,7 +61,7 @@ namespace OpenRA.Graphics Widget.DoDraw(world); Timer.Time( "widgets: {0}" ); - var cursorName = Widget.RootWidget.GetCursorOuter(mousePos) ?? Game.controller.ChooseCursor( world ); + var cursorName = Widget.RootWidget.GetCursorOuter(mousePos) ?? "default"; var c = new Cursor(cursorName); c.Draw((int)cursorFrame, mousePos + Location); Timer.Time( "cursors: {0}" ); @@ -86,21 +79,12 @@ namespace OpenRA.Graphics cursorFrame += 0.5f; } - IHandleInput dragRegion = null; public void DispatchMouseInput(World world, MouseInput mi) { if (mi.Event == MouseInputEvent.Move) mousePos = mi.Location; - - if (dragRegion != null) { - dragRegion.HandleInput( world, mi ); - if (mi.Event == MouseInputEvent.Up) dragRegion = null; - return; - } - - dragRegion = regions.FirstOrDefault(r => r.HandleInput(world, mi)); - if (mi.Event != MouseInputEvent.Down) - dragRegion = null; + + Widget.HandleInput(world, mi); } public float2 ViewToWorld(MouseInput mi) diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 7692185abb..457ad480f5 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -228,6 +228,7 @@ + diff --git a/OpenRA.Game/Widgets/DefaultInputControllerWidget.cs b/OpenRA.Game/Widgets/DefaultInputControllerWidget.cs new file mode 100644 index 0000000000..893a461517 --- /dev/null +++ b/OpenRA.Game/Widgets/DefaultInputControllerWidget.cs @@ -0,0 +1,87 @@ +#region Copyright & License Information +/* + * Copyright 2007-2010 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see LICENSE. + */ +#endregion + +using System; +using OpenRA.Orders; + +namespace OpenRA.Widgets +{ + class DefaultInputControllerWidget : Widget + { + public DefaultInputControllerWidget() : base() {} + protected DefaultInputControllerWidget(DefaultInputControllerWidget widget) : base(widget) {} + public override void DrawInner( World world ) { } + + // TODO: need a mechanism to say "i'll only handle this info if NOTHING else has" + // For now, ensure that this widget recieves the input last or it will eat it + float2 dragStart, dragEnd; + public override bool HandleInputInner(MouseInput mi) + { + var xy = Game.viewport.ViewToWorld(mi); + var world = Game.world; + if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Down) + { + dragStart = dragEnd = xy; + Game.controller.ApplyOrders(world, xy, mi); + } + + if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Move) + dragEnd = xy; + + if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Up) + { + if (Game.controller.orderGenerator is UnitOrderGenerator) + { + var newSelection = Game.world.SelectActorsInBox(Game.CellSize * dragStart, Game.CellSize * xy); + Game.controller.selection.Combine(world, newSelection, mi.Modifiers.HasModifier(Modifiers.Shift), dragStart == xy); + } + + dragStart = dragEnd = xy; + } + + if (mi.Button == MouseButton.None && mi.Event == MouseInputEvent.Move) + dragStart = dragEnd = xy; + + if (mi.Button == MouseButton.Right && mi.Event == MouseInputEvent.Down) + Game.controller.ApplyOrders(world, xy, mi); + + Game.controller.dragStart = dragStart; + Game.controller.dragEnd = dragEnd; + return true; + } + + public override string GetCursor(int2 pos) + { + var world = Game.world; + int sync = world.SyncHash(); + try + { + if (!world.GameHasStarted) + return "default"; + + var mi = new MouseInput + { + Location = pos, + Button = MouseButton.Right, + Modifiers = Game.controller.GetModifiers() + }; + + return Game.controller.orderGenerator.GetCursor( world, Game.viewport.ViewToWorld(mi).ToInt2(), mi ); + } + finally + { + if( sync != world.SyncHash() ) + throw new InvalidOperationException( "Desync in InputControllerWidget.GetCursor" ); + } + } + + public override Widget Clone() { return new DefaultInputControllerWidget(this); } + } +} \ No newline at end of file diff --git a/OpenRA.Game/Widgets/RadarBinWidget.cs b/OpenRA.Game/Widgets/RadarBinWidget.cs index a0207de506..eccdcc56d4 100644 --- a/OpenRA.Game/Widgets/RadarBinWidget.cs +++ b/OpenRA.Game/Widgets/RadarBinWidget.cs @@ -104,10 +104,10 @@ namespace OpenRA.Widgets Location = (loc * Game.CellSize - Game.viewport.Location).ToInt2() }; - Game.controller.HandleInput(Game.world, fakemi); + Widget.HandleInput(Game.world, fakemi); fakemi.Event = MouseInputEvent.Up; - Game.controller.HandleInput(Game.world, fakemi); + Widget.HandleInput(Game.world, fakemi); } return true; diff --git a/OpenRA.Game/Widgets/Widget.cs b/OpenRA.Game/Widgets/Widget.cs index 4b032b549c..d40033f289 100644 --- a/OpenRA.Game/Widgets/Widget.cs +++ b/OpenRA.Game/Widgets/Widget.cs @@ -17,7 +17,7 @@ using OpenRA.Graphics; namespace OpenRA.Widgets { - public abstract class Widget : IHandleInput + public abstract class Widget { // Info defined in YAML public string Id = null; @@ -205,7 +205,7 @@ namespace OpenRA.Widgets public static int TicksSinceLastMove = 0; public static int2 LastMousePos; - public bool HandleInput(World world, MouseInput mi) + public static bool HandleInput(World world, MouseInput mi) { if (SelectedWidget != null && SelectedWidget.HandleMouseInputOuter(mi)) return true; diff --git a/mods/cnc/chrome/ingame.yaml b/mods/cnc/chrome/ingame.yaml index 61e3dda39a..6915fc8a54 100644 --- a/mods/cnc/chrome/ingame.yaml +++ b/mods/cnc/chrome/ingame.yaml @@ -5,6 +5,11 @@ Container@ROOT: Delegate:IngameChromeDelegate Visible:false Children: + DefaultInputController: + X:0 + Y:0 + Width:WINDOW_RIGHT + Height:WINDOW_BOTTOM Timer@GAME_TIMER: Id:GAME_TIMER X: WINDOW_RIGHT/2 diff --git a/mods/cnc/mod.yaml b/mods/cnc/mod.yaml index 4c1f67c0db..226f45882c 100644 --- a/mods/cnc/mod.yaml +++ b/mods/cnc/mod.yaml @@ -40,11 +40,11 @@ Assemblies: mods/cnc/OpenRA.Mods.Cnc.dll: Cnc mod traits ChromeLayout: + mods/cnc/chrome/ingame.yaml: mods/cnc/chrome/mainmenu.yaml: mods/cnc/chrome/settings.yaml: mods/cnc/chrome/gamelobby.yaml: mods/cnc/chrome/serverbrowser.yaml: - mods/cnc/chrome/ingame.yaml: Weapons: mods/cnc/weapons.yaml: diff --git a/mods/ra/chrome/ingame.yaml b/mods/ra/chrome/ingame.yaml index f80de4bdb7..69aa7455be 100644 --- a/mods/ra/chrome/ingame.yaml +++ b/mods/ra/chrome/ingame.yaml @@ -5,6 +5,11 @@ Container@ROOT: Delegate:IngameChromeDelegate Visible:false Children: + DefaultInputController: + X:0 + Y:0 + Width:WINDOW_RIGHT + Height:WINDOW_BOTTOM Timer@GAME_TIMER: Id:GAME_TIMER X: WINDOW_RIGHT/2 diff --git a/mods/ra/mod.yaml b/mods/ra/mod.yaml index 6145239320..a6a1daf14b 100644 --- a/mods/ra/mod.yaml +++ b/mods/ra/mod.yaml @@ -39,11 +39,11 @@ Assemblies: mods/ra/OpenRA.Mods.RA.dll: Traits used ChromeLayout: + mods/ra/chrome/ingame.yaml: mods/ra/chrome/mainmenu.yaml: mods/ra/chrome/settings.yaml: mods/ra/chrome/gamelobby.yaml: mods/ra/chrome/serverbrowser.yaml: - mods/ra/chrome/ingame.yaml: Weapons: mods/ra/weapons.yaml