From f175af854158db6cd8afca919c384dbb8d418567 Mon Sep 17 00:00:00 2001 From: Nathan Brown Date: Fri, 19 Nov 2021 21:21:30 -0800 Subject: [PATCH] Add fit and snap functionalities Fit sets width or height equal to surface.Image width or height. Snap moves object(s) to one edge of the Image. --- .../Drawing/DrawableContainerList.cs | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/Greenshot.Editor/Drawing/DrawableContainerList.cs b/src/Greenshot.Editor/Drawing/DrawableContainerList.cs index e0222a7d7..a0a95b79a 100644 --- a/src/Greenshot.Editor/Drawing/DrawableContainerList.cs +++ b/src/Greenshot.Editor/Drawing/DrawableContainerList.cs @@ -711,6 +711,76 @@ namespace Greenshot.Editor.Drawing }; menu.Items.Add(item); + // Fit width + item = new ToolStripMenuItem("Fit to width") + { + Image = (Image)EditorFormResources.GetObject("copyToolStripMenuItem.Image") + }; + item.Click += delegate + { + this[0].Width = surface.Image.Width; + this[0].MoveBy(-this[0].Location.X, 0); + surface.DeselectAllElements(); + }; + menu.Items.Add(item); + + // Fit height + item = new ToolStripMenuItem("Fit to height") + { + Image = (Image)EditorFormResources.GetObject("copyToolStripMenuItem.Image") + }; + item.Click += delegate + { + this[0].Height = surface.Image.Height; + this[0].MoveBy(0, -this[0].Location.Y); + surface.DeselectAllElements(); + }; + menu.Items.Add(item); + + // Snap left + item = new ToolStripMenuItem("Snap left") + { + Image = (Image)EditorFormResources.GetObject("copyToolStripMenuItem.Image") + }; + item.Click += delegate + { + SnapAllToEdge("left", surface, this); + }; + menu.Items.Add(item); + + // Snap right + item = new ToolStripMenuItem("Snap right") + { + Image = (Image)EditorFormResources.GetObject("copyToolStripMenuItem.Image") + }; + item.Click += delegate + { + SnapAllToEdge("right", surface, this); + }; + menu.Items.Add(item); + + // Snap to top + item = new ToolStripMenuItem("Snap to top") + { + Image = (Image)EditorFormResources.GetObject("copyToolStripMenuItem.Image") + }; + item.Click += delegate + { + SnapAllToEdge("top", surface, this); + }; + menu.Items.Add(item); + + // Snap to bottom + item = new ToolStripMenuItem("Snap to bottom") + { + Image = (Image)EditorFormResources.GetObject("copyToolStripMenuItem.Image") + }; + item.Click += delegate + { + SnapAllToEdge("bottom", surface, this); + }; + menu.Items.Add(item); + // Delete item = new ToolStripMenuItem(Language.GetString(LangKey.editor_deleteelement)) { @@ -837,5 +907,28 @@ namespace Greenshot.Editor.Drawing drawableContainer.AdjustToDpi(dpi); } } + + public void SnapToEdge(string direction, ISurface surface, IDrawableContainer target) + { + int xMovement = 0, yMovement = 0; + if (direction == "left") + xMovement = -target.Location.X; + else if (direction == "right") + xMovement = surface.Image.Width - target.Location.X - target.Width; + else if (direction == "top") + yMovement = -target.Location.Y; + else if (direction == "bottom") + yMovement = surface.Image.Height - target.Location.Y - target.Height; + target.MoveBy(xMovement, yMovement); + } + + public void SnapAllToEdge(string direction, ISurface surface, IDrawableContainerList containers) + { + foreach (var item in containers) + { + SnapToEdge(direction, surface, item); + } + surface.DeselectAllElements(); + } } } \ No newline at end of file