ShareX/ShareX.ScreenCaptureLib/Helpers/ImageEditorHistory.cs

156 lines
5.1 KiB
C#
Raw Normal View History

2023-12-27 17:57:06 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2023-12-27 17:57:06 +13:00
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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace ShareX.ScreenCaptureLib
{
internal class ImageEditorHistory : IDisposable
{
2023-12-27 18:26:53 +13:00
public bool CanUndo => undoMementoStack.Count > 0;
public bool CanRedo => redoMementoStack.Count > 0;
2023-12-27 17:57:06 +13:00
2023-12-27 20:27:54 +13:00
private readonly ShapeManager shapeManager;
2023-12-27 18:26:53 +13:00
private Stack<ImageEditorMemento> undoMementoStack = new Stack<ImageEditorMemento>();
private Stack<ImageEditorMemento> redoMementoStack = new Stack<ImageEditorMemento>();
2023-12-27 17:57:06 +13:00
public ImageEditorHistory(ShapeManager shapeManager)
{
this.shapeManager = shapeManager;
}
2023-12-27 18:26:53 +13:00
private void AddMemento(ImageEditorMemento memento)
{
2023-12-27 18:26:53 +13:00
undoMementoStack.Push(memento);
2023-12-27 17:57:06 +13:00
foreach (ImageEditorMemento redoMemento in redoMementoStack)
{
2023-12-27 20:27:54 +13:00
redoMemento?.Dispose();
}
2023-12-27 17:57:06 +13:00
redoMementoStack.Clear();
}
private ImageEditorMemento GetMementoFromCanvas()
{
2023-12-27 17:57:06 +13:00
List<BaseShape> shapes = shapeManager.Shapes.Select(x => x.Duplicate()).ToList();
2023-12-27 18:26:53 +13:00
Bitmap canvas = (Bitmap)shapeManager.Form.Canvas.Clone();
2023-12-27 22:20:03 +13:00
return new ImageEditorMemento(shapes, shapeManager.Form.CanvasRectangle, canvas);
2023-12-27 18:26:53 +13:00
}
2023-12-27 18:26:53 +13:00
private ImageEditorMemento GetMementoFromShapes()
{
List<BaseShape> shapes = shapeManager.Shapes.Select(x => x.Duplicate()).ToList();
2023-12-27 22:20:03 +13:00
return new ImageEditorMemento(shapes, shapeManager.Form.CanvasRectangle);
}
public void CreateCanvasMemento()
{
2023-12-27 18:26:53 +13:00
ImageEditorMemento memento = GetMementoFromCanvas();
AddMemento(memento);
}
public void CreateShapesMemento()
{
2023-12-27 21:42:38 +13:00
if (!shapeManager.IsCurrentShapeTypeRegion && shapeManager.CurrentTool != ShapeType.ToolCrop && shapeManager.CurrentTool != ShapeType.ToolCutOut)
{
ImageEditorMemento memento = GetMementoFromShapes();
AddMemento(memento);
}
}
public void Undo()
{
2023-12-27 18:26:53 +13:00
if (CanUndo)
{
2023-12-27 20:27:54 +13:00
ImageEditorMemento undoMemento = undoMementoStack.Pop();
2023-12-27 20:27:54 +13:00
if (undoMemento.Shapes != null)
{
2023-12-27 20:27:54 +13:00
if (undoMemento.Canvas == null)
{
2023-12-27 20:27:54 +13:00
ImageEditorMemento redoMemento = GetMementoFromShapes();
redoMementoStack.Push(redoMemento);
2023-12-27 20:27:54 +13:00
shapeManager.RestoreState(undoMemento);
}
else
{
2023-12-27 20:27:54 +13:00
ImageEditorMemento redoMemento = GetMementoFromCanvas();
redoMementoStack.Push(redoMemento);
2023-12-27 20:27:54 +13:00
shapeManager.RestoreState(undoMemento);
}
}
}
}
public void Redo()
{
2023-12-27 18:26:53 +13:00
if (CanRedo)
{
2023-12-27 17:57:06 +13:00
ImageEditorMemento redoMemento = redoMementoStack.Pop();
if (redoMemento.Shapes != null)
{
if (redoMemento.Canvas == null)
{
2023-12-27 20:27:54 +13:00
ImageEditorMemento undoMemento = GetMementoFromShapes();
undoMementoStack.Push(undoMemento);
2023-12-27 18:26:53 +13:00
shapeManager.RestoreState(redoMemento);
}
else
{
2023-12-27 20:27:54 +13:00
ImageEditorMemento undoMemento = GetMementoFromCanvas();
undoMementoStack.Push(undoMemento);
2023-12-27 18:26:53 +13:00
shapeManager.RestoreState(redoMemento);
}
}
}
}
2023-12-27 17:57:06 +13:00
public void Dispose()
{
2023-12-27 20:27:54 +13:00
foreach (ImageEditorMemento undoMemento in undoMementoStack)
{
2023-12-27 20:27:54 +13:00
undoMemento?.Dispose();
}
2023-12-27 17:57:06 +13:00
2023-12-27 18:26:53 +13:00
undoMementoStack.Clear();
2023-12-27 17:57:06 +13:00
foreach (ImageEditorMemento redoMemento in redoMementoStack)
{
2023-12-27 20:27:54 +13:00
redoMemento?.Dispose();
2023-12-27 17:57:06 +13:00
}
2023-12-27 17:57:06 +13:00
redoMementoStack.Clear();
}
}
2023-12-27 17:57:06 +13:00
}