Skip to content

Commit

Permalink
Fixed UndoManager, Selection and close individual documents
Browse files Browse the repository at this point in the history
  • Loading branch information
flabbet committed Dec 19, 2020
1 parent bfeb340 commit d841ed1
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 87 deletions.
13 changes: 13 additions & 0 deletions PixiEditor/Models/Controllers/BitmapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ public static bool IsOperationTool(Tool tool)
return tool is BitmapOperationTool;
}

public void CloseDocument(Document document)
{
int nextIndex = 0;
if (document == ActiveDocument)
{
nextIndex = Documents.Count > 1 ? Documents.IndexOf(document) : -1;
nextIndex += nextIndex > 0 ? -1 : 0;
}

Documents.Remove(document);
ActiveDocument = nextIndex >= 0 ? Documents[nextIndex] : null;
}

public void ExecuteTool(Coordinates newPosition, bool clickedOnCanvas)
{
if (SelectedTool.CanStartOutsideCanvas || clickedOnCanvas)
Expand Down
2 changes: 1 addition & 1 deletion PixiEditor/Models/Controllers/BitmapOperationsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void DeletePixels(Layer[] layers, Coordinates[] pixels)
layers[i].SetPixels(changes);
}

UndoManager.AddUndoChange(new Change("UndoChanges", old, newChange, "Deleted pixels"));
Manager.ActiveDocument.UndoManager.AddUndoChange(new Change("UndoChanges", old, newChange, "Deleted pixels"));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion PixiEditor/Models/Controllers/ClipboardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void PasteFromClipboard()
{
AddImageToLayers(image);
int latestLayerIndex = ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.Count - 1;
UndoManager.AddUndoChange(
ViewModelMain.Current.BitmapManager.ActiveDocument.UndoManager.AddUndoChange(
new Change(RemoveLayerProcess, new object[] { latestLayerIndex }, AddLayerProcess, new object[] { image }));
}
}
Expand Down
30 changes: 18 additions & 12 deletions PixiEditor/Models/Controllers/UndoManager.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
using System.Collections.Generic;
using System.Linq;
using PixiEditor.Models.DataHolders;
using PixiEditor.ViewModels;

namespace PixiEditor.Models.Controllers
{
public static class UndoManager
public class UndoManager
{
private static bool lastChangeWasUndo;
private bool lastChangeWasUndo;

public static Stack<Change> UndoStack { get; set; } = new Stack<Change>();
public Stack<Change> UndoStack { get; set; } = new Stack<Change>();

public static Stack<Change> RedoStack { get; set; } = new Stack<Change>();
public Stack<Change> RedoStack { get; set; } = new Stack<Change>();

public static bool CanUndo => UndoStack.Count > 0;
public bool CanUndo => UndoStack.Count > 0;

public static bool CanRedo => RedoStack.Count > 0;
public bool CanRedo => RedoStack.Count > 0;

public static object MainRoot { get; set; }
public object MainRoot { get; set; }

public UndoManager()
{
SetMainRoot(ViewModelMain.Current.UndoSubViewModel);
}

/// <summary>
/// Sets object(root) in which undo properties are stored.
/// </summary>
/// <param name="root">Parent object.</param>
public static void SetMainRoot(object root)
public void SetMainRoot(object root)
{
MainRoot = root;
}

/// <summary>
/// Adds property change to UndoStack.
/// </summary>
public static void AddUndoChange(Change change)
public void AddUndoChange(Change change)
{
lastChangeWasUndo = false;

Expand All @@ -47,7 +53,7 @@ public static void AddUndoChange(Change change)
/// <summary>
/// Sets top property in UndoStack to Old Value.
/// </summary>
public static void Undo()
public void Undo()
{
lastChangeWasUndo = true;
Change change = UndoStack.Pop();
Expand All @@ -66,7 +72,7 @@ public static void Undo()
/// <summary>
/// Sets top property from RedoStack to old value.
/// </summary>
public static void Redo()
public void Redo()
{
lastChangeWasUndo = true;
Change change = RedoStack.Pop();
Expand All @@ -82,7 +88,7 @@ public static void Redo()
UndoStack.Push(change);
}

private static void SetPropertyValue(object target, string propName, object value)
private void SetPropertyValue(object target, string propName, object value)
{
string[] bits = propName.Split('.');
for (int i = 0; i < bits.Length - 1; i++)
Expand Down
47 changes: 47 additions & 0 deletions PixiEditor/Models/DataHolders/Document.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
Expand All @@ -8,8 +9,10 @@
using PixiEditor.Helpers;
using PixiEditor.Models.Controllers;
using PixiEditor.Models.Enums;
using PixiEditor.Models.IO;
using PixiEditor.Models.Layers;
using PixiEditor.Models.Position;
using PixiEditor.ViewModels;

namespace PixiEditor.Models.DataHolders
{
Expand All @@ -23,13 +26,17 @@ public Document(int width, int height)
{
Width = width;
Height = height;
RequestCloseDocumentCommand = new RelayCommand(RequestCloseDocument);
UndoManager = new UndoManager();
DocumentSizeChanged?.Invoke(this, new DocumentSizeChangedEventArgs(0, 0, width, height));
}

public event EventHandler<DocumentSizeChangedEventArgs> DocumentSizeChanged;

public event EventHandler<LayersChangedEventArgs> LayersChanged;

public RelayCommand RequestCloseDocumentCommand { get; set; }

private string documentFilePath = string.Empty;

public string DocumentFilePath
Expand Down Expand Up @@ -82,6 +89,20 @@ public int Height
}
}

private Selection selection = new Selection(Array.Empty<Coordinates>());

public Selection ActiveSelection
{
get => selection;
set
{
selection = value;
RaisePropertyChanged("ActiveSelection");
}
}

public UndoManager UndoManager { get; set; }

public ObservableCollection<Layer> Layers { get; set; } = new ObservableCollection<Layer>();

public Layer ActiveLayer => Layers.Count > 0 ? Layers[ActiveLayerIndex] : null;
Expand All @@ -97,6 +118,24 @@ public int ActiveLayerIndex
}
}

public void SaveWithDialog()
{
bool savedSuccessfully = Exporter.SaveAsEditableFileWithDialog(this, out string path);
DocumentFilePath = path;
ChangesSaved = savedSuccessfully;
}

public void Save()
{
Save(DocumentFilePath);
}

public void Save(string path)
{
DocumentFilePath = Exporter.SaveAsEditableFile(this, path);
ChangesSaved = true;
}

public ObservableCollection<Color> Swatches { get; set; } = new ObservableCollection<Color>();

/// <summary>
Expand Down Expand Up @@ -247,6 +286,9 @@ public void ClipCanvas()
"Clip canvas"));
}

/// <summary>
/// Centers content inside document.
/// </summary>
public void CenterContent()
{
DoubleCords points = GetEdgePoints();
Expand Down Expand Up @@ -277,6 +319,11 @@ public void CenterContent()
"Center content"));
}

private void RequestCloseDocument(object parameter)
{
ViewModelMain.Current.DocumentSubViewModel.RequestCloseDocument(this);
}

private int GetOffsetXForAnchor(int srcWidth, int destWidth, AnchorPoint anchor)
{
if (anchor.HasFlag(AnchorPoint.Center))
Expand Down
3 changes: 2 additions & 1 deletion PixiEditor/Models/Tools/Tool.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Windows.Input;
using PixiEditor.Helpers;
using PixiEditor.Models.Controllers;
using PixiEditor.Models.Tools.ToolSettings;
using PixiEditor.Models.Tools.ToolSettings.Toolbars;

Expand Down Expand Up @@ -72,7 +73,7 @@ public virtual void OnMouseMove(MouseEventArgs e)
{
}

public virtual void AfterAddedUndo()
public virtual void AfterAddedUndo(UndoManager undoManager)
{
}
}
Expand Down
14 changes: 7 additions & 7 deletions PixiEditor/Models/Tools/Tools/MoveTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public override void OnKeyUp(KeyEventArgs e)
}
}

public override void AfterAddedUndo()
public override void AfterAddedUndo(UndoManager undoManager)
{
if (currentSelection != null && currentSelection.Length != 0)
{
// Inject to default undo system change custom changes made by this tool
foreach (var item in startPixelColors)
{
BitmapPixelChanges beforeMovePixels = BitmapPixelChanges.FromArrays(startSelection, item.Value);
Change changes = UndoManager.UndoStack.Peek();
Change changes = undoManager.UndoStack.Peek();
int layerIndex = ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.IndexOf(item.Key);

((LayerChange[])changes.OldValue).First(x => x.LayerIndex == layerIndex).PixelChanges.ChangedPixels
Expand All @@ -86,7 +86,7 @@ public override void OnStoppedRecordingMouseUp(MouseEventArgs e)
{
if (currentSelection != null && currentSelection.Length == 0)
{
UndoManager.AddUndoChange(new Change(
ViewModelMain.Current.BitmapManager.ActiveDocument.UndoManager.AddUndoChange(new Change(
ApplyOffsets,
new object[] { startingOffsets },
ApplyOffsets,
Expand All @@ -105,10 +105,10 @@ public override LayerChange[] Use(Layer layer, Coordinates[] mouseMove, Color co
ResetSelectionValues(start);

// Move offset if no selection
if (ViewModelMain.Current.SelectionSubViewModel.ActiveSelection != null &&
ViewModelMain.Current.SelectionSubViewModel.ActiveSelection.SelectedPoints.Count > 0)
Selection selection = ViewModelMain.Current.BitmapManager.ActiveDocument.ActiveSelection;
if (selection != null && selection.SelectedPoints.Count > 0)
{
currentSelection = ViewModelMain.Current.SelectionSubViewModel.ActiveSelection.SelectedPoints.ToArray();
currentSelection = selection.SelectedPoints.ToArray();
}
else
{
Expand Down Expand Up @@ -161,7 +161,7 @@ public BitmapPixelChanges MoveSelection(Layer layer, Coordinates[] mouseMove)
currentSelection = TranslateSelection(end, out Coordinates[] previousSelection);
if (updateViewModelSelection)
{
ViewModelMain.Current.SelectionSubViewModel.ActiveSelection.SetSelection(currentSelection, SelectionType.New);
ViewModelMain.Current.BitmapManager.ActiveDocument.ActiveSelection.SetSelection(currentSelection, SelectionType.New);
}

ClearSelectedPixels(layer, previousSelection);
Expand Down
19 changes: 11 additions & 8 deletions PixiEditor/Models/Tools/Tools/SelectTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,26 @@ public override void OnRecordingLeftMouseDown(MouseEventArgs e)
SelectionType = selectionType;

oldSelection = null;
if (ViewModelMain.Current.SelectionSubViewModel.ActiveSelection != null &&
ViewModelMain.Current.SelectionSubViewModel.ActiveSelection.SelectedPoints != null)
Selection selection = ViewModelMain.Current.BitmapManager.ActiveDocument.ActiveSelection;
if (selection != null && selection.SelectedPoints != null)
{
oldSelection = ViewModelMain.Current.SelectionSubViewModel.ActiveSelection;
oldSelection = selection;
}
}

public override void OnStoppedRecordingMouseUp(MouseEventArgs e)
{
if (ViewModelMain.Current.SelectionSubViewModel.ActiveSelection.SelectedPoints.Count() <= 1)
if (ViewModelMain.Current.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints.Count() <= 1)
{
// If we have not selected multiple points, clear the selection
ViewModelMain.Current.SelectionSubViewModel.ActiveSelection.Clear();
ViewModelMain.Current.BitmapManager.ActiveDocument.ActiveSelection.Clear();
}

UndoManager.AddUndoChange(
new Change("ActiveSelection", oldSelection, ViewModelMain.Current.SelectionSubViewModel.ActiveSelection, "Select pixels", ViewModelMain.Current.SelectionSubViewModel));
ViewModelMain.Current.BitmapManager.ActiveDocument.UndoManager.AddUndoChange(
new Change("ActiveSelection",
oldSelection,
ViewModelMain.Current.BitmapManager.ActiveDocument.ActiveSelection,
"Select pixels", ViewModelMain.Current.SelectionSubViewModel));
}

public override void Use(Coordinates[] pixels)
Expand Down Expand Up @@ -87,7 +90,7 @@ public IEnumerable<Coordinates> GetAllSelection(Document document)
private void Select(Coordinates[] pixels)
{
IEnumerable<Coordinates> selection = GetRectangleSelectionForPoints(pixels[^1], pixels[0]);
ViewModelMain.Current.SelectionSubViewModel.ActiveSelection.SetSelection(selection, SelectionType);
ViewModelMain.Current.BitmapManager.ActiveDocument.ActiveSelection.SetSelection(selection, SelectionType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Cut(object parameter)
Copy(null);
Owner.BitmapManager.BitmapOperations.DeletePixels(
new[] { Owner.BitmapManager.ActiveDocument.ActiveLayer },
Owner.SelectionSubViewModel.ActiveSelection.SelectedPoints.ToArray());
Owner.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints.ToArray());
}

public void Paste(object parameter)
Expand All @@ -57,7 +57,7 @@ private void Copy(object parameter)
{
ClipboardController.CopyToClipboard(
new[] { Owner.BitmapManager.ActiveDocument.ActiveLayer },
Owner.SelectionSubViewModel.ActiveSelection.SelectedPoints.ToArray(),
Owner.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints.ToArray(),
Owner.BitmapManager.ActiveDocument.Width,
Owner.BitmapManager.ActiveDocument.Height);
}
Expand Down
24 changes: 22 additions & 2 deletions PixiEditor/ViewModels/SubViewModels/Main/DocumentViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Linq;
using System;
using System.Linq;
using PixiEditor.Helpers;
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.Dialogs;
using PixiEditor.Models.Enums;

namespace PixiEditor.ViewModels.SubViewModels.Main
{
Expand Down Expand Up @@ -30,11 +33,28 @@ public void ClipCanvas(object parameter)
Owner.BitmapManager.ActiveDocument?.ClipCanvas();
}

public void RequestCloseDocument(Document document)
{
if (!document.ChangesSaved)
{
ConfirmationType result = ConfirmationDialog.Show(ConfirmationDialogMessage);
if (result == ConfirmationType.Yes)
{
Owner.FileSubViewModel.SaveDocument(false);
}
else if (result == ConfirmationType.Canceled)
{
return;
}
}
Owner.BitmapManager.CloseDocument(document);
}

private void DeletePixels(object parameter)
{
Owner.BitmapManager.BitmapOperations.DeletePixels(
new[] { Owner.BitmapManager.ActiveLayer },
Owner.SelectionSubViewModel.ActiveSelection.SelectedPoints.ToArray());
Owner.BitmapManager.ActiveDocument.ActiveSelection.SelectedPoints.ToArray());
}

private void OpenResizePopup(object parameter)
Expand Down
Loading

0 comments on commit d841ed1

Please sign in to comment.