Skip to content
Permalink
Browse files

[Transform] Deal with ChildrenCollection todo

  • Loading branch information
Eideren authored and xen2 committed May 19, 2019
1 parent 7af2c14 commit 8b0821cbfe3bff90d9be16500e4556a3934542b3
@@ -37,7 +37,7 @@ public override IGraphNode FindTarget(IGraphNode sourceNode, IGraphNode target)
/// <inheritdoc/>
public override bool IsChildPartReference(IGraphNode node, Index index)
{
return (node as IObjectNode)?.Type == typeof(TransformChildrenCollection);
return (node as IObjectNode)?.Type == typeof(TransformComponent.TransformChildrenCollection);
}

/// <inheritdoc/>
@@ -71,8 +71,6 @@ protected override void OnEntityComponentAdding(Entity entity, TransformComponen
{
InternalAddEntity(child.Entity);
}

((TrackingCollection<TransformComponent>)data.Children).CollectionChanged += Children_CollectionChanged;
}

/// <inheritdoc/>
@@ -93,8 +91,6 @@ protected override void OnEntityComponentRemoved(Entity entity, TransformCompone
{
TransformationRoots.Remove(component);
}

((TrackingCollection<TransformComponent>)data.Children).CollectionChanged -= Children_CollectionChanged;
}

internal void UpdateTransformations(FastCollection<TransformComponent> transformationComponents)
@@ -172,43 +168,36 @@ private static void UpdateTransfromationsRecursive(Scene scene)
UpdateTransfromationsRecursive(childScene);
}
}

private void Children_CollectionChanged(object sender, TrackingCollectionChangedEventArgs e)
internal void NotifyChildrenCollectionChanged(TransformComponent transformComponent, bool added)
{
var transformComponent = (TransformComponent)e.Item;

// Ignore if transform component is being moved inside the same root scene (no need to add/remove)
if (transformComponent.IsMovingInsideRootScene)
{
// Still need to update transformation roots
if (transformComponent.Parent == null)
{
switch (e.Action)
if(added)
{
TransformationRoots.Add(transformComponent);
}
else
{
case NotifyCollectionChangedAction.Add:
TransformationRoots.Add(transformComponent);
break;
case NotifyCollectionChangedAction.Remove:
TransformationRoots.Remove(transformComponent);
break;
default:
throw new NotSupportedException();
TransformationRoots.Remove(transformComponent);
}
}
return;
}

// Added/removed children of entities in the entity manager have to be added/removed of the entity manager.
switch (e.Action)
else
{
case NotifyCollectionChangedAction.Add:
if (added)
{
InternalAddEntity(transformComponent.Entity);
break;
case NotifyCollectionChangedAction.Remove:
}
else
{
InternalRemoveEntity(transformComponent.Entity, false);
break;
default:
throw new NotSupportedException();
}
}
}
}

This file was deleted.

@@ -17,7 +17,7 @@ namespace Xenko.Engine
/// Defines Position, Rotation and Scale of its <see cref="Entity"/>.
/// </summary>
[DataContract("TransformComponent")]
[DataSerializerGlobal(null, typeof(TrackingCollection<TransformComponent>))]
[DataSerializerGlobal(null, typeof(FastCollection<TransformComponent>))]
[DefaultEntityComponentProcessor(typeof(TransformProcessor))]
[Display("Transform", Expand = ExpandRule.Once)]
[ComponentOrder(0)]
@@ -89,8 +89,7 @@ public sealed class TransformComponent : EntityComponent //, IEnumerable<Transfo
/// </summary>
public TransformComponent()
{
children = new TransformChildrenCollection();
children.CollectionChanged += ChildrenCollectionChanged;
children = new TransformChildrenCollection(this);

UseTRS = true;
Scale = Vector3.One;
@@ -316,38 +315,68 @@ internal void UpdateWorldMatrixInternal(bool recursive)
}
}

private void AddItem(TransformComponent item)
[DataContract]
public class TransformChildrenCollection : FastCollection<TransformComponent>
{
if (item.Parent != null)
throw new InvalidOperationException("This TransformComponent already has a Parent, detach it first.");
TransformComponent transform;
Entity Entity => transform.Entity;

item.parent = this;
public TransformChildrenCollection(TransformComponent transformParam)
{
transform = transformParam;
}

Entity?.EntityManager?.OnHierarchyChanged(item.Entity);
}
private void OnTransformAdded(TransformComponent item)
{
if (item.Parent != null)
throw new InvalidOperationException("This TransformComponent already has a Parent, detach it first.");

private void RemoveItem(TransformComponent item)
{
if (item.Parent != this)
throw new InvalidOperationException("This TransformComponent's parent is not the expected value.");
item.parent = transform;

item.parent = null;
Entity?.EntityManager?.OnHierarchyChanged(item.Entity);
Entity?.EntityManager?.GetProcessor<TransformProcessor>().NotifyChildrenCollectionChanged(item, true);
}
private void OnTransformRemoved(TransformComponent item)
{
if (item.Parent != transform)
throw new InvalidOperationException("This TransformComponent's parent is not the expected value.");

Entity?.EntityManager?.OnHierarchyChanged(item.Entity);
}
item.parent = null;

private void ChildrenCollectionChanged(object sender, TrackingCollectionChangedEventArgs e)
{
switch (e.Action)
Entity?.EntityManager?.OnHierarchyChanged(item.Entity);
Entity?.EntityManager?.GetProcessor<TransformProcessor>().NotifyChildrenCollectionChanged(item, false);
}

/// <inheritdoc/>
protected override void InsertItem(int index, TransformComponent item)
{
case NotifyCollectionChangedAction.Add:
AddItem((TransformComponent)e.Item);
break;
case NotifyCollectionChangedAction.Remove:
RemoveItem((TransformComponent)e.Item);
break;
default:
throw new NotSupportedException();
base.InsertItem(index, item);
OnTransformAdded(item);
}

/// <inheritdoc/>
protected override void RemoveItem(int index)
{
OnTransformRemoved(this[index]);
base.RemoveItem(index);
}

/// <inheritdoc/>
protected override void ClearItems()
{
for (var i = Count - 1; i >= 0; --i)
OnTransformRemoved(this[i]);
base.ClearItems();
}

/// <inheritdoc/>
protected override void SetItem(int index, TransformComponent item)
{
OnTransformRemoved(this[index]);

base.SetItem(index, item);

OnTransformAdded(this[index]);
}
}
}

0 comments on commit 8b0821c

Please sign in to comment.
You can’t perform that action at this time.