Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
HierarchicalProcessor.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.Specialized;
6 using SiliconStudio.Core.Collections;
7 using SiliconStudio.Paradox.EntityModel;
8 
9 namespace SiliconStudio.Paradox.Engine
10 {
11  /// <summary>
12  /// This processor will take care of adding/removing children of every Entity added/removed in the EntitySystem.
13  /// It will also exposes a list of root entities.
14  /// </summary>
15  public class HierarchicalProcessor : EntityProcessor<TransformationComponent>
16  {
17  private readonly TrackingHashSet<Entity> rootEntities;
18 
20  : base(new[] { TransformationComponent.Key })
21  {
22  rootEntities = new TrackingHashSet<Entity>();
23  rootEntities.CollectionChanged += rootEntities_CollectionChanged;
24  }
25 
26  /// <summary>
27  /// Gets the list of root entities (entities which have no <see cref="TransformationComponent.Parent"/>).
28  /// </summary>
29  public ISet<Entity> RootEntities
30  {
31  get { return rootEntities; }
32  }
33 
34  /// <inheritdoc/>
36  {
37  return entity.Transformation;
38  }
39 
40  /// <inheritdoc/>
41  protected internal override void OnSystemAdd()
42  {
43 
44  }
45 
46  protected override void OnEnabledChanged(Entity entity, bool enabled)
47  {
48  foreach (var child in entity.Transformation.Children)
49  {
50  EntitySystem.SetEnabled(child.Entity, enabled);
51  }
52  }
53 
54  /// <inheritdoc/>
55  protected override void OnEntityAdding(Entity entity, TransformationComponent transformationComponent)
56  {
57  foreach (var child in transformationComponent.Children)
58  {
59  InternalAddEntity(child.Entity);
60  }
61 
62  if (transformationComponent.Parent == null)
63  rootEntities.Add(entity);
64 
65  ((TrackingCollection<TransformationComponent>)transformationComponent.Children).CollectionChanged += Children_CollectionChanged;
66  }
67 
68  /// <inheritdoc/>
69  protected override void OnEntityRemoved(Entity entity, TransformationComponent transformationComponent)
70  {
71  var entityToRemove = new List<Entity>();
72  foreach (var child in transformationComponent.Children)
73  {
74  entityToRemove.Add(child.Entity);
75  }
76 
77  foreach (var childEntity in entityToRemove)
78  {
79  InternalRemoveEntity(childEntity, false);
80  }
81 
82  // If sub entity is removed but its parent is still there, it needs to be detached.
83  // Note that this behavior is still not totally fixed yet, it might change.
84  if (transformationComponent.Parent != null && EntitySystem.Contains(transformationComponent.Parent.Entity))
85  transformationComponent.Parent = null;
86 
87  rootEntities.Remove(entity);
88 
89  ((TrackingCollection<TransformationComponent>)transformationComponent.Children).CollectionChanged -= Children_CollectionChanged;
90  }
91 
92  void rootEntities_CollectionChanged(object sender, TrackingCollectionChangedEventArgs e)
93  {
94  switch (e.Action)
95  {
96  case NotifyCollectionChangedAction.Add:
97  EntitySystem.Add((Entity)e.Item);
98  break;
99  case NotifyCollectionChangedAction.Remove:
100  EntitySystem.Remove((Entity)e.Item);
101  break;
102  default:
103  throw new NotSupportedException();
104  }
105  }
106 
107  private void Children_CollectionChanged(object sender, TrackingCollectionChangedEventArgs e)
108  {
109  // Added/removed children of entities in the entity manager have to be added/removed of the entity manager.
110  switch (e.Action)
111  {
112  case NotifyCollectionChangedAction.Add:
113  InternalAddEntity(((TransformationComponent)e.Item).Entity);
114  break;
115  case NotifyCollectionChangedAction.Remove:
116  // If a child is removed, it is still kept in Entities.
117  // Entities.Remove(child) should be used to remove entities (this will detach child from its parent)
118  //InternalRemoveEntity(((TransformationComponent)e.Item).Entity);
119  break;
120  default:
121  throw new NotSupportedException();
122  }
123  }
124  }
125 }
override void OnEntityRemoved(Entity entity, TransformationComponent transformationComponent)
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
Defines Position, Rotation and Scale of its Entity.
Entity processor, triggered on various EntitySystem events such as Entity and Component additions and...
This processor will take care of adding/removing children of every Entity added/removed in the Entity...
object Item
Gets the added or removed item (if dictionary, value only).
Manage a collection of entities.
Definition: EntitySystem.cs:22
NotifyCollectionChangedAction Action
Gets the type of action performed. Allowed values are NotifyCollectionChangedAction.Add and NotifyCollectionChangedAction.Remove.
override TransformationComponent GenerateAssociatedData(Entity entity)
override void OnEntityAdding(Entity entity, TransformationComponent transformationComponent)
override void OnEnabledChanged(Entity entity, bool enabled)
bool Contains(Entity item)
Determines whether this instance contains the specified entity.