mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
combine distance and angle for gamepad ui navigation
This commit is contained in:
parent
35e3896dc1
commit
d5f3453c71
1 changed files with 7 additions and 4 deletions
|
@ -377,17 +377,20 @@ namespace MLEM.Ui {
|
||||||
return children.FirstOrDefault(c => c.CanBeSelected);
|
return children.FirstOrDefault(c => c.CanBeSelected);
|
||||||
} else {
|
} else {
|
||||||
Element closest = null;
|
Element closest = null;
|
||||||
float closestDistSq = 0;
|
float closestPriority = 0;
|
||||||
foreach (var child in children) {
|
foreach (var child in children) {
|
||||||
if (!child.CanBeSelected || child == this.SelectedElement)
|
if (!child.CanBeSelected || child == this.SelectedElement)
|
||||||
continue;
|
continue;
|
||||||
var (xOffset, yOffset) = child.Area.Center - this.SelectedElement.Area.Center;
|
var (xOffset, yOffset) = child.Area.Center - this.SelectedElement.Area.Center;
|
||||||
if (Math.Abs(direction.Angle() - Math.Atan2(yOffset, xOffset)) >= MathHelper.PiOver2 - Element.Epsilon)
|
var angle = Math.Abs(direction.Angle() - (float) Math.Atan2(yOffset, xOffset));
|
||||||
|
if (angle >= MathHelper.PiOver2 - Element.Epsilon)
|
||||||
continue;
|
continue;
|
||||||
var distSq = child.Area.DistanceSquared(this.SelectedElement.Area);
|
var distSq = child.Area.DistanceSquared(this.SelectedElement.Area);
|
||||||
if (closest == null || distSq < closestDistSq) {
|
// both distance and angle play a role in a destination button's priority, so we combine them
|
||||||
|
var priority = distSq * (angle / MathHelper.TwoPi + 1);
|
||||||
|
if (closest == null || priority < closestPriority) {
|
||||||
closest = child;
|
closest = child;
|
||||||
closestDistSq = distSq;
|
closestPriority = priority;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return closest;
|
return closest;
|
||||||
|
|
Loading…
Reference in a new issue