Spaces -> Tabs in ViewportScrollControllerWidget.

This commit is contained in:
Paul Chote
2013-07-28 18:39:59 +12:00
parent 7c91d6976d
commit 40c6264aca

View File

@@ -15,35 +15,35 @@ using OpenRA.Graphics;
namespace OpenRA.Widgets namespace OpenRA.Widgets
{ {
public class ViewportScrollControllerWidget : Widget public class ViewportScrollControllerWidget : Widget
{ {
public int EdgeScrollThreshold = 15; public int EdgeScrollThreshold = 15;
public int EdgeCornerScrollThreshold = 35; public int EdgeCornerScrollThreshold = 35;
ScrollDirection Keyboard; ScrollDirection Keyboard;
ScrollDirection Edge; ScrollDirection Edge;
public ViewportScrollControllerWidget() : base() { } public ViewportScrollControllerWidget() : base() { }
protected ViewportScrollControllerWidget(ViewportScrollControllerWidget widget) protected ViewportScrollControllerWidget(ViewportScrollControllerWidget widget)
: base(widget) { } : base(widget) { }
public override bool HandleMouseInput(MouseInput mi) public override bool HandleMouseInput(MouseInput mi)
{ {
var scrolltype = Game.Settings.Game.MouseScroll; var scrolltype = Game.Settings.Game.MouseScroll;
if (scrolltype == MouseScrollType.Disabled) if (scrolltype == MouseScrollType.Disabled)
return false; return false;
if (mi.Event == MouseInputEvent.Move && if (mi.Event == MouseInputEvent.Move &&
(mi.Button == MouseButton.Middle || mi.Button == (MouseButton.Left | MouseButton.Right))) (mi.Button == MouseButton.Middle || mi.Button == (MouseButton.Left | MouseButton.Right)))
{ {
var d = scrolltype == MouseScrollType.Inverted ? -1 : 1; var d = scrolltype == MouseScrollType.Inverted ? -1 : 1;
Game.viewport.Scroll((Viewport.LastMousePos - mi.Location) * d); Game.viewport.Scroll((Viewport.LastMousePos - mi.Location) * d);
return true; return true;
} }
return false; return false;
} }
static readonly Dictionary<ScrollDirection, string> directions = new Dictionary<ScrollDirection, string> static readonly Dictionary<ScrollDirection, string> directions = new Dictionary<ScrollDirection, string>
{ {
{ ScrollDirection.Up | ScrollDirection.Left, "scroll-tl" }, { ScrollDirection.Up | ScrollDirection.Left, "scroll-tl" },
{ ScrollDirection.Up | ScrollDirection.Right, "scroll-tr" }, { ScrollDirection.Up | ScrollDirection.Right, "scroll-tr" },
@@ -56,117 +56,117 @@ namespace OpenRA.Widgets
{ ScrollDirection.Right, "scroll-r" }, { ScrollDirection.Right, "scroll-r" },
}; };
public static string GetScrollCursor(Widget w, ScrollDirection edge, int2 pos) public static string GetScrollCursor(Widget w, ScrollDirection edge, int2 pos)
{ {
if (!Game.Settings.Game.ViewportEdgeScroll || Ui.MouseOverWidget != w) if (!Game.Settings.Game.ViewportEdgeScroll || Ui.MouseOverWidget != w)
return null; return null;
var blockedDirections = Game.viewport.GetBlockedDirections(); var blockedDirections = Game.viewport.GetBlockedDirections();
foreach (var dir in directions) foreach (var dir in directions)
if (edge.Includes(dir.Key)) if (edge.Includes(dir.Key))
return dir.Value + (blockedDirections.Includes(dir.Key) ? "-blocked" : ""); return dir.Value + (blockedDirections.Includes(dir.Key) ? "-blocked" : "");
return null; return null;
} }
public override string GetCursor(int2 pos) { return GetScrollCursor(this, Edge, pos); } public override string GetCursor(int2 pos) { return GetScrollCursor(this, Edge, pos); }
public override bool YieldKeyboardFocus() public override bool YieldKeyboardFocus()
{ {
Keyboard = ScrollDirection.None; Keyboard = ScrollDirection.None;
return base.YieldKeyboardFocus(); return base.YieldKeyboardFocus();
} }
public override bool HandleKeyPress(KeyInput e) public override bool HandleKeyPress(KeyInput e)
{ {
switch (e.KeyName) switch (e.KeyName)
{ {
case "up": Keyboard = Keyboard.Set(ScrollDirection.Up, e.Event == KeyInputEvent.Down); return true; 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 "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 "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 "right": Keyboard = Keyboard.Set(ScrollDirection.Right, e.Event == KeyInputEvent.Down); return true;
} }
return false; return false;
} }
public override void Tick() public override void Tick()
{ {
Edge = ScrollDirection.None; Edge = ScrollDirection.None;
if (Game.Settings.Game.ViewportEdgeScroll && Game.HasInputFocus) if (Game.Settings.Game.ViewportEdgeScroll && Game.HasInputFocus)
{ {
Edge = CheckForDirections(); Edge = CheckForDirections();
} }
Scroll(); Scroll();
} }
ScrollDirection CheckForDirections() ScrollDirection CheckForDirections()
{ {
// First let's check if the mouse is on the corners: // First let's check if the mouse is on the corners:
if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeCornerScrollThreshold && if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeCornerScrollThreshold &&
Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeCornerScrollThreshold) //Bottom Right Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeCornerScrollThreshold) //Bottom Right
{ {
return ScrollDirection.Right | ScrollDirection.Down; return ScrollDirection.Right | ScrollDirection.Down;
} }
else if (Viewport.LastMousePos.X < EdgeCornerScrollThreshold && else if (Viewport.LastMousePos.X < EdgeCornerScrollThreshold &&
Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeCornerScrollThreshold) //Bottom Left Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeCornerScrollThreshold) //Bottom Left
{ {
return ScrollDirection.Down | ScrollDirection.Left; return ScrollDirection.Down | ScrollDirection.Left;
} }
else if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeCornerScrollThreshold && else if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeCornerScrollThreshold &&
Viewport.LastMousePos.Y < EdgeCornerScrollThreshold) //Top Right Viewport.LastMousePos.Y < EdgeCornerScrollThreshold) //Top Right
{ {
return ScrollDirection.Right | ScrollDirection.Up; return ScrollDirection.Right | ScrollDirection.Up;
} }
else if (Viewport.LastMousePos.X < EdgeCornerScrollThreshold && else if (Viewport.LastMousePos.X < EdgeCornerScrollThreshold &&
Viewport.LastMousePos.Y < EdgeCornerScrollThreshold) //Top Left Viewport.LastMousePos.Y < EdgeCornerScrollThreshold) //Top Left
{ {
return ScrollDirection.Left | ScrollDirection.Up; return ScrollDirection.Left | ScrollDirection.Up;
} }
//Check for corner ends here now let's check the edges: //Check for corner ends here now let's check the edges:
// Check for edge-scroll // Check for edge-scroll
if (Viewport.LastMousePos.X < EdgeScrollThreshold) if (Viewport.LastMousePos.X < EdgeScrollThreshold)
return ScrollDirection.Left; return ScrollDirection.Left;
if (Viewport.LastMousePos.Y < EdgeScrollThreshold) if (Viewport.LastMousePos.Y < EdgeScrollThreshold)
return ScrollDirection.Up; return ScrollDirection.Up;
if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeScrollThreshold) if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeScrollThreshold)
return ScrollDirection.Right; return ScrollDirection.Right;
if (Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeScrollThreshold) if (Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeScrollThreshold)
return ScrollDirection.Down; return ScrollDirection.Down;
//Check for edge-scroll ends here.If none of above then return none. //Check for edge-scroll ends here.If none of above then return none.
return ScrollDirection.None; return ScrollDirection.None;
} }
void Scroll() void Scroll()
{ {
if (Keyboard != ScrollDirection.None || Edge != ScrollDirection.None) if (Keyboard != ScrollDirection.None || Edge != ScrollDirection.None)
{ {
var scroll = new float2(0, 0); var scroll = new float2(0, 0);
if (Keyboard.Includes(ScrollDirection.Up) || Edge.Includes(ScrollDirection.Up)) if (Keyboard.Includes(ScrollDirection.Up) || Edge.Includes(ScrollDirection.Up))
scroll += new float2(0, -1); scroll += new float2(0, -1);
if (Keyboard.Includes(ScrollDirection.Right) || Edge.Includes(ScrollDirection.Right)) if (Keyboard.Includes(ScrollDirection.Right) || Edge.Includes(ScrollDirection.Right))
scroll += new float2(1, 0); scroll += new float2(1, 0);
if (Keyboard.Includes(ScrollDirection.Down) || Edge.Includes(ScrollDirection.Down)) if (Keyboard.Includes(ScrollDirection.Down) || Edge.Includes(ScrollDirection.Down))
scroll += new float2(0, 1); scroll += new float2(0, 1);
if (Keyboard.Includes(ScrollDirection.Left) || Edge.Includes(ScrollDirection.Left)) if (Keyboard.Includes(ScrollDirection.Left) || Edge.Includes(ScrollDirection.Left))
scroll += new float2(-1, 0); scroll += new float2(-1, 0);
float length = Math.Max(1, scroll.Length); float length = Math.Max(1, scroll.Length);
scroll.X = (scroll.X / length) * Game.Settings.Game.ViewportEdgeScrollStep; scroll.X = (scroll.X / length) * Game.Settings.Game.ViewportEdgeScrollStep;
scroll.Y = (scroll.Y / length) * Game.Settings.Game.ViewportEdgeScrollStep; scroll.Y = (scroll.Y / length) * Game.Settings.Game.ViewportEdgeScrollStep;
Game.viewport.Scroll(scroll); Game.viewport.Scroll(scroll);
} }
} }
public override Widget Clone() { return new ViewportScrollControllerWidget(this); } public override Widget Clone() { return new ViewportScrollControllerWidget(this); }
} }
} }