Clean up new methods

This commit is contained in:
Nathan Brown 2022-08-28 22:25:02 -07:00
commit b6ae224952

View file

@ -35,6 +35,14 @@ using Greenshot.Editor.Memento;
namespace Greenshot.Editor.Drawing namespace Greenshot.Editor.Drawing
{ {
public enum Direction
{
LEFT,
RIGHT,
TOP,
BOTTOM,
}
/// <summary> /// <summary>
/// Dispatches most of a DrawableContainer's public properties and methods to a list of DrawableContainers. /// Dispatches most of a DrawableContainer's public properties and methods to a list of DrawableContainers.
/// </summary> /// </summary>
@ -608,7 +616,7 @@ namespace Greenshot.Editor.Drawing
}; };
item.Click += delegate item.Click += delegate
{ {
PushOut("right", surface, this[0], this); PushOut(Direction.RIGHT, surface, this[0]);
}; };
alignSubmenu.DropDownItems.Add(item); alignSubmenu.DropDownItems.Add(item);
@ -619,7 +627,7 @@ namespace Greenshot.Editor.Drawing
}; };
item.Click += delegate item.Click += delegate
{ {
PushOut("left", surface, this[0], this); PushOut(Direction.LEFT, surface, this[0]);
}; };
alignSubmenu.DropDownItems.Add(item); alignSubmenu.DropDownItems.Add(item);
@ -630,7 +638,7 @@ namespace Greenshot.Editor.Drawing
}; };
item.Click += delegate item.Click += delegate
{ {
PushOut("top", surface, this[0], this); PushOut(Direction.TOP, surface, this[0]);
}; };
alignSubmenu.DropDownItems.Add(item); alignSubmenu.DropDownItems.Add(item);
@ -641,7 +649,7 @@ namespace Greenshot.Editor.Drawing
}; };
item.Click += delegate item.Click += delegate
{ {
PushOut("bottom", surface, this[0], this); PushOut(Direction.BOTTOM, surface, this[0]);
}; };
alignSubmenu.DropDownItems.Add(item); alignSubmenu.DropDownItems.Add(item);
menu.Items.Add(alignSubmenu); menu.Items.Add(alignSubmenu);
@ -661,7 +669,7 @@ namespace Greenshot.Editor.Drawing
MakeBoundsChangeUndoable(false); MakeBoundsChangeUndoable(false);
item.Width = surface.Image.Width; item.Width = surface.Image.Width;
} }
SnapAllToEdge("left", surface, this); SnapAllToEdge(Direction.LEFT, surface, this);
surface.Invalidate(); // not sure if this belongs surface.Invalidate(); // not sure if this belongs
}; };
fitSubmenu.DropDownItems.Add(item); fitSubmenu.DropDownItems.Add(item);
@ -678,7 +686,7 @@ namespace Greenshot.Editor.Drawing
MakeBoundsChangeUndoable(false); MakeBoundsChangeUndoable(false);
item.Height = surface.Image.Height; item.Height = surface.Image.Height;
} }
SnapAllToEdge("top", surface, this); SnapAllToEdge(Direction.TOP, surface, this);
surface.Invalidate(); // not sure if this belongs surface.Invalidate(); // not sure if this belongs
}; };
fitSubmenu.DropDownItems.Add(item); fitSubmenu.DropDownItems.Add(item);
@ -693,7 +701,7 @@ namespace Greenshot.Editor.Drawing
}; };
item.Click += delegate item.Click += delegate
{ {
SnapAllToEdge("left", surface, this); SnapAllToEdge(Direction.LEFT, surface, this);
}; };
snapSubmenu.DropDownItems.Add(item); snapSubmenu.DropDownItems.Add(item);
@ -704,7 +712,7 @@ namespace Greenshot.Editor.Drawing
}; };
item.Click += delegate item.Click += delegate
{ {
SnapAllToEdge("right", surface, this); SnapAllToEdge(Direction.RIGHT, surface, this);
}; };
snapSubmenu.DropDownItems.Add(item); snapSubmenu.DropDownItems.Add(item);
@ -715,7 +723,7 @@ namespace Greenshot.Editor.Drawing
}; };
item.Click += delegate item.Click += delegate
{ {
SnapAllToEdge("top", surface, this); SnapAllToEdge(Direction.TOP, surface, this);
}; };
snapSubmenu.DropDownItems.Add(item); snapSubmenu.DropDownItems.Add(item);
@ -726,7 +734,7 @@ namespace Greenshot.Editor.Drawing
}; };
item.Click += delegate item.Click += delegate
{ {
SnapAllToEdge("bottom", surface, this); SnapAllToEdge(Direction.BOTTOM, surface, this);
}; };
snapSubmenu.DropDownItems.Add(item); snapSubmenu.DropDownItems.Add(item);
menu.Items.Add(snapSubmenu); menu.Items.Add(snapSubmenu);
@ -858,52 +866,70 @@ namespace Greenshot.Editor.Drawing
} }
} }
public void SnapToEdge(string direction, ISurface surface, IDrawableContainer target) /// <summary>
/// Move an element to one edge of the surface.
/// </summary>
/// <param name="direction">Direction to move the element.</param>
/// <param name="surface"></param>
/// <param name="targetElement"></param>
public void SnapToEdge(Direction direction, ISurface surface, IDrawableContainer targetElement)
{ {
int xMovement = 0, yMovement = 0; int xMovement = 0, yMovement = 0;
if (direction == "left")
xMovement = -target.Location.X; if (direction == Direction.LEFT)
else if (direction == "right") xMovement = -targetElement.Location.X;
xMovement = surface.Image.Width - target.Location.X - target.Width; else if (direction == Direction.RIGHT)
else if (direction == "top") xMovement = surface.Image.Width - targetElement.Location.X - targetElement.Width;
yMovement = -target.Location.Y; else if (direction == Direction.TOP)
else if (direction == "bottom") yMovement = -targetElement.Location.Y;
yMovement = surface.Image.Height - target.Location.Y - target.Height; else if (direction == Direction.BOTTOM)
target.MakeBoundsChangeUndoable(false); yMovement = surface.Image.Height - targetElement.Location.Y - targetElement.Height;
target.MoveBy(xMovement, yMovement);
targetElement.MakeBoundsChangeUndoable(allowMerge: false);
targetElement.MoveBy(xMovement, yMovement);
} }
public void SnapAllToEdge(string direction, ISurface surface, IDrawableContainerList containers) /// <summary>
/// Moves all selected elements to one edge of the surface.
/// </summary>
/// <param name="direction"></param>
/// <param name="surface"></param>
/// <param name="elements"></param>
public void SnapAllToEdge(Direction direction, ISurface surface, IDrawableContainerList elements)
{ {
foreach (var item in containers) foreach (var item in elements)
{ {
SnapToEdge(direction, surface, item); SnapToEdge(direction, surface, item);
} }
surface.DeselectAllElements(); surface.DeselectAllElements();
} }
public void PushOut(string direction, ISurface surface, IDrawableContainer target, IDrawableContainerList parent) /// <summary>
/// Push an element entirely outside the current bounds of the surface, expanding the surface to accomodate it.
/// </summary>
/// <param name="direction">Direction in which to move element.</param>
/// <param name="surface"></param>
/// <param name="targetElement"></param>
public void PushOut(Direction direction, ISurface surface, IDrawableContainer targetElement)
{ {
// Make calculations
int left = 0, right = 0, top = 0, bottom = 0; int left = 0, right = 0, top = 0, bottom = 0;
if (direction == "left") if (direction == Direction.LEFT)
left = target.Width; left = targetElement.Width;
else if (direction == "right") else if (direction == Direction.RIGHT)
right = target.Width; right = targetElement.Width;
else if (direction == "top") else if (direction == Direction.TOP)
top = target.Height; top = targetElement.Height;
else if (direction == "bottom") else if (direction == Direction.BOTTOM)
bottom = target.Height; bottom = targetElement.Height;
// Use surface function
surface.ResizeCanvas(left, right, top, bottom); surface.ResizeCanvas(left, right, top, bottom);
// Move target object // Move target object
SnapToEdge(direction, surface, target); SnapToEdge(direction, surface, targetElement);
if (direction == "left" || direction == "right") if (direction == Direction.LEFT || direction == Direction.RIGHT)
SnapToEdge("top", surface, target); SnapToEdge(Direction.TOP, surface, targetElement);
else if (direction == "top" || direction == "bottom") else if (direction == Direction.TOP || direction == Direction.BOTTOM)
SnapToEdge("left", surface, target); SnapToEdge(Direction.LEFT, surface, targetElement);
surface.DeselectAllElements(); surface.DeselectAllElements();
} }
} }