unsnarled the input a bit
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,6 +5,7 @@ obj
|
||||
*.ncb
|
||||
*.vcproj*
|
||||
*.suo
|
||||
*.user
|
||||
|
||||
# Red Alert binary files
|
||||
*.[mM][iI][xX]
|
||||
|
||||
@@ -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); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 (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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<MouseInput> 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)
|
||||
public bool HandleMouseInput(MouseInput mi)
|
||||
{
|
||||
mouseHandler(this, new MouseEventArgs(e.Button, e.Clicks, e.X - rect.Left, e.Y - rect.Top, e.Delta));
|
||||
/* 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<MouseInput> 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<MouseInput> mouseHandler)
|
||||
{
|
||||
this.location = location;
|
||||
this.Size = size;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<Region> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -71,10 +71,12 @@ namespace OpenRa.Game
|
||||
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 };
|
||||
}
|
||||
|
||||
@@ -123,12 +123,15 @@ namespace OpenRa.Game
|
||||
return null;
|
||||
}
|
||||
|
||||
void MouseHandler(object sender, MouseEventArgs e)
|
||||
void MouseHandler(MouseInput mi)
|
||||
{
|
||||
float2 point = new float2(e.Location);
|
||||
if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Down)
|
||||
{
|
||||
var point = new float2(mi.Location.X, mi.Location.Y);
|
||||
Build(GetItem(point));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PlaceBuilding : IOrderGenerator
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user