1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 01:42:55 +08:00

Improve closest origin selection to include effects of rotation/flip

Closes https://github.com/ppy/osu/issues/28237.

Solution as proposed here:
https://github.com/ppy/osu/pull/28089#issuecomment-2095372157

For flips and rotations by 90 degrees this does what you would expect it
to. For arbitrary rotations it *sort of kind of* attempts to do this but
the results are a bit wonky - probably still better than what was there
before, though?
This commit is contained in:
Bartłomiej Dach 2024-05-20 10:14:08 +02:00
parent c03f68413a
commit 3da3b91be5
No known key found for this signature in database

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
@ -421,12 +422,41 @@ namespace osu.Game.Overlays.SkinEditor
drawable.Position -= drawable.AnchorPosition - previousAnchor;
}
private static void applyOrigin(Drawable drawable, Anchor origin)
private static void applyOrigin(Drawable drawable, Anchor screenSpaceOrigin)
{
if (origin == drawable.Origin) return;
var boundingBox = drawable.ScreenSpaceDrawQuad.AABBFloat;
var targetScreenSpacePosition = screenSpaceOrigin.PositionOnQuad(boundingBox);
Anchor localOrigin = Anchor.TopLeft;
float smallestDistanceFromTargetPosition = float.PositiveInfinity;
void checkOrigin(Anchor originToTest)
{
Vector2 positionToTest = drawable.ToScreenSpace(originToTest.PositionOnQuad(drawable.DrawRectangle));
float testedDistance = Vector2.Distance(targetScreenSpacePosition, positionToTest);
if (testedDistance < smallestDistanceFromTargetPosition)
{
localOrigin = originToTest;
smallestDistanceFromTargetPosition = testedDistance;
}
}
checkOrigin(Anchor.TopLeft);
checkOrigin(Anchor.TopCentre);
checkOrigin(Anchor.TopRight);
checkOrigin(Anchor.CentreLeft);
checkOrigin(Anchor.Centre);
checkOrigin(Anchor.CentreRight);
checkOrigin(Anchor.BottomLeft);
checkOrigin(Anchor.BottomCentre);
checkOrigin(Anchor.BottomRight);
var previousOrigin = drawable.ToParentSpace(drawable.OriginPosition);
drawable.Origin = origin;
drawable.Origin = localOrigin;
drawable.Position += drawable.ToParentSpace(drawable.OriginPosition) - previousOrigin;
}