|
@@ -1,5 +1,5 @@
|
|
-using PixiEditor.ChangeableDocument.Actions.Generated;
|
|
|
|
-using PixiEditor.ChangeableDocument.Enums;
|
|
|
|
|
|
+using PixiEditor.ChangeableDocument.Enums;
|
|
|
|
+using PixiEditor.Models.Enums;
|
|
using PixiEditor.ViewModels.SubViewModels.Document;
|
|
using PixiEditor.ViewModels.SubViewModels.Document;
|
|
|
|
|
|
namespace PixiEditor.Models.DocumentModels;
|
|
namespace PixiEditor.Models.DocumentModels;
|
|
@@ -16,10 +16,10 @@ internal class DocumentStructureHelper
|
|
|
|
|
|
public void CreateNewStructureMember(StructureMemberType type)
|
|
public void CreateNewStructureMember(StructureMemberType type)
|
|
{
|
|
{
|
|
- var member = doc.SelectedStructureMember;
|
|
|
|
|
|
+ StructureMemberViewModel? member = doc.SelectedStructureMember;
|
|
if (member is null)
|
|
if (member is null)
|
|
{
|
|
{
|
|
- var guid = Guid.NewGuid();
|
|
|
|
|
|
+ Guid guid = Guid.NewGuid();
|
|
//put member on top
|
|
//put member on top
|
|
helpers.ActionAccumulator.AddActions(new CreateStructureMember_Action(doc.StructureRoot.GuidValue, guid, doc.StructureRoot.Children.Count, type));
|
|
helpers.ActionAccumulator.AddActions(new CreateStructureMember_Action(doc.StructureRoot.GuidValue, guid, doc.StructureRoot.Children.Count, type));
|
|
helpers.ActionAccumulator.AddFinishedActions(new StructureMemberName_Action(guid, type == StructureMemberType.Layer ? "New Layer" : "New Folder"));
|
|
helpers.ActionAccumulator.AddFinishedActions(new StructureMemberName_Action(guid, type == StructureMemberType.Layer ? "New Layer" : "New Folder"));
|
|
@@ -27,7 +27,7 @@ internal class DocumentStructureHelper
|
|
}
|
|
}
|
|
if (member is FolderViewModel folder)
|
|
if (member is FolderViewModel folder)
|
|
{
|
|
{
|
|
- var guid = Guid.NewGuid();
|
|
|
|
|
|
+ Guid guid = Guid.NewGuid();
|
|
//put member inside folder on top
|
|
//put member inside folder on top
|
|
helpers.ActionAccumulator.AddActions(new CreateStructureMember_Action(folder.GuidValue, guid, folder.Children.Count, type));
|
|
helpers.ActionAccumulator.AddActions(new CreateStructureMember_Action(folder.GuidValue, guid, folder.Children.Count, type));
|
|
helpers.ActionAccumulator.AddFinishedActions(new StructureMemberName_Action(guid, type == StructureMemberType.Layer ? "New Layer" : "New Folder"));
|
|
helpers.ActionAccumulator.AddFinishedActions(new StructureMemberName_Action(guid, type == StructureMemberType.Layer ? "New Layer" : "New Folder"));
|
|
@@ -35,12 +35,12 @@ internal class DocumentStructureHelper
|
|
}
|
|
}
|
|
if (member is LayerViewModel layer)
|
|
if (member is LayerViewModel layer)
|
|
{
|
|
{
|
|
- var guid = Guid.NewGuid();
|
|
|
|
|
|
+ Guid guid = Guid.NewGuid();
|
|
//put member above the layer
|
|
//put member above the layer
|
|
- var path = FindPath(layer.GuidValue);
|
|
|
|
|
|
+ List<StructureMemberViewModel>? path = FindPath(layer.GuidValue);
|
|
if (path.Count < 2)
|
|
if (path.Count < 2)
|
|
throw new InvalidOperationException("Couldn't find a path to the selected member");
|
|
throw new InvalidOperationException("Couldn't find a path to the selected member");
|
|
- var parent = (FolderViewModel)path[1];
|
|
|
|
|
|
+ FolderViewModel? parent = (FolderViewModel)path[1];
|
|
helpers.ActionAccumulator.AddActions(new CreateStructureMember_Action(parent.GuidValue, guid, parent.Children.IndexOf(layer) + 1, type));
|
|
helpers.ActionAccumulator.AddActions(new CreateStructureMember_Action(parent.GuidValue, guid, parent.Children.IndexOf(layer) + 1, type));
|
|
helpers.ActionAccumulator.AddFinishedActions(new StructureMemberName_Action(guid, type == StructureMemberType.Layer ? "New Layer" : "New Folder"));
|
|
helpers.ActionAccumulator.AddFinishedActions(new StructureMemberName_Action(guid, type == StructureMemberType.Layer ? "New Layer" : "New Folder"));
|
|
return;
|
|
return;
|
|
@@ -51,7 +51,7 @@ internal class DocumentStructureHelper
|
|
public StructureMemberViewModel FindOrThrow(Guid guid) => Find(guid) ?? throw new ArgumentException("Could not find member with guid " + guid.ToString());
|
|
public StructureMemberViewModel FindOrThrow(Guid guid) => Find(guid) ?? throw new ArgumentException("Could not find member with guid " + guid.ToString());
|
|
public StructureMemberViewModel? Find(Guid guid)
|
|
public StructureMemberViewModel? Find(Guid guid)
|
|
{
|
|
{
|
|
- var list = FindPath(guid);
|
|
|
|
|
|
+ List<StructureMemberViewModel>? list = FindPath(guid);
|
|
return list.Count > 0 ? list[0] : null;
|
|
return list.Count > 0 ? list[0] : null;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -61,13 +61,13 @@ internal class DocumentStructureHelper
|
|
}
|
|
}
|
|
private StructureMemberViewModel? FindFirstWhere(Predicate<StructureMemberViewModel> predicate, FolderViewModel folderVM)
|
|
private StructureMemberViewModel? FindFirstWhere(Predicate<StructureMemberViewModel> predicate, FolderViewModel folderVM)
|
|
{
|
|
{
|
|
- foreach (var child in folderVM.Children)
|
|
|
|
|
|
+ foreach (StructureMemberViewModel? child in folderVM.Children)
|
|
{
|
|
{
|
|
if (predicate(child))
|
|
if (predicate(child))
|
|
return child;
|
|
return child;
|
|
if (child is FolderViewModel innerFolderVM)
|
|
if (child is FolderViewModel innerFolderVM)
|
|
{
|
|
{
|
|
- var result = FindFirstWhere(predicate, innerFolderVM);
|
|
|
|
|
|
+ StructureMemberViewModel? result = FindFirstWhere(predicate, innerFolderVM);
|
|
if (result is not null)
|
|
if (result is not null)
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
@@ -77,14 +77,14 @@ internal class DocumentStructureHelper
|
|
|
|
|
|
public (StructureMemberViewModel, FolderViewModel) FindChildAndParentOrThrow(Guid childGuid)
|
|
public (StructureMemberViewModel, FolderViewModel) FindChildAndParentOrThrow(Guid childGuid)
|
|
{
|
|
{
|
|
- var path = FindPath(childGuid);
|
|
|
|
|
|
+ List<StructureMemberViewModel>? path = FindPath(childGuid);
|
|
if (path.Count < 2)
|
|
if (path.Count < 2)
|
|
throw new ArgumentException("Couldn't find child and parent");
|
|
throw new ArgumentException("Couldn't find child and parent");
|
|
return (path[0], (FolderViewModel)path[1]);
|
|
return (path[0], (FolderViewModel)path[1]);
|
|
}
|
|
}
|
|
public List<StructureMemberViewModel> FindPath(Guid guid)
|
|
public List<StructureMemberViewModel> FindPath(Guid guid)
|
|
{
|
|
{
|
|
- var list = new List<StructureMemberViewModel>();
|
|
|
|
|
|
+ List<StructureMemberViewModel>? list = new List<StructureMemberViewModel>();
|
|
if (FillPath(doc.StructureRoot, guid, list))
|
|
if (FillPath(doc.StructureRoot, guid, list))
|
|
list.Add(doc.StructureRoot);
|
|
list.Add(doc.StructureRoot);
|
|
return list;
|
|
return list;
|
|
@@ -96,7 +96,7 @@ internal class DocumentStructureHelper
|
|
{
|
|
{
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
- foreach (var member in folder.Children)
|
|
|
|
|
|
+ foreach (StructureMemberViewModel? member in folder.Children)
|
|
{
|
|
{
|
|
if (member is LayerViewModel childLayer && childLayer.GuidValue == guid)
|
|
if (member is LayerViewModel childLayer && childLayer.GuidValue == guid)
|
|
{
|
|
{
|
|
@@ -115,29 +115,71 @@ internal class DocumentStructureHelper
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
- public void MoveStructureMember(Guid guid, bool toSmallerIndex)
|
|
|
|
|
|
+ private void HandleMoveInside(List<StructureMemberViewModel> memberToMovePath, List<StructureMemberViewModel> memberToMoveIntoPath)
|
|
{
|
|
{
|
|
- var path = FindPath(guid);
|
|
|
|
- if (path.Count < 2)
|
|
|
|
- throw new ArgumentException("Couldn't find the member to be moved");
|
|
|
|
- if (path.Count == 2)
|
|
|
|
- {
|
|
|
|
- int curIndex = doc.StructureRoot.Children.IndexOf(path[0]);
|
|
|
|
- if ((curIndex == 0 && toSmallerIndex) || (curIndex == doc.StructureRoot.Children.Count - 1 && !toSmallerIndex))
|
|
|
|
- return;
|
|
|
|
- helpers.ActionAccumulator.AddFinishedActions(new MoveStructureMember_Action(guid, doc.StructureRoot.GuidValue, toSmallerIndex ? curIndex - 1 : curIndex + 1));
|
|
|
|
|
|
+ if (memberToMoveIntoPath[0] is not FolderViewModel folder)
|
|
return;
|
|
return;
|
|
- }
|
|
|
|
- var folder = (FolderViewModel)path[1];
|
|
|
|
- int index = folder.Children.IndexOf(path[0]);
|
|
|
|
- if ((toSmallerIndex && index > 0) || (!toSmallerIndex && index < folder.Children.Count - 1))
|
|
|
|
|
|
+ int index = folder.Children.Count;
|
|
|
|
+ if (memberToMoveIntoPath[0].GuidValue == memberToMovePath[1].GuidValue) // member is already in this folder
|
|
|
|
+ index--;
|
|
|
|
+ helpers.ActionAccumulator.AddFinishedActions(new MoveStructureMember_Action(memberToMovePath[0].GuidValue, folder.GuidValue, index));
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void HandleMoveAboveBelow(List<StructureMemberViewModel> memberToMovePath, List<StructureMemberViewModel> memberToMoveRelativeToPath, bool above)
|
|
|
|
+ {
|
|
|
|
+ FolderViewModel targetFolder = (FolderViewModel)memberToMoveRelativeToPath[1];
|
|
|
|
+ if (memberToMovePath[1].GuidValue == memberToMoveRelativeToPath[1].GuidValue)
|
|
{
|
|
{
|
|
- helpers.ActionAccumulator.AddFinishedActions(new MoveStructureMember_Action(guid, path[1].GuidValue, toSmallerIndex ? index - 1 : index + 1));
|
|
|
|
|
|
+ // members are in the same folder
|
|
|
|
+ int indexOfMemberToMove = targetFolder.Children.IndexOf(memberToMovePath[0]);
|
|
|
|
+ int indexOfMemberToMoveAbove = targetFolder.Children.IndexOf(memberToMoveRelativeToPath[0]);
|
|
|
|
+ int index = indexOfMemberToMoveAbove;
|
|
|
|
+ if (above)
|
|
|
|
+ index++;
|
|
|
|
+ if (indexOfMemberToMove < indexOfMemberToMoveAbove)
|
|
|
|
+ index--;
|
|
|
|
+ helpers.ActionAccumulator.AddFinishedActions(new MoveStructureMember_Action(memberToMovePath[0].GuidValue, targetFolder.GuidValue, index));
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- int parentIndex = ((FolderViewModel)path[2]).Children.IndexOf(folder);
|
|
|
|
- helpers.ActionAccumulator.AddFinishedActions(new MoveStructureMember_Action(guid, path[2].GuidValue, toSmallerIndex ? parentIndex : parentIndex + 1));
|
|
|
|
|
|
+ // members are in different folders
|
|
|
|
+ int index = targetFolder.Children.IndexOf(memberToMoveRelativeToPath[0]);
|
|
|
|
+ if (above)
|
|
|
|
+ index++;
|
|
|
|
+ helpers.ActionAccumulator.AddFinishedActions(new MoveStructureMember_Action(memberToMovePath[0].GuidValue, targetFolder.GuidValue, index));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void TryMoveStructureMember(Guid memberToMove, Guid memberToMoveIntoOrNextTo, StructureMemberPlacement placement)
|
|
|
|
+ {
|
|
|
|
+ List<StructureMemberViewModel> memberPath = FindPath(memberToMove);
|
|
|
|
+ List<StructureMemberViewModel> refPath = FindPath(memberToMoveIntoOrNextTo);
|
|
|
|
+ if (memberPath.Count < 2 || refPath.Count < 2)
|
|
|
|
+ return;
|
|
|
|
+ switch (placement)
|
|
|
|
+ {
|
|
|
|
+ case StructureMemberPlacement.Above:
|
|
|
|
+ HandleMoveAboveBelow(memberPath, refPath, true);
|
|
|
|
+ break;
|
|
|
|
+ case StructureMemberPlacement.Below:
|
|
|
|
+ HandleMoveAboveBelow(memberPath, refPath, false);
|
|
|
|
+ break;
|
|
|
|
+ case StructureMemberPlacement.Inside:
|
|
|
|
+ HandleMoveInside(memberPath, refPath);
|
|
|
|
+ break;
|
|
|
|
+ case StructureMemberPlacement.BelowOutsideFolder:
|
|
|
|
+ {
|
|
|
|
+ FolderViewModel refFolder = (FolderViewModel)refPath[1];
|
|
|
|
+ int refIndexInParent = refFolder.Children.IndexOf(refPath[0]);
|
|
|
|
+ if (refIndexInParent > 0 || refPath.Count == 2)
|
|
|
|
+ {
|
|
|
|
+ HandleMoveAboveBelow(memberPath, refPath, false);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ HandleMoveAboveBelow(memberPath, FindPath(refPath[1].GuidValue), false);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|