public ScriptTreeLayer AddLayer(int layerNumber) { var layer = new ScriptTreeLayer(this, layerNumber); if (Layers.Count == layerNumber) Layers.Add(layer); else throw new Exception("Attempt to add layer " + layerNumber + " when layer count is " + Layers.Count); return layer; }
/// <summary> /// Inserts empty layers below layerNumber in the tree to accomodate an upcoming merge /// </summary> public void ExtendBelowLayer(int layerNumber, int numberOfLayersToInsert) { // Example: 0, 1, 2, 3 // insert (a, b) below 1 // goal: 0, 1, 2, 3, 4, 5 // start at 2, add 2 and 3, // increase old 2 and 3 to 4 and 5, meaning after insert start at 4th index // These are Lists so this operation can be expensive layerNumber++; // Build the layer list var newLayers = new ScriptTreeLayer[numberOfLayersToInsert]; for (int i = 0; i < numberOfLayersToInsert; i++) newLayers[i] = new ScriptTreeLayer(this, layerNumber + i); // Insert empty layers all at once to prevent repeated Layers.InsertRange(layerNumber, newLayers); // Fix layer numbers in the downstream for (int i = layerNumber + numberOfLayersToInsert; i < Layers.Count; i++) Layers[i].LayerNumber = i; }