diff --git a/.gitignore b/.gitignore index 250beacab8..168a3f1d19 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ obj *.ncb *.vcproj* *.suo +*.user # Red Alert binary files *.[mM][iI][xX] diff --git a/OpenRa.DataStructures/int2.cs b/OpenRa.DataStructures/int2.cs index 5aab4b0a71..5891005684 100644 --- a/OpenRa.DataStructures/int2.cs +++ b/OpenRa.DataStructures/int2.cs @@ -36,5 +36,6 @@ namespace OpenRa public static readonly int2 Zero = new int2(0, 0); public Point ToPoint() { return new Point(X, Y); } + public PointF ToPointF() { return new PointF(X, Y); } } } diff --git a/OpenRa.Game/Controller.cs b/OpenRa.Game/Controller.cs index 7a3a8b31f4..07f1f26a3d 100644 --- a/OpenRa.Game/Controller.cs +++ b/OpenRa.Game/Controller.cs @@ -17,12 +17,16 @@ namespace OpenRa.Game this.game = game; } - public void WorldClicked(object sender, MouseEventArgs e) + public void HandleMouseInput(MouseInput mi) { - var xy = (1 / 24.0f) * (new float2(e.Location) + game.viewport.Location); - if (orderGenerator != null) - orderGenerator.Order(game, new int2((int)xy.X, (int)xy.Y)).Apply(game); - // todo: route all orders through netcode + if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Down) + { + var xy = (1 / 24.0f) * (new float2(mi.Location.X, mi.Location.Y) + game.viewport.Location); + + if (orderGenerator != null) + orderGenerator.Order(game, new int2((int)xy.X, (int)xy.Y)).Apply(game); + // todo: route all orders through netcode + } } } } diff --git a/OpenRa.Game/Graphics/Region.cs b/OpenRa.Game/Graphics/Region.cs index 6a35060fc0..093ab919fc 100644 --- a/OpenRa.Game/Graphics/Region.cs +++ b/OpenRa.Game/Graphics/Region.cs @@ -6,18 +6,18 @@ namespace OpenRa.Game.Graphics { class Region { - float2 location; + int2 location; Viewport viewport; - public float2 Location + public int2 Location { - get { return location + viewport.Location; } // WTF HACK HACK HACK + get { return location + new int2( (int)viewport.Location.X, (int)viewport.Location.Y ); } // WTF HACK HACK HACK } public readonly float2 Size; Action drawFunction; - MouseEventHandler mouseHandler; + Action mouseHandler; Rectangle rect; static int2 MakeSize(Viewport v, DockStyle d, int size) @@ -37,12 +37,19 @@ namespace OpenRa.Game.Graphics } } - public void Clicked(MouseEventArgs e) - { - mouseHandler(this, new MouseEventArgs(e.Button, e.Clicks, e.X - rect.Left, e.Y - rect.Top, e.Delta)); - } + public bool HandleMouseInput(MouseInput mi) + { + /* todo: route to the mousehandler once that's sorted */ + if (mouseHandler != null) mouseHandler(new MouseInput + { + Button = mi.Button, + Event = mi.Event, + Location = mi.Location - Location + }); + return mouseHandler != null; + } - public static Region Create(Viewport v, DockStyle d, int size, Action f, MouseEventHandler m) + public static Region Create(Viewport v, DockStyle d, int size, Action f, Action m) { int2 s = MakeSize(v, d, size); @@ -61,7 +68,7 @@ namespace OpenRa.Game.Graphics } } - Region(int2 location, int2 size, Viewport viewport, Action drawFunction, MouseEventHandler mouseHandler) + Region(int2 location, int2 size, Viewport viewport, Action drawFunction, Action mouseHandler) { this.location = location; this.Size = size; diff --git a/OpenRa.Game/Graphics/TerrainRenderer.cs b/OpenRa.Game/Graphics/TerrainRenderer.cs index cb6dca3f60..079be6f0ce 100644 --- a/OpenRa.Game/Graphics/TerrainRenderer.cs +++ b/OpenRa.Game/Graphics/TerrainRenderer.cs @@ -20,7 +20,7 @@ namespace OpenRa.Game.Graphics public TerrainRenderer(Renderer renderer, Map map, Viewport viewport) { this.renderer = renderer; - region = Region.Create(viewport, DockStyle.Left, viewport.Width - 128, Draw, (_, e) => { } ); + region = Region.Create(viewport, DockStyle.Left, viewport.Width - 128, Draw, null ); viewport.AddRegion(region); this.map = map; diff --git a/OpenRa.Game/Graphics/Viewport.cs b/OpenRa.Game/Graphics/Viewport.cs index 1bc83e6b78..30f601967f 100644 --- a/OpenRa.Game/Graphics/Viewport.cs +++ b/OpenRa.Game/Graphics/Viewport.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System; +using System.Linq; namespace OpenRa.Game.Graphics { @@ -44,6 +46,18 @@ namespace OpenRa.Game.Graphics renderer.EndFrame(); } - public IEnumerable Regions { get { return regions; } } + Region dragRegion = null; + public void DispatchMouseInput(MouseInput mi) + { + if (dragRegion != null) { + dragRegion.HandleMouseInput( mi ); + if (mi.Event == MouseInputEvent.Up) dragRegion = null; + return; + } + + dragRegion = regions.FirstOrDefault(r => r.Contains(mi.Location) && r.HandleMouseInput(mi)); + if (mi.Event != MouseInputEvent.Down) + dragRegion = null; + } } } diff --git a/OpenRa.Game/Graphics/WorldRenderer.cs b/OpenRa.Game/Graphics/WorldRenderer.cs index d060b4bbae..8c46ad7fda 100644 --- a/OpenRa.Game/Graphics/WorldRenderer.cs +++ b/OpenRa.Game/Graphics/WorldRenderer.cs @@ -15,7 +15,9 @@ namespace OpenRa.Game.Graphics // TODO: this is layout policy. it belongs at a higher level than this. region = Region.Create(world.game.viewport, DockStyle.Left, - world.game.viewport.Width - 128, Draw, world.game.controller.WorldClicked); // TODO: world.WorldClicked is part of the CONTROLLER + world.game.viewport.Width - 128, Draw, + world.game.controller.HandleMouseInput); + world.game.viewport.AddRegion(region); spriteRenderer = new SpriteRenderer(renderer, true); diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 8b3c163eb0..37c869ecdd 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -66,16 +66,18 @@ namespace OpenRa.Game int2 lastPos; - protected override void OnMouseDown(MouseEventArgs e) - { - base.OnMouseDown(e); - lastPos = new int2(e.Location); + protected override void OnMouseDown(MouseEventArgs e) + { + base.OnMouseDown(e); + lastPos = new int2(e.Location); - if (e.Button == MouseButtons.Left) - foreach (var region in game.viewport.Regions) - if (region.Contains(lastPos)) - region.Clicked(e); - } + game.viewport.DispatchMouseInput(new MouseInput + { + Button = e.Button, + Event = MouseInputEvent.Down, + Location = new int2(e.Location) + }); + } protected override void OnMouseMove(MouseEventArgs e) { @@ -88,9 +90,37 @@ namespace OpenRa.Game lastPos = p; } + game.viewport.DispatchMouseInput(new MouseInput + { + Button = e.Button, + Event = MouseInputEvent.Move, + Location = new int2(e.Location) + }); + if (game.controller.orderGenerator != null) game.controller.orderGenerator.PrepareOverlay(game, new int2(e.Location.X / 24, e.Location.Y / 24)); } + + 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) + }); + } } + + struct MouseInput + { + public MouseInputEvent Event; + public int2 Location; + public MouseButtons Button; + } + + enum MouseInputEvent { Down, Move, Up }; } diff --git a/OpenRa.Game/Sidebar.cs b/OpenRa.Game/Sidebar.cs index 465776a8e2..6802ca2493 100644 --- a/OpenRa.Game/Sidebar.cs +++ b/OpenRa.Game/Sidebar.cs @@ -123,10 +123,13 @@ namespace OpenRa.Game return null; } - void MouseHandler(object sender, MouseEventArgs e) + void MouseHandler(MouseInput mi) { - float2 point = new float2(e.Location); - Build(GetItem(point)); + if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Down) + { + var point = new float2(mi.Location.X, mi.Location.Y); + Build(GetItem(point)); + } } }