2 using System.Collections.Generic;
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;
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>();
24 isSelectedProperty =
new PropertyKey<bool>(
"IsSelected", typeof(
EntityHierarchyEnumerator),
new StaticDefaultValueMetadata(
false) { PropertyUpdateCallback = IsSelectedChanged });
26 this.EntitySystem = EntitySystem;
27 this.selectedEntitiesContext = selectedEntitiesContext;
32 get {
return selectedEntities; }
35 public void GenerateChildren(ViewModelContext context, IViewModelNode viewModelNode, ref
bool handled)
37 if (viewModelNode.NodeValue is
Entity)
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 =>
44 var transformationComponent = x.Transformation;
45 var parent = transformationComponent != null ? transformationComponent.Parent : null;
46 return new ViewModelReference(parent != null ? parent.Entity : null);
48 viewModelNode.Children.Add(
new ViewModelNode(
"HierarchicalEntities", EnumerableViewModelContent.FromUnaryLambda<ViewModelReference, Entity>(
new ParentNodeValueViewModelContent(),
51 var result = Enumerable.Empty<ViewModelReference>();
54 var transformationComponent = entity.Transformation;
55 if (transformationComponent != null)
56 result = result.Concat(transformationComponent.Children
57 .Select(x =>
new ViewModelReference(x.Entity,
true)));
60 var meshComponent = entity.Get(ModelComponent.Key);
61 if (meshComponent != null && meshComponent.InstantiatedSubMeshes != null)
62 result = result.Concat(meshComponent.InstantiatedSubMeshes.Select((x, i) =>
64 effectMeshIndices.GetOrCreateValue(x.Value).Index = i;
65 return new ViewModelReference(x.Value,
true);
71 viewModelNode.Children.Add(
new ViewModelNode(
"EventOpen",
new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) =>
73 ScriptDebug.SelectEntity((Entity)viewModel2.Parent.NodeValue);
76 viewModelNode.Children.Add(
new ViewModelNode(
"CreateNewEntity",
new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) =>
78 var entity = (Entity)viewModel2.Parent.NodeValue;
79 var newEntity =
new Entity(
"New Entity");
80 entity.Transformation.Children.Add(newEntity.Transformation);
83 viewModelNode.Children.Add(
new ViewModelNode(
"Remove",
new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) =>
85 var entity = (Entity)viewModel2.Parent.NodeValue;
91 else if (viewModelNode.NodeValue is EffectMesh)
93 viewModelNode.Children.Add(
new ViewModelNode(
"Name",
new LambdaViewModelContent<string>(
new NullViewModelContent(), (content) =>
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;
103 viewModelNode.Children.Add(
new ViewModelNode(
"EventOpen",
new RootViewModelContent((ExecuteCommand)((viewModel2, parameter) =>
105 selectedEntitiesContext.ViewModelByGuid.Clear();
106 selectedEntitiesContext.Root =
new ViewModelNode(
"Root",
new RootViewModelContent(
new[] {
new ViewModelReference(viewModel2.Parent.NodeValue,
true) }, typeof(IList<ViewModelReference>)));
113 private void IsSelectedChanged(PropertyContainer propertyContainer, PropertyKey propertykey,
object newvalue,
object oldvalue)
116 selectedEntities.Add((
Entity)propertyContainer);
118 selectedEntities.Remove((
Entity)propertyContainer);
121 private class EffectMeshIndex
Game entity. It usually aggregates multiple EntityComponent
TrackingHashSet< Entity > SelectedEntities
Manage a collection of entities.
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)