diff --git a/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf b/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf index b23bc5e..65c3e70 100644 --- a/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf +++ b/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf @@ -161,7 +161,7 @@ namespace GlitchyEditor.EditWindows if (entityResult case .Ok(let entity)) { - new PropertiesWindow(_editor, .Entity(entity)); + new PropertiesWindow(_editor, .Entity(entity.UUID)); } } } diff --git a/GlitchyEditor/src/EditWindows/InspectorWindow.bf b/GlitchyEditor/src/EditWindows/InspectorWindow.bf index 9ca3701..aa3b677 100644 --- a/GlitchyEditor/src/EditWindows/InspectorWindow.bf +++ b/GlitchyEditor/src/EditWindows/InspectorWindow.bf @@ -15,7 +15,7 @@ enum SelectedObject { case None; case Asset(AssetHandle AssetHandle); - case Entity(Entity entity); + case Entity(UUID entityId); } class InspectorWindow : EditorWindow @@ -66,7 +66,7 @@ class InspectorWindow : EditorWindow if (entityResult case .Ok(let selectedEntity)) { - _selectedObject = .Entity(selectedEntity); + _selectedObject = .Entity(selectedEntity.UUID); return; } } @@ -91,26 +91,49 @@ class InspectorWindow : EditorWindow ImGui.Checkbox("Lock", &_lockCurrentSelection); ImGui.Separator(); + DeselectIfInvalid(); + ShowSelectedObject(_selectedObject, _editor); } + private void DeselectIfInvalid() + { + if (_selectedObject case .Entity(let entityId) && + _editor.CurrentScene.GetEntityByID(entityId) case .Err) + { + _selectedObject = .None; + } + else if (_selectedObject case .Asset(let assetHandle) && + _editor.ContentManager.AssetHierarchy.GetNodeFromAssetHandle(assetHandle) case .Err) + { + _selectedObject = .None; + } + } + public static void ShowSelectedObject(SelectedObject object, Editor editor) { if (object case .Asset(let assetHandle)) { ShowAssetProperties(assetHandle, editor); } - else if (object case .Entity(let entity)) + else if (object case .Entity(let entityId)) { - ShowEntityProperties(entity); + ShowEntityProperties(entityId, editor); } } private static void ShowAssetProperties(AssetHandle assetHandle, Editor editor) { - TreeNode assetNode = TrySilent!(editor.ContentManager.AssetHierarchy.GetNodeFromAssetHandle(assetHandle)); + Result> assetNode = editor.ContentManager.AssetHierarchy.GetNodeFromAssetHandle(assetHandle); - AssetFile assetFile = assetNode->AssetFile; + if (assetNode case .Err) + { + ImGui.TextWrapped($"ERROR!\n\nAsset {assetHandle} doesn't exist."); + + return; + } + + AssetFile assetFile = assetNode->Value.AssetFile; if (ImGui.BeginPropertyTable("asset_properties", ImGui.GetID("asset_properties"))) { @@ -137,8 +160,17 @@ class InspectorWindow : EditorWindow ImGui.EndDisabled(); } - private static void ShowEntityProperties(Entity entity) + private static void ShowEntityProperties(UUID entityId, Editor editor) { - ComponentEditWindow.ShowComponents(entity); + Result entityResult = editor.CurrentScene.GetEntityByID(entityId); + + if (entityResult case .Ok(let entity)) + { + ComponentEditWindow.ShowComponents(entity); + } + else + { + ImGui.TextWrapped($"ERROR!\n\nEntity {entityId} doesn't exist."); + } } } diff --git a/GlitchyEngine/src/World/EcsWorld.bf b/GlitchyEngine/src/World/EcsWorld.bf index c82edc8..5477a40 100644 --- a/GlitchyEngine/src/World/EcsWorld.bf +++ b/GlitchyEngine/src/World/EcsWorld.bf @@ -116,6 +116,8 @@ namespace GlitchyEngine.World */ public void RemoveEntity(EcsEntity entity) { + Log.EngineLogger.AssertDebug(IsValid(entity)); + var listEntity = ref _entities[entity.Index]; if(entity != listEntity.ID) return; @@ -176,6 +178,8 @@ namespace GlitchyEngine.World */ public T* AssignComponent(EcsEntity entity, T value = T()) where T : struct, new { + Log.EngineLogger.AssertDebug(IsValid(entity)); + if(entity.Index > _entities.Count) return null; @@ -213,6 +217,8 @@ namespace GlitchyEngine.World */ public void RemoveComponent(EcsEntity entity) where T : struct, new { + Log.EngineLogger.AssertDebug(IsValid(entity)); + if(entity.Index > _entities.Count) return; @@ -233,6 +239,8 @@ namespace GlitchyEngine.World public void RemoveComponent(EcsEntity entity) where T : struct, new, IDisposableComponent { + Log.EngineLogger.AssertDebug(IsValid(entity)); + if(entity.Index > _entities.Count) return; @@ -257,6 +265,8 @@ namespace GlitchyEngine.World /// Returns whether or not the given entity has the specified component. public bool HasComponent(EcsEntity entity) where T : struct, new { + Log.EngineLogger.AssertDebug(IsValid(entity)); + var listEntity = ref _entities[entity.Index]; if(entity != listEntity.ID) return false; @@ -270,6 +280,8 @@ namespace GlitchyEngine.World public T* GetComponent(EcsEntity entity) where T : struct, new { + Log.EngineLogger.AssertDebug(IsValid(entity)); + var listEntity = ref _entities[entity.Index]; if(entity != listEntity.ID) return null;