DuplicateNode_Change.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
  2. using PixiEditor.ChangeableDocument.ChangeInfos.NodeGraph;
  3. namespace PixiEditor.ChangeableDocument.Changes.NodeGraph;
  4. internal class DuplicateNode_Change : Change
  5. {
  6. private Guid nodeGuid;
  7. private Guid createdNodeGuid;
  8. [GenerateMakeChangeAction]
  9. public DuplicateNode_Change(Guid nodeGuid, Guid newGuid)
  10. {
  11. this.nodeGuid = nodeGuid;
  12. createdNodeGuid = newGuid;
  13. }
  14. public override bool InitializeAndValidate(Document target)
  15. {
  16. return target.TryFindNode(nodeGuid, out Node node) && node.GetNodeTypeUniqueName() != OutputNode.UniqueName;
  17. }
  18. public override OneOf<None, IChangeInfo, List<IChangeInfo>> Apply(Document target, bool firstApply,
  19. out bool ignoreInUndo)
  20. {
  21. Node existingNode = target.FindNode(nodeGuid);
  22. Node clone = existingNode.Clone();
  23. clone.Id = createdNodeGuid;
  24. target.NodeGraph.AddNode(clone);
  25. ignoreInUndo = false;
  26. return CreateNode_ChangeInfo.CreateFromNode(clone);
  27. }
  28. public override OneOf<None, IChangeInfo, List<IChangeInfo>> Revert(Document target)
  29. {
  30. var node = target.FindNode(createdNodeGuid);
  31. target.NodeGraph.RemoveNode(node);
  32. node.Dispose();
  33. return new DeleteNode_ChangeInfo(node.Id);
  34. }
  35. public override bool IsMergeableWith(Change other)
  36. {
  37. return other is DuplicateNode_Change;
  38. }
  39. }