Refactor existing hodgepodge of hardcoded mouse/keyboard events into DefaultInputController.

This commit is contained in:
Paul Chote
2010-07-25 18:12:02 +12:00
parent 2248320af7
commit eac49ca641
10 changed files with 115 additions and 118 deletions

View File

@@ -9,7 +9,9 @@
#endregion
using System;
using System.Linq;
using OpenRA.Orders;
using OpenRA.Traits;
namespace OpenRA.Widgets
{
@@ -18,7 +20,12 @@ namespace OpenRA.Widgets
public DefaultInputControllerWidget() : base() {}
protected DefaultInputControllerWidget(DefaultInputControllerWidget widget) : base(widget) {}
public override void DrawInner( World world ) { }
static internal bool scrollUp = false;
static internal bool scrollDown = false;
static internal bool scrollLeft = false;
static internal bool scrollRight = false;
// 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;
@@ -45,6 +52,11 @@ namespace OpenRA.Widgets
dragStart = dragEnd = xy;
}
if (mi.Event == MouseInputEvent.Move &&
(mi.Button == MouseButton.Middle || mi.Button == (MouseButton.Left | MouseButton.Right)))
Game.viewport.Scroll(Widget.LastMousePos - mi.Location);
if (mi.Button == MouseButton.None && mi.Event == MouseInputEvent.Move)
dragStart = dragEnd = xy;
@@ -82,6 +94,79 @@ namespace OpenRA.Widgets
}
}
public override bool LoseFocus (MouseInput mi)
{
scrollUp = scrollDown = scrollLeft = scrollRight = false;
return base.LoseFocus(mi);
}
public override bool HandleKeyPressInner(KeyInput e)
{
// Take the input if *nothing* else is focused
if (!Focused && Widget.SelectedWidget != null)
return false;
if (e.Event == KeyInputEvent.Down)
{
switch (e.KeyName)
{
case "up": scrollUp = true; break;
case "down": scrollDown = true; break;
case "left": scrollLeft = true; break;
case "right": scrollRight = true; break;
}
if (e.KeyName.Length == 1 && char.IsDigit(e.KeyName[0]))
Game.controller.selection.DoControlGroup(Game.world, e.KeyName[0] - '0', e.Modifiers);
if (e.KeyChar == 08)
GotoNextBase();
}
else
{
switch (e.KeyName)
{
case "up": scrollUp = false; break;
case "down": scrollDown = false; break;
case "left": scrollLeft = false; break;
case "right": scrollRight = false; break;
}
}
return true;
}
public override void Tick(World world)
{
if (scrollUp == true)
Game.viewport.Scroll(new float2(0, -10));
if (scrollRight == true)
Game.viewport.Scroll(new float2(10, 0));
if (scrollDown == true)
Game.viewport.Scroll(new float2(0, 10));
if (scrollLeft == true)
Game.viewport.Scroll(new float2(-10, 0));
}
public void GotoNextBase()
{
var bases = Game.world.Queries.OwnedBy[Game.world.LocalPlayer].WithTrait<BaseBuilding>().ToArray();
if (!bases.Any()) return;
var next = bases
.Select( b => b.Actor )
.SkipWhile(b => Game.controller.selection.Actors.Contains(b))
.Skip(1)
.FirstOrDefault();
if (next == null)
next = bases.Select(b => b.Actor).First();
Game.controller.selection.Combine(Game.world, new Actor[] { next }, false, true);
Game.viewport.Center(Game.controller.selection.Actors);
}
public override Widget Clone() { return new DefaultInputControllerWidget(this); }
}
}