public void SetSelectedEntity(SEntityId entityId)
        {
            bool CheckEntity(COutlinerEntityViewModel vm)
            {
                if (vm.EntityId == entityId)
                {
                    vm.IsSelected = true;
                    return(true);
                }

                for (int i = 0, count = vm.Children.Count; i < count; i++)
                {
                    if (CheckEntity(vm.Children[i]))
                    {
                        vm.IsExpanded = true;
                        return(true);
                    }
                }

                return(false);
            }

            for (int i = 0, count = m_entities.Count; i < count; i++)
            {
                COutlinerEntityViewModel entry = m_entities[i];
                if (CheckEntity(entry))
                {
                    if (entry.Children.Count > 0)
                    {
                        entry.IsExpanded = true;
                    }
                    return;
                }
            }
        }
Exemplo n.º 2
0
        private void InspectEntity(SEntityId entityId)
        {
            CEngine engine = CEngine.Instance;

            engine.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
            {
                CUpdateScheduler scheduler = engine.CurrentWorld.UpdateScheduler;
                if (m_updateScope != null && m_updateScope.IsConnected())
                {
                    scheduler.Disconnect(m_updateScope);
                }

                CEntity entity = entityId.GetEntity();
                if (entity != null)
                {
                    m_selectedObject = new CEditableObject(entityId);
                    m_updateScope    = scheduler.Connect(UpdateCallback, EUpdatePriority.ResourceLoading);

                    Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() =>
                    {
                        m_desiredTarget = new CEditableObject(entityId);
                        UnmarkEverything();
                        MarkEntityInInspector(true);
                    }));
                }
            });
        }
Exemplo n.º 3
0
 public void QueueEntityInformationUpdate(SEntityId entityId, bool bReselectTarget = false)
 {
     CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
     {
         UpdateEntityInformation_EngineThread(entityId, bReselectTarget);
     });
 }
Exemplo n.º 4
0
        public static void DetachEntityFromAllParents(SEntityId entityId)
        {
            CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
            {
                CEntity target = entityId.GetEntity();
                if (target == null || target.RootComponent == null || target.RootComponent.ParentComponent == null)
                {
                    return;
                }

                SEntityComponentId oldRootParent = new SEntityComponentId(target.RootComponent.ParentComponent);

                void Do()
                {
                    CWorld world   = CEngine.Instance.CurrentWorld;
                    CEntity entity = entityId.GetEntity();

                    if (entity != null)
                    {
                        entity.Detach();
                    }
                }

                void Undo()
                {
                    CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
                    {
                        CWorld world              = CEngine.Instance.CurrentWorld;
                        CEntity entity            = entityId.GetEntity();
                        CSceneComponent oldParent = oldRootParent.GetComponent <CSceneComponent>();

                        if (oldParent == null)
                        {
                            LogUtility.Log("[UndoRedo] The old parent is invalid! Undo stack has been corrupted and cleared.");
                            UndoRedoUtility.Purge(null);
                            return;
                        }

                        if (entity != null)
                        {
                            entity.AttachToComponent(oldParent);
                        }
                    });
                }

                void Redo()
                {
                    CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
                    {
                        Do();
                    });
                }

                Do();

                CRelayUndoItem item = new CRelayUndoItem(Undo, Redo);
                UndoRedoUtility.Record(item);
            });
        }
        public COutlinerEntityViewModel(CWorldOutlinerViewModel vm, SEntityId entityId, string name, COutlinerEntityViewModel parent, int index)
        {
            EntityId    = entityId;
            m_viewModel = vm;
            m_name      = name;
            Parent      = parent;
            Index       = index;

            InitCommands();
        }
Exemplo n.º 6
0
        public static void DestroyEntity(SEntityId entityId)
        {
            CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
            {
                CEntity entity = entityId.GetEntity();
                if (entity != null)
                {
                    entity.Destroy();
                }

                UndoRedoUtility.Purge(null);
            });
        }
Exemplo n.º 7
0
        private void OnSpawnSphereEntity(object arg)
        {
            CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
            {
                CEntity newEntity = CEngine.Instance.CurrentWorld.SpawnEntity <CEntity>();
                SEntityId id      = new SEntityId(newEntity.Id);

                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                {
                    Instance.SetSelectedObject(new CEditableObject(id));
                }));
                UndoRedoUtility.Purge(null);
            });
        }
Exemplo n.º 8
0
        public static void PickRootComponent(SEntityId entityId)
        {
            CWorldOutlinerViewModel worldOutliner = CWorkspace.Instance.GetTool <CWorldOutlinerViewModel>();

            if (worldOutliner != null)
            {
                CEntity entity = entityId.GetEntity();
                if (entity == null)
                {
                    return;
                }

                CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
                {
                    worldOutliner.PickingComponentId.GetComponent <CScenePickingComponent>()?.Pick(entity.RootComponent);
                });
            }
        }
Exemplo n.º 9
0
        private void OnSpawnCubeEntity(object arg)
        {
            CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
            {
                CMeshAsset cubeAsset = CImportManager.Instance.MeshImporter.LoadMeshAsync("TestResources/Cube.fbx");
                CEntity cubeEntity   = CEngine.Instance.CurrentWorld.SpawnEntity <CEntity>();
                cubeEntity.AddComponent <CSceneComponent>(true, true);
                CMeshComponent meshComponent = cubeEntity.AddComponent <CMeshComponent>(true, true);
                meshComponent.LocalPosition  = new Vector3(0, 0, 0.5f);
                meshComponent.Mesh           = cubeAsset;
                SEntityId id = new SEntityId(cubeEntity.Id);

                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                {
                    Instance.SetSelectedObject(new CEditableObject(id));
                }));
                UndoRedoUtility.Purge(null);
            });
        }
Exemplo n.º 10
0
        private void InternalAddComponent(object argument)
        {
            AddComponentMenuOpen = false;

            if ((argument is Type type) && m_selectedObject != null)
            {
                SEntityId selectedEntityId = m_selectedObject.GetTargetEntityId();

                CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
                {
                    CEntity entity = selectedEntityId.GetEntity();
                    if (entity != null)
                    {
                        entity.AddComponent(type, true, true);

                        UpdateEntityInformation_EngineThread(selectedEntityId);
                        UndoRedoUtility.Purge(null);
                    }
                });
            }
        }
        internal void AttachEntities(SEntityId child, SEntityId parent)
        {
            CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
            {
                CEntity childEntity  = child.GetEntity();
                CEntity parentEntity = parent.GetEntity();

                if (childEntity != null && parentEntity != null)
                {
                    CEntity oldParent     = childEntity.Parent;
                    bool bOldParentExists = oldParent != null;
                    SEntityId oldParentId = bOldParentExists ? new SEntityId(oldParent.Id) : SEntityId.Invalid;

                    if (parentEntity.RootComponent != null && childEntity.RootComponent != null)
                    {
                        if (!parentEntity.RootComponent.IsChildOf(childEntity.RootComponent))
                        {
                            childEntity.AttachToEntity(parentEntity);

                            void Undo()
                            {
                                CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
                                {
                                    CEntity childInst = child.GetEntity();

                                    if (childInst != null)
                                    {
                                        if (bOldParentExists)
                                        {
                                            CEntity oldParentInst = oldParentId.GetEntity();
                                            if (oldParentInst != null && childInst != null)
                                            {
                                                childInst.AttachToEntity(oldParentInst);
                                            }
                                        }
                                        else
                                        {
                                            childInst.Detach();
                                        }
                                    }
                                });
                            }

                            void Redo()
                            {
                                CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
                                {
                                    CEntity newParentInst = parent.GetEntity();
                                    CEntity childInst     = child.GetEntity();

                                    if (newParentInst != null && childInst != null)
                                    {
                                        childInst.AttachToEntity(newParentInst);
                                    }
                                });
                            }

                            CRelayUndoItem item = new CRelayUndoItem(Undo, Redo);
                            UndoRedoUtility.Record(item);
                        }
                    }
                }
            });
        }
Exemplo n.º 12
0
 public CEditableObject(SEntityId entityId)
 {
     m_entityId = entityId;
     Type       = EObjectType.Entity;
 }
Exemplo n.º 13
0
        private void UpdateEntityInformation_EngineThread(SEntityId entityId, bool bReselectTarget = false)
        {
            void CreateViewModel(CSceneComponent component, CInspectorSceneComponentViewModel parent, CEntity owner)
            {
                if (component.Owner != owner || !component.ShowInInspector || component.MarkedForDestruction)
                {
                    return;
                }

                SEntityComponentId id = new SEntityComponentId(component, false);

                CInspectorSceneComponentViewModel vm = new CInspectorSceneComponentViewModel(this, component.Name, id, "sceneComponent");

                parent.Children.Add(vm);

                foreach (var child in component.Children)
                {
                    CreateViewModel(child, vm, owner);
                }
            }

            CEntity entity = entityId.GetEntity();

            if (entity != null)
            {
                CInspectorEntityViewModel                 entityInfo           = null;
                CInspectorSceneComponentViewModel         sceneComponentsInfo  = null;
                List <CInspectorEntityComponentViewModel> entityComponentsInfo = null;

                sceneComponentsInfo = null;
                if (entity.RootComponent != null)
                {
                    sceneComponentsInfo = new CInspectorSceneComponentViewModel(this, entity.RootComponent.Name, new SEntityComponentId(entity.RootComponent, false), "sceneComponent");
                    foreach (var child in entity.RootComponent.Children)
                    {
                        CreateViewModel(child, sceneComponentsInfo, entity);
                    }
                }

                List <CEntityComponent> components = new List <CEntityComponent>(8);
                entityComponentsInfo = new List <CInspectorEntityComponentViewModel>(4);
                entity.GetComponents(components);

                for (int i = components.Count - 1; i >= 0; i--)
                {
                    if (!(components[i] is CSceneComponent) && components[i].ShowInInspector && !components[i].MarkedForDestruction)
                    {
                        entityComponentsInfo.Add(new CInspectorEntityComponentViewModel(this, components[i].Name, new SEntityComponentId(components[i], false)));
                    }
                }

                entityInfo = new CInspectorEntityViewModel(this, entity.Name, new SEntityId(entity.Id));

                string entityName = entity.Name;

                Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (Action)(() =>
                {
                    m_view?.LockInspector(false);
                    UpdateEntityInformation_EditorThread(sceneComponentsInfo, entityComponentsInfo, entityInfo);

                    m_entityName = entityName;
                    RaisePropertyChanged(nameof(EntityName));
                    m_view.SetEntityName(entityName);

                    if (bReselectTarget)
                    {
                        CWorkspace.Instance.SetSelectedObject(m_selectedObject, true);
                    }
                }));
            }
            else
            {
                //Clear component list when selecting invalid entity
                Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                {
                    UpdateEntityInformation_EditorThread(null, null, null);
                }));
            }
        }
Exemplo n.º 14
0
        private void InitializeEntityMenues()
        {
            List <CAddComponentEntryViewModel> tempList = new List <CAddComponentEntryViewModel>(128);
            Dictionary <string, List <CAddComponentEntryViewModel> > components = new Dictionary <string, List <CAddComponentEntryViewModel> >(12);

            foreach (var type in CKlaxScriptRegistry.Instance.Types)
            {
                if (type.Type.IsSubclassOf(typeof(CEntityComponent)))
                {
                    KlaxComponentAttribute attribute = type.Type.GetCustomAttribute <KlaxComponentAttribute>();

                    if (attribute.HideInEditor)
                    {
                        continue;
                    }

                    CAddComponentEntryViewModel vm = new CAddComponentEntryViewModel(type.Name, type.Type);

                    if (components.TryGetValue(attribute.Category, out List <CAddComponentEntryViewModel> list))
                    {
                        list.Add(vm);
                    }
                    else
                    {
                        List <CAddComponentEntryViewModel> newList = new List <CAddComponentEntryViewModel>(8);
                        components.Add(attribute.Category, newList);

                        newList.Add(vm);
                    }
                }
            }

            List <CAddComponentCategoryViewModel> orderedList = new List <CAddComponentCategoryViewModel>(components.Count);

            foreach (var entry in components)
            {
                CAddComponentCategoryViewModel vm = new CAddComponentCategoryViewModel(entry.Key, entry.Value);
                orderedList.Add(vm);
            }

            orderedList = orderedList.OrderBy(p => p.Name).ToList();
            //Always show Common on top
            for (int i = 0, count = orderedList.Count; i < count; i++)
            {
                if (orderedList[i].Name == "Common")
                {
                    ContainerUtilities.Swap(orderedList, i, 0);
                    break;
                }
            }

            m_addComponentMenuCategories = new ObservableCollection <CAddComponentCategoryViewModel>(orderedList);

            m_entityCommands = new ObservableCollection <CEntityCommandViewModel>();
            m_entityCommands.Add(new CEntityCommandViewModel("Save as Asset", new CRelayCommand((obj) =>
            {
                EntityCommandsMenuOpen = false;

                CAssetBrowserViewModel assetBrowser = CWorkspace.Instance.GetTool <CAssetBrowserViewModel>();
                string assetPath = assetBrowser.ActiveDirectory;
                SEntityId id     = m_selectedObject.GetTargetEntityId();

                CEngine.Instance.Dispatch(EEngineUpdatePriority.BeginFrame, () =>
                {
                    CEntity entity = id.GetEntity();
                    CEntityAsset <CEntity> .CreateFromEntity(entity, assetPath);
                    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                    {
                        assetBrowser.UpdateShownAssets();
                    }));
                });
            })));
        }
Exemplo n.º 15
0
 public CInspectorEntityViewModel(IInspectorViewModel vm, string name, SEntityId entityId)
     : base(vm, name)
 {
     EntityId = entityId;
 }