From 6a3c58b9adfc6184f23ae0eefa6c39c38bbfdc47 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 19 May 2021 19:58:55 +0900 Subject: [PATCH] Implement proper scaling algorithms --- .../Skinning/Editor/SkinSelectionHandler.cs | 98 +++++++++++++++++-- 1 file changed, 88 insertions(+), 10 deletions(-) diff --git a/osu.Game/Skinning/Editor/SkinSelectionHandler.cs b/osu.Game/Skinning/Editor/SkinSelectionHandler.cs index 2eb4ea107d..18c9341db9 100644 --- a/osu.Game/Skinning/Editor/SkinSelectionHandler.cs +++ b/osu.Game/Skinning/Editor/SkinSelectionHandler.cs @@ -34,9 +34,94 @@ namespace osu.Game.Skinning.Editor { adjustScaleFromAnchor(ref scale, anchor); - foreach (var c in SelectedBlueprints) - // TODO: this is temporary and will be fixed with a separate refactor of selection transform logic. - ((Drawable)c.Item).Scale += scale * 0.02f; + if (SelectedBlueprints.Count > 1) + { + var selectionQuad = GetSurroundingQuad(SelectedBlueprints.SelectMany(b => + b.Item.ScreenSpaceDrawQuad.GetVertices().ToArray())); + + // the selection quad is always upright, so use a rect to make mutating the values easier. + var adjustedRect = selectionQuad.AABBFloat; + + // for now aspect lock scale adjustments that occur at corners. + if (!anchor.HasFlagFast(Anchor.x1) && !anchor.HasFlagFast(Anchor.y1)) + scale.Y = scale.X / selectionQuad.Width * selectionQuad.Height; + + if (anchor.HasFlagFast(Anchor.x0)) + { + adjustedRect.X -= scale.X; + adjustedRect.Width += scale.X; + } + else if (anchor.HasFlagFast(Anchor.x2)) + { + adjustedRect.Width += scale.X; + } + + if (anchor.HasFlagFast(Anchor.y0)) + { + adjustedRect.Y -= scale.Y; + adjustedRect.Height += scale.Y; + } + else if (anchor.HasFlagFast(Anchor.y2)) + { + adjustedRect.Height += scale.Y; + } + + // scale adjust should match that of the quad itself. + var scaledDelta = new Vector2( + adjustedRect.Width / selectionQuad.Width - 1, + adjustedRect.Height / selectionQuad.Height - 1 + ); + + foreach (var b in SelectedBlueprints) + { + var drawableItem = (Drawable)b.Item; + + if (SelectedBlueprints.Count > 1) + { + // each drawable's relative position should be maintained in the scaled quad. + var screenPosition = b.ScreenSpaceSelectionPoint; + + var relativePositionInOriginal = + new Vector2( + (screenPosition.X - selectionQuad.TopLeft.X) / selectionQuad.Width, + (screenPosition.Y - selectionQuad.TopLeft.Y) / selectionQuad.Height + ); + + var newPositionInAdjusted = new Vector2( + adjustedRect.TopLeft.X + adjustedRect.Width * relativePositionInOriginal.X, + adjustedRect.TopLeft.Y + adjustedRect.Height * relativePositionInOriginal.Y + ); + + drawableItem.Position = drawableItem.Parent.ToLocalSpace(newPositionInAdjusted) - drawableItem.AnchorPosition; + drawableItem.Scale += scaledDelta; + } + } + } + else + { + var blueprint = SelectedBlueprints.First(); + var drawableItem = (Drawable)blueprint.Item; + + // the number of local "pixels" the drag operation resulted in. + // our goal is to increase the drawable's draw size by this amount. + var scaledDelta = drawableItem.ScreenSpaceDeltaToParentSpace(scale); + + scaledDelta = new Vector2( + scaledDelta.X / drawableItem.DrawWidth, + scaledDelta.Y / drawableItem.DrawHeight + ); + + // handle the case where scaling with a centre origin needs double the adjustments to match + // user cursor movement. + if (drawableItem.Origin.HasFlagFast(Anchor.x1)) scaledDelta.X *= 2; + if (drawableItem.Origin.HasFlagFast(Anchor.y1)) scaledDelta.Y *= 2; + + // for now aspect lock scale adjustments that occur at corners. + if (!anchor.HasFlagFast(Anchor.x1) && !anchor.HasFlagFast(Anchor.y1)) + scaledDelta.Y = scaledDelta.X; + + drawableItem.Scale += scaledDelta; + } return true; } @@ -158,13 +243,6 @@ namespace osu.Game.Skinning.Editor // reverse the scale direction if dragging from top or left. if ((reference & Anchor.x0) > 0) scale.X = -scale.X; if ((reference & Anchor.y0) > 0) scale.Y = -scale.Y; - - // for now aspect lock scale adjustments that occur at corners. - if (!reference.HasFlagFast(Anchor.x1) && !reference.HasFlagFast(Anchor.y1)) - { - // TODO: temporary implementation - only dragging the corner handles across the X axis changes size. - scale.Y = scale.X; - } } public class AnchorMenuItem : TernaryStateMenuItem