Skip to content

Commit 7cbc4a2

Browse files
Bugra Cuhadarogluchrisforbes
Bugra Cuhadaroglu
authored andcommitted
Fix OpenRA#2720 - Diagonal scrolling area in viewport corners are too small
I added two options, one is for EdgeScrollThreshold and the other one is EdgeCornerScrollThreshold. You can modify these threshold as much as you want. [Squashed down into one commit -- chrisf]
1 parent 9db06ca commit 7cbc4a2

File tree

1 file changed

+144
-101
lines changed

1 file changed

+144
-101
lines changed

OpenRA.Game/Widgets/ViewportScrollControllerWidget.cs

Lines changed: 144 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,35 @@
1515

1616
namespace OpenRA.Widgets
1717
{
18-
public class ViewportScrollControllerWidget : Widget
19-
{
20-
public int EdgeScrollThreshold = 15;
21-
22-
ScrollDirection Keyboard;
23-
ScrollDirection Edge;
24-
25-
public ViewportScrollControllerWidget() : base() {}
26-
protected ViewportScrollControllerWidget(ViewportScrollControllerWidget widget)
27-
: base(widget) {}
28-
29-
public override bool HandleMouseInput(MouseInput mi)
30-
{
31-
var scrolltype = Game.Settings.Game.MouseScroll;
32-
if (scrolltype == MouseScrollType.Disabled)
33-
return false;
34-
35-
if (mi.Event == MouseInputEvent.Move &&
36-
(mi.Button == MouseButton.Middle || mi.Button == (MouseButton.Left | MouseButton.Right)))
37-
{
38-
var d = scrolltype == MouseScrollType.Inverted ? -1 : 1;
39-
Game.viewport.Scroll((Viewport.LastMousePos - mi.Location) * d);
40-
return true;
41-
}
42-
return false;
43-
}
44-
45-
static readonly Dictionary<ScrollDirection, string> directions = new Dictionary<ScrollDirection, string>
18+
public class ViewportScrollControllerWidget : Widget
19+
{
20+
public int EdgeScrollThreshold = 15;
21+
public int EdgeCornerScrollThreshold = 35;
22+
23+
ScrollDirection Keyboard;
24+
ScrollDirection Edge;
25+
26+
public ViewportScrollControllerWidget() : base() { }
27+
protected ViewportScrollControllerWidget(ViewportScrollControllerWidget widget)
28+
: base(widget) { }
29+
30+
public override bool HandleMouseInput(MouseInput mi)
31+
{
32+
var scrolltype = Game.Settings.Game.MouseScroll;
33+
if (scrolltype == MouseScrollType.Disabled)
34+
return false;
35+
36+
if (mi.Event == MouseInputEvent.Move &&
37+
(mi.Button == MouseButton.Middle || mi.Button == (MouseButton.Left | MouseButton.Right)))
38+
{
39+
var d = scrolltype == MouseScrollType.Inverted ? -1 : 1;
40+
Game.viewport.Scroll((Viewport.LastMousePos - mi.Location) * d);
41+
return true;
42+
}
43+
return false;
44+
}
45+
46+
static readonly Dictionary<ScrollDirection, string> directions = new Dictionary<ScrollDirection, string>
4647
{
4748
{ ScrollDirection.Up | ScrollDirection.Left, "scroll-tl" },
4849
{ ScrollDirection.Up | ScrollDirection.Right, "scroll-tr" },
@@ -55,77 +56,119 @@ public override bool HandleMouseInput(MouseInput mi)
5556
{ ScrollDirection.Right, "scroll-r" },
5657
};
5758

58-
public static string GetScrollCursor(Widget w, ScrollDirection edge, int2 pos)
59-
{
60-
if (!Game.Settings.Game.ViewportEdgeScroll || Ui.MouseOverWidget != w)
61-
return null;
62-
63-
var blockedDirections = Game.viewport.GetBlockedDirections();
64-
65-
foreach( var dir in directions )
66-
if (edge.Includes( dir.Key ))
67-
return dir.Value + (blockedDirections.Includes( dir.Key ) ? "-blocked" : "");
68-
69-
return null;
70-
}
71-
72-
public override string GetCursor(int2 pos) { return GetScrollCursor(this, Edge, pos); }
73-
74-
public override bool LoseFocus (MouseInput mi)
75-
{
76-
Keyboard = ScrollDirection.None;
77-
return base.LoseFocus(mi);
78-
}
79-
80-
public override bool HandleKeyPress(KeyInput e)
81-
{
82-
switch (e.KeyName)
83-
{
84-
case "up": Keyboard = Keyboard.Set(ScrollDirection.Up, e.Event == KeyInputEvent.Down); return true;
85-
case "down": Keyboard = Keyboard.Set(ScrollDirection.Down, e.Event == KeyInputEvent.Down); return true;
86-
case "left": Keyboard = Keyboard.Set(ScrollDirection.Left, e.Event == KeyInputEvent.Down); return true;
87-
case "right": Keyboard = Keyboard.Set(ScrollDirection.Right, e.Event == KeyInputEvent.Down); return true;
88-
}
89-
return false;
90-
}
91-
92-
public override void Tick()
93-
{
94-
Edge = ScrollDirection.None;
95-
if (Game.Settings.Game.ViewportEdgeScroll && Game.HasInputFocus)
96-
{
97-
// Check for edge-scroll
98-
if (Viewport.LastMousePos.X < EdgeScrollThreshold)
99-
Edge = Edge.Set(ScrollDirection.Left, true);
100-
if (Viewport.LastMousePos.Y < EdgeScrollThreshold)
101-
Edge = Edge.Set(ScrollDirection.Up, true);
102-
if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeScrollThreshold)
103-
Edge = Edge.Set(ScrollDirection.Right, true);
104-
if (Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeScrollThreshold)
105-
Edge = Edge.Set(ScrollDirection.Down, true);
106-
}
107-
108-
if(Keyboard != ScrollDirection.None || Edge != ScrollDirection.None)
109-
{
110-
var scroll = new float2(0, 0);
111-
112-
if (Keyboard.Includes(ScrollDirection.Up) || Edge.Includes(ScrollDirection.Up))
113-
scroll += new float2(0, -1);
114-
if (Keyboard.Includes(ScrollDirection.Right) || Edge.Includes(ScrollDirection.Right))
115-
scroll += new float2(1, 0);
116-
if (Keyboard.Includes(ScrollDirection.Down) || Edge.Includes(ScrollDirection.Down))
117-
scroll += new float2(0, 1);
118-
if (Keyboard.Includes(ScrollDirection.Left) || Edge.Includes(ScrollDirection.Left))
119-
scroll += new float2(-1, 0);
120-
121-
float length = Math.Max(1, scroll.Length);
122-
scroll.X = (scroll.X / length) * Game.Settings.Game.ViewportEdgeScrollStep;
123-
scroll.Y = (scroll.Y / length) * Game.Settings.Game.ViewportEdgeScrollStep;
124-
125-
Game.viewport.Scroll(scroll);
126-
}
127-
}
128-
129-
public override Widget Clone() { return new ViewportScrollControllerWidget(this); }
130-
}
59+
public static string GetScrollCursor(Widget w, ScrollDirection edge, int2 pos)
60+
{
61+
if (!Game.Settings.Game.ViewportEdgeScroll || Ui.MouseOverWidget != w)
62+
return null;
63+
64+
var blockedDirections = Game.viewport.GetBlockedDirections();
65+
66+
foreach (var dir in directions)
67+
if (edge.Includes(dir.Key))
68+
return dir.Value + (blockedDirections.Includes(dir.Key) ? "-blocked" : "");
69+
70+
return null;
71+
}
72+
73+
public override string GetCursor(int2 pos) { return GetScrollCursor(this, Edge, pos); }
74+
75+
public override bool LoseFocus(MouseInput mi)
76+
{
77+
Keyboard = ScrollDirection.None;
78+
return base.LoseFocus(mi);
79+
}
80+
81+
public override bool HandleKeyPress(KeyInput e)
82+
{
83+
switch (e.KeyName)
84+
{
85+
case "up": Keyboard = Keyboard.Set(ScrollDirection.Up, e.Event == KeyInputEvent.Down); return true;
86+
case "down": Keyboard = Keyboard.Set(ScrollDirection.Down, e.Event == KeyInputEvent.Down); return true;
87+
case "left": Keyboard = Keyboard.Set(ScrollDirection.Left, e.Event == KeyInputEvent.Down); return true;
88+
case "right": Keyboard = Keyboard.Set(ScrollDirection.Right, e.Event == KeyInputEvent.Down); return true;
89+
}
90+
return false;
91+
}
92+
93+
public override void Tick()
94+
{
95+
Edge = ScrollDirection.None;
96+
if (Game.Settings.Game.ViewportEdgeScroll && Game.HasInputFocus)
97+
{
98+
Edge = CheckForDirections();
99+
Scroll();
100+
}
101+
102+
103+
}
104+
105+
ScrollDirection CheckForDirections()
106+
{
107+
// First let's check if the mouse is on the corners:
108+
if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeCornerScrollThreshold &&
109+
Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeCornerScrollThreshold) //Bottom Right
110+
{
111+
return ScrollDirection.Right | ScrollDirection.Down;
112+
}
113+
else if (Viewport.LastMousePos.X < EdgeCornerScrollThreshold &&
114+
Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeCornerScrollThreshold) //Bottom Left
115+
{
116+
return ScrollDirection.Down | ScrollDirection.Left;
117+
}
118+
119+
else if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeCornerScrollThreshold &&
120+
Viewport.LastMousePos.Y < EdgeCornerScrollThreshold) //Top Right
121+
{
122+
return ScrollDirection.Right | ScrollDirection.Up;
123+
}
124+
125+
else if (Viewport.LastMousePos.X < EdgeCornerScrollThreshold &&
126+
Viewport.LastMousePos.Y < EdgeCornerScrollThreshold) //Top Left
127+
{
128+
return ScrollDirection.Left | ScrollDirection.Up;
129+
}
130+
131+
//Check for corner ends here now let's check the edges:
132+
133+
// Check for edge-scroll
134+
if (Viewport.LastMousePos.X < EdgeScrollThreshold)
135+
return ScrollDirection.Left;
136+
if (Viewport.LastMousePos.Y < EdgeScrollThreshold)
137+
return ScrollDirection.Up;
138+
if (Viewport.LastMousePos.X >= Game.viewport.Width - EdgeScrollThreshold)
139+
return ScrollDirection.Right;
140+
if (Viewport.LastMousePos.Y >= Game.viewport.Height - EdgeScrollThreshold)
141+
return ScrollDirection.Down;
142+
143+
144+
//Check for edge-scroll ends here.If none of above then return none.
145+
return ScrollDirection.None;
146+
}
147+
148+
149+
void Scroll()
150+
{
151+
if (Keyboard != ScrollDirection.None || Edge != ScrollDirection.None)
152+
{
153+
var scroll = new float2(0, 0);
154+
155+
if (Keyboard.Includes(ScrollDirection.Up) || Edge.Includes(ScrollDirection.Up))
156+
scroll += new float2(0, -1);
157+
if (Keyboard.Includes(ScrollDirection.Right) || Edge.Includes(ScrollDirection.Right))
158+
scroll += new float2(1, 0);
159+
if (Keyboard.Includes(ScrollDirection.Down) || Edge.Includes(ScrollDirection.Down))
160+
scroll += new float2(0, 1);
161+
if (Keyboard.Includes(ScrollDirection.Left) || Edge.Includes(ScrollDirection.Left))
162+
scroll += new float2(-1, 0);
163+
164+
float length = Math.Max(1, scroll.Length);
165+
scroll.X = (scroll.X / length) * Game.Settings.Game.ViewportEdgeScrollStep;
166+
scroll.Y = (scroll.Y / length) * Game.Settings.Game.ViewportEdgeScrollStep;
167+
168+
Game.viewport.Scroll(scroll);
169+
}
170+
}
171+
172+
public override Widget Clone() { return new ViewportScrollControllerWidget(this); }
173+
}
131174
}

0 commit comments

Comments
 (0)