mirror of
https://github.com/greenshot/greenshot
synced 2025-08-22 06:23:24 -07:00
Switch and utility method refactoring
(cherry picked from commit 0181c07ac020120d524a521ae04beabc3cb2fcca) (cherry picked from commit c06b945e3793b5de396f73481a856cb154578466)
This commit is contained in:
parent
6bc7548c73
commit
7c2fdd7c2d
1 changed files with 45 additions and 19 deletions
|
@ -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)
|
|
||||||
|
switch (direction)
|
||||||
|
{
|
||||||
|
case Direction.LEFT:
|
||||||
left = targetElement.Width;
|
left = targetElement.Width;
|
||||||
else if (direction == Direction.RIGHT)
|
break;
|
||||||
|
case Direction.RIGHT:
|
||||||
right = targetElement.Width;
|
right = targetElement.Width;
|
||||||
else if (direction == Direction.TOP)
|
break;
|
||||||
|
case Direction.TOP:
|
||||||
top = targetElement.Height;
|
top = targetElement.Height;
|
||||||
else if (direction == Direction.BOTTOM)
|
break;
|
||||||
|
case Direction.BOTTOM:
|
||||||
bottom = targetElement.Height;
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue