compute lower bound in clamp scale

This commit is contained in:
OliBomby 2024-09-21 20:34:29 +02:00
parent 54e68005ef
commit 0677430975
2 changed files with 27 additions and 8 deletions

View File

@ -241,33 +241,42 @@ namespace osu.Game.Rulesets.Osu.Edit
foreach (var point in points) foreach (var point in points)
{ {
scale = clampToBound(scale, point, Vector2.Zero); scale = clampToBound(scale, point, Vector2.Zero, OsuPlayfield.BASE_SIZE);
scale = clampToBound(scale, point, OsuPlayfield.BASE_SIZE);
} }
return Vector2.ComponentMax(scale, new Vector2(Precision.FLOAT_EPSILON)); return Vector2.ComponentMax(scale, new Vector2(Precision.FLOAT_EPSILON));
float minPositiveComponent(Vector2 v) => MathF.Min(v.X < 0 ? float.PositiveInfinity : v.X, v.Y < 0 ? float.PositiveInfinity : v.Y); float minComponent(Vector2 v) => MathF.Min(v.X, v.Y);
float maxComponent(Vector2 v) => MathF.Max(v.X, v.Y);
Vector2 clampToBound(Vector2 s, Vector2 p, Vector2 bound) Vector2 clampToBound(Vector2 s, Vector2 p, Vector2 lowerBound, Vector2 upperBound)
{ {
p -= actualOrigin; p -= actualOrigin;
bound -= actualOrigin; lowerBound -= actualOrigin;
upperBound -= actualOrigin;
var a = new Vector2(cos * cos * p.X - sin * cos * p.Y, -sin * cos * p.X + sin * sin * p.Y); var a = new Vector2(cos * cos * p.X - sin * cos * p.Y, -sin * cos * p.X + sin * sin * p.Y);
var b = new Vector2(sin * sin * p.X + sin * cos * p.Y, sin * cos * p.X + cos * cos * p.Y); var b = new Vector2(sin * sin * p.X + sin * cos * p.Y, sin * cos * p.X + cos * cos * p.Y);
switch (adjustAxis) switch (adjustAxis)
{ {
case Axes.X: case Axes.X:
s.X = MathF.Min(scale.X, minPositiveComponent(Vector2.Divide(bound - b, a))); var lowerBounds = Vector2.Divide(lowerBound - b, a);
var upperBounds = Vector2.Divide(upperBound - b, a);
if (a.X < 0)
(lowerBounds, upperBounds) = (upperBounds, lowerBounds);
s.X = MathHelper.Clamp(s.X, maxComponent(lowerBounds), minComponent(upperBounds));
break; break;
case Axes.Y: case Axes.Y:
s.Y = MathF.Min(scale.Y, minPositiveComponent(Vector2.Divide(bound - a, b))); var lowerBoundsY = Vector2.Divide(lowerBound - a, b);
var upperBoundsY = Vector2.Divide(upperBound - a, b);
if (b.Y < 0)
(lowerBoundsY, upperBoundsY) = (upperBoundsY, lowerBoundsY);
s.Y = MathHelper.Clamp(s.Y, maxComponent(lowerBoundsY), minComponent(upperBoundsY));
break; break;
case Axes.Both: case Axes.Both:
s = Vector2.ComponentMin(s, s * minPositiveComponent(Vector2.Divide(bound, a * s.X + b * s.Y))); // s = Vector2.ComponentMin(s, s * minPositiveComponent(Vector2.Divide(bound, a * s.X + b * s.Y)));
break; break;
} }

View File

@ -189,6 +189,16 @@ namespace osu.Game.Rulesets.Osu.Edit
scale.Y = max_scale; scale.Y = max_scale;
scaleInputBindable.MaxValue = MathF.Max(1, MathF.Min(scale.X, scale.Y)); scaleInputBindable.MaxValue = MathF.Max(1, MathF.Min(scale.X, scale.Y));
const float min_scale = -10;
scale = scaleHandler.ClampScaleToPlayfieldBounds(new Vector2(min_scale), getOriginPosition(scaleInfo.Value), getAdjustAxis(scaleInfo.Value), getRotation(scaleInfo.Value));
if (!scaleInfo.Value.XAxis)
scale.X = max_scale;
if (!scaleInfo.Value.YAxis)
scale.Y = max_scale;
scaleInputBindable.MinValue = MathF.Min(-1, MathF.Max(scale.X, scale.Y));
} }
private void setOrigin(ScaleOrigin origin) private void setOrigin(ScaleOrigin origin)