1
0
forked from Clones/Controlify

improve snapInDirection logic (#179)

Co-authored-by: Xander <xander@isxander.dev>
This commit is contained in:
Arno Keesman
2023-10-30 20:21:01 +01:00
committed by GitHub
parent 3ec8daa125
commit 0206186575

View File

@ -214,16 +214,21 @@ public class VirtualMouseHandler {
SnapPoint snapPoint = pair.getFirst(); SnapPoint snapPoint = pair.getFirst();
Vector2d dist = pair.getSecond(); Vector2d dist = pair.getSecond();
// distance in the correct orthogonal direction
double distance = Math.abs(direction.getAxis() == ScreenAxis.HORIZONTAL ? dist.x : dist.y); double distance = Math.abs(direction.getAxis() == ScreenAxis.HORIZONTAL ? dist.x : dist.y);
// distance in the incorrect orthogonal direction
double deviation = Math.abs(direction.getAxis() == ScreenAxis.HORIZONTAL ? dist.y : dist.x); double deviation = Math.abs(direction.getAxis() == ScreenAxis.HORIZONTAL ? dist.y : dist.x);
pair.getSecond().set(distance, deviation); // punish deviation significantly
pair.getSecond().set(distance, deviation * 4);
return distance >= snapPoint.range(); // reject if the deviation is double the correct direction away
return distance >= snapPoint.range() && (deviation < distance * 2);
}) })
// pick the closest point // pick the closest point
.min(Comparator.comparingDouble(pair -> { .min(Comparator.comparingDouble(pair -> {
Vector2d distDev = pair.getSecond(); Vector2d distDev = pair.getSecond();
// x = dist, y = deviation
return distDev.x + distDev.y; return distDev.x + distDev.y;
})) }))
.map(Pair::getFirst); .map(Pair::getFirst);