Move Game.Controller.HandleInput into a widget; works but is hacky

This commit is contained in:
Paul Chote
2010-07-25 17:45:53 +12:00
parent f61421edd0
commit 2248320af7
10 changed files with 110 additions and 86 deletions

View File

@@ -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<float2, float2>? 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; }

View File

@@ -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<IHandleInput> 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)

View File

@@ -228,6 +228,7 @@
<Compile Include="Widgets\TimerWidget.cs" />
<Compile Include="Widgets\ShpImageWidget.cs" />
<Compile Include="Widgets\OrderButtonWidget.cs" />
<Compile Include="Widgets\DefaultInputControllerWidget.cs" />
<Compile Include="Traits\DrawLineToTarget.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -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); }
}
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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

View File

@@ -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:

View File

@@ -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

View File

@@ -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