Clean up ViewportScrollControllerWidget.

This commit is contained in:
Paul Chote
2013-07-28 18:52:08 +12:00
parent 40c6264aca
commit 6722db20e3

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* Copyright 2007-2013 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,
@@ -20,12 +20,41 @@ namespace OpenRA.Widgets
public int EdgeScrollThreshold = 15;
public int EdgeCornerScrollThreshold = 35;
ScrollDirection Keyboard;
ScrollDirection Edge;
static readonly Dictionary<ScrollDirection, string> ScrollCursors = new Dictionary<ScrollDirection, string>
{
{ ScrollDirection.Up | ScrollDirection.Left, "scroll-tl" },
{ ScrollDirection.Up | ScrollDirection.Right, "scroll-tr" },
{ ScrollDirection.Down | ScrollDirection.Left, "scroll-bl" },
{ ScrollDirection.Down | ScrollDirection.Right, "scroll-br" },
{ ScrollDirection.Up, "scroll-t" },
{ ScrollDirection.Down, "scroll-b" },
{ ScrollDirection.Left, "scroll-l" },
{ ScrollDirection.Right, "scroll-r" },
};
public ViewportScrollControllerWidget() : base() { }
protected ViewportScrollControllerWidget(ViewportScrollControllerWidget widget)
: base(widget) { }
static readonly Dictionary<ScrollDirection, float2> ScrollOffsets = new Dictionary<ScrollDirection, float2>
{
{ ScrollDirection.Up, new float2(0, -1) },
{ ScrollDirection.Down, new float2(0, 1) },
{ ScrollDirection.Left, new float2(-1, 0) },
{ ScrollDirection.Right, new float2(1, 0) },
};
ScrollDirection keyboardDirections;
ScrollDirection edgeDirections;
public static string GetScrollCursor(Widget w, ScrollDirection edge, int2 pos)
{
if (!Game.Settings.Game.ViewportEdgeScroll || Ui.MouseOverWidget != w)
return null;
var blockedDirections = Game.viewport.GetBlockedDirections();
foreach (var dir in ScrollCursors)
if (edge.Includes(dir.Key))
return dir.Value + (blockedDirections.Includes(dir.Key) ? "-blocked" : "");
return null;
}
public override bool HandleMouseInput(MouseInput mi)
{
@@ -40,41 +69,15 @@ namespace OpenRA.Widgets
Game.viewport.Scroll((Viewport.LastMousePos - mi.Location) * d);
return true;
}
return false;
}
static readonly Dictionary<ScrollDirection, string> directions = new Dictionary<ScrollDirection, string>
{
{ ScrollDirection.Up | ScrollDirection.Left, "scroll-tl" },
{ ScrollDirection.Up | ScrollDirection.Right, "scroll-tr" },
{ ScrollDirection.Down | ScrollDirection.Left, "scroll-bl" },
{ ScrollDirection.Down | ScrollDirection.Right, "scroll-br" },
{ ScrollDirection.Up, "scroll-t" },
{ ScrollDirection.Down, "scroll-b" },
{ ScrollDirection.Left, "scroll-l" },
{ ScrollDirection.Right, "scroll-r" },
};
public static string GetScrollCursor(Widget w, ScrollDirection edge, int2 pos)
{
if (!Game.Settings.Game.ViewportEdgeScroll || Ui.MouseOverWidget != w)
return null;
var blockedDirections = Game.viewport.GetBlockedDirections();
foreach (var dir in directions)
if (edge.Includes(dir.Key))
return dir.Value + (blockedDirections.Includes(dir.Key) ? "-blocked" : "");
return null;
}
public override string GetCursor(int2 pos) { return GetScrollCursor(this, Edge, pos); }
public override string GetCursor(int2 pos) { return GetScrollCursor(this, edgeDirections, pos); }
public override bool YieldKeyboardFocus()
{
Keyboard = ScrollDirection.None;
keyboardDirections = ScrollDirection.None;
return base.YieldKeyboardFocus();
}
@@ -82,91 +85,49 @@ namespace OpenRA.Widgets
{
switch (e.KeyName)
{
case "up": Keyboard = Keyboard.Set(ScrollDirection.Up, e.Event == KeyInputEvent.Down); return true;
case "down": Keyboard = Keyboard.Set(ScrollDirection.Down, e.Event == KeyInputEvent.Down); return true;
case "left": Keyboard = Keyboard.Set(ScrollDirection.Left, e.Event == KeyInputEvent.Down); return true;
case "right": Keyboard = Keyboard.Set(ScrollDirection.Right, e.Event == KeyInputEvent.Down); return true;
case "up": keyboardDirections = keyboardDirections.Set(ScrollDirection.Up, e.Event == KeyInputEvent.Down); return true;
case "down": keyboardDirections = keyboardDirections.Set(ScrollDirection.Down, e.Event == KeyInputEvent.Down); return true;
case "left": keyboardDirections = keyboardDirections.Set(ScrollDirection.Left, e.Event == KeyInputEvent.Down); return true;
case "right": keyboardDirections = keyboardDirections.Set(ScrollDirection.Right, e.Event == KeyInputEvent.Down); return true;
}
return false;
}
public override void Tick()
{
Edge = ScrollDirection.None;
edgeDirections = ScrollDirection.None;
if (Game.Settings.Game.ViewportEdgeScroll && Game.HasInputFocus)
edgeDirections = CheckForDirections();
if (keyboardDirections != ScrollDirection.None || edgeDirections != ScrollDirection.None)
{
Edge = CheckForDirections();
}
Scroll();
}
var scroll = float2.Zero;
ScrollDirection CheckForDirections()
{
// First let's check if the mouse is on the corners:
if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeCornerScrollThreshold &&
Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeCornerScrollThreshold) //Bottom Right
{
return ScrollDirection.Right | ScrollDirection.Down;
}
else if (Viewport.LastMousePos.X < EdgeCornerScrollThreshold &&
Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeCornerScrollThreshold) //Bottom Left
{
return ScrollDirection.Down | ScrollDirection.Left;
}
foreach (var kv in ScrollOffsets)
if (keyboardDirections.Includes(kv.Key) || edgeDirections.Includes(kv.Key))
scroll += kv.Value;
else if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeCornerScrollThreshold &&
Viewport.LastMousePos.Y < EdgeCornerScrollThreshold) //Top Right
{
return ScrollDirection.Right | ScrollDirection.Up;
}
else if (Viewport.LastMousePos.X < EdgeCornerScrollThreshold &&
Viewport.LastMousePos.Y < EdgeCornerScrollThreshold) //Top Left
{
return ScrollDirection.Left | ScrollDirection.Up;
}
//Check for corner ends here now let's check the edges:
// Check for edge-scroll
if (Viewport.LastMousePos.X < EdgeScrollThreshold)
return ScrollDirection.Left;
if (Viewport.LastMousePos.Y < EdgeScrollThreshold)
return ScrollDirection.Up;
if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeScrollThreshold)
return ScrollDirection.Right;
if (Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeScrollThreshold)
return ScrollDirection.Down;
//Check for edge-scroll ends here.If none of above then return none.
return ScrollDirection.None;
}
void Scroll()
{
if (Keyboard != ScrollDirection.None || Edge != ScrollDirection.None)
{
var scroll = new float2(0, 0);
if (Keyboard.Includes(ScrollDirection.Up) || Edge.Includes(ScrollDirection.Up))
scroll += new float2(0, -1);
if (Keyboard.Includes(ScrollDirection.Right) || Edge.Includes(ScrollDirection.Right))
scroll += new float2(1, 0);
if (Keyboard.Includes(ScrollDirection.Down) || Edge.Includes(ScrollDirection.Down))
scroll += new float2(0, 1);
if (Keyboard.Includes(ScrollDirection.Left) || Edge.Includes(ScrollDirection.Left))
scroll += new float2(-1, 0);
float length = Math.Max(1, scroll.Length);
scroll.X = (scroll.X / length) * Game.Settings.Game.ViewportEdgeScrollStep;
scroll.Y = (scroll.Y / length) * Game.Settings.Game.ViewportEdgeScrollStep;
var length = Math.Max(1, scroll.Length);
scroll *= (1f / length) * Game.Settings.Game.ViewportEdgeScrollStep;
Game.viewport.Scroll(scroll);
}
}
public override Widget Clone() { return new ViewportScrollControllerWidget(this); }
ScrollDirection CheckForDirections()
{
var directions = ScrollDirection.None;
if (Viewport.LastMousePos.X < EdgeScrollThreshold)
directions |= ScrollDirection.Left;
if (Viewport.LastMousePos.Y < EdgeScrollThreshold)
directions |= ScrollDirection.Up;
if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeScrollThreshold)
directions |= ScrollDirection.Right;
if (Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeScrollThreshold)
directions |= ScrollDirection.Down;
return directions;
}
}
}