Switch and utility method refactoring

(cherry picked from commit 0181c07ac020120d524a521ae04beabc3cb2fcca)
(cherry picked from commit c06b945e3793b5de396f73481a856cb154578466)
This commit is contained in:
Nathan Brown 2022-10-14 22:28:26 -07:00
commit 7c2fdd7c2d

View file

@ -930,19 +930,33 @@ namespace Greenshot.Editor.Drawing
/// <param name="targetElement"></param> /// <param name="targetElement"></param>
public void SnapToEdge(Direction direction, ISurface surface, IDrawableContainer targetElement) public void SnapToEdge(Direction direction, ISurface surface, IDrawableContainer targetElement)
{ {
int xMovement = 0, yMovement = 0; Size surfaceBounds = new(surface.Image.Width, surface.Image.Height);
NativeRectFloat newBounds = SnapHelper(direction, targetElement.Bounds, surfaceBounds);
if (direction == Direction.LEFT)
xMovement = -targetElement.Location.X;
else if (direction == Direction.RIGHT)
xMovement = surface.Image.Width - targetElement.Location.X - targetElement.Width;
else if (direction == Direction.TOP)
yMovement = -targetElement.Location.Y;
else if (direction == Direction.BOTTOM)
yMovement = surface.Image.Height - targetElement.Location.Y - targetElement.Height;
targetElement.MakeBoundsChangeUndoable(allowMerge: false); targetElement.MakeBoundsChangeUndoable(allowMerge: false);
targetElement.MoveBy(xMovement, yMovement); targetElement.ApplyBounds(newBounds);
}
public static NativeRectFloat SnapHelper(Direction direction, NativeRect bounds, Size surfaceSize)
{
switch (direction)
{
case Direction.LEFT:
bounds = bounds.ChangeX(0);
break;
case Direction.RIGHT:
bounds = bounds.Offset(offsetX: surfaceSize.Width - bounds.Right);
break;
case Direction.TOP:
bounds = bounds.ChangeY(0);
break;
case Direction.BOTTOM:
bounds = bounds.Offset(offsetY: surfaceSize.Height - bounds.Bottom);
break;
default:
break;
}
return bounds;
} }
/// <summary> /// <summary>
@ -969,23 +983,35 @@ namespace Greenshot.Editor.Drawing
public void PushOut(Direction direction, ISurface surface, IDrawableContainer targetElement) public void PushOut(Direction direction, ISurface surface, IDrawableContainer targetElement)
{ {
int left = 0, right = 0, top = 0, bottom = 0; int left = 0, right = 0, top = 0, bottom = 0;
if (direction == Direction.LEFT)
left = targetElement.Width; switch (direction)
else if (direction == Direction.RIGHT) {
right = targetElement.Width; case Direction.LEFT:
else if (direction == Direction.TOP) left = targetElement.Width;
top = targetElement.Height; break;
else if (direction == Direction.BOTTOM) case Direction.RIGHT:
bottom = targetElement.Height; right = targetElement.Width;
break;
case Direction.TOP:
top = targetElement.Height;
break;
case Direction.BOTTOM:
bottom = targetElement.Height;
break;
default:
break;
}
surface.ResizeCanvas(left, right, top, bottom); surface.ResizeCanvas(left, right, top, bottom);
// Move target object // Move target object
SnapToEdge(direction, surface, targetElement); SnapToEdge(direction, surface, targetElement);
if (direction == Direction.LEFT || direction == Direction.RIGHT) if (direction == Direction.LEFT || direction == Direction.RIGHT)
SnapToEdge(Direction.TOP, surface, targetElement); SnapToEdge(Direction.TOP, surface, targetElement);
else if (direction == Direction.TOP || direction == Direction.BOTTOM) else if (direction == Direction.TOP || direction == Direction.BOTTOM)
SnapToEdge(Direction.LEFT, surface, targetElement); SnapToEdge(Direction.LEFT, surface, targetElement);
surface.DeselectAllElements(); surface.DeselectAllElements();
} }
} }