Refactor Align/Stack function to use existing functionality. Also make undoable.

This commit is contained in:
Nathan Brown 2021-11-25 20:33:36 -08:00
commit c9714ff507
5 changed files with 147 additions and 45 deletions

View file

@ -38,6 +38,7 @@ namespace Greenshot.Base.Interfaces
event SurfaceMessageEventHandler SurfaceMessage; event SurfaceMessageEventHandler SurfaceMessage;
event SurfaceDrawingModeEventHandler DrawingModeChanged; event SurfaceDrawingModeEventHandler DrawingModeChanged;
event SurfaceElementEventHandler MovingElementChanged; event SurfaceElementEventHandler MovingElementChanged;
event SurfaceExpandedEventHandler SurfaceExpanded;
/// <summary> /// <summary>
/// Start value of the step-labels (counts) /// Start value of the step-labels (counts)
@ -161,6 +162,7 @@ namespace Greenshot.Base.Interfaces
void RemoveElement(IDrawableContainer elementToRemove, bool makeUndoable = true, bool invalidate = true, bool generateEvents = true); void RemoveElement(IDrawableContainer elementToRemove, bool makeUndoable = true, bool invalidate = true, bool generateEvents = true);
void SendMessageEvent(object source, SurfaceMessageTyp messageType, string message); void SendMessageEvent(object source, SurfaceMessageTyp messageType, string message);
void ResizeCanvas(int left, int right, int top, int bottom);
void ApplyBitmapEffect(IEffect effect); void ApplyBitmapEffect(IEffect effect);
void RemoveCursor(); void RemoveCursor();
bool HasCursor { get; } bool HasCursor { get; }

View file

@ -0,0 +1,27 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: https://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
namespace Greenshot.Base.Interfaces
{
public delegate void SurfaceExpandedEventHandler(object sender, EventArgs e);
}

View file

@ -882,52 +882,101 @@ namespace Greenshot.Editor.Drawing
public void AlignOrStack(string direction, ISurface surface, IDrawableContainer target, IDrawableContainerList parent) public void AlignOrStack(string direction, ISurface surface, IDrawableContainer target, IDrawableContainerList parent)
{ {
int newBackgroundWidth = 0, newBackgroundHeight = 0, imageBorder = 0, xMovement = 0, yMovement = 0; // Make calculations
int oldImageX = 0, oldImageY = 0; int left = 0, right = 0, top = 0, bottom = 0;
// Calculate height and width of new frame. if (direction == "left")
// Also calculate movement for target object and old image. left = target.Width;
if (direction == "right" || direction == "left") else if (direction == "right")
{ right = target.Width;
newBackgroundWidth = target.Width + surface.Image.Width; else if (direction == "top")
newBackgroundHeight = Math.Max(surface.Image.Height, target.Height); top = target.Height;
if (direction == "right") else if (direction == "bottom")
imageBorder = surface.Image.Width; bottom = target.Height;
if (direction == "left")
oldImageX = target.Width; // Use surface function
} surface.ResizeCanvas(left, right, top, bottom);
// Move target object
SnapToEdge(direction, surface, target);
if (direction == "left" || direction == "right")
SnapToEdge("top", surface, target);
else if (direction == "top" || direction == "bottom") else if (direction == "top" || direction == "bottom")
{ SnapToEdge("left", surface, target);
newBackgroundWidth = Math.Max(surface.Image.Width, target.Width); surface.DeselectAllElements();
newBackgroundHeight = target.Height + surface.Image.Height;
if (direction == "top") //if (direction == "left")
oldImageY = target.Height; //{
if (direction == "bottom") // left = target.Width;
imageBorder = surface.Image.Height; // bottom = Math.Max(target.Height - surface.Image.Height, 0);
} // xMovement = -target.Location.X;
// Create image for use as new frame. // yMovement = -target.Location.Y;
Bitmap newImage = new Bitmap(newBackgroundWidth, newBackgroundHeight); //}
// Save old image. //else if (direction == "right")
Bitmap oldImage = (Bitmap)ImageHelper.Clone(surface.Image); //{
// Set background to new, larger frame. // right = target.Width;
surface.Image = newImage; // bottom = Math.Max(target.Height - surface.Image.Height, 0);
if (direction == "right" || direction == "left") // xMovement = surface.Image.Width - target.Location.X;
{ // yMovement = -target.Location.Y;
xMovement = imageBorder - target.Location.X; //}
yMovement = -target.Location.Y; //else if (direction == "top")
} //{
else if (direction == "top" || direction == "bottom") // top = target.Height;
{ // right = Math.Max(target.Width - surface.Image.Width, 0);
xMovement = -target.Location.X; //}
yMovement = imageBorder - target.Location.Y; //else if (direction == "bottom")
} //{
// Move object to open space // bottom = target.Height;
target.MoveBy(xMovement, yMovement); // right = Math.Max(target.Width - surface.Image.Width, 0);
// Push original image to bottom. //}
var oldImageContainer = surface.AddImageContainer(oldImage, oldImageX, oldImageY);
IDrawableContainerList oldImageContainerList = parent.Clone();
oldImageContainerList[0] = oldImageContainer;
surface.Elements.PushElementsToBottom(oldImageContainerList); //int newBackgroundWidth = 0, newBackgroundHeight = 0, imageBorder = 0;
//_surfaceSizeChanged(this, null); //int oldImageX = 0, oldImageY = 0;
//// Calculate height and width of new frame.
//// Also calculate movement for target object and old image.
//if (direction == "right" || direction == "left")
//{
// newBackgroundWidth = target.Width + surface.Image.Width;
// newBackgroundHeight = Math.Max(surface.Image.Height, target.Height);
// if (direction == "right")
// imageBorder = surface.Image.Width;
// if (direction == "left")
// oldImageX = target.Width;
//}
//else if (direction == "top" || direction == "bottom")
//{
// newBackgroundWidth = Math.Max(surface.Image.Width, target.Width);
// newBackgroundHeight = target.Height + surface.Image.Height;
// if (direction == "top")
// oldImageY = target.Height;
// if (direction == "bottom")
// imageBorder = surface.Image.Height;
//}
//// Create image for use as new frame.
//Bitmap newImage = new Bitmap(newBackgroundWidth, newBackgroundHeight);
//// Save old image.
//Bitmap oldImage = (Bitmap)ImageHelper.Clone(surface.Image);
//// Set background to new, larger frame.
//surface.Image = newImage;
//if (direction == "right" || direction == "left")
//{
// xMovement = imageBorder - target.Location.X;
// yMovement = -target.Location.Y;
//}
//else if (direction == "top" || direction == "bottom")
//{
// xMovement = -target.Location.X;
// yMovement = imageBorder - target.Location.Y;
//}
//// Move object to open space
//target.MoveBy(xMovement, yMovement);
//// Push original image to bottom.
//var oldImageContainer = surface.AddImageContainer(oldImage, oldImageX, oldImageY);
//IDrawableContainerList oldImageContainerList = parent.Clone();
//oldImageContainerList[0] = oldImageContainer;
//surface.Elements.PushElementsToBottom(oldImageContainerList);
////_surfaceSizeChanged(this, null);
} }
} }
} }

View file

@ -104,6 +104,14 @@ namespace Greenshot.Editor.Drawing
remove => _surfaceSizeChanged -= value; remove => _surfaceSizeChanged -= value;
} }
[NonSerialized] private SurfaceExpandedEventHandler _surfaceExpanded;
public event SurfaceExpandedEventHandler SurfaceExpanded
{
add => _surfaceExpanded += value;
remove => _surfaceExpanded -= value;
}
[NonSerialized] private SurfaceMessageEventHandler _surfaceMessage; [NonSerialized] private SurfaceMessageEventHandler _surfaceMessage;
public event SurfaceMessageEventHandler SurfaceMessage public event SurfaceMessageEventHandler SurfaceMessage
@ -964,6 +972,13 @@ namespace Greenshot.Editor.Drawing
} }
} }
public void ResizeCanvas(int left, int right, int top, int bottom)
{
var resizeEffect = new ResizeCanvasEffect(left, right, top, bottom);
ApplyBitmapEffect(resizeEffect);
_surfaceExpanded(this, null);
}
/// <summary> /// <summary>
/// Apply a bitmap effect to the surface /// Apply a bitmap effect to the surface
/// </summary> /// </summary>

View file

@ -229,6 +229,7 @@ namespace Greenshot.Editor.Forms
_surface.MovingElementChanged += delegate { RefreshEditorControls(); }; _surface.MovingElementChanged += delegate { RefreshEditorControls(); };
_surface.DrawingModeChanged += Surface_DrawingModeChanged; _surface.DrawingModeChanged += Surface_DrawingModeChanged;
_surface.SurfaceSizeChanged += SurfaceSizeChanged; _surface.SurfaceSizeChanged += SurfaceSizeChanged;
_surface.SurfaceExpanded += SurfaceExpanded;
_surface.SurfaceMessage += SurfaceMessageReceived; _surface.SurfaceMessage += SurfaceMessageReceived;
_surface.FieldAggregator.FieldChanged += FieldAggregatorFieldChanged; _surface.FieldAggregator.FieldChanged += FieldAggregatorFieldChanged;
SurfaceSizeChanged(Surface, null); SurfaceSizeChanged(Surface, null);
@ -514,6 +515,14 @@ namespace Greenshot.Editor.Forms
AlignCanvasPositionAfterResize(); AlignCanvasPositionAfterResize();
} }
/// <summary>
/// Used when expanding the surface in one direction to accomodate shifting an object to one side.
/// </summary>
private void SurfaceExpanded(object sender, EventArgs e)
{
UpdateUndoRedoSurfaceDependencies();
}
public ISurface Surface public ISurface Surface
{ {
get { return _surface; } get { return _surface; }