DuplicateLayer_Change.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using PixiEditor.ChangeableDocument.Changeables.Graph;
  2. using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  3. using PixiEditor.ChangeableDocument.ChangeInfos.NodeGraph;
  4. using PixiEditor.ChangeableDocument.ChangeInfos.Structure;
  5. using PixiEditor.ChangeableDocument.Changes.NodeGraph;
  6. namespace PixiEditor.ChangeableDocument.Changes.Structure;
  7. internal class DuplicateLayer_Change : Change
  8. {
  9. private readonly Guid layerGuid;
  10. private Guid duplicateGuid;
  11. private ConnectionsData? connectionsData;
  12. [GenerateMakeChangeAction]
  13. public DuplicateLayer_Change(Guid layerGuid)
  14. {
  15. this.layerGuid = layerGuid;
  16. }
  17. public override bool InitializeAndValidate(Document target)
  18. {
  19. if (!target.TryFindMember<LayerNode>(layerGuid, out LayerNode? layer))
  20. return false;
  21. duplicateGuid = Guid.NewGuid();
  22. connectionsData = NodeOperations.CreateConnectionsData(layer);
  23. return true;
  24. }
  25. public override OneOf<None, IChangeInfo, List<IChangeInfo>> Apply(Document target, bool firstApply,
  26. out bool ignoreInUndo)
  27. {
  28. (LayerNode existingLayer, Node parent) = ((LayerNode, Node))target.FindChildAndParentOrThrow(layerGuid);
  29. LayerNode clone = (LayerNode)existingLayer.Clone();
  30. clone.Id = duplicateGuid;
  31. InputProperty<Painter?> targetInput = parent.InputProperties.FirstOrDefault(x =>
  32. x.ValueType == typeof(Painter) &&
  33. x.Connection is { Node: StructureNode }) as InputProperty<Painter?>;
  34. List<IChangeInfo> operations = new();
  35. target.NodeGraph.AddNode(clone);
  36. operations.Add(CreateLayer_ChangeInfo.FromLayer(clone));
  37. operations.AddRange(NodeOperations.AppendMember(targetInput, clone.Output, clone.Background, clone.Id));
  38. ignoreInUndo = false;
  39. return operations;
  40. }
  41. public override OneOf<None, IChangeInfo, List<IChangeInfo>> Revert(Document target)
  42. {
  43. var (member, parent) = target.FindChildAndParentOrThrow(duplicateGuid);
  44. target.NodeGraph.RemoveNode(member);
  45. member.Dispose();
  46. List<IChangeInfo> changes = new();
  47. changes.AddRange(NodeOperations.DetachStructureNode(member));
  48. changes.Add(new DeleteStructureMember_ChangeInfo(member.Id));
  49. if (connectionsData is not null)
  50. {
  51. Node originalNode = target.FindNodeOrThrow<Node>(layerGuid);
  52. changes.AddRange(NodeOperations.ConnectStructureNodeProperties(connectionsData, originalNode, target.NodeGraph));
  53. }
  54. return changes;
  55. }
  56. }