1
0
Fork 0
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:
Ell 2022-03-14 15:39:32 +01:00
parent 35e3896dc1
commit d5f3453c71

View file

@ -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;