Simplify HandleScale method

This commit is contained in:
Dean Herbert 2020-09-30 15:17:27 +09:00
parent d6c88ecd69
commit 7fad9ce34a
1 changed files with 19 additions and 19 deletions

View File

@ -43,13 +43,7 @@ public override bool HandleMovement(MoveSelectionEvent moveEvent) =>
public override bool HandleScale(Vector2 scale, Anchor reference) public override bool HandleScale(Vector2 scale, Anchor reference)
{ {
// cancel out scale in axes we don't care about (based on which drag handle was used). adjustScaleFromAnchor(ref scale, reference);
if ((reference & Anchor.x1) > 0) scale.X = 0;
if ((reference & Anchor.y1) > 0) scale.Y = 0;
// 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;
var hitObjects = selectedMovableObjects; var hitObjects = selectedMovableObjects;
@ -58,11 +52,11 @@ public override bool HandleScale(Vector2 scale, Anchor reference)
// is not looking to change the duration of the slider but expand the whole pattern. // is not looking to change the duration of the slider but expand the whole pattern.
if (hitObjects.Length == 1 && hitObjects.First() is Slider slider) if (hitObjects.Length == 1 && hitObjects.First() is Slider slider)
{ {
var quad = getSurroundingQuad(slider.Path.ControlPoints.Select(p => p.Position.Value)); Quad quad = getSurroundingQuad(slider.Path.ControlPoints.Select(p => p.Position.Value));
Vector2 delta = Vector2.One + new Vector2(scale.X / quad.Width, scale.Y / quad.Height); Vector2 pathRelativeDeltaScale = new Vector2(1 + scale.X / quad.Width, 1 + scale.Y / quad.Height);
foreach (var point in slider.Path.ControlPoints) foreach (var point in slider.Path.ControlPoints)
point.Position.Value *= delta; point.Position.Value *= pathRelativeDeltaScale;
} }
else else
{ {
@ -72,23 +66,29 @@ public override bool HandleScale(Vector2 scale, Anchor reference)
Quad quad = getSurroundingQuad(hitObjects); Quad quad = getSurroundingQuad(hitObjects);
Vector2 minPosition = quad.TopLeft;
Vector2 size = quad.Size;
Vector2 newSize = size + scale;
foreach (var h in hitObjects) foreach (var h in hitObjects)
{ {
if (scale.X != 1) h.Position = new Vector2(
h.Position = new Vector2(minPosition.X + (h.X - minPosition.X) / size.X * newSize.X, h.Y); quad.TopLeft.X + (h.X - quad.TopLeft.X) / quad.Width * (quad.Width + scale.X),
if (scale.Y != 1) quad.TopLeft.Y + (h.Y - quad.TopLeft.Y) / quad.Height * (quad.Height + scale.Y)
h.Position = new Vector2(h.X, minPosition.Y + (h.Y - minPosition.Y) / size.Y * newSize.Y); );
} }
} }
return true; return true;
} }
private static void adjustScaleFromAnchor(ref Vector2 scale, Anchor reference)
{
// cancel out scale in axes we don't care about (based on which drag handle was used).
if ((reference & Anchor.x1) > 0) scale.X = 0;
if ((reference & Anchor.y1) > 0) scale.Y = 0;
// 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;
}
public override bool HandleRotation(float delta) public override bool HandleRotation(float delta)
{ {
var hitObjects = selectedMovableObjects; var hitObjects = selectedMovableObjects;