public bool CanRemoveCleanly(HamTimelineNode node) { int dependants = 0; List<int> descendants = node.GetDescendantIDs(); if (descendants.Count == 0 && node.ID == this.OriginNodeID) { // If we're deleting the origin node, we need a replacement return false; } for (int i = 0; i < descendants.Count; ++i) { HamTimelineNode d = this.Nodes[descendants[i]]; if (d.UniquelyParented) { dependants += 1; } } return dependants < 2; }
public void DeleteNode(HamTimelineNode node) { // It's expected that there is at most 1 uniquely parented descendant // Find the dependant node, removing linkages to the current node in all descendants as we go HamTimelineNode dependant = null; List<int> descendants = node.GetDescendantIDs(); for (int i = 0; i < descendants.Count; ++i) { HamTimelineNode descendant = this.Nodes[descendants[i]]; if (descendant.UniquelyParented) { dependant = descendant; } descendant.PreviousNodeIDs.Remove(node.ID); } // Remove linkages to this node in the parents, linking the parents to the dependant node instead if it exists for (int i = 0; i < node.PreviousNodeIDs.Count; ++i) { HamTimelineNode parent = this.Nodes[node.PreviousNodeIDs[i]]; int pIndex = parent.GetIndexOfDescendant(node.ID); parent.SetDescendant((dependant == null) ? InvalidID : dependant.ID, pIndex); } // If the node we're deleting is the origin, set any descendant (preferably the dependant) as the new origin if (node.ID == this.OriginNodeID) { if (dependant != null) { this.OriginNodeID = dependant.ID; } else { // There is assumed to be at least 1 descendant this.OriginNodeID = descendants[0]; } } // Remove this node completely this.Nodes.Remove(node.ID); this.NodeLinkageDirty = true; SanityCheck(); }