public void Insert(int index, object value)
{
if (index == Count)
index--;
if (PreventIsertOfNext != null && PreventIsertOfNext == value)
{
PreventIsertOfNext = null;
return;
}
var count = 0;
if (value is Script) // TODO: test me
{
var scriptIndex = 0;
foreach (var script in Scripts)
{
if (count > index)
{
break;
}
count += script.Bricks.Count + 1;
scriptIndex++;
}
Scripts.Insert(scriptIndex, (Script) value);
OnScriptAdded((Script) value, count + 1);
}
if (value is Brick)
{
var brickCount = 0;
_lastInsertedBrick = (Brick) value;
_lastInsertedIndex = index;
if (index == 0) // Cannot insert brick before first sprite
{
index = 1;
}
foreach (var script in Scripts)
{
count++;
foreach (var brick in script.Bricks)
{
if (count == index)
{
script.Bricks.Insert(brickCount, (Brick) value);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, value, index));
return;
}
count++;
brickCount++;
}
if (count == index)
{
script.Bricks.Insert(brickCount, (Brick) value);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, value, index));
return;
}
brickCount = 0;
}
}
}