Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
EntityHierarchyEnumerator.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Runtime.CompilerServices;
5 using SiliconStudio.Paradox.Effects;
6 using SiliconStudio.Paradox.Engine;
7 using SiliconStudio.Paradox.EntityModel;
8 using SiliconStudio.Paradox.Games;
9 using SiliconStudio.Paradox.Games.Collections;
10 using SiliconStudio.Paradox.Games.ViewModel;
11 
12 namespace ScriptTest
13 {
15  {
16  private TrackingHashSet<Entity> selectedEntities = new TrackingHashSet<Entity>();
17  private PropertyKey<bool> isSelectedProperty;
18  private ViewModelContext selectedEntitiesContext;
19  private ConditionalWeakTable<EffectMesh, EffectMeshIndex> effectMeshIndices = new ConditionalWeakTable<EffectMesh, EffectMeshIndex>();
20  private EntitySystem EntitySystem;
21 
22  public EntityHierarchyEnumerator(EntitySystem EntitySystem, ViewModelContext selectedEntitiesContext)
23  {
24  isSelectedProperty = new PropertyKey<bool>("IsSelected", typeof(EntityHierarchyEnumerator), new StaticDefaultValueMetadata(false) { PropertyUpdateCallback = IsSelectedChanged });
25 
26  this.EntitySystem = EntitySystem;
27  this.selectedEntitiesContext = selectedEntitiesContext;
28  }
29 
30  public TrackingHashSet<Entity> SelectedEntities
31  {
32  get { return selectedEntities; }
33  }
34 
35  public void GenerateChildren(ViewModelContext context, IViewModelNode viewModelNode, ref bool handled)
36  {
37  if (viewModelNode.NodeValue is Entity)
38  {
39  viewModelNode.Children.Add(new ViewModelNode("Name", new PropertyInfoViewModelContent(new ParentNodeValueViewModelContent(), typeof(Entity).GetProperty("Name"))));
40  viewModelNode.Children.Add(new ViewModelNode("Guid", new PropertyInfoViewModelContent(new ParentNodeValueViewModelContent(), typeof(Entity).GetProperty("Guid"))));
41  viewModelNode.Children.Add(new ViewModelNode("IsSelected", new PropertyKeyViewModelContent(new ParentNodeValueViewModelContent(), isSelectedProperty)));
42  viewModelNode.Children.Add(new ViewModelNode("ParentReference", LambdaViewModelContent<ViewModelReference>.FromParent<Entity>(x =>
43  {
44  var transformationComponent = x.Transformation;
45  var parent = transformationComponent != null ? transformationComponent.Parent : null;
46  return new ViewModelReference(parent != null ? parent.Entity : null);
47  })));
48  viewModelNode.Children.Add(new ViewModelNode("HierarchicalEntities", EnumerableViewModelContent.FromUnaryLambda<ViewModelReference, Entity>(new ParentNodeValueViewModelContent(),
49  (entity) =>
50  {
51  var result = Enumerable.Empty<ViewModelReference>();
52 
53  // Enumerates children nodes
54  var transformationComponent = entity.Transformation;
55  if (transformationComponent != null)
56  result = result.Concat(transformationComponent.Children
57  .Select(x => new ViewModelReference(x.Entity, true)));
58 
59  // Enumerates EffectMesh
60  var meshComponent = entity.Get(ModelComponent.Key);
61  if (meshComponent != null && meshComponent.InstantiatedSubMeshes != null)
62  result = result.Concat(meshComponent.InstantiatedSubMeshes.Select((x, i) =>
63  {
64  effectMeshIndices.GetOrCreateValue(x.Value).Index = i;
65  return new ViewModelReference(x.Value, true);
66  }));
67 
68  return result;
69  })));
70 
71  viewModelNode.Children.Add(new ViewModelNode("EventOpen", new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) =>
72  {
73  ScriptDebug.SelectEntity((Entity)viewModel2.Parent.NodeValue);
74  }))));
75 
76  viewModelNode.Children.Add(new ViewModelNode("CreateNewEntity", new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) =>
77  {
78  var entity = (Entity)viewModel2.Parent.NodeValue;
79  var newEntity = new Entity("New Entity");
80  entity.Transformation.Children.Add(newEntity.Transformation);
81  }))));
82 
83  viewModelNode.Children.Add(new ViewModelNode("Remove", new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) =>
84  {
85  var entity = (Entity)viewModel2.Parent.NodeValue;
86  EntitySystem.Remove(entity);
87  }))));
88 
89  handled = true;
90  }
91  else if (viewModelNode.NodeValue is EffectMesh)
92  {
93  viewModelNode.Children.Add(new ViewModelNode("Name", new LambdaViewModelContent<string>(new NullViewModelContent(), (content) =>
94  {
95  var effectMesh = (EffectMesh)content.OwnerNode.Parent.NodeValue;
96  var result = effectMeshIndices.GetOrCreateValue(effectMesh).Index.ToString();
97  if (effectMesh.Name != null)
98  result += " - " + effectMesh.Name;
99 
100  return result;
101  })));
102 
103  viewModelNode.Children.Add(new ViewModelNode("EventOpen", new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) =>
104  {
105  selectedEntitiesContext.ViewModelByGuid.Clear();
106  selectedEntitiesContext.Root = new ViewModelNode("Root", new RootViewModelContent(new[] { new ViewModelReference(viewModel2.Parent.NodeValue, true) }, typeof(IList<ViewModelReference>)));
107  }))));
108 
109  handled = true;
110  }
111  }
112 
113  private void IsSelectedChanged(PropertyContainer propertyContainer, PropertyKey propertykey, object newvalue, object oldvalue)
114  {
115  if ((bool)newvalue)
116  selectedEntities.Add((Entity)propertyContainer);
117  else
118  selectedEntities.Remove((Entity)propertyContainer);
119  }
120 
121  private class EffectMeshIndex
122  {
123  public int Index;
124  }
125  }
126 }
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
Manage a collection of entities.
Definition: EntitySystem.cs:22
void Remove(Entity entity)
Removes the entity from the EntitySystem. It works weither entity has a parent or not...
void GenerateChildren(ViewModelContext context, IViewModelNode viewModelNode, ref bool handled)
EntityHierarchyEnumerator(EntitySystem EntitySystem, ViewModelContext selectedEntitiesContext)