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
|
|
|
|
|
Copyright (c) 2007-2023 ShareX Team
|
|
|
|
|
|
|
|
|
|
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;
|
2023-12-27 13:58:05 +13:00
|
|
|
|
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 18:26:53 +13:00
|
|
|
|
private Stack<ImageEditorMemento> undoMementoStack = new Stack<ImageEditorMemento>();
|
2023-12-27 13:58:05 +13:00
|
|
|
|
private Stack<ImageEditorMemento> redoMementoStack = new Stack<ImageEditorMemento>();
|
|
|
|
|
private readonly ShapeManager shapeManager;
|
2023-12-27 17:57:06 +13:00
|
|
|
|
|
2023-12-27 13:58:05 +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 13:58:05 +13:00
|
|
|
|
{
|
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 13:58:05 +13:00
|
|
|
|
{
|
2023-12-27 17:57:06 +13:00
|
|
|
|
redoMemento.Dispose();
|
2023-12-27 13:58:05 +13:00
|
|
|
|
}
|
2023-12-27 17:57:06 +13:00
|
|
|
|
|
2023-12-27 13:58:05 +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();
|
|
|
|
|
return new ImageEditorMemento(shapes, canvas);
|
|
|
|
|
}
|
2023-12-27 13:58:05 +13:00
|
|
|
|
|
2023-12-27 18:26:53 +13:00
|
|
|
|
private ImageEditorMemento GetMementoFromShapes()
|
|
|
|
|
{
|
|
|
|
|
List<BaseShape> shapes = shapeManager.Shapes.Select(x => x.Duplicate()).ToList();
|
|
|
|
|
return new ImageEditorMemento(shapes);
|
2023-12-27 13:58:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreateCanvasMemento()
|
|
|
|
|
{
|
2023-12-27 18:26:53 +13:00
|
|
|
|
ImageEditorMemento memento = GetMementoFromCanvas();
|
|
|
|
|
AddMemento(memento);
|
2023-12-27 13:58:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CreateShapesMemento()
|
|
|
|
|
{
|
2023-12-27 18:26:53 +13:00
|
|
|
|
ImageEditorMemento memento = GetMementoFromShapes();
|
|
|
|
|
AddMemento(memento);
|
2023-12-27 13:58:05 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Undo()
|
|
|
|
|
{
|
2023-12-27 18:26:53 +13:00
|
|
|
|
if (CanUndo)
|
2023-12-27 13:58:05 +13:00
|
|
|
|
{
|
|
|
|
|
ImageEditorMemento redoMemento;
|
2023-12-27 18:26:53 +13:00
|
|
|
|
ImageEditorMemento memento = undoMementoStack.Pop();
|
2023-12-27 13:58:05 +13:00
|
|
|
|
|
|
|
|
|
if (memento.Shapes != null)
|
|
|
|
|
{
|
|
|
|
|
if (memento.Canvas == null)
|
|
|
|
|
{
|
2023-12-27 18:26:53 +13:00
|
|
|
|
redoMemento = GetMementoFromShapes();
|
2023-12-27 13:58:05 +13:00
|
|
|
|
redoMementoStack.Push(redoMemento);
|
|
|
|
|
|
2023-12-27 18:26:53 +13:00
|
|
|
|
shapeManager.RestoreState(memento);
|
2023-12-27 13:58:05 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
redoMemento = GetMementoFromCanvas();
|
|
|
|
|
redoMementoStack.Push(redoMemento);
|
|
|
|
|
|
2023-12-27 18:26:53 +13:00
|
|
|
|
shapeManager.RestoreState(memento);
|
2023-12-27 13:58:05 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Redo()
|
|
|
|
|
{
|
2023-12-27 18:26:53 +13:00
|
|
|
|
if (CanRedo)
|
2023-12-27 13:58:05 +13:00
|
|
|
|
{
|
2023-12-27 17:57:06 +13:00
|
|
|
|
ImageEditorMemento redoMemento = redoMementoStack.Pop();
|
|
|
|
|
|
2023-12-27 13:58:05 +13:00
|
|
|
|
if (redoMemento.Shapes != null)
|
|
|
|
|
{
|
|
|
|
|
if (redoMemento.Canvas == null)
|
|
|
|
|
{
|
2023-12-27 18:26:53 +13:00
|
|
|
|
ImageEditorMemento memento = GetMementoFromShapes();
|
|
|
|
|
undoMementoStack.Push(memento);
|
2023-12-27 13:58:05 +13:00
|
|
|
|
|
2023-12-27 18:26:53 +13:00
|
|
|
|
shapeManager.RestoreState(redoMemento);
|
2023-12-27 13:58:05 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-12-27 17:57:06 +13:00
|
|
|
|
ImageEditorMemento memento = GetMementoFromCanvas();
|
2023-12-27 18:26:53 +13:00
|
|
|
|
undoMementoStack.Push(memento);
|
2023-12-27 13:58:05 +13:00
|
|
|
|
|
2023-12-27 18:26:53 +13:00
|
|
|
|
shapeManager.RestoreState(redoMemento);
|
2023-12-27 13:58:05 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-27 17:57:06 +13:00
|
|
|
|
|
2023-12-27 13:58:05 +13:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2023-12-27 18:26:53 +13:00
|
|
|
|
foreach (ImageEditorMemento memento in undoMementoStack)
|
2023-12-27 13:58:05 +13:00
|
|
|
|
{
|
2023-12-27 17:57:06 +13:00
|
|
|
|
memento.Dispose();
|
2023-12-27 13:58:05 +13:00
|
|
|
|
}
|
2023-12-27 17:57:06 +13:00
|
|
|
|
|
2023-12-27 18:26:53 +13:00
|
|
|
|
undoMementoStack.Clear();
|
2023-12-27 13:58:05 +13:00
|
|
|
|
|
2023-12-27 17:57:06 +13:00
|
|
|
|
foreach (ImageEditorMemento redoMemento in redoMementoStack)
|
|
|
|
|
{
|
|
|
|
|
redoMemento.Dispose();
|
|
|
|
|
}
|
2023-12-27 13:58:05 +13:00
|
|
|
|
|
2023-12-27 17:57:06 +13:00
|
|
|
|
redoMementoStack.Clear();
|
2023-12-27 13:58:05 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-27 17:57:06 +13:00
|
|
|
|
}
|