Skip to content

Commit

Permalink
Fixed groups bug
Browse files Browse the repository at this point in the history
  • Loading branch information
flabbet committed Dec 13, 2021
1 parent acd72db commit 54c3ae1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
18 changes: 13 additions & 5 deletions PixiEditor/Models/Controllers/UndoManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -103,18 +103,25 @@ public void Redo()
/// Merges multiple undo changes into one.
/// </summary>
/// <param name="amount">Amount of changes to squash.</param>
public void SquashUndoChanges(int amount)
/// <param name="reverseOrderInReverseProcess">Reverses order of execution changes in reverseProcess (undo)</param>
public void SquashUndoChanges(int amount, bool reverseOrderInReverseProcess = false)
{
string description = UndoStack.ElementAt(UndoStack.Count - amount).Description;
SquashUndoChanges(amount, description);
if (string.IsNullOrEmpty(description))
{
description = $"Squash {amount} undo changes.";
}

SquashUndoChanges(amount, description, reverseOrderInReverseProcess);
}

/// <summary>
/// Merges multiple undo changes into one.
/// </summary>
/// <param name="amount">Amount of changes to squash.</param>
/// <param name="description">Final change description.</param>
public void SquashUndoChanges(int amount, string description)
/// <param name="reverseOrderInReverseProcess">Reverses order of execution changes in reverseProcess (undo)</param>
public void SquashUndoChanges(int amount, string description, bool reverseOrderInReverseProcess = false)
{
Change[] changes = new Change[amount];
for (int i = 0; i < amount; i++)
Expand All @@ -124,7 +131,8 @@ public void SquashUndoChanges(int amount, string description)

Action<object[]> reverseProcess = (object[] props) =>
{
foreach (var prop in props)
IEnumerable<object> enumerable = reverseOrderInReverseProcess ? props.Reverse() : props;
foreach (var prop in enumerable)
{
Change change = (Change)prop;
if (change.ReverseProcess == null)
Expand Down
2 changes: 1 addition & 1 deletion PixiEditor/Models/DataHolders/Document/Document.Layers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public void AddLayerStructureToUndo(WpfObservableRangeCollection<GuidStructureIt
BuildLayerStructureProcess,
new object[] { oldLayerStructureGroups },
BuildLayerStructureProcess,
new object[] { LayerStructure.CloneGroups() }));
new object[] { LayerStructure.CloneGroups() }, "Reload LayerStructure"));
}

public Layer MergeLayers(Layer[] layersToMerge, bool nameOfLast, int index)
Expand Down
5 changes: 2 additions & 3 deletions PixiEditor/Models/Tools/BitmapOperationTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public override void BeforeUse()
/// <summary>
/// Executes undo adding procedure.
/// </summary>
/// <param name="document">Active document</param>
/// <remarks>When overriding, set UseDefaultUndoMethod to false.</remarks>
public override void AfterUse()
{
Expand All @@ -40,8 +39,8 @@ public override void AfterUse()
var document = ViewModels.ViewModelMain.Current.BitmapManager.ActiveDocument;
var args = new object[] { _change.Document };
document.UndoManager.AddUndoChange(_change.ToChange(StorageBasedChange.BasicUndoProcess, args));
document.AddLayerStructureToUndo(document.LayerStructure.Groups);
document.UndoManager.SquashUndoChanges(2);
document.AddLayerStructureToUndo(document.LayerStructure.CloneGroups());
document.UndoManager.SquashUndoChanges(2, true);
_change = null;
}
}
Expand Down
4 changes: 1 addition & 3 deletions PixiEditor/Models/Undo/StorageBasedChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class StorageBasedChange
public UndoLayer[] StoredLayers { get; set; }

private List<Guid> layersToStore;

public Document Document { get; }

public StorageBasedChange(Document doc, IEnumerable<Layer> layers, bool saveOnStartup = true)
Expand Down Expand Up @@ -260,11 +259,10 @@ public static void BasicUndoProcess(Layer[] layers, UndoLayer[] data, object[] a
for (int i = 0; i < layers.Length; i++)
{
Layer layer = layers[i];
document.RemoveLayer(data[i].LayerIndex, false);

document.RemoveLayer(data[i].LayerIndex, false);
document.Layers.Insert(data[i].LayerIndex, layer);


if (data[i].IsActive)
{
document.SetMainActiveLayer(data[i].LayerIndex);
Expand Down
5 changes: 5 additions & 0 deletions PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,18 @@ public void NewLayer(object parameter)

Guid lastActiveLayerGuid = doc.ActiveLayerGuid;


doc.AddNewLayer($"New Layer {Owner.BitmapManager.ActiveDocument.Layers.Count}");

var oldGroups = doc.LayerStructure.CloneGroups();

if (doc.Layers.Count > 1)
{
doc.MoveLayerInStructure(doc.Layers[^1].LayerGuid, lastActiveLayerGuid, true);
Guid? parent = parameter is Layer or LayerStructureItemContainer ? activeLayerParent?.GroupGuid : activeLayerParent.Parent?.GroupGuid;
doc.LayerStructure.AssignParent(doc.ActiveLayerGuid, parent);
doc.AddLayerStructureToUndo(oldGroups);
doc.UndoManager.SquashUndoChanges(3, "Add New Layer");
}
if (control != null)
{
Expand Down

0 comments on commit 54c3ae1

Please sign in to comment.