mirror of
https://github.com/greenshot/greenshot
synced 2025-08-22 06:23:24 -07:00
Refactor Align/Stack function to use existing functionality. Also make undoable.
This commit is contained in:
parent
968faae0da
commit
c9714ff507
5 changed files with 147 additions and 45 deletions
|
@ -38,6 +38,7 @@ namespace Greenshot.Base.Interfaces
|
|||
event SurfaceMessageEventHandler SurfaceMessage;
|
||||
event SurfaceDrawingModeEventHandler DrawingModeChanged;
|
||||
event SurfaceElementEventHandler MovingElementChanged;
|
||||
event SurfaceExpandedEventHandler SurfaceExpanded;
|
||||
|
||||
/// <summary>
|
||||
/// 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 SendMessageEvent(object source, SurfaceMessageTyp messageType, string message);
|
||||
void ResizeCanvas(int left, int right, int top, int bottom);
|
||||
void ApplyBitmapEffect(IEffect effect);
|
||||
void RemoveCursor();
|
||||
bool HasCursor { get; }
|
||||
|
|
27
src/Greenshot.Base/Interfaces/SurfaceExpandedEventHandler.cs
Normal file
27
src/Greenshot.Base/Interfaces/SurfaceExpandedEventHandler.cs
Normal 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);
|
||||
}
|
|
@ -882,52 +882,101 @@ namespace Greenshot.Editor.Drawing
|
|||
|
||||
public void AlignOrStack(string direction, ISurface surface, IDrawableContainer target, IDrawableContainerList parent)
|
||||
{
|
||||
int newBackgroundWidth = 0, newBackgroundHeight = 0, imageBorder = 0, xMovement = 0, yMovement = 0;
|
||||
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;
|
||||
}
|
||||
// Make calculations
|
||||
int left = 0, right = 0, top = 0, bottom = 0;
|
||||
if (direction == "left")
|
||||
left = target.Width;
|
||||
else if (direction == "right")
|
||||
right = target.Width;
|
||||
else if (direction == "top")
|
||||
top = target.Height;
|
||||
else if (direction == "bottom")
|
||||
bottom = target.Height;
|
||||
|
||||
// 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")
|
||||
{
|
||||
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);
|
||||
SnapToEdge("left", surface, target);
|
||||
surface.DeselectAllElements();
|
||||
|
||||
//if (direction == "left")
|
||||
//{
|
||||
// left = target.Width;
|
||||
// bottom = Math.Max(target.Height - surface.Image.Height, 0);
|
||||
// xMovement = -target.Location.X;
|
||||
// yMovement = -target.Location.Y;
|
||||
//}
|
||||
//else if (direction == "right")
|
||||
//{
|
||||
// right = target.Width;
|
||||
// bottom = Math.Max(target.Height - surface.Image.Height, 0);
|
||||
// xMovement = surface.Image.Width - target.Location.X;
|
||||
// yMovement = -target.Location.Y;
|
||||
//}
|
||||
//else if (direction == "top")
|
||||
//{
|
||||
// top = target.Height;
|
||||
// right = Math.Max(target.Width - surface.Image.Width, 0);
|
||||
//}
|
||||
//else if (direction == "bottom")
|
||||
//{
|
||||
// bottom = target.Height;
|
||||
// right = Math.Max(target.Width - surface.Image.Width, 0);
|
||||
//}
|
||||
|
||||
|
||||
|
||||
//int newBackgroundWidth = 0, newBackgroundHeight = 0, imageBorder = 0;
|
||||
//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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -104,6 +104,14 @@ namespace Greenshot.Editor.Drawing
|
|||
remove => _surfaceSizeChanged -= value;
|
||||
}
|
||||
|
||||
[NonSerialized] private SurfaceExpandedEventHandler _surfaceExpanded;
|
||||
|
||||
public event SurfaceExpandedEventHandler SurfaceExpanded
|
||||
{
|
||||
add => _surfaceExpanded += value;
|
||||
remove => _surfaceExpanded -= value;
|
||||
}
|
||||
|
||||
[NonSerialized] private 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>
|
||||
/// Apply a bitmap effect to the surface
|
||||
/// </summary>
|
||||
|
|
|
@ -229,6 +229,7 @@ namespace Greenshot.Editor.Forms
|
|||
_surface.MovingElementChanged += delegate { RefreshEditorControls(); };
|
||||
_surface.DrawingModeChanged += Surface_DrawingModeChanged;
|
||||
_surface.SurfaceSizeChanged += SurfaceSizeChanged;
|
||||
_surface.SurfaceExpanded += SurfaceExpanded;
|
||||
_surface.SurfaceMessage += SurfaceMessageReceived;
|
||||
_surface.FieldAggregator.FieldChanged += FieldAggregatorFieldChanged;
|
||||
SurfaceSizeChanged(Surface, null);
|
||||
|
@ -514,6 +515,14 @@ namespace Greenshot.Editor.Forms
|
|||
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
|
||||
{
|
||||
get { return _surface; }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue