Compare commits
16 Commits
a1c5ccf937
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
| d4238e3086 | |||
| f552a4e9e1 | |||
| 5b34da6d6c | |||
| 1f1e21905e | |||
| acd8e60ffb | |||
| c6e58b057c | |||
| 34dc6fc8c9 | |||
| d9343a94dc | |||
| b84ee586bf | |||
| 52dfa12e84 | |||
| 326aee2b1c | |||
| 2cbe1ca789 | |||
| 1fe080dd87 | |||
| 211ea2254d | |||
| 914dde2448 | |||
| 5222e801b9 |
@@ -1,11 +1,17 @@
|
||||
<Project>
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Debug_Editor'">
|
||||
<DefineConstants>$(DefineConstants);DEBUG;GHOST_EDITOR;GHOST_SAFETY_CHECKS;</DefineConstants>
|
||||
<DefineConstants>$(DefineConstants);DEBUG;TRACE;GHOST_EDITOR;GHOST_SAFETY_CHECKS;</DefineConstants>
|
||||
<Optimize>false</Optimize>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>portable</DebugType>
|
||||
<TieredCompilation>false</TieredCompilation>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Release_Editor'">
|
||||
<DefineConstants>$(DefineConstants);GHOST_EDITOR;GHOST_SAFETY_CHECKS;</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Release_Dev'">
|
||||
<DefineConstants>$(DefineConstants);GHOST_SAFETY_CHECKS;</DefineConstants>
|
||||
<Optimize>true</Optimize>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using Antlr4.Runtime.Misc;
|
||||
using Ghost.DSL.ShaderParser.Model;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace Ghost.DSL.ShaderParser;
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Engine;
|
||||
using Ghost.Engine.Streaming;
|
||||
|
||||
namespace Ghost.Editor.Core.Assets;
|
||||
@@ -33,7 +32,7 @@ public sealed class CustomAssetHandlerAttribute : Attribute
|
||||
} = true;
|
||||
}
|
||||
|
||||
public interface IAsset : IDisposable
|
||||
public abstract class IAsset : GhostObject
|
||||
{
|
||||
public Guid ID
|
||||
{
|
||||
@@ -49,6 +48,14 @@ public interface IAsset : IDisposable
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
protected IAsset(Guid id, Guid typeId, IAssetSettings? settings)
|
||||
:base(id)
|
||||
{
|
||||
ID = id;
|
||||
TypeID = typeId;
|
||||
Settings = settings;
|
||||
}
|
||||
}
|
||||
|
||||
public interface IAssetExportOptions;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using Ghost.Engine;
|
||||
using Ghost.Engine.Streaming;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Ghost.Editor.Core.Assets;
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ using Ghost.Graphics.RHI;
|
||||
using Ghost.Graphics.Utilities;
|
||||
using Ghost.MeshOptimizer;
|
||||
using Ghost.Ufbx;
|
||||
using Misaki.HighPerformance.Jobs;
|
||||
using Misaki.HighPerformance.LowLevel;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
@@ -13,11 +12,9 @@ using Misaki.HighPerformance.Mathematics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
using TLSFPool = Misaki.HighPerformance.LowLevel.Buffer.MemoryPool<Misaki.HighPerformance.LowLevel.Buffer.TLSF, Misaki.HighPerformance.LowLevel.Buffer.TLSF.CreationOptions>;
|
||||
|
||||
namespace Ghost.Editor.Core.Assets;
|
||||
|
||||
internal readonly unsafe struct MeshParsingJob : IJob
|
||||
internal unsafe class MeshParsingJob
|
||||
{
|
||||
private struct GeometryPart : IDisposable
|
||||
{
|
||||
@@ -38,14 +35,20 @@ internal readonly unsafe struct MeshParsingJob : IJob
|
||||
|
||||
private readonly string _filePath;
|
||||
private readonly AllocationHandle _allocationHandle;
|
||||
private readonly MeshAssetSettings _settings;
|
||||
private readonly ModelAssetSettings _settings;
|
||||
|
||||
public MeshParsingJob(MeshNode rootNode, string filePath, AllocationHandle allocationHandle, MeshAssetSettings settings)
|
||||
private readonly TaskCompletionSource<Result> _taskCompletionSource;
|
||||
|
||||
public Task<Result> Task => _taskCompletionSource.Task;
|
||||
|
||||
public MeshParsingJob(MeshNode rootNode, string filePath, AllocationHandle allocationHandle, ModelAssetSettings settings)
|
||||
{
|
||||
_rootNode = rootNode;
|
||||
_filePath = filePath;
|
||||
_allocationHandle = allocationHandle;
|
||||
_settings = settings;
|
||||
|
||||
_taskCompletionSource = new TaskCompletionSource<Result>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -321,7 +324,7 @@ internal readonly unsafe struct MeshParsingJob : IJob
|
||||
};
|
||||
}
|
||||
|
||||
public void Execute(ref readonly JobExecutionContext context)
|
||||
public Result Execute()
|
||||
{
|
||||
var error = new ufbx_error();
|
||||
var load_Opts = new ufbx_load_opts
|
||||
@@ -354,15 +357,20 @@ internal readonly unsafe struct MeshParsingJob : IJob
|
||||
using var scene = new DisposablePtr<ufbx_scene>(ufbx_scene.LoadFile((sbyte*)str.GetUnsafePtr(), &load_Opts, &error));
|
||||
if (scene.Get() == null)
|
||||
{
|
||||
Logger.Error(error.description.ToString());
|
||||
return;
|
||||
return Result.Failure(error.description.ToString());
|
||||
}
|
||||
|
||||
ParseHierarchy(scene.Get()->root_node, _rootNode, AllocationHandle.TLSF);
|
||||
|
||||
return Result.Success();
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class MeshProcessor
|
||||
internal static partial class MeshProcessor
|
||||
{
|
||||
|
||||
public static Task<Result> ParseMeshAsync(MeshNode root, string sourcePath, AllocationHandle allocationHandle, ModelAssetSettings meshSettings, CancellationToken token = default)
|
||||
{
|
||||
var parseJob = new MeshParsingJob(root, sourcePath, allocationHandle, meshSettings);
|
||||
return Task.Run(parseJob.Execute, token);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ using Misaki.HighPerformance.Mathematics;
|
||||
using Misaki.HighPerformance.Mathematics.Geometry;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace Ghost.Editor.Core.Assets;
|
||||
|
||||
@@ -709,10 +710,25 @@ internal static unsafe partial class MeshProcessor
|
||||
var materialIndex = context.materialIndex;
|
||||
|
||||
// Ensure lists are initialized
|
||||
if (!meshletData->groups.IsCreated) meshletData->groups = new UnsafeList<MeshletGroup>(16, AllocationHandle.TLSF);
|
||||
if (!meshletData->meshlets.IsCreated) meshletData->meshlets = new UnsafeList<Meshlet>(64, AllocationHandle.TLSF);
|
||||
if (!meshletData->meshletVertices.IsCreated) meshletData->meshletVertices = new UnsafeList<uint>(128, AllocationHandle.TLSF);
|
||||
if (!meshletData->meshletTriangles.IsCreated) meshletData->meshletTriangles = new UnsafeList<uint>(128, AllocationHandle.TLSF);
|
||||
if (!meshletData->groups.IsCreated)
|
||||
{
|
||||
meshletData->groups = new UnsafeList<MeshletGroup>(16, AllocationHandle.TLSF);
|
||||
}
|
||||
|
||||
if (!meshletData->meshlets.IsCreated)
|
||||
{
|
||||
meshletData->meshlets = new UnsafeList<Meshlet>(64, AllocationHandle.TLSF);
|
||||
}
|
||||
|
||||
if (!meshletData->meshletVertices.IsCreated)
|
||||
{
|
||||
meshletData->meshletVertices = new UnsafeList<uint>(128, AllocationHandle.TLSF);
|
||||
}
|
||||
|
||||
if (!meshletData->meshletTriangles.IsCreated)
|
||||
{
|
||||
meshletData->meshletTriangles = new UnsafeList<uint>(128, AllocationHandle.TLSF);
|
||||
}
|
||||
|
||||
var meshletGroup = new MeshletGroup
|
||||
{
|
||||
@@ -770,15 +786,14 @@ internal static unsafe partial class MeshProcessor
|
||||
|
||||
internal static partial class MeshProcessor
|
||||
{
|
||||
|
||||
private struct MeshletBuildJob : IJob
|
||||
private class MeshletBuildJob
|
||||
{
|
||||
public ClodConfig clodConfig;
|
||||
public ClodMesh clodMesh;
|
||||
|
||||
public MeshletContext context;
|
||||
|
||||
public readonly void Execute(ref readonly JobExecutionContext ctx)
|
||||
public void Execute()
|
||||
{
|
||||
Build(in clodConfig, in clodMesh, context, MeshletOutputCallback);
|
||||
}
|
||||
@@ -789,9 +804,7 @@ internal static partial class MeshProcessor
|
||||
/// Each <see cref="MaterialPartInfo"/> describes a material partition's index range within the unified buffer.
|
||||
/// Meshlets are built per-part and tagged with the corresponding <c>localMaterialIndex</c>.
|
||||
/// </summary>
|
||||
public static async Task<DisposablePtr<MeshletMeshData>> BuildMeshletsAsync(JobScheduler jobScheduler,
|
||||
ReadOnlyView<Vertex> vertices, ReadOnlyView<uint> indices, ReadOnlyView<MaterialPartInfo> parts,
|
||||
CancellationToken token)
|
||||
public static async Task<DisposablePtr<MeshletMeshData>> BuildMeshletsAsync(ReadOnlyView<Vertex> vertices, ReadOnlyView<uint> indices, ReadOnlyView<MaterialPartInfo> parts, CancellationToken token)
|
||||
{
|
||||
Logger.DebugAssert(vertices.Count > 0, "Mesh must have vertices to build meshlets.");
|
||||
Logger.DebugAssert(indices.Count > 0, "Mesh must have indices to build meshlets.");
|
||||
@@ -821,8 +834,6 @@ internal static partial class MeshProcessor
|
||||
simplifyFallbackSloppy = true,
|
||||
};
|
||||
|
||||
var jobs = new MeshletBuildJob[parts.Length];
|
||||
|
||||
IntPtr meshletData;
|
||||
unsafe
|
||||
{
|
||||
@@ -836,6 +847,7 @@ internal static partial class MeshProcessor
|
||||
for (var i = 0; i < parts.Length; i++)
|
||||
{
|
||||
ref readonly var part = ref parts[i];
|
||||
MeshletBuildJob job;
|
||||
|
||||
unsafe
|
||||
{
|
||||
@@ -859,21 +871,15 @@ internal static partial class MeshProcessor
|
||||
materialIndex = part.materialIndex
|
||||
};
|
||||
|
||||
var job = new MeshletBuildJob
|
||||
job = new MeshletBuildJob
|
||||
{
|
||||
clodConfig = config,
|
||||
clodMesh = clodMesh,
|
||||
context = context
|
||||
};
|
||||
|
||||
jobs[i] = job;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var job in jobs)
|
||||
{
|
||||
var handle = jobScheduler.Schedule(in job);
|
||||
await jobScheduler.WaitAsync(handle, token);
|
||||
await Task.Run(job.Execute, token);
|
||||
}
|
||||
|
||||
unsafe
|
||||
@@ -956,8 +962,15 @@ internal static partial class MeshProcessor
|
||||
|
||||
var extents = centroidMax - centroidMin;
|
||||
var splitAxis = 0;
|
||||
if (extents.y > extents.x && extents.y > extents.z) splitAxis = 1;
|
||||
if (extents.z > extents.x && extents.z > extents.y) splitAxis = 2;
|
||||
if (extents.y > extents.x && extents.y > extents.z)
|
||||
{
|
||||
splitAxis = 1;
|
||||
}
|
||||
|
||||
if (extents.z > extents.x && extents.z > extents.y)
|
||||
{
|
||||
splitAxis = 2;
|
||||
}
|
||||
|
||||
var splitPoint = centroidMin[splitAxis] + extents[splitAxis] * 0.5f;
|
||||
|
||||
@@ -1008,8 +1021,15 @@ internal static partial class MeshProcessor
|
||||
{
|
||||
gathered.Clear();
|
||||
var node = binaryNodes[nodeIndex];
|
||||
if (node.leftChild != -1) gathered.Add(node.leftChild);
|
||||
if (node.rightChild != -1) gathered.Add(node.rightChild);
|
||||
if (node.leftChild != -1)
|
||||
{
|
||||
gathered.Add(node.leftChild);
|
||||
}
|
||||
|
||||
if (node.rightChild != -1)
|
||||
{
|
||||
gathered.Add(node.rightChild);
|
||||
}
|
||||
|
||||
while (gathered.Count < 4)
|
||||
{
|
||||
@@ -1034,12 +1054,22 @@ internal static partial class MeshProcessor
|
||||
}
|
||||
}
|
||||
|
||||
if (largestInternalIndex == -1) break; // all gathered are leaves
|
||||
if (largestInternalIndex == -1)
|
||||
{
|
||||
break; // all gathered are leaves
|
||||
}
|
||||
|
||||
gathered.RemoveAt(listIndexToRemove);
|
||||
var largestNode = binaryNodes[largestInternalIndex];
|
||||
if (largestNode.leftChild != -1) gathered.Add(largestNode.leftChild);
|
||||
if (largestNode.rightChild != -1) gathered.Add(largestNode.rightChild);
|
||||
if (largestNode.leftChild != -1)
|
||||
{
|
||||
gathered.Add(largestNode.leftChild);
|
||||
}
|
||||
|
||||
if (largestNode.rightChild != -1)
|
||||
{
|
||||
gathered.Add(largestNode.rightChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1137,20 +1167,19 @@ internal static partial class MeshProcessor
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe struct BuildClusterLodHierarchyJob : IJob
|
||||
private unsafe class BuildClusterLodHierarchyJob
|
||||
{
|
||||
public MeshletMeshData* meshletData;
|
||||
|
||||
public readonly void Execute(ref readonly JobExecutionContext ctx)
|
||||
public void Execute()
|
||||
{
|
||||
using var scope = AllocationManager.CreateStackScope();
|
||||
using var meshletIndices = new UnsafeArray<int>(meshletData->meshletCount, scope.AllocationHandle);
|
||||
using var meshletIndices = new UnsafeArray<int>(meshletData->meshletCount, AllocationHandle.TLSF);
|
||||
for (var i = 0; i < meshletData->meshletCount; i++)
|
||||
{
|
||||
meshletIndices[i] = i;
|
||||
}
|
||||
|
||||
var binaryNodes = new UnsafeList<TempBinaryNode>(meshletData->meshletCount * 2, scope.AllocationHandle);
|
||||
var binaryNodes = new UnsafeList<TempBinaryNode>(meshletData->meshletCount * 2, AllocationHandle.TLSF);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -1201,14 +1230,13 @@ internal static partial class MeshProcessor
|
||||
/// Builds a cluster LOD hierarchy from the input meshlet data.
|
||||
/// </summary>
|
||||
/// <param name="meshletData">The meshlet data.</param>
|
||||
public static async Task BuildClusterLodHierarchyAsync(JobScheduler jobScheduler, SharedPtr<MeshletMeshData> meshletData, CancellationToken token)
|
||||
public static Task BuildClusterLodHierarchyAsync(SharedPtr<MeshletMeshData> meshletData, CancellationToken token)
|
||||
{
|
||||
if (meshletData.GetRef().meshletCount == 0)
|
||||
{
|
||||
return;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
JobHandle handle;
|
||||
unsafe
|
||||
{
|
||||
var job = new BuildClusterLodHierarchyJob
|
||||
@@ -1216,9 +1244,7 @@ internal static partial class MeshProcessor
|
||||
meshletData = meshletData.Get()
|
||||
};
|
||||
|
||||
handle = jobScheduler.Schedule(in job);
|
||||
return Task.Run(job.Execute, token);
|
||||
}
|
||||
|
||||
await jobScheduler.WaitAsync(handle, token);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Core.Utilities;
|
||||
using Ghost.Editor.Core.Services;
|
||||
using Ghost.Engine;
|
||||
using Ghost.Engine.Streaming;
|
||||
using Ghost.Graphics.Core;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.Jobs;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
@@ -15,7 +13,6 @@ using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using TerraFX.Interop.Mimalloc;
|
||||
|
||||
namespace Ghost.Editor.Core.Assets;
|
||||
|
||||
@@ -72,54 +69,25 @@ public sealed class ModelManifestMetadata
|
||||
|
||||
internal sealed class ImportedModelAsset : IAsset
|
||||
{
|
||||
public Guid ID
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public Guid TypeID => typeof(MeshAsset).GUID;
|
||||
|
||||
public IAssetSettings? Settings
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public ModelManifest Manifest
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public ImportedModelAsset(Guid id, IAssetSettings? settings, ModelManifest manifest)
|
||||
: base(id, typeof(ModelAsset).GUID, settings)
|
||||
{
|
||||
ID = id;
|
||||
Settings = settings;
|
||||
Manifest = manifest;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[Guid(GUID)]
|
||||
public abstract class MeshAsset : IAsset
|
||||
public abstract class ModelAsset : IAsset
|
||||
{
|
||||
public const string GUID = "B99CA68E-EE7A-4822-BF1C-AA0A5120C36A";
|
||||
|
||||
private MeshNode _root;
|
||||
|
||||
public Guid ID
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public IAssetSettings Settings
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public Guid TypeID => typeof(MeshAsset).GUID;
|
||||
|
||||
public MeshNode Root
|
||||
{
|
||||
get => _root;
|
||||
@@ -130,17 +98,18 @@ public abstract class MeshAsset : IAsset
|
||||
}
|
||||
}
|
||||
|
||||
internal MeshAsset(MeshNode root, Guid id, MeshAssetSettings settings)
|
||||
internal ModelAsset(MeshNode root, Guid id, ModelAssetSettings settings)
|
||||
: base(id, typeof(ModelAsset).GUID, settings)
|
||||
{
|
||||
_root = root;
|
||||
|
||||
ID = id;
|
||||
Settings = settings;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
_root?.Dispose();
|
||||
if (disposing)
|
||||
{
|
||||
_root?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +130,7 @@ public enum VertexDataSource
|
||||
ComputedIfMissing
|
||||
}
|
||||
|
||||
public class MeshAssetSettings : IAssetSettings
|
||||
public class ModelAssetSettings : IAssetSettings
|
||||
{
|
||||
public VertexDataSource NormalDataSource
|
||||
{
|
||||
@@ -174,7 +143,7 @@ public class MeshAssetSettings : IAssetSettings
|
||||
} = VertexDataSource.ComputedIfMissing;
|
||||
}
|
||||
|
||||
internal class ObjAssetSettings : MeshAssetSettings
|
||||
internal class ObjAssetSettings : ModelAssetSettings
|
||||
{
|
||||
public CoordinateAxis ObjectUpAxis
|
||||
{
|
||||
@@ -197,12 +166,12 @@ internal class ObjAssetSettings : MeshAssetSettings
|
||||
} = 1.0f;
|
||||
}
|
||||
|
||||
internal class FbxAssetSettings : MeshAssetSettings
|
||||
internal class FbxAssetSettings : ModelAssetSettings
|
||||
{
|
||||
}
|
||||
|
||||
[CustomAssetHandler(AssetTypeId = MeshAsset.GUID, RuntimeAssetType = AssetType.Mesh, Extensions = new[] { ".fbx", ".obj" })]
|
||||
internal class MeshAssetHandler : IImportableAssetHandler, IPackableAssetHandler
|
||||
[CustomAssetHandler(AssetTypeId = ModelAsset.GUID, RuntimeAssetType = AssetType.Mesh, Extensions = new[] { ".fbx", ".obj" })]
|
||||
internal class ModelAssetHandler : IImportableAssetHandler, IPackableAssetHandler
|
||||
{
|
||||
private static readonly JsonSerializerOptions s_jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
@@ -210,8 +179,6 @@ internal class MeshAssetHandler : IImportableAssetHandler, IPackableAssetHandler
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
};
|
||||
|
||||
private readonly JobScheduler _jobScheduler = EditorApplication.GetService<EngineCore>().JobScheduler;
|
||||
|
||||
public IAssetSettings? CreateDefaultSettings(string ext)
|
||||
{
|
||||
if (string.Equals(ext, ".obj", StringComparison.OrdinalIgnoreCase))
|
||||
@@ -265,10 +232,12 @@ internal class MeshAssetHandler : IImportableAssetHandler, IPackableAssetHandler
|
||||
var meshSettings = ResolveSettings(sourcePath, settings);
|
||||
|
||||
using var root = new MeshNode();
|
||||
var result = await MeshProcessor.ParseMeshAsync(root, sourcePath, AllocationHandle.TLSF, meshSettings, token).ConfigureAwait(false);
|
||||
|
||||
var parseJob = new MeshParsingJob(root, sourcePath, AllocationHandle.Persistent, meshSettings);
|
||||
var handle = _jobScheduler.Schedule(in parseJob);
|
||||
await _jobScheduler.WaitAsync(handle, token);
|
||||
if (result.IsFailure)
|
||||
{
|
||||
return Result.Failure(result.Message);
|
||||
}
|
||||
|
||||
var manifest = new ModelManifest
|
||||
{
|
||||
@@ -296,9 +265,9 @@ internal class MeshAssetHandler : IImportableAssetHandler, IPackableAssetHandler
|
||||
return ValueTask.FromResult(Result.Failure("Packing model assets is not supported yet."));
|
||||
}
|
||||
|
||||
private static MeshAssetSettings ResolveSettings(string sourcePath, IAssetSettings? settings)
|
||||
private static ModelAssetSettings ResolveSettings(string sourcePath, IAssetSettings? settings)
|
||||
{
|
||||
if (settings is MeshAssetSettings meshSettings)
|
||||
if (settings is ModelAssetSettings meshSettings)
|
||||
{
|
||||
return meshSettings;
|
||||
}
|
||||
@@ -355,7 +324,7 @@ internal class MeshAssetHandler : IImportableAssetHandler, IPackableAssetHandler
|
||||
node.Name,
|
||||
stablePath,
|
||||
$"{sourcePath}#Mesh/{stablePath}",
|
||||
typeof(MeshAsset).GUID));
|
||||
typeof(ModelAsset).GUID));
|
||||
}
|
||||
else if (node is LightMeshNode)
|
||||
{
|
||||
@@ -377,8 +346,8 @@ internal class MeshAssetHandler : IImportableAssetHandler, IPackableAssetHandler
|
||||
|
||||
private async ValueTask<(int materialSlotCount, int lodLevelCount)> WriteMeshContentAsync(string targetPath, GeometryMeshNode geometry, CancellationToken token)
|
||||
{
|
||||
using var meshletData = await MeshProcessor.BuildMeshletsAsync(_jobScheduler, geometry.Vertices, geometry.Indices, geometry.MaterialParts, token).ConfigureAwait(false);
|
||||
await MeshProcessor.BuildClusterLodHierarchyAsync(_jobScheduler, meshletData.Share(), token).ConfigureAwait(false);
|
||||
using var meshletData = await MeshProcessor.BuildMeshletsAsync(geometry.Vertices, geometry.Indices, geometry.MaterialParts, token).ConfigureAwait(false);
|
||||
await MeshProcessor.BuildClusterLodHierarchyAsync(meshletData.Share(), token).ConfigureAwait(false);
|
||||
|
||||
var bounds = ComputeBounds(geometry.Vertices);
|
||||
var header = new MeshContentHeader
|
||||
@@ -1,5 +1,3 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Engine;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ghost.Editor.Core.Assets;
|
||||
@@ -7,43 +5,29 @@ namespace Ghost.Editor.Core.Assets;
|
||||
[Guid(GUID)]
|
||||
public sealed class SceneAsset : IAsset
|
||||
{
|
||||
public const string GUID = "1B5E3F2A-8D91-4C67-BE32-A0F9C6D4E781";
|
||||
public const string GUID = "1B5E3F2A-8D91-4C67-BE32-A0F9C6D4E781";
|
||||
|
||||
private static readonly Guid s_typeID = Guid.Parse(GUID);
|
||||
public ushort RuntimeSceneID
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public Guid ID
|
||||
{
|
||||
get;
|
||||
}
|
||||
public string SceneName
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public Guid TypeID => s_typeID;
|
||||
public int EntityCount
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public IAssetSettings? Settings
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public string SceneName
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public int EntityCount
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public SceneAsset(Guid id, IAssetSettings? settings)
|
||||
{
|
||||
ID = id;
|
||||
Settings = settings;
|
||||
SceneName = string.Empty;
|
||||
EntityCount = 0;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
public SceneAsset(Guid id, IAssetSettings? settings)
|
||||
: base(id, typeof(SceneAsset).GUID, settings)
|
||||
{
|
||||
SceneName = string.Empty;
|
||||
EntityCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SceneAssetSettings : IAssetSettings
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Editor.Core.Services;
|
||||
using Ghost.Engine;
|
||||
using Ghost.Engine.Streaming;
|
||||
|
||||
namespace Ghost.Editor.Core.Assets;
|
||||
@@ -7,6 +9,26 @@ namespace Ghost.Editor.Core.Assets;
|
||||
[CustomAssetHandler(AssetTypeId = SceneAsset.GUID, RuntimeAssetType = AssetType.Scene, Extensions = new[] { ".gscene" })]
|
||||
internal class SceneAssetHandler : IImportableAssetHandler, IPackableAssetHandler
|
||||
{
|
||||
[AssetOpenHandler(".gscene")]
|
||||
private static async Task<Result> OpenAsync(string path)
|
||||
{
|
||||
// Actually double clicking the asset in content browser will just open it.
|
||||
// We probably shouldn't do the actual loading in OpenAsync, but let's keep it simple for now.
|
||||
// OpenAsync usually returns immediately if there's no UI, or we should use AssetRegistry.LoadAssetAsync
|
||||
var assetRegistry = EditorApplication.GetService<IAssetRegistry>();
|
||||
var id = Guid.NewGuid(); // Wait, how do we know the ID?
|
||||
// AssetMeta handles this. This method is just a quick hack for double clicking.
|
||||
var data = await SceneSerializationService.DeserializeSceneFileAsync(path);
|
||||
if (data == null)
|
||||
{
|
||||
return Result.Failure("Failed to load scene.");
|
||||
}
|
||||
|
||||
var service = EditorApplication.GetService<SceneSerializationService>();
|
||||
service.LoadSceneIntoEditorWorld(data, SceneLoadingType.Single, null);
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
public IAssetSettings? CreateDefaultSettings(string ext)
|
||||
{
|
||||
return new SceneAssetSettings();
|
||||
@@ -26,8 +48,22 @@ internal class SceneAssetHandler : IImportableAssetHandler, IPackableAssetHandle
|
||||
{
|
||||
SceneName = Path.GetFileNameWithoutExtension(assetPath),
|
||||
EntityCount = data?.Entities?.Count ?? 0,
|
||||
RuntimeSceneID = Ghost.Engine.Core.Scene.INVALID_ID // Default
|
||||
};
|
||||
|
||||
if (data != null)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<IAsset>();
|
||||
var service = EditorApplication.GetService<SceneSerializationService>();
|
||||
service.LoadSceneIntoEditorWorld(data, SceneLoadingType.Single, (scene) =>
|
||||
{
|
||||
asset.RuntimeSceneID = scene.ID;
|
||||
EditorApplication.GetService<IEditorWorldService>().RegisterSceneAsset(scene.ID, asset);
|
||||
tcs.TrySetResult(asset);
|
||||
});
|
||||
return Result.Success(await tcs.Task);
|
||||
}
|
||||
|
||||
return Result.Success<IAsset>(asset);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -36,14 +72,41 @@ internal class SceneAssetHandler : IImportableAssetHandler, IPackableAssetHandle
|
||||
}
|
||||
}
|
||||
|
||||
public ValueTask<Result> SaveAssetAsync(string targetPath, IAsset asset, CancellationToken token = default)
|
||||
public async ValueTask<Result> SaveAssetAsync(string targetPath, IAsset asset, CancellationToken token = default)
|
||||
{
|
||||
if (asset is not SceneAsset sceneAsset)
|
||||
{
|
||||
return ValueTask.FromResult(Result.Failure("Asset type is not SceneAsset"));
|
||||
return Result.Failure("Asset type is not SceneAsset");
|
||||
}
|
||||
|
||||
return ValueTask.FromResult(Result.Failure("Scene saving is handled by SceneSerializationService directly."));
|
||||
var worldService = EditorApplication.GetService<IEditorWorldService>();
|
||||
var tcs = new TaskCompletionSource<byte[]>();
|
||||
|
||||
worldService.Defer(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var scene = Ghost.Engine.Core.Scene.FromID(sceneAsset.RuntimeSceneID);
|
||||
var service = EditorApplication.GetService<SceneSerializationService>();
|
||||
var bytes = service.SerializeSceneToMemory(scene);
|
||||
tcs.TrySetResult(bytes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
}
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
var bytes = await tcs.Task;
|
||||
await File.WriteAllBytesAsync(targetPath, bytes, token);
|
||||
return Result.Success();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result.Failure($"Failed to save scene: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask<Result<ImportedSubAsset[]>> ImportAsync(string sourcePath, string targetPath, Guid id, IAssetSettings? settings, CancellationToken token = default)
|
||||
|
||||
@@ -11,32 +11,16 @@ public sealed partial class GraphicsShaderAsset : IAsset
|
||||
{
|
||||
public const string GUID = "7BD4591C-B017-4814-AA0B-3F30EB3E727E";
|
||||
|
||||
public Guid ID
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public IAssetSettings? Settings
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public Guid TypeID => typeof(GraphicsShaderAsset).GUID;
|
||||
|
||||
public GraphicsShaderDescriptor Descriptor
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
internal GraphicsShaderAsset(GraphicsShaderDescriptor descriptor, Guid id)
|
||||
: base(id, typeof(GraphicsShaderAsset).GUID, null)
|
||||
{
|
||||
ID = id;
|
||||
Descriptor = descriptor;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[Guid(GUID)]
|
||||
@@ -44,32 +28,16 @@ public sealed partial class ComputeShaderAsset : IAsset
|
||||
{
|
||||
public const string GUID = "EA881979-CD8D-4088-B568-D42645F18C2A";
|
||||
|
||||
public Guid ID
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public IAssetSettings? Settings
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public Guid TypeID => typeof(ComputeShaderAsset).GUID;
|
||||
|
||||
public ComputeShaderDescriptor Descriptor
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
internal ComputeShaderAsset(ComputeShaderDescriptor descriptor, Guid id)
|
||||
: base(id, typeof(ComputeShaderAsset).GUID, null)
|
||||
{
|
||||
ID = id;
|
||||
Descriptor = descriptor;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// Shader does not handle import/export via asset registry, it will handled by the hot reload system.
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Engine;
|
||||
using Ghost.Engine.Streaming;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Ghost.StbI;
|
||||
@@ -55,11 +54,6 @@ public unsafe class TextureAsset : IAsset
|
||||
{
|
||||
public const string GUID = "27965FFF-860C-40EF-9123-1874D7DE9CDC";
|
||||
|
||||
private static readonly Guid s_typeID = Guid.Parse(GUID);
|
||||
|
||||
private readonly Guid _id;
|
||||
private readonly IAssetSettings _settings;
|
||||
|
||||
private readonly IntPtr _textureData;
|
||||
private readonly uint _width;
|
||||
private readonly uint _height;
|
||||
@@ -67,10 +61,6 @@ public unsafe class TextureAsset : IAsset
|
||||
private readonly uint _colorComponents;
|
||||
private readonly uint _dimension;
|
||||
|
||||
public Guid ID => _id;
|
||||
public Guid TypeID => typeof(TextureAsset).GUID;
|
||||
public IAssetSettings Settings => _settings;
|
||||
|
||||
public IntPtr TextureData => _textureData;
|
||||
public uint Width => _width;
|
||||
public uint Height => _height;
|
||||
@@ -79,10 +69,8 @@ public unsafe class TextureAsset : IAsset
|
||||
public uint ColorComponents => _colorComponents;
|
||||
|
||||
internal TextureAsset([OwnershipTransfer] IntPtr data, TextureContentHeader header, Guid id, IAssetSettings settings)
|
||||
: base(id, typeof(TextureAsset).GUID, settings)
|
||||
{
|
||||
_id = id;
|
||||
_settings = settings;
|
||||
|
||||
_textureData = data;
|
||||
_width = header.width;
|
||||
_height = header.height;
|
||||
@@ -91,15 +79,9 @@ public unsafe class TextureAsset : IAsset
|
||||
_colorComponents = header.colorComponents;
|
||||
}
|
||||
|
||||
~TextureAsset()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
StbIApi.ImageFree((void*)_textureData);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using Windows.System;
|
||||
|
||||
namespace Ghost.Editor.Core;
|
||||
|
||||
/// <summary>
|
||||
@@ -19,6 +21,19 @@ public class CustomEditorAttribute : DiscoverableAttributeBase
|
||||
}
|
||||
}
|
||||
|
||||
public class AssetOpenHandlerAttribute : DiscoverableAttributeBase
|
||||
{
|
||||
internal string[] Extensions
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public AssetOpenHandlerAttribute(params string[] extensions)
|
||||
{
|
||||
Extensions = extensions;
|
||||
}
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class ContextMenuItemAttribute : DiscoverableAttributeBase
|
||||
{
|
||||
@@ -37,10 +52,35 @@ public sealed class ContextMenuItemAttribute : DiscoverableAttributeBase
|
||||
get;
|
||||
}
|
||||
|
||||
public ContextMenuItemAttribute(string tag, string name, int group = 0)
|
||||
public int Priority
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public ContextMenuItemAttribute(string tag, string name, int group = 0, int priority = 0)
|
||||
{
|
||||
Tag = tag;
|
||||
Name = name;
|
||||
Group = group;
|
||||
Priority = priority;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ShortcutAttribute : DiscoverableAttributeBase
|
||||
{
|
||||
public VirtualKey Key
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public VirtualKeyModifiers Modifiers
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public ShortcutAttribute(VirtualKey key, VirtualKeyModifiers modifiers = VirtualKeyModifiers.None)
|
||||
{
|
||||
Key = key;
|
||||
Modifiers = modifiers;
|
||||
}
|
||||
}
|
||||
@@ -58,4 +58,7 @@ public interface IAssetRegistry : IDisposable
|
||||
ValueTask<Result> SaveAssetIfDirtyAsync(IAsset asset, CancellationToken token = default);
|
||||
ValueTask<Result> SaveAssetIfDirtyAsync(Guid id, CancellationToken token = default);
|
||||
ValueTask<Result[]> SaveDirtyAssetsAsync();
|
||||
|
||||
Task<Result> OpenAssetAsync(Guid id);
|
||||
Task<Result> OpenAssetAsync(string assetPath);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
using Ghost.Core;
|
||||
|
||||
namespace Ghost.Editor.Core.Contracts;
|
||||
|
||||
public interface IDirtyTrackerService
|
||||
{
|
||||
/// <summary>
|
||||
/// Marks the specified object as dirty.
|
||||
/// </summary>
|
||||
void MarkDirty(GhostObject obj);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the specified object is dirty compared to its clean state.
|
||||
/// </summary>
|
||||
bool IsDirty(GhostObject obj);
|
||||
|
||||
/// <summary>
|
||||
/// Marks the specified object as clean (e.g., after a successful save).
|
||||
/// </summary>
|
||||
void MarkClean(GhostObject obj);
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of all currently dirty objects.
|
||||
/// </summary>
|
||||
IReadOnlyList<GhostObject> GetDirtyObjects();
|
||||
}
|
||||
@@ -9,5 +9,8 @@ public interface IInspectable
|
||||
|
||||
UIElement? CreateHeader();
|
||||
|
||||
UIElement? CreateInspector();
|
||||
IInspectorModel CreateInspectorModel();
|
||||
|
||||
// void OnSelected();
|
||||
// void OnDeselected();
|
||||
}
|
||||
26
src/Editor/Ghost.Editor.Core/Contracts/IInspectorModel.cs
Normal file
26
src/Editor/Ghost.Editor.Core/Contracts/IInspectorModel.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
namespace Ghost.Editor.Core.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Represents an active model for an object being inspected.
|
||||
/// Responsible for generating its own UI.
|
||||
/// </summary>
|
||||
public interface IInspectorModel : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Generate the UI element that represents the body of the inspector for this model.
|
||||
/// </summary>
|
||||
UIElement BuildUI();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An inspector model that requires continuous synchronization (e.g. per-frame updates).
|
||||
/// </summary>
|
||||
public interface ISyncableInspectorModel : IInspectorModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Called per-frame to sync data (e.g. from ECS to UI and back).
|
||||
/// </summary>
|
||||
void Sync();
|
||||
}
|
||||
@@ -60,7 +60,7 @@ public enum ShaderStage
|
||||
Library // For ray tracing shaders or work graph shaders that don't fit into the traditional shader stages
|
||||
}
|
||||
|
||||
public interface IShaderCompiler : IDisposable
|
||||
internal interface IShaderCompiler : IDisposable
|
||||
{
|
||||
Result<UnsafeArray<byte>> Compile(ref readonly ShaderCompilationConfig config, AllocationHandle handle);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,9 @@ public sealed partial class Float3Field : ValueControl<float3>
|
||||
_yComponent = GetTemplateChild("YComponent") as NumberBox;
|
||||
_zComponent = GetTemplateChild("ZComponent") as NumberBox;
|
||||
|
||||
SuppressChangedEvent = true;
|
||||
SyncFromValue();
|
||||
SuppressChangedEvent = false;
|
||||
|
||||
_xComponent?.ValueChanged += OnComponentChanged;
|
||||
_yComponent?.ValueChanged += OnComponentChanged;
|
||||
@@ -44,11 +46,9 @@ public sealed partial class Float3Field : ValueControl<float3>
|
||||
|
||||
private void SyncFromValue()
|
||||
{
|
||||
SuppressChangedEvent = true;
|
||||
_xComponent?.Value = Value.x;
|
||||
_yComponent?.Value = Value.y;
|
||||
_zComponent?.Value = Value.z;
|
||||
SuppressChangedEvent = false;
|
||||
}
|
||||
|
||||
private void OnComponentChanged(NumberBox sender, NumberBoxValueChangedEventArgs args)
|
||||
@@ -63,7 +63,6 @@ public sealed partial class Float3Field : ValueControl<float3>
|
||||
(float)(_yComponent?.Value ?? 0),
|
||||
(float)(_zComponent?.Value ?? 0));
|
||||
|
||||
RiseChangedEvent(Value, newValue);
|
||||
Value = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,18 @@ public sealed partial class PropertyField : ContentControl
|
||||
typeof(PropertyField),
|
||||
new PropertyMetadata(default(string)));
|
||||
|
||||
public bool IsEditable
|
||||
{
|
||||
get => (bool)GetValue(IsEditableProperty);
|
||||
set => SetValue(IsEditableProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty IsEditableProperty = DependencyProperty.Register(
|
||||
nameof(IsEditable),
|
||||
typeof(bool),
|
||||
typeof(PropertyField),
|
||||
new PropertyMetadata(true));
|
||||
|
||||
public PropertyField()
|
||||
{
|
||||
DefaultStyleKey = typeof(PropertyField);
|
||||
|
||||
@@ -8,24 +8,22 @@
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="local:PropertyField">
|
||||
<Grid Height="32" Margin="2,4">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="125" />
|
||||
<ColumnDefinition Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackPanel Margin="2,4" Spacing="4">
|
||||
<TextBlock
|
||||
Grid.Column="0"
|
||||
Margin="0,0,0,4"
|
||||
VerticalAlignment="Center"
|
||||
Style="{StaticResource BodyTextBlockStyle}"
|
||||
Text="{TemplateBinding Label}"
|
||||
TextTrimming="CharacterEllipsis" />
|
||||
|
||||
<ContentPresenter
|
||||
Grid.Column="1"
|
||||
<ContentControl
|
||||
Margin="2,0,0,0"
|
||||
HorizontalAlignment="Stretch"
|
||||
HorizontalContentAlignment="Stretch"
|
||||
Content="{TemplateBinding Content}"
|
||||
ContentTemplate="{TemplateBinding ContentTemplate}" />
|
||||
</Grid>
|
||||
ContentTemplate="{TemplateBinding ContentTemplate}"
|
||||
IsEnabled="{TemplateBinding IsEditable}" />
|
||||
</StackPanel>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
|
||||
@@ -4,10 +4,10 @@ namespace Ghost.Editor.Core.Controls;
|
||||
|
||||
public partial class ControlsDictionary : ResourceDictionary
|
||||
{
|
||||
private const string _DICTIONARY_PATH = "ms-appx:///Ghost.Editor.Core/Controls/ControlsDictionary.xaml";
|
||||
private const string DICTIONARY_PATH = "ms-appx:///Ghost.Editor.Core/Controls/ControlsDictionary.xaml";
|
||||
|
||||
public ControlsDictionary()
|
||||
{
|
||||
Source = new Uri(_DICTIONARY_PATH, UriKind.Absolute);
|
||||
Source = new Uri(DICTIONARY_PATH, UriKind.Absolute);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,5 @@
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="ms-appx:///Ghost.Editor.Core/Controls/BasicInput/PropertyField.xaml" />
|
||||
<ResourceDictionary Source="ms-appx:///Ghost.Editor.Core/Controls/BasicInput/Float3Field.xaml" />
|
||||
|
||||
<ResourceDictionary Source="ms-appx:///Ghost.Editor.Core/Controls/Internal/ComponentView.xaml" />
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
|
||||
@@ -1,157 +0,0 @@
|
||||
using Ghost.Editor.Core.Inspector;
|
||||
using Ghost.Editor.Core.Resources;
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Ghost.Entities;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ghost.Editor.Core.Controls;
|
||||
|
||||
internal sealed unsafe partial class ComponentView : Control
|
||||
{
|
||||
private delegate void EditorUpdate();
|
||||
|
||||
private StackPanel? _contentContainer;
|
||||
|
||||
private readonly World? _world;
|
||||
private readonly Entity _entity = Entity.Invalid;
|
||||
private readonly Type? _componentType;
|
||||
private readonly ComponentInfo _componentInfo;
|
||||
|
||||
private object? _managedInstance;
|
||||
private void* _pComponentData;
|
||||
|
||||
private ComponentEditor? _customEditor;
|
||||
private PropertyField[]? _propertyFields;
|
||||
private EditorUpdate? _editorUpdate;
|
||||
|
||||
public string HeaderText
|
||||
{
|
||||
get => (string)GetValue(HeaderTextProperty);
|
||||
set => SetValue(HeaderTextProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty HeaderTextProperty =
|
||||
DependencyProperty.Register(nameof(HeaderText), typeof(string), typeof(ComponentView), new PropertyMetadata(string.Empty));
|
||||
|
||||
internal ComponentView()
|
||||
{
|
||||
DefaultStyleKey = typeof(ComponentView);
|
||||
|
||||
Unloaded += (s, e) =>
|
||||
{
|
||||
_customEditor?.Destroy();
|
||||
|
||||
_contentContainer = null;
|
||||
_customEditor = null;
|
||||
_propertyFields = null;
|
||||
};
|
||||
}
|
||||
|
||||
public ComponentView(string header, World world, Entity entity, Type componentType) : this()
|
||||
{
|
||||
HeaderText = header;
|
||||
|
||||
_world = world;
|
||||
_entity = entity;
|
||||
_componentType = componentType;
|
||||
_componentInfo = ComponentRegistry.GetComponentInfo(componentType);
|
||||
}
|
||||
|
||||
protected override void OnApplyTemplate()
|
||||
{
|
||||
_contentContainer = (StackPanel)GetTemplateChild("ContentContainer");
|
||||
|
||||
base.OnApplyTemplate();
|
||||
ReBuild();
|
||||
}
|
||||
|
||||
private void ReflectionUpdate()
|
||||
{
|
||||
if (_propertyFields == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var propertyField in _propertyFields)
|
||||
{
|
||||
propertyField.UpdateValue();
|
||||
}
|
||||
}
|
||||
|
||||
private void CustomEditorUpdate()
|
||||
{
|
||||
_customEditor?.Update();
|
||||
}
|
||||
|
||||
public void ReBuild()
|
||||
{
|
||||
if (_contentContainer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_contentContainer.Children.Clear();
|
||||
if (_world == null || _componentType == null || _entity == Entity.Invalid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_propertyFields != null)
|
||||
{
|
||||
foreach (var propertyField in _propertyFields)
|
||||
{
|
||||
propertyField.OnValueChanged -= OnPropertyValueChanged;
|
||||
}
|
||||
}
|
||||
|
||||
var componentObject = new ComponentObject(_world, _entity);
|
||||
var editorType = TypeCache.GetTypes().FirstOrDefault(t =>
|
||||
typeof(ComponentEditor).IsAssignableFrom(t) &&
|
||||
t.GetCustomAttribute<CustomEditorAttribute>()?.TargetType.IsAssignableFrom(_componentType) == true);
|
||||
|
||||
if (editorType != null)
|
||||
{
|
||||
_customEditor = (ComponentEditor)Activator.CreateInstance(editorType)!;
|
||||
_customEditor.Initialize(componentObject);
|
||||
_customEditor.Create(_contentContainer);
|
||||
}
|
||||
else
|
||||
{
|
||||
var fields = _componentType.GetFields(StaticResource.ComponentPropertyBindingFlags);
|
||||
_propertyFields = new PropertyField[fields.Length];
|
||||
|
||||
_pComponentData = _world.EntityManager.GetComponent(_entity, _componentInfo.id);
|
||||
_managedInstance = Marshal.PtrToStructure((nint)_pComponentData, _componentType);
|
||||
if (_managedInstance == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < fields.Length; i++)
|
||||
{
|
||||
var field = fields[i];
|
||||
var propertyField = PropertyField.Create(field.Name, field, _managedInstance);
|
||||
propertyField.OnValueChanged += OnPropertyValueChanged;
|
||||
|
||||
_propertyFields[i] = propertyField;
|
||||
_contentContainer.Children.Add(propertyField);
|
||||
}
|
||||
}
|
||||
|
||||
_editorUpdate = _customEditor == null ? ReflectionUpdate : CustomEditorUpdate;
|
||||
_editorUpdate();
|
||||
}
|
||||
|
||||
private void OnPropertyValueChanged(PropertyField field)
|
||||
{
|
||||
if (_managedInstance == null || _pComponentData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Marshal.StructureToPtr(_managedInstance, (nint)_pComponentData, false);
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<ResourceDictionary
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="using:Ghost.Editor.Core.Controls">
|
||||
|
||||
<Style TargetType="local:ComponentView">
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="local:ComponentView">
|
||||
<StackPanel Margin="0,0,0,16">
|
||||
<Border
|
||||
Padding="8"
|
||||
HorizontalAlignment="Stretch"
|
||||
Background="{ThemeResource SolidBackgroundFillColorSecondaryBrush}">
|
||||
<TextBlock Style="{StaticResource BodyStrongTextBlockStyle}" Text="{TemplateBinding HeaderText}" />
|
||||
</Border>
|
||||
<StackPanel
|
||||
x:Name="ContentContainer"
|
||||
Margin="8,2,2,0"
|
||||
Spacing="2" />
|
||||
</StackPanel>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
</ResourceDictionary>
|
||||
@@ -1,44 +1,12 @@
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using System.Reflection;
|
||||
|
||||
|
||||
namespace Ghost.Editor.Core.Controls;
|
||||
|
||||
public sealed partial class ContextFlyout : MenuFlyout
|
||||
{
|
||||
private class MenuNode
|
||||
{
|
||||
public required string Name
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
public MethodInfo? Method
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public List<MenuNode> Children
|
||||
{
|
||||
get;
|
||||
} = new();
|
||||
|
||||
public int RawGroup
|
||||
{
|
||||
get; set;
|
||||
} = int.MaxValue;
|
||||
|
||||
// The calculated group used for sorting (min of children for folders)
|
||||
public int EffectiveGroup
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _isPopulated;
|
||||
|
||||
public string Tag
|
||||
public string ContextMenuTag
|
||||
{
|
||||
get; set;
|
||||
} = string.Empty;
|
||||
@@ -48,160 +16,13 @@ public sealed partial class ContextFlyout : MenuFlyout
|
||||
Opening += ContextFlyout_Opening;
|
||||
}
|
||||
|
||||
// Recursively sorts nodes and calculates folder pGroups
|
||||
private static void PrepareNodes(List<MenuNode> nodes)
|
||||
{
|
||||
if (nodes.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
if (node.Children.Count > 0)
|
||||
{
|
||||
// Go deep first
|
||||
PrepareNodes(node.Children);
|
||||
|
||||
// A folder's group is determined by its highest priority child (lowest group number).
|
||||
// This ensures a "File" folder (containing Group 0 items) sits at the top
|
||||
// alongside other Group 0 leaf items.
|
||||
node.EffectiveGroup = node.Children.Min(c => c.EffectiveGroup);
|
||||
}
|
||||
else
|
||||
{
|
||||
node.EffectiveGroup = node.RawGroup;
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by Group, then by Name
|
||||
nodes.Sort((a, b) =>
|
||||
{
|
||||
var groupCompare = a.EffectiveGroup.CompareTo(b.EffectiveGroup);
|
||||
return groupCompare != 0
|
||||
? groupCompare
|
||||
: string.CompareOrdinal(a.Name, b.Name);
|
||||
});
|
||||
}
|
||||
|
||||
// Recursively builds the UI elements
|
||||
private static void BuildNodes(List<MenuNode> nodes, IList<MenuFlyoutItemBase> targetCollection)
|
||||
{
|
||||
if (nodes.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var currentGroup = nodes[0].EffectiveGroup;
|
||||
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
if (node.EffectiveGroup != currentGroup)
|
||||
{
|
||||
targetCollection.Add(new MenuFlyoutSeparator());
|
||||
currentGroup = node.EffectiveGroup;
|
||||
}
|
||||
|
||||
if (node.Children.Count > 0)
|
||||
{
|
||||
var subItem = new MenuFlyoutSubItem
|
||||
{
|
||||
Text = node.Name
|
||||
};
|
||||
|
||||
// Recursively render children into the subitem
|
||||
BuildNodes(node.Children, subItem.Items);
|
||||
targetCollection.Add(subItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
var menuItem = new MenuFlyoutItem
|
||||
{
|
||||
Text = node.Name
|
||||
};
|
||||
|
||||
var methodToInvoke = node.Method;
|
||||
menuItem.Click += (_, _) =>
|
||||
{
|
||||
methodToInvoke?.Invoke(null, null);
|
||||
};
|
||||
|
||||
targetCollection.Add(menuItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PopulateContextMenu()
|
||||
{
|
||||
var methods = TypeCache.GetMethodsWithAttribute<ContextMenuItemAttribute>();
|
||||
if (methods == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. Build the Tree
|
||||
var rootNodes = new List<MenuNode>();
|
||||
|
||||
foreach (var method in methods)
|
||||
{
|
||||
var attr = method.GetCustomAttribute<ContextMenuItemAttribute>();
|
||||
if (attr == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Filter tags
|
||||
if (!string.Equals(attr.Tag, Tag, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var nameSpan = attr.Name.AsSpan();
|
||||
var pathParts = nameSpan.Split('/');
|
||||
|
||||
var currentLevel = rootNodes;
|
||||
MenuNode? currentNode = null;
|
||||
|
||||
foreach (var range in pathParts)
|
||||
{
|
||||
var part = nameSpan[range.Start..range.End];
|
||||
|
||||
MenuNode? foundNode = null;
|
||||
|
||||
// Try to find existing node in the current level
|
||||
foreach (var node in currentLevel)
|
||||
{
|
||||
if (part.Equals(node.Name.AsSpan(), StringComparison.Ordinal))
|
||||
{
|
||||
foundNode = node;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (foundNode == null)
|
||||
{
|
||||
foundNode = new MenuNode { Name = part.ToString() };
|
||||
currentLevel.Add(foundNode);
|
||||
}
|
||||
|
||||
currentNode = foundNode;
|
||||
|
||||
// If this is the last part, it's the executable item
|
||||
if (range.End.Value == nameSpan.Length)
|
||||
{
|
||||
currentNode.Method = method;
|
||||
currentNode.RawGroup = attr.Group;
|
||||
}
|
||||
|
||||
currentLevel = currentNode.Children;
|
||||
}
|
||||
}
|
||||
|
||||
PrepareNodes(rootNodes);
|
||||
BuildNodes(rootNodes, Items);
|
||||
var rootNodes = MenuUtility.BuildTree(ContextMenuTag);
|
||||
MenuUtility.BuildNodes(rootNodes, Items);
|
||||
}
|
||||
|
||||
private async void ContextFlyout_Opening(object? sender, object e)
|
||||
private void ContextFlyout_Opening(object? sender, object e)
|
||||
{
|
||||
if (_isPopulated)
|
||||
{
|
||||
@@ -211,4 +32,4 @@ public sealed partial class ContextFlyout : MenuFlyout
|
||||
PopulateContextMenu();
|
||||
_isPopulated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
53
src/Editor/Ghost.Editor.Core/Controls/Menu/MenuContextBar.cs
Normal file
53
src/Editor/Ghost.Editor.Core/Controls/Menu/MenuContextBar.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Ghost.Core;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Ghost.Editor.Core.Controls;
|
||||
|
||||
public sealed partial class MenuContextBar : MenuBar
|
||||
{
|
||||
private bool _isPopulated;
|
||||
|
||||
public string ContextMenuTag
|
||||
{
|
||||
get; set;
|
||||
} = string.Empty;
|
||||
|
||||
public MenuContextBar()
|
||||
{
|
||||
Loaded += MenuContextBar_Loaded;
|
||||
}
|
||||
|
||||
private void MenuContextBar_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_isPopulated)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PopulateMenu();
|
||||
_isPopulated = true;
|
||||
}
|
||||
|
||||
private void PopulateMenu()
|
||||
{
|
||||
var rootNodes = MenuUtility.BuildTree(ContextMenuTag);
|
||||
|
||||
foreach (var node in rootNodes)
|
||||
{
|
||||
if (node.Children.Count == 0)
|
||||
{
|
||||
Logger.Warning($"Menu item '{node.Name}' cannot be placed at the root of a MenuContextBar because it lacks a parent group.");
|
||||
continue;
|
||||
}
|
||||
|
||||
var menuBarItem = new MenuBarItem
|
||||
{
|
||||
Title = node.Name
|
||||
};
|
||||
|
||||
MenuUtility.BuildNodes(node.Children, menuBarItem.Items);
|
||||
Items.Add(menuBarItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
236
src/Editor/Ghost.Editor.Core/Controls/Menu/MenuUtility.cs
Normal file
236
src/Editor/Ghost.Editor.Core/Controls/Menu/MenuUtility.cs
Normal file
@@ -0,0 +1,236 @@
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using System.Reflection;
|
||||
using Windows.System;
|
||||
|
||||
namespace Ghost.Editor.Core.Controls;
|
||||
|
||||
internal class MenuNode
|
||||
{
|
||||
public required string Name
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
public MethodInfo? Method
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public List<MenuNode> Children
|
||||
{
|
||||
get;
|
||||
} = new();
|
||||
|
||||
public int RawGroup
|
||||
{
|
||||
get; set;
|
||||
} = int.MaxValue;
|
||||
|
||||
// The calculated group used for sorting (min of children for folders)
|
||||
public int EffectiveGroup
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public int RawPriority
|
||||
{
|
||||
get; set;
|
||||
} = 0;
|
||||
|
||||
public int EffectivePriority
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public VirtualKey ShortCut
|
||||
{
|
||||
get; set;
|
||||
} = VirtualKey.None;
|
||||
|
||||
public VirtualKeyModifiers ShortCutModifiers
|
||||
{
|
||||
get; set;
|
||||
} = VirtualKeyModifiers.None;
|
||||
}
|
||||
|
||||
internal static class MenuUtility
|
||||
{
|
||||
// Recursively sorts nodes and calculates folder pGroups
|
||||
public static void PrepareNodes(List<MenuNode> nodes)
|
||||
{
|
||||
if (nodes.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
if (node.Children.Count > 0)
|
||||
{
|
||||
// Go deep first
|
||||
PrepareNodes(node.Children);
|
||||
|
||||
// A folder's group is determined by its highest priority child (lowest group number).
|
||||
// This ensures a "File" folder (containing Group 0 items) sits at the top
|
||||
// alongside other Group 0 leaf items.
|
||||
node.EffectiveGroup = node.Children.Min(c => c.EffectiveGroup);
|
||||
node.EffectivePriority = node.Children.Max(c => c.EffectivePriority);
|
||||
}
|
||||
else
|
||||
{
|
||||
node.EffectiveGroup = node.RawGroup;
|
||||
node.EffectivePriority = node.RawPriority;
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by Group, then by Priority (higher first), then by Name
|
||||
nodes.Sort((a, b) =>
|
||||
{
|
||||
var groupCompare = a.EffectiveGroup.CompareTo(b.EffectiveGroup);
|
||||
if (groupCompare != 0)
|
||||
{
|
||||
return groupCompare;
|
||||
}
|
||||
|
||||
var priorityCompare = b.EffectivePriority.CompareTo(a.EffectivePriority);
|
||||
return priorityCompare != 0
|
||||
? priorityCompare
|
||||
: string.CompareOrdinal(a.Name, b.Name);
|
||||
});
|
||||
}
|
||||
|
||||
// Recursively builds the UI elements
|
||||
public static void BuildNodes(List<MenuNode> nodes, IList<MenuFlyoutItemBase> targetCollection)
|
||||
{
|
||||
if (nodes.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var currentGroup = nodes[0].EffectiveGroup;
|
||||
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
if (node.EffectiveGroup != currentGroup)
|
||||
{
|
||||
targetCollection.Add(new MenuFlyoutSeparator());
|
||||
currentGroup = node.EffectiveGroup;
|
||||
}
|
||||
|
||||
if (node.Children.Count > 0)
|
||||
{
|
||||
var subItem = new MenuFlyoutSubItem
|
||||
{
|
||||
Text = node.Name
|
||||
};
|
||||
|
||||
// Recursively render children into the subitem
|
||||
BuildNodes(node.Children, subItem.Items);
|
||||
targetCollection.Add(subItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
var menuItem = new MenuFlyoutItem
|
||||
{
|
||||
Text = node.Name
|
||||
};
|
||||
|
||||
var methodToInvoke = node.Method;
|
||||
menuItem.Click += (_, _) =>
|
||||
{
|
||||
methodToInvoke?.Invoke(null, null);
|
||||
};
|
||||
|
||||
if (node.ShortCut != VirtualKey.None)
|
||||
{
|
||||
menuItem.KeyboardAccelerators.Add(new Microsoft.UI.Xaml.Input.KeyboardAccelerator
|
||||
{
|
||||
Key = node.ShortCut,
|
||||
Modifiers = node.ShortCutModifiers
|
||||
});
|
||||
}
|
||||
|
||||
targetCollection.Add(menuItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<MenuNode> BuildTree(string tag)
|
||||
{
|
||||
var methods = TypeCache.GetMethodsWithAttribute<ContextMenuItemAttribute>();
|
||||
if (methods == null)
|
||||
{
|
||||
return new List<MenuNode>();
|
||||
}
|
||||
|
||||
// 1. Build the Tree
|
||||
var rootNodes = new List<MenuNode>();
|
||||
|
||||
foreach (var method in methods)
|
||||
{
|
||||
var attr = method.GetCustomAttribute<ContextMenuItemAttribute>();
|
||||
if (attr == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Filter tags
|
||||
if (!string.Equals(attr.Tag, tag, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var nameSpan = attr.Name.AsSpan();
|
||||
var pathParts = nameSpan.Split('/');
|
||||
|
||||
var currentLevel = rootNodes;
|
||||
MenuNode? currentNode = null;
|
||||
|
||||
foreach (var range in pathParts)
|
||||
{
|
||||
var part = nameSpan[range.Start..range.End];
|
||||
|
||||
MenuNode? foundNode = null;
|
||||
|
||||
// Try to find existing node in the current level
|
||||
foreach (var node in currentLevel)
|
||||
{
|
||||
if (part.Equals(node.Name.AsSpan(), StringComparison.Ordinal))
|
||||
{
|
||||
foundNode = node;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (foundNode == null)
|
||||
{
|
||||
foundNode = new MenuNode { Name = part.ToString() };
|
||||
currentLevel.Add(foundNode);
|
||||
}
|
||||
|
||||
currentNode = foundNode;
|
||||
|
||||
// If this is the last part, it's the executable item
|
||||
if (range.End.Value == nameSpan.Length)
|
||||
{
|
||||
currentNode.Method = method;
|
||||
currentNode.RawGroup = attr.Group;
|
||||
currentNode.RawPriority = attr.Priority;
|
||||
|
||||
var shortCutAttr = method.GetCustomAttribute<ShortcutAttribute>();
|
||||
if (shortCutAttr != null)
|
||||
{
|
||||
currentNode.ShortCut = shortCutAttr.Key;
|
||||
currentNode.ShortCutModifiers = shortCutAttr.Modifiers;
|
||||
}
|
||||
}
|
||||
|
||||
currentLevel = currentNode.Children;
|
||||
}
|
||||
}
|
||||
|
||||
PrepareNodes(rootNodes);
|
||||
return rootNodes;
|
||||
}
|
||||
}
|
||||
32
src/Editor/Ghost.Editor.Core/Controls/ReferenceField.xaml
Normal file
32
src/Editor/Ghost.Editor.Core/Controls/ReferenceField.xaml
Normal file
@@ -0,0 +1,32 @@
|
||||
<UserControl
|
||||
x:Class="Ghost.Editor.Core.Controls.ReferenceField"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d"
|
||||
AllowDrop="{x:Bind AllowDrop, Mode=OneWay}"
|
||||
DragOver="OnDragOver"
|
||||
Drop="OnDrop">
|
||||
|
||||
<Grid CornerRadius="4" BorderThickness="1" x:Name="RootBorder" Background="{ThemeResource CardBackgroundFillColorDefaultBrush}" BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<FontIcon Grid.Column="0" Margin="8,0,0,0" Glyph="{x:Bind IconGlyph, Mode=OneWay}" FontSize="12" Foreground="{ThemeResource TextFillColorSecondaryBrush}" VerticalAlignment="Center" />
|
||||
|
||||
<TextBlock Grid.Column="1" Margin="8,4,8,4" Text="{x:Bind DisplayText, Mode=OneWay}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" FontSize="12" Foreground="{ThemeResource TextFillColorPrimaryBrush}" />
|
||||
|
||||
<Button Grid.Column="2" x:Name="GotoButton" Margin="0,0,4,0" Padding="4" Background="Transparent" BorderThickness="0" Click="OnGotoButtonClicked" Visibility="Collapsed" VerticalAlignment="Center">
|
||||
<FontIcon Glyph="" FontSize="12" />
|
||||
</Button>
|
||||
|
||||
<Button Grid.Column="3" x:Name="ClearButton" Margin="0,0,4,0" Padding="4" Background="Transparent" BorderThickness="0" Click="OnClearButtonClicked" Visibility="Collapsed" VerticalAlignment="Center">
|
||||
<FontIcon Glyph="" FontSize="10" />
|
||||
</Button>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
158
src/Editor/Ghost.Editor.Core/Controls/ReferenceField.xaml.cs
Normal file
158
src/Editor/Ghost.Editor.Core/Controls/ReferenceField.xaml.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Windows.ApplicationModel.DataTransfer;
|
||||
|
||||
namespace Ghost.Editor.Core.Controls;
|
||||
|
||||
public sealed partial class ReferenceField : UserControl
|
||||
{
|
||||
public static readonly DependencyProperty DisplayTextProperty =
|
||||
DependencyProperty.Register(nameof(DisplayText), typeof(string), typeof(ReferenceField), new PropertyMetadata(string.Empty, OnStateChanged));
|
||||
|
||||
public static readonly DependencyProperty TypeLabelProperty =
|
||||
DependencyProperty.Register(nameof(TypeLabel), typeof(string), typeof(ReferenceField), new PropertyMetadata("Object", OnStateChanged));
|
||||
|
||||
public static readonly DependencyProperty IconGlyphProperty =
|
||||
DependencyProperty.Register(nameof(IconGlyph), typeof(string), typeof(ReferenceField), new PropertyMetadata("\uEA86"));
|
||||
|
||||
public static readonly DependencyProperty HasValueProperty =
|
||||
DependencyProperty.Register(nameof(HasValue), typeof(bool), typeof(ReferenceField), new PropertyMetadata(false, OnStateChanged));
|
||||
|
||||
public static readonly DependencyProperty IsReadOnlyProperty =
|
||||
DependencyProperty.Register(nameof(IsReadOnly), typeof(bool), typeof(ReferenceField), new PropertyMetadata(false, OnStateChanged));
|
||||
|
||||
public string DisplayText
|
||||
{
|
||||
get => (string)GetValue(DisplayTextProperty);
|
||||
set => SetValue(DisplayTextProperty, value);
|
||||
}
|
||||
|
||||
public string TypeLabel
|
||||
{
|
||||
get => (string)GetValue(TypeLabelProperty);
|
||||
set => SetValue(TypeLabelProperty, value);
|
||||
}
|
||||
|
||||
public string IconGlyph
|
||||
{
|
||||
get => (string)GetValue(IconGlyphProperty);
|
||||
set => SetValue(IconGlyphProperty, value);
|
||||
}
|
||||
|
||||
public bool HasValue
|
||||
{
|
||||
get => (bool)GetValue(HasValueProperty);
|
||||
set => SetValue(HasValueProperty, value);
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get => (bool)GetValue(IsReadOnlyProperty);
|
||||
set => SetValue(IsReadOnlyProperty, value);
|
||||
}
|
||||
|
||||
public Func<DragEventArgs, bool>? ValidateDrop;
|
||||
public Action<DragEventArgs>? OnDropAccepted;
|
||||
public Action? OnClearClicked;
|
||||
public Action? OnGotoClicked;
|
||||
|
||||
private readonly SolidColorBrush _accentBrush;
|
||||
private readonly SolidColorBrush _errorBrush;
|
||||
private readonly SolidColorBrush _defaultBorderBrush;
|
||||
|
||||
public ReferenceField()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_accentBrush = (SolidColorBrush)Application.Current.Resources["SystemControlHighlightAccentBrush"];
|
||||
_errorBrush = new SolidColorBrush(Microsoft.UI.Colors.Red);
|
||||
_defaultBorderBrush = (SolidColorBrush)Application.Current.Resources["CardStrokeColorDefaultBrush"];
|
||||
|
||||
UpdateState();
|
||||
}
|
||||
|
||||
private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
((ReferenceField)d).UpdateState();
|
||||
}
|
||||
|
||||
private void UpdateState()
|
||||
{
|
||||
if (HasValue)
|
||||
{
|
||||
ClearButton.Visibility = IsReadOnly ? Visibility.Collapsed : Visibility.Visible;
|
||||
GotoButton.Visibility = Visibility.Visible;
|
||||
}
|
||||
else
|
||||
{
|
||||
ClearButton.Visibility = Visibility.Collapsed;
|
||||
GotoButton.Visibility = Visibility.Collapsed;
|
||||
if (string.IsNullOrEmpty(DisplayText))
|
||||
{
|
||||
// We shouldn't change DependencyProperty value here to avoid loops,
|
||||
// but we can bind a different text if needed. For now, rely on caller to set DisplayText to "None (Type)".
|
||||
}
|
||||
}
|
||||
|
||||
AllowDrop = !IsReadOnly;
|
||||
}
|
||||
|
||||
private void OnDragOver(object sender, DragEventArgs e)
|
||||
{
|
||||
if (IsReadOnly)
|
||||
{
|
||||
e.AcceptedOperation = DataPackageOperation.None;
|
||||
return;
|
||||
}
|
||||
|
||||
var isValid = ValidateDrop?.Invoke(e) ?? false;
|
||||
|
||||
if (isValid)
|
||||
{
|
||||
e.AcceptedOperation = DataPackageOperation.Link;
|
||||
RootBorder.BorderBrush = _accentBrush;
|
||||
RootBorder.BorderThickness = new Thickness(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
e.AcceptedOperation = DataPackageOperation.None;
|
||||
// Optionally set error brush
|
||||
RootBorder.BorderBrush = _errorBrush;
|
||||
RootBorder.BorderThickness = new Thickness(1);
|
||||
}
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
protected override void OnDragLeave(DragEventArgs e)
|
||||
{
|
||||
base.OnDragLeave(e);
|
||||
RootBorder.BorderBrush = _defaultBorderBrush;
|
||||
RootBorder.BorderThickness = new Thickness(1);
|
||||
}
|
||||
|
||||
private void OnDrop(object sender, DragEventArgs e)
|
||||
{
|
||||
RootBorder.BorderBrush = _defaultBorderBrush;
|
||||
RootBorder.BorderThickness = new Thickness(1);
|
||||
|
||||
if (IsReadOnly) return;
|
||||
|
||||
var isValid = ValidateDrop?.Invoke(e) ?? false;
|
||||
if (isValid)
|
||||
{
|
||||
OnDropAccepted?.Invoke(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnClearButtonClicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
OnClearClicked?.Invoke();
|
||||
}
|
||||
|
||||
private void OnGotoButtonClicked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
OnGotoClicked?.Invoke();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,20 @@
|
||||
using Ghost.Editor.Core.Event;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Ghost.Editor.Core.Controls;
|
||||
|
||||
public partial class ValueControl<T> : Control
|
||||
public interface INotifyValueChanged<T>
|
||||
{
|
||||
T Value { get; set; }
|
||||
|
||||
event ValueChangedEventHandler<T>? OnValueChanged;
|
||||
|
||||
void SetValueWithoutNotify(T value);
|
||||
}
|
||||
|
||||
public abstract class ValueControl<T> : Control, INotifyValueChanged<T>
|
||||
{
|
||||
private bool _suppressChangedEvent;
|
||||
|
||||
@@ -39,7 +49,7 @@ public partial class ValueControl<T> : Control
|
||||
{
|
||||
valueControl.ValueChanged((T)e.OldValue, (T)e.NewValue);
|
||||
|
||||
if (!valueControl._suppressChangedEvent)
|
||||
if (!valueControl.SuppressChangedEvent)
|
||||
{
|
||||
valueControl.OnValueChanged?.Invoke(valueControl, new((T)e.OldValue, (T)e.NewValue));
|
||||
}
|
||||
@@ -55,16 +65,26 @@ public partial class ValueControl<T> : Control
|
||||
OnValueChanged?.Invoke(this, new(oldValue, newValue));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of the control.
|
||||
/// </summary>
|
||||
/// <param name="value">The new value to set.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetValue(T value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the _value without notifying the change event.
|
||||
/// </summary>
|
||||
/// <param name="value">The new _value to set.</param>
|
||||
/// <remarks>This method only suppresses the change event notification, not the <see cref="ValueChanged(T, T)"/> method.
|
||||
/// Useful when you need to change the _value programmatically without triggering the change event.</remarks>
|
||||
public void SetValueWithoutNotifying(T value)
|
||||
public void SetValueWithoutNotify(T value)
|
||||
{
|
||||
_suppressChangedEvent = true;
|
||||
SetValue(ValueProperty, value);
|
||||
_suppressChangedEvent = false;
|
||||
SuppressChangedEvent = true;
|
||||
SetValue(value);
|
||||
SuppressChangedEvent = false;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,18 @@
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Ghost.Editor.Core;
|
||||
|
||||
public enum EditorState
|
||||
{
|
||||
Idle,
|
||||
Playing,
|
||||
Paused,
|
||||
Compiling,
|
||||
}
|
||||
|
||||
public static class EditorApplication
|
||||
{
|
||||
public const string ASSETS_FOLDER_NAME = "Assets";
|
||||
@@ -54,6 +63,11 @@ public static class EditorApplication
|
||||
}
|
||||
}
|
||||
|
||||
public static EditorState State
|
||||
{
|
||||
get; internal set;
|
||||
} = EditorState.Idle;
|
||||
|
||||
internal static void Initialize(IServiceProvider serviceProvider, string projectPath, string projectName)
|
||||
{
|
||||
projectPath = PathUtility.Normalize(projectPath);
|
||||
@@ -64,7 +78,7 @@ public static class EditorApplication
|
||||
s_currentProjectPath = projectPath;
|
||||
s_currentProjectName = projectName;
|
||||
|
||||
s_assetsFolderPath = Path.Combine(projectPath, ASSETS_FOLDER_NAME);
|
||||
s_assetsFolderPath = Path.Combine(projectPath, ASSETS_FOLDER_NAME);
|
||||
s_packagesFolderPath = Path.Combine(projectPath, PACKAGES_FOLDER_NAME);
|
||||
s_libraryFolderPath = Path.Combine(projectPath, LIBRARY_FOLDER_NAME);
|
||||
s_configFolderPath = Path.Combine(projectPath, CONFIG_FOLDER_NAME);
|
||||
@@ -89,12 +103,25 @@ public static class EditorApplication
|
||||
public static T GetService<T>()
|
||||
where T : class
|
||||
{
|
||||
if (s_serviceProvider?.GetService(typeof(T)) is not T service)
|
||||
if (TryGetService<T>(out var service))
|
||||
{
|
||||
throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices.");
|
||||
return service;
|
||||
}
|
||||
|
||||
return service;
|
||||
throw new ArgumentException("Requested service of type " + typeof(T).FullName + " is not registered.");
|
||||
}
|
||||
|
||||
public static bool TryGetService<T>([NotNullWhen(true)] out T? service)
|
||||
where T : class
|
||||
{
|
||||
if (s_serviceProvider?.GetService(typeof(T)) is T resolvedService)
|
||||
{
|
||||
service = resolvedService;
|
||||
return true;
|
||||
}
|
||||
|
||||
service = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
internal static void Shutdown()
|
||||
|
||||
@@ -10,13 +10,38 @@
|
||||
<SupportedOSPlatformVersion>10.0.20348.0</SupportedOSPlatformVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
||||
<NoWarn>$(NoWarn);MVVMTK0050</NoWarn>
|
||||
<Configurations>Debug;Release;Debug_Editor;Release_Editor</Configurations>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug_Editor|x64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug_Editor|ARM64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release_Editor|x64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release_Editor|ARM64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Content Remove="Assets\MeshNode.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentIcons.WinUI" Version="2.1.326" />
|
||||
<PackageReference Include="FluentIcons.WinUI" Version="2.1.328" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.28000.1839" />
|
||||
<PackageReference Include="Microsoft.WindowsAppSDK" Version="2.1.3" />
|
||||
@@ -42,8 +67,5 @@
|
||||
<Page Update="Controls\BasicInput\Vector3Field.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
<Page Update="Controls\Internal\ComponentView.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
100
src/Editor/Ghost.Editor.Core/GhostObject.cs
Normal file
100
src/Editor/Ghost.Editor.Core/GhostObject.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
|
||||
namespace Ghost.Editor.Core;
|
||||
|
||||
/// <summary>
|
||||
/// The base class for all objects that can be tracked and recorded by the Undo system.
|
||||
/// </summary>
|
||||
public abstract class GhostObject : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// A persistent unique identifier used to track this object across Undo/Redo operations,
|
||||
/// even if the underlying object is destroyed and resurrected.
|
||||
/// </summary>
|
||||
public Guid InstanceID { get; protected set; }
|
||||
|
||||
// Use WeakReference so we don't prevent Garbage Collection of dead objects
|
||||
private static readonly Dictionary<Guid, WeakReference<GhostObject>> s_objectRegistry = new();
|
||||
|
||||
public static event Action<GhostObject>? OnObjectModified;
|
||||
|
||||
protected GhostObject()
|
||||
{
|
||||
InstanceID = Guid.NewGuid();
|
||||
s_objectRegistry[InstanceID] = new WeakReference<GhostObject>(this);
|
||||
}
|
||||
|
||||
protected GhostObject(Guid instanceID)
|
||||
{
|
||||
InstanceID = instanceID;
|
||||
s_objectRegistry[InstanceID] = new WeakReference<GhostObject>(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves a GhostObject by its InstanceID in O(1) time.
|
||||
/// </summary>
|
||||
public static GhostObject? Find(Guid id)
|
||||
{
|
||||
if (s_objectRegistry.TryGetValue(id, out var weakRef))
|
||||
{
|
||||
if (weakRef.TryGetTarget(out var obj))
|
||||
{
|
||||
return obj;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Dead object, GC has collected it
|
||||
s_objectRegistry.Remove(id);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called before mutating state.
|
||||
/// Hooks into the Undo and Dirty Tracking systems.
|
||||
/// </summary>
|
||||
public virtual void Modify()
|
||||
{
|
||||
OnObjectModified?.Invoke(this);
|
||||
|
||||
// TODO: Unify RecordObject in future sessions. For now, we skip IUndoService.RecordObject here
|
||||
// since specialized methods are still required in UndoService.
|
||||
|
||||
// Mark dirty for persistence directly
|
||||
EditorApplication.GetService<IDirtyTrackerService>().MarkDirty(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the state of this object into a binary format.
|
||||
/// </summary>
|
||||
public virtual void SerializeState(BinaryWriter writer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the state of this object from a binary format.
|
||||
/// </summary>
|
||||
public virtual void DeserializeState(BinaryReader reader)
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
s_objectRegistry.Remove(InstanceID);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
~GhostObject()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Core.Attributes;
|
||||
using Ghost.Entities;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector;
|
||||
|
||||
/// <summary>
|
||||
/// Metadata for an entire ECS component type, including all its editable fields.
|
||||
/// </summary>
|
||||
public sealed class ComponentDescriptor
|
||||
{
|
||||
public Type ComponentType { get; }
|
||||
public Identifier<IComponent> ComponentId { get; }
|
||||
public string DisplayName { get; }
|
||||
public int Size { get; }
|
||||
public bool IsShared { get; }
|
||||
public PropertyDescriptor[] Properties { get; }
|
||||
|
||||
private ComponentDescriptor(Type componentType, Identifier<IComponent> componentId, string displayName, int size, bool isShared, PropertyDescriptor[] properties)
|
||||
{
|
||||
ComponentType = componentType;
|
||||
ComponentId = componentId;
|
||||
DisplayName = displayName;
|
||||
Size = size;
|
||||
IsShared = isShared;
|
||||
Properties = properties;
|
||||
}
|
||||
|
||||
public static ComponentDescriptor Create(Type componentType)
|
||||
{
|
||||
var componentId = ComponentRegistry.GetComponentID(componentType);
|
||||
var info = ComponentRegistry.GetComponentInfo(componentId);
|
||||
|
||||
var nameAttr = componentType.GetCustomAttribute<InspectorNameAttribute>();
|
||||
var displayName = nameAttr?.Name ?? componentType.Name;
|
||||
|
||||
var properties = new List<PropertyDescriptor>();
|
||||
var fields = componentType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
|
||||
foreach (var field in fields)
|
||||
{
|
||||
if (field.GetCustomAttribute<HideInInspectorAttribute>() != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: Exclude internal/private fields unless they have a specific attribute, but for now we just show public or specifically included.
|
||||
if (!field.IsPublic && field.GetCustomAttribute<InspectorNameAttribute>() == null)
|
||||
{
|
||||
// In GhostEngine we often use public fields for component data, or private fields with [InspectorName].
|
||||
// We'll just include public fields by default, and any non-public with specific attributes.
|
||||
if (field.GetCustomAttribute<ReadOnlyInInspectorAttribute>() == null &&
|
||||
field.GetCustomAttribute<InspectorGroupAttribute>() == null)
|
||||
{
|
||||
continue; // Skip normal private fields
|
||||
}
|
||||
}
|
||||
|
||||
properties.Add(new PropertyDescriptor(field, 0));
|
||||
}
|
||||
|
||||
return new ComponentDescriptor(componentType, componentId, displayName, info.size, info.isShared, properties.ToArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Entities;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector;
|
||||
|
||||
// TODO: We can use source generator to directly generate ComponentDescriptor on each component type and avoid reflection and caching altogether. This is just a quick solution for now.
|
||||
|
||||
/// <summary>
|
||||
/// Thread-safe cache of ComponentDescriptor per component type.
|
||||
/// </summary>
|
||||
public static class ComponentDescriptorRegistry
|
||||
{
|
||||
private static readonly Dictionary<nint, ComponentDescriptor> s_cache = new();
|
||||
private static readonly Lock s_lock = new();
|
||||
|
||||
public static ComponentDescriptor GetOrCreate(Type componentType)
|
||||
{
|
||||
var handle = componentType.TypeHandle.Value;
|
||||
|
||||
lock (s_lock)
|
||||
{
|
||||
if (s_cache.TryGetValue(handle, out var descriptor))
|
||||
{
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
descriptor = ComponentDescriptor.Create(componentType);
|
||||
s_cache[handle] = descriptor;
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
public static ComponentDescriptor GetOrCreate(Identifier<IComponent> componentId)
|
||||
{
|
||||
return GetOrCreate(ComponentRegistry.s_runtimeIDToType[componentId.Value]);
|
||||
}
|
||||
}
|
||||
@@ -1,40 +1,16 @@
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector;
|
||||
|
||||
public abstract class ComponentEditor
|
||||
{
|
||||
private ComponentObject _componentObject;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the underlying component object used by this class to manage its functionality.
|
||||
/// </summary>
|
||||
protected ComponentObject ComponentObject => _componentObject;
|
||||
|
||||
internal void Initialize(ComponentObject componentObject)
|
||||
{
|
||||
_componentObject = componentObject;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the component editor is created.
|
||||
/// </summary>
|
||||
/// <param name="container">The container to add the editor controls to.</param>
|
||||
public virtual void Create(StackPanel container)
|
||||
{
|
||||
}
|
||||
/// <param name="root">The root panel to which the editor should add its UI elements.</param>
|
||||
/// <param name="componentNode">The component node being edited.</param>
|
||||
public abstract void Create(Panel root, ComponentNode componentNode);
|
||||
|
||||
/// <summary>
|
||||
/// Called when the component editor needs to update its UI based on the current state of the component data.
|
||||
/// </summary>
|
||||
public virtual void Update()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the component editor is destroyed.
|
||||
/// </summary>
|
||||
public virtual void Destroy()
|
||||
{
|
||||
}
|
||||
public virtual void Destroy() { }
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector;
|
||||
|
||||
/// <summary>
|
||||
/// Registry mapping ECS component types to their custom UI editor types.
|
||||
/// </summary>
|
||||
public static class ComponentEditorRegistry
|
||||
{
|
||||
private static readonly Dictionary<Type, Type> s_editors = new();
|
||||
|
||||
static ComponentEditorRegistry()
|
||||
{
|
||||
var editorTypes = TypeCache.GetTypesWithAttribute<CustomEditorAttribute>();
|
||||
if (editorTypes == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var editorType in editorTypes)
|
||||
{
|
||||
var attr = editorType.GetCustomAttribute<CustomEditorAttribute>();
|
||||
if (attr != null && attr.TargetType != null)
|
||||
{
|
||||
if (typeof(ComponentEditor).IsAssignableFrom(editorType))
|
||||
{
|
||||
s_editors[attr.TargetType] = editorType;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a custom editor exists for the given component type.
|
||||
/// </summary>
|
||||
public static bool HasCustomEditor(Type componentType)
|
||||
{
|
||||
return s_editors.ContainsKey(componentType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates the custom editor for the given component type, or null if none exists.
|
||||
/// </summary>
|
||||
public static ComponentEditor? CreateCustomEditor(Type componentType)
|
||||
{
|
||||
if (s_editors.TryGetValue(componentType, out var editorType))
|
||||
{
|
||||
return (ComponentEditor?)Activator.CreateInstance(editorType);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
using Ghost.Entities;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector;
|
||||
|
||||
public readonly struct ComponentObject
|
||||
{
|
||||
private readonly World _world;
|
||||
private readonly Entity _entity;
|
||||
|
||||
internal ComponentObject(World world, Entity entity)
|
||||
{
|
||||
_world = world;
|
||||
_entity = entity;
|
||||
}
|
||||
|
||||
public ref T GetData<T>()
|
||||
where T : unmanaged, IComponentData
|
||||
{
|
||||
return ref _world.EntityManager.GetComponent<T>(_entity);
|
||||
}
|
||||
|
||||
public void SetData<T>(in T data)
|
||||
where T : unmanaged, IComponentData
|
||||
{
|
||||
_world.EntityManager.SetComponent(_entity, data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace Ghost.Editor.Core.Inspector;
|
||||
|
||||
/// <summary>
|
||||
/// Marks a class as a custom property drawer for a specific type.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public sealed class CustomPropertyDrawerAttribute : DiscoverableAttributeBase
|
||||
{
|
||||
public Type TargetFieldType { get; }
|
||||
|
||||
public CustomPropertyDrawerAttribute(Type targetFieldType)
|
||||
{
|
||||
TargetFieldType = targetFieldType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector.Drawers;
|
||||
|
||||
public sealed class EmptyDrawer<T> : PropertyDrawer<T> where T : unmanaged
|
||||
{
|
||||
public override FrameworkElement CreateControlT(PropertyNode<T> model)
|
||||
{
|
||||
// For a nested struct, the PropertyField will draw the Label,
|
||||
// and this empty border will be the Content (taking no space).
|
||||
// The children properties will be drawn underneath.
|
||||
return new Border();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using Ghost.Editor.Core.Controls;
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Ghost.Entities;
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector.Drawers;
|
||||
|
||||
internal class EntityDrawer : PropertyDrawer<Entity>
|
||||
{
|
||||
public override FrameworkElement CreateControlT(PropertyNode<Entity> model)
|
||||
{
|
||||
static void UpdateUI(Entity val, ReferenceField field)
|
||||
{
|
||||
if (val.IsValid)
|
||||
{
|
||||
field.HasValue = true;
|
||||
|
||||
// TODO: For now, just display the Entity ID. We could resolve its SceneGraph Node name in the future.
|
||||
field.DisplayText = $"Entity {val.ID}:{val.Generation}";
|
||||
}
|
||||
else
|
||||
{
|
||||
field.HasValue = false;
|
||||
field.DisplayText = "None (Entity)";
|
||||
}
|
||||
}
|
||||
|
||||
var field = new ReferenceField
|
||||
{
|
||||
TypeLabel = "Entity",
|
||||
IconGlyph = "\uF158",
|
||||
Margin = new Thickness(0, 2, 0, 2),
|
||||
ValidateDrop = (args) =>
|
||||
{
|
||||
// TODO: Implement drag and drop for entities from the hierarchy
|
||||
return false;
|
||||
},
|
||||
};
|
||||
|
||||
field.OnClearClicked = () =>
|
||||
{
|
||||
model.SetValueFromUI(Entity.Invalid);
|
||||
UpdateUI(Entity.Invalid, field);
|
||||
};
|
||||
|
||||
UpdateUI(model.Value, field);
|
||||
|
||||
model.OnValueChanged += (val) =>
|
||||
{
|
||||
field.DispatcherQueue.TryEnqueue(() =>
|
||||
{
|
||||
UpdateUI(val, field);
|
||||
});
|
||||
};
|
||||
|
||||
return field;
|
||||
}
|
||||
}
|
||||
38
src/Editor/Ghost.Editor.Core/Inspector/Drawers/EnumDrawer.cs
Normal file
38
src/Editor/Ghost.Editor.Core/Inspector/Drawers/EnumDrawer.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector.Drawers;
|
||||
|
||||
public sealed class EnumDrawer<T> : PropertyDrawer<T>
|
||||
where T : unmanaged, Enum
|
||||
{
|
||||
public override FrameworkElement CreateControlT(PropertyNode<T> model)
|
||||
{
|
||||
var comboBox = new ComboBox
|
||||
{
|
||||
ItemsSource = Enum.GetNames(typeof(T)),
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch,
|
||||
IsEnabled = !model.Descriptor.IsReadOnly,
|
||||
SelectedItem = model.Value.ToString()
|
||||
};
|
||||
|
||||
comboBox.SelectionChanged += (s, e) =>
|
||||
{
|
||||
if (comboBox.SelectedItem is string str)
|
||||
{
|
||||
if (Enum.TryParse<T>(str, out var parsed))
|
||||
{
|
||||
model.SetValueFromUI(parsed);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
model.OnValueChanged += (newVal) =>
|
||||
{
|
||||
comboBox.SelectedItem = newVal.ToString();
|
||||
};
|
||||
|
||||
return comboBox;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Ghost.Editor.Core.Controls;
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector.Drawers;
|
||||
|
||||
public sealed class Float3Drawer : PropertyDrawer<float3>
|
||||
{
|
||||
public override FrameworkElement CreateControlT(SceneGraph.PropertyNode<float3> node)
|
||||
{
|
||||
var field = new Float3Field
|
||||
{
|
||||
IsEnabled = !node.Descriptor.IsReadOnly,
|
||||
Value = node.Value
|
||||
};
|
||||
|
||||
field.BindTwoWay(node);
|
||||
|
||||
return field;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Editor.Core.Controls;
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector.Drawers;
|
||||
|
||||
internal class HandleDrawer<T> : PropertyDrawer<Handle<T>> where T : unmanaged
|
||||
{
|
||||
public override FrameworkElement CreateControlT(PropertyNode<Handle<T>> model)
|
||||
{
|
||||
static void UpdateUI(HandlePropertyNode<T> handleNode, ReferenceField field)
|
||||
{
|
||||
var guid = handleNode?.AssetGuid ?? Guid.Empty;
|
||||
field.HasValue = guid != Guid.Empty;
|
||||
field.DisplayText = guid != Guid.Empty ? $"{typeof(T).Name} ({guid.ToString().Substring(0, 8)})" : $"None ({typeof(T).Name})";
|
||||
}
|
||||
|
||||
var field = new ReferenceField
|
||||
{
|
||||
TypeLabel = typeof(T).Name,
|
||||
Margin = new Thickness(0, 2, 0, 2)
|
||||
};
|
||||
|
||||
var handleNode = model as HandlePropertyNode<T>;
|
||||
Logger.DebugAssert(handleNode != null);
|
||||
|
||||
field.ValidateDrop = (args) =>
|
||||
{
|
||||
// For now, assume payload has standard string Guid or we implement format
|
||||
return args.DataView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.Text);
|
||||
};
|
||||
|
||||
field.OnDropAccepted = async (args) =>
|
||||
{
|
||||
if (handleNode == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var text = await args.DataView.GetTextAsync();
|
||||
if (Guid.TryParse(text, out var guid))
|
||||
{
|
||||
handleNode.SetHandleFromAsset(guid);
|
||||
UpdateUI(handleNode, field);
|
||||
}
|
||||
};
|
||||
|
||||
field.OnClearClicked = () =>
|
||||
{
|
||||
if (handleNode != null)
|
||||
{
|
||||
handleNode.ClearHandle();
|
||||
UpdateUI(handleNode, field);
|
||||
}
|
||||
};
|
||||
|
||||
UpdateUI(handleNode, field);
|
||||
|
||||
// When ECS value changes outside of UI
|
||||
model.OnValueChanged += (val) =>
|
||||
{
|
||||
// UI Thread check usually required here, but property model events should be on UI thread or marshaled
|
||||
field.DispatcherQueue.TryEnqueue(() =>
|
||||
{
|
||||
UpdateUI(handleNode, field);
|
||||
});
|
||||
};
|
||||
|
||||
return field;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector.Drawers;
|
||||
|
||||
public sealed class NumberBoxDrawer<T> : PropertyDrawer<T>
|
||||
where T : unmanaged, INumber<T>, IMinMaxValue<T>
|
||||
{
|
||||
private readonly int _fractionDigits;
|
||||
private readonly double _min;
|
||||
private readonly double _max;
|
||||
|
||||
public NumberBoxDrawer(int fractionDigits, double min, double max)
|
||||
{
|
||||
_fractionDigits = fractionDigits;
|
||||
_min = min;
|
||||
_max = max;
|
||||
}
|
||||
|
||||
public static unsafe NumberBoxDrawer<T> CreateFloatingPoint()
|
||||
{
|
||||
var digits = sizeof(T) > 4 ? 6 : 3;
|
||||
return new NumberBoxDrawer<T>(digits, double.CreateTruncating(T.MinValue), double.CreateTruncating(T.MaxValue));
|
||||
}
|
||||
|
||||
public static NumberBoxDrawer<T> CreateInteger()
|
||||
{
|
||||
return new NumberBoxDrawer<T>(0, double.CreateTruncating(T.MinValue), double.CreateTruncating(T.MaxValue));
|
||||
}
|
||||
|
||||
public override FrameworkElement CreateControlT(SceneGraph.PropertyNode<T> model)
|
||||
{
|
||||
var box = new NumberBox
|
||||
{
|
||||
SpinButtonPlacementMode = NumberBoxSpinButtonPlacementMode.Inline,
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch,
|
||||
MaxWidth = double.PositiveInfinity, // To fill PropertyField
|
||||
Maximum = _max,
|
||||
Minimum = _min,
|
||||
Value = double.CreateTruncating(model.Value)
|
||||
};
|
||||
|
||||
var formatter = new Windows.Globalization.NumberFormatting.DecimalFormatter
|
||||
{
|
||||
FractionDigits = _fractionDigits
|
||||
};
|
||||
box.NumberFormatter = formatter;
|
||||
|
||||
box.ValueChanged += (s, e) =>
|
||||
{
|
||||
if (double.IsNaN(e.NewValue)) return;
|
||||
model.SetValueFromUI(T.CreateTruncating(e.NewValue));
|
||||
};
|
||||
|
||||
model.OnValueChanged += (newVal) =>
|
||||
{
|
||||
box.Value = double.CreateTruncating(newVal);
|
||||
};
|
||||
|
||||
return box;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector.Drawers;
|
||||
|
||||
public sealed class ReadOnlyDrawer<T> : PropertyDrawer<T> where T : unmanaged
|
||||
{
|
||||
public override FrameworkElement CreateControlT(PropertyNode<T> model)
|
||||
{
|
||||
var box = new TextBox
|
||||
{
|
||||
Text = model.Value.ToString(),
|
||||
IsReadOnly = true,
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch,
|
||||
Foreground = (Microsoft.UI.Xaml.Media.Brush)Application.Current.Resources["TextFillColorSecondaryBrush"]
|
||||
};
|
||||
|
||||
model.OnValueChanged += (newVal) =>
|
||||
{
|
||||
box.Text = newVal.ToString();
|
||||
};
|
||||
|
||||
return box;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector.Drawers;
|
||||
|
||||
public sealed class ToggleSwitchDrawer : PropertyDrawer<bool>
|
||||
{
|
||||
public override FrameworkElement CreateControlT(SceneGraph.PropertyNode<bool> model)
|
||||
{
|
||||
var toggle = new ToggleSwitch
|
||||
{
|
||||
OnContent = "",
|
||||
OffContent = "",
|
||||
IsOn = model.Value
|
||||
};
|
||||
|
||||
toggle.Toggled += (s, e) =>
|
||||
{
|
||||
model.SetValueFromUI(toggle.IsOn);
|
||||
};
|
||||
|
||||
model.OnValueChanged += (newVal) =>
|
||||
{
|
||||
toggle.IsOn = newVal;
|
||||
};
|
||||
|
||||
return toggle;
|
||||
}
|
||||
}
|
||||
191
src/Editor/Ghost.Editor.Core/Inspector/EntityInspectorModel.cs
Normal file
191
src/Editor/Ghost.Editor.Core/Inspector/EntityInspectorModel.cs
Normal file
@@ -0,0 +1,191 @@
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Ghost.Entities;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector;
|
||||
|
||||
/// <summary>
|
||||
/// Model for an entire entity being inspected.
|
||||
/// Discovers components from archetype, builds ComponentModels.
|
||||
/// </summary>
|
||||
public sealed class EntityInspectorModel : ISyncableInspectorModel
|
||||
{
|
||||
private readonly World _world;
|
||||
private readonly Entity _entity;
|
||||
private EntityNode? _entityNode;
|
||||
private readonly List<ComponentNode> _components = new();
|
||||
private readonly List<ComponentEditor> _activeCustomEditors = new();
|
||||
private int _lastArchetypeId = -1;
|
||||
|
||||
public World World => _world;
|
||||
public Entity Entity => _entity;
|
||||
public IReadOnlyList<ComponentNode> Components => _components;
|
||||
|
||||
public EntityInspectorModel(World world, Entity entity)
|
||||
{
|
||||
_world = world;
|
||||
_entity = entity;
|
||||
}
|
||||
|
||||
private void RebuildComponentList()
|
||||
{
|
||||
_components.Clear();
|
||||
|
||||
if (!_world.EntityManager.Exists(_entity))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_entityNode == null)
|
||||
{
|
||||
var syncService = EditorApplication.GetService<Services.SceneGraphSyncService>();
|
||||
if (syncService != null && syncService.TryGetNode(_entity, out var node))
|
||||
{
|
||||
_entityNode = node;
|
||||
}
|
||||
}
|
||||
|
||||
if (_entityNode != null)
|
||||
{
|
||||
// Update components list in EntityNode first
|
||||
_entityNode.BuildComponents();
|
||||
|
||||
foreach (var compNode in _entityNode.Components)
|
||||
{
|
||||
_components.Add(compNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when entity archetype may have changed.
|
||||
/// Returns true if structure was rebuilt (components added/removed).
|
||||
/// </summary>
|
||||
public bool RefreshStructure()
|
||||
{
|
||||
var locationResult = _world.EntityManager.GetEntityLocation(_entity);
|
||||
if (locationResult.IsFailure)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var location = locationResult.Value;
|
||||
if (location.archetypeID == _lastArchetypeId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_lastArchetypeId = location.archetypeID;
|
||||
RebuildComponentList();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void BuildPropertyUI(PropertyNode propNode, Panel container)
|
||||
{
|
||||
var drawer = PropertyDrawerRegistry.GetDrawer(propNode.Descriptor.ValueType);
|
||||
var control = drawer.CreateControl(propNode);
|
||||
|
||||
var propertyField = new Controls.PropertyField
|
||||
{
|
||||
Label = propNode.Descriptor.DisplayName,
|
||||
Content = control,
|
||||
IsEditable = !propNode.Descriptor.IsReadOnly
|
||||
};
|
||||
|
||||
container.Children.Add(propertyField);
|
||||
|
||||
if (propNode.Children != null && propNode.Children.Length > 0)
|
||||
{
|
||||
var childrenPanel = new StackPanel { Spacing = 4, Margin = new Thickness(12, 4, 0, 0) };
|
||||
foreach (var child in propNode.Children)
|
||||
{
|
||||
BuildPropertyUI(child, childrenPanel);
|
||||
}
|
||||
container.Children.Add(childrenPanel);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read all component values from ECS -> model.
|
||||
/// </summary>
|
||||
public void SyncFromECS()
|
||||
{
|
||||
if (!_world.EntityManager.Exists(_entity))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var comp in _components)
|
||||
{
|
||||
foreach (var prop in comp.Properties)
|
||||
{
|
||||
prop.Sync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Sync()
|
||||
{
|
||||
if (!_world.EntityManager.Exists(_entity))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RefreshStructure();
|
||||
SyncFromECS();
|
||||
}
|
||||
|
||||
// TODO: Deselect is not supported yet.
|
||||
|
||||
public UIElement BuildUI()
|
||||
{
|
||||
RefreshStructure();
|
||||
|
||||
var container = new StackPanel { Spacing = 4 };
|
||||
|
||||
foreach (var compNode in _components)
|
||||
{
|
||||
// TODO: Use a more compact UI for components
|
||||
var expander = new Expander
|
||||
{
|
||||
Header = compNode.Descriptor.DisplayName,
|
||||
HorizontalAlignment = HorizontalAlignment.Stretch,
|
||||
HorizontalContentAlignment = HorizontalAlignment.Stretch,
|
||||
IsExpanded = true,
|
||||
Margin = new Thickness(4, 2, 4, 2)
|
||||
};
|
||||
|
||||
var propertiesPanel = new StackPanel { Spacing = 8 };
|
||||
|
||||
if (ComponentEditorRegistry.HasCustomEditor(compNode.ComponentType))
|
||||
{
|
||||
var editor = ComponentEditorRegistry.CreateCustomEditor(compNode.ComponentType);
|
||||
if (editor != null)
|
||||
{
|
||||
editor.Create(propertiesPanel, compNode);
|
||||
_activeCustomEditors.Add(editor);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var propNode in compNode.Properties)
|
||||
{
|
||||
BuildPropertyUI(propNode, propertiesPanel);
|
||||
}
|
||||
}
|
||||
|
||||
expander.Content = propertiesPanel;
|
||||
container.Children.Add(expander);
|
||||
}
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_components.Clear();
|
||||
_activeCustomEditors.Clear();
|
||||
}
|
||||
}
|
||||
120
src/Editor/Ghost.Editor.Core/Inspector/PropertyDescriptor.cs
Normal file
120
src/Editor/Ghost.Editor.Core/Inspector/PropertyDescriptor.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using Ghost.Core.Attributes;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector;
|
||||
|
||||
/// <summary>
|
||||
/// Describes a single editable field within an ECS component.
|
||||
/// Knows how to read/write a specific field directly from/to unmanaged memory.
|
||||
/// </summary>
|
||||
public sealed class PropertyDescriptor
|
||||
{
|
||||
public string Name { get; }
|
||||
public string DisplayName { get; }
|
||||
public Type ValueType { get; }
|
||||
public int OffsetInComponent { get; }
|
||||
public bool IsReadOnly { get; }
|
||||
|
||||
// For nested structs (e.g. float4x4 -> float4 -> float)
|
||||
public PropertyDescriptor[]? Children { get; }
|
||||
|
||||
// TODO: Use source generators to build these at compile time and avoid all reflection/attributes at runtime.
|
||||
internal PropertyDescriptor(FieldInfo fieldInfo, int parentOffset)
|
||||
{
|
||||
Name = fieldInfo.Name;
|
||||
ValueType = fieldInfo.FieldType;
|
||||
OffsetInComponent = parentOffset + (int)Marshal.OffsetOf(fieldInfo.DeclaringType!, fieldInfo.Name);
|
||||
|
||||
IsReadOnly = fieldInfo.GetCustomAttribute<ReadOnlyInInspectorAttribute>() != null;
|
||||
|
||||
var nameAttr = fieldInfo.GetCustomAttribute<InspectorNameAttribute>();
|
||||
DisplayName = nameAttr?.Name ?? FormatName(Name);
|
||||
|
||||
// Handle nested structs if this is an unmanaged struct that is not a primitive or common vector type we have custom drawers for.
|
||||
if (ValueType.IsValueType && !ValueType.IsPrimitive && !ValueType.IsEnum)
|
||||
{
|
||||
if (!PropertyDrawerRegistry.HasCustomDrawer(ValueType))
|
||||
{
|
||||
var children = new List<PropertyDescriptor>();
|
||||
var fields = ValueType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
foreach (var nestedField in fields)
|
||||
{
|
||||
if (!nestedField.IsPublic &&
|
||||
nestedField.GetCustomAttribute<InspectorGroupAttribute>() == null &&
|
||||
nestedField.GetCustomAttribute<ReadOnlyInInspectorAttribute>() == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
children.Add(new PropertyDescriptor(nestedField, OffsetInComponent));
|
||||
}
|
||||
if (children.Count > 0)
|
||||
{
|
||||
Children = children.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal PropertyDescriptor(string name, Type type, int offset, bool isReadOnly, PropertyDescriptor[]? children = null)
|
||||
{
|
||||
Name = name;
|
||||
DisplayName = FormatName(name);
|
||||
ValueType = type;
|
||||
OffsetInComponent = offset;
|
||||
IsReadOnly = isReadOnly;
|
||||
Children = children;
|
||||
}
|
||||
|
||||
private static string FormatName(string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
if (name.StartsWith('_'))
|
||||
{
|
||||
name = name.Substring(1);
|
||||
}
|
||||
|
||||
if (name.Length == 0)
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
return char.ToUpperInvariant(name[0]) + name.Substring(1);
|
||||
}
|
||||
|
||||
public unsafe object ReadBoxed(void* pComponent)
|
||||
{
|
||||
var src = (byte*)pComponent + OffsetInComponent;
|
||||
return Marshal.PtrToStructure((nint)src, ValueType)!;
|
||||
}
|
||||
|
||||
public unsafe void WriteBoxed(void* pComponent, object value)
|
||||
{
|
||||
if (IsReadOnly)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var dst = (byte*)pComponent + OffsetInComponent;
|
||||
Marshal.StructureToPtr(value, (nint)dst, false);
|
||||
}
|
||||
|
||||
public unsafe ref T Read<T>(void* pComponent) where T : unmanaged
|
||||
{
|
||||
return ref *(T*)((byte*)pComponent + OffsetInComponent);
|
||||
}
|
||||
|
||||
public unsafe void Write<T>(void* pComponent, in T value) where T : unmanaged
|
||||
{
|
||||
if (IsReadOnly)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
*(T*)((byte*)pComponent + OffsetInComponent) = value;
|
||||
}
|
||||
}
|
||||
25
src/Editor/Ghost.Editor.Core/Inspector/PropertyDrawer.cs
Normal file
25
src/Editor/Ghost.Editor.Core/Inspector/PropertyDrawer.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for type-specific property UI factories.
|
||||
/// </summary>
|
||||
public abstract class PropertyDrawer
|
||||
{
|
||||
/// <summary>
|
||||
/// Create the UI control bound to the given property node.
|
||||
/// </summary>
|
||||
public abstract FrameworkElement CreateControl(PropertyNode model);
|
||||
}
|
||||
|
||||
public abstract class PropertyDrawer<T> : PropertyDrawer where T : unmanaged
|
||||
{
|
||||
public sealed override FrameworkElement CreateControl(PropertyNode model)
|
||||
{
|
||||
return CreateControlT((PropertyNode<T>)model);
|
||||
}
|
||||
|
||||
public abstract FrameworkElement CreateControlT(PropertyNode<T> model);
|
||||
}
|
||||
122
src/Editor/Ghost.Editor.Core/Inspector/PropertyDrawerRegistry.cs
Normal file
122
src/Editor/Ghost.Editor.Core/Inspector/PropertyDrawerRegistry.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Editor.Core.Inspector.Drawers;
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Ghost.Entities;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Ghost.Editor.Core.Inspector;
|
||||
|
||||
/// <summary>
|
||||
/// Discovers PropertyDrawer subclasses and maps field types to drawers.
|
||||
/// </summary>
|
||||
public static class PropertyDrawerRegistry
|
||||
{
|
||||
private static readonly Dictionary<Type, PropertyDrawer> s_drawers = new();
|
||||
private static bool s_initialized;
|
||||
private static readonly Lock s_lock = new();
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
lock (s_lock)
|
||||
{
|
||||
if (s_initialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Register built-in drawers
|
||||
s_drawers[typeof(float)] = NumberBoxDrawer<float>.CreateFloatingPoint();
|
||||
s_drawers[typeof(double)] = NumberBoxDrawer<double>.CreateFloatingPoint();
|
||||
s_drawers[typeof(int)] = NumberBoxDrawer<int>.CreateInteger();
|
||||
s_drawers[typeof(uint)] = NumberBoxDrawer<uint>.CreateInteger();
|
||||
s_drawers[typeof(short)] = NumberBoxDrawer<short>.CreateInteger();
|
||||
s_drawers[typeof(ushort)] = NumberBoxDrawer<ushort>.CreateInteger();
|
||||
s_drawers[typeof(long)] = NumberBoxDrawer<long>.CreateInteger();
|
||||
s_drawers[typeof(ulong)] = NumberBoxDrawer<ulong>.CreateInteger();
|
||||
s_drawers[typeof(sbyte)] = NumberBoxDrawer<sbyte>.CreateInteger();
|
||||
s_drawers[typeof(byte)] = NumberBoxDrawer<byte>.CreateInteger();
|
||||
s_drawers[typeof(bool)] = new ToggleSwitchDrawer();
|
||||
|
||||
s_drawers[typeof(float3)] = new Float3Drawer();
|
||||
|
||||
s_drawers[typeof(Entity)] = new EntityDrawer();
|
||||
|
||||
// Discover user-defined drawers via TypeCache
|
||||
var customDrawers = TypeCache.GetTypesWithAttribute<CustomPropertyDrawerAttribute>();
|
||||
if (customDrawers != null)
|
||||
{
|
||||
foreach (var typeInfo in customDrawers)
|
||||
{
|
||||
var type = typeInfo.AsType();
|
||||
var attr = type.GetCustomAttribute<CustomPropertyDrawerAttribute>();
|
||||
if (attr != null && typeof(PropertyDrawer).IsAssignableFrom(type))
|
||||
{
|
||||
if (Activator.CreateInstance(typeInfo) is PropertyDrawer drawer)
|
||||
{
|
||||
s_drawers[attr.TargetFieldType] = drawer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s_initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool HasCustomDrawer(Type fieldType)
|
||||
{
|
||||
if (!s_initialized)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
return s_drawers.ContainsKey(fieldType);
|
||||
}
|
||||
|
||||
public static PropertyDrawer GetDrawer(Type fieldType)
|
||||
{
|
||||
if (!s_initialized)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
if (s_drawers.TryGetValue(fieldType, out var drawer))
|
||||
{
|
||||
return drawer;
|
||||
}
|
||||
|
||||
if (fieldType.IsEnum)
|
||||
{
|
||||
var enumDrawerType = typeof(EnumDrawer<>).MakeGenericType(fieldType);
|
||||
var enumDrawer = (PropertyDrawer)Activator.CreateInstance(enumDrawerType)!;
|
||||
s_drawers[fieldType] = enumDrawer;
|
||||
return enumDrawer;
|
||||
}
|
||||
|
||||
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Handle<>))
|
||||
{
|
||||
var argType = fieldType.GetGenericArguments()[0];
|
||||
var handleDrawerType = typeof(HandleDrawer<>).MakeGenericType(argType);
|
||||
var handleDrawer = (PropertyDrawer)Activator.CreateInstance(handleDrawerType)!;
|
||||
s_drawers[fieldType] = handleDrawer;
|
||||
return handleDrawer;
|
||||
}
|
||||
|
||||
// Fallback for unknown types. If it's an unmanaged struct with fields, we use EmptyDrawer
|
||||
// to let the children render. If it's a primitive or something else, use ReadOnlyDrawer.
|
||||
Type genericDrawerType;
|
||||
if (fieldType.IsValueType && !fieldType.IsPrimitive && !fieldType.IsEnum)
|
||||
{
|
||||
genericDrawerType = typeof(EmptyDrawer<>);
|
||||
}
|
||||
else
|
||||
{
|
||||
genericDrawerType = typeof(ReadOnlyDrawer<>);
|
||||
}
|
||||
|
||||
var drawerType = genericDrawerType.MakeGenericType(fieldType);
|
||||
var drawerInstance = (PropertyDrawer)Activator.CreateInstance(drawerType)!;
|
||||
s_drawers[fieldType] = drawerInstance;
|
||||
return drawerInstance;
|
||||
}
|
||||
}
|
||||
227
src/Editor/Ghost.Editor.Core/SceneGraph/ComponentNode.cs
Normal file
227
src/Editor/Ghost.Editor.Core/SceneGraph/ComponentNode.cs
Normal file
@@ -0,0 +1,227 @@
|
||||
using Ghost.Editor.Core.Inspector;
|
||||
using Ghost.Editor.Core.Services;
|
||||
using Ghost.Entities;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Ghost.Editor.Core.SceneGraph;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a single component on an entity within the Editor's scene graph.
|
||||
/// Acts as the middleware between the Inspector's PropertyModels and the actual ECS memory.
|
||||
/// </summary>
|
||||
public unsafe class ComponentNode
|
||||
{
|
||||
private readonly IUndoService _undoService;
|
||||
private readonly IEditorWorldService _worldService;
|
||||
|
||||
private readonly Dictionary<string, int> _propertyIndices;
|
||||
protected readonly World _world;
|
||||
|
||||
public EntityNode EntityNode { get; }
|
||||
|
||||
public Type ComponentType { get; }
|
||||
public ComponentDescriptor Descriptor { get; }
|
||||
public PropertyNode[] Properties { get; }
|
||||
public string Name => Descriptor.DisplayName;
|
||||
|
||||
internal ComponentNode(World world, EntityNode entityNode, Type componentType, ComponentDescriptor descriptor)
|
||||
{
|
||||
_undoService = EditorApplication.GetService<IUndoService>();
|
||||
_worldService = EditorApplication.GetService<IEditorWorldService>();
|
||||
|
||||
_propertyIndices = new Dictionary<string, int>(descriptor.Properties.Length);
|
||||
_world = world;
|
||||
|
||||
EntityNode = entityNode;
|
||||
|
||||
ComponentType = componentType;
|
||||
Descriptor = descriptor;
|
||||
|
||||
Properties = new PropertyNode[descriptor.Properties.Length];
|
||||
for (var i = 0; i < descriptor.Properties.Length; i++)
|
||||
{
|
||||
_propertyIndices[descriptor.Properties[i].Name] = i;
|
||||
|
||||
// TODO: We should use a registry/factory for different PropertyNode types instead of hardcoding HandlePropertyNode here. This is just a quick solution for handles for now.
|
||||
var prop = descriptor.Properties[i];
|
||||
if (prop.ValueType.IsGenericType && prop.ValueType.GetGenericTypeDefinition() == typeof(Ghost.Core.Handle<>))
|
||||
{
|
||||
var nodeType = typeof(HandlePropertyNode<>).MakeGenericType(prop.ValueType.GetGenericArguments()[0]);
|
||||
Properties[i] = (PropertyNode)Activator.CreateInstance(nodeType, prop, this)!;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create a standard PropertyNode<T> for non-handle types
|
||||
// We use MakeGenericType to create the correct PropertyNode<T> based on FieldType
|
||||
var nodeType = typeof(PropertyNode<>).MakeGenericType(prop.ValueType);
|
||||
Properties[i] = (PropertyNode)Activator.CreateInstance(nodeType, prop, this, null)!;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetPropertyValue<T>(PropertyDescriptor property, T value)
|
||||
where T : unmanaged
|
||||
{
|
||||
if (property.ValueType != typeof(T))
|
||||
{
|
||||
throw new ArgumentException("Property type does not match value type");
|
||||
}
|
||||
|
||||
_undoService.RecordEntityComponent(this, $"Edit property {property.DisplayName} on {Descriptor.DisplayName}");
|
||||
_worldService.Defer(() =>
|
||||
{
|
||||
if (Descriptor.IsShared)
|
||||
{
|
||||
var ptr = _world.EntityManager.GetSharedComponent(EntityNode.Entity, Descriptor.ComponentId);
|
||||
if (ptr != null)
|
||||
{
|
||||
using var scope = AllocationManager.CreateStackScope();
|
||||
using var buffer = new MemoryBlock((nuint)Descriptor.Size, 16, scope.AllocationHandle);
|
||||
System.Runtime.CompilerServices.Unsafe.CopyBlock(buffer.GetUnsafePtr(), ptr, (uint)Descriptor.Size);
|
||||
property.Write(buffer.GetUnsafePtr(), value);
|
||||
_world.EntityManager.SetSharedComponent(EntityNode.Entity, Descriptor.ComponentId, buffer.GetUnsafePtr());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var pComponent = GetComponentPointer();
|
||||
property.Write(pComponent, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void SetComponent<T>(T value)
|
||||
where T : unmanaged
|
||||
{
|
||||
if (typeof(T) != ComponentType)
|
||||
{
|
||||
throw new ArgumentException("Value type does not match component type");
|
||||
}
|
||||
|
||||
_undoService.RecordEntityComponent(this, $"Edit component {Descriptor.DisplayName}");
|
||||
_worldService.Defer(() =>
|
||||
{
|
||||
if (Descriptor.IsShared)
|
||||
{
|
||||
using var scope = AllocationManager.CreateStackScope();
|
||||
using var buffer = new MemoryBlock((nuint)Descriptor.Size, 16, scope.AllocationHandle);
|
||||
buffer.GetElementAt<T>(0) = value;
|
||||
_world.EntityManager.SetSharedComponent(EntityNode.Entity, Descriptor.ComponentId, buffer.GetUnsafePtr());
|
||||
}
|
||||
else
|
||||
{
|
||||
var pComponent = GetComponentPointer();
|
||||
*(T*)pComponent = value;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public PropertyNode GetProperty(string propertyName)
|
||||
{
|
||||
if (_propertyIndices.TryGetValue(propertyName, out var index))
|
||||
{
|
||||
return Properties[index];
|
||||
}
|
||||
|
||||
throw new ArgumentException($"Property '{propertyName}' not found in component '{Name}'");
|
||||
}
|
||||
|
||||
public PropertyNode<T> GetProperty<T>(string propertyName)
|
||||
where T : unmanaged
|
||||
{
|
||||
var prop = GetProperty(propertyName);
|
||||
if (prop is PropertyNode<T> typedProp)
|
||||
{
|
||||
return typedProp;
|
||||
}
|
||||
|
||||
throw new ArgumentException($"Property '{propertyName}' is not of type {typeof(T).Name}");
|
||||
}
|
||||
|
||||
public void* GetComponentPointer()
|
||||
{
|
||||
if (Descriptor.IsShared)
|
||||
{
|
||||
return _world.EntityManager.GetSharedComponent(EntityNode.Entity, Descriptor.ComponentId);
|
||||
}
|
||||
else
|
||||
{
|
||||
return _world.EntityManager.GetComponent(EntityNode.Entity, Descriptor.ComponentId);
|
||||
}
|
||||
}
|
||||
|
||||
public T GetComponent<T>()
|
||||
where T : unmanaged
|
||||
{
|
||||
if (typeof(T) != ComponentType)
|
||||
{
|
||||
throw new ArgumentException("Field type does not match component type");
|
||||
}
|
||||
|
||||
var pComponent = GetComponentPointer();
|
||||
return *(T*)pComponent;
|
||||
}
|
||||
|
||||
public T GetPropertyValue<T>(PropertyDescriptor field)
|
||||
where T : unmanaged
|
||||
{
|
||||
var pComponent = GetComponentPointer();
|
||||
return field.Read<T>(pComponent);
|
||||
}
|
||||
|
||||
/// <summary>Serialize this component to JSON. Base reads from ECS directly.</summary>
|
||||
public virtual void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, Action<object>? preSerialize = null)
|
||||
{
|
||||
var boxed = System.Runtime.InteropServices.Marshal.PtrToStructure((nint)GetComponentPointer(), ComponentType);
|
||||
if (boxed != null)
|
||||
{
|
||||
preSerialize?.Invoke(boxed);
|
||||
|
||||
var jsonString = JsonSerializer.Serialize(boxed, ComponentType, options);
|
||||
using var doc = JsonDocument.Parse(jsonString);
|
||||
var root = System.Text.Json.Nodes.JsonObject.Create(doc.RootElement);
|
||||
if (root != null)
|
||||
{
|
||||
foreach (var prop in Properties)
|
||||
{
|
||||
prop.SerializeOverride(root, boxed);
|
||||
}
|
||||
root.WriteTo(writer, options);
|
||||
return;
|
||||
}
|
||||
|
||||
JsonSerializer.Serialize(writer, boxed, ComponentType, options);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Deserialize from JSON and apply to ECS. Base writes to ECS directly.</summary>
|
||||
public virtual void Deserialize(JsonElement element, JsonSerializerOptions options, Action<object>? postDeserialize = null)
|
||||
{
|
||||
var boxed = element.Deserialize(ComponentType, options);
|
||||
if (boxed != null)
|
||||
{
|
||||
postDeserialize?.Invoke(boxed);
|
||||
|
||||
foreach (var prop in Properties)
|
||||
{
|
||||
prop.DeserializeOverride(element, boxed);
|
||||
}
|
||||
|
||||
_worldService.Defer(() =>
|
||||
{
|
||||
if (Descriptor.IsShared)
|
||||
{
|
||||
using var scope = AllocationManager.CreateStackScope();
|
||||
using var buffer = new MemoryBlock((nuint)Descriptor.Size, 16, scope.AllocationHandle);
|
||||
System.Runtime.InteropServices.Marshal.StructureToPtr(boxed, (nint)buffer.GetUnsafePtr(), false);
|
||||
_world.EntityManager.SetSharedComponent(EntityNode.Entity, Descriptor.ComponentId, buffer.GetUnsafePtr());
|
||||
}
|
||||
else
|
||||
{
|
||||
System.Runtime.InteropServices.Marshal.StructureToPtr(boxed, (nint)GetComponentPointer(), false);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Entities;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
@@ -10,11 +11,46 @@ public sealed partial class EntityNode : SceneGraphNode
|
||||
{
|
||||
get;
|
||||
}
|
||||
public List<ComponentNode> Components { get; } = new();
|
||||
|
||||
public EntityNode(World world, Entity entity, string name)
|
||||
public SceneNode? SceneNode { get; }
|
||||
|
||||
internal EntityNode(World world, Entity entity, string name, SceneNode? sceneNode)
|
||||
: base(world, name)
|
||||
{
|
||||
Entity = entity;
|
||||
SceneNode = sceneNode;
|
||||
}
|
||||
|
||||
public override SceneNode? GetOwningSceneNode() => SceneNode;
|
||||
|
||||
public void BuildComponents()
|
||||
{
|
||||
Components.Clear();
|
||||
var locationResult = World.EntityManager.GetEntityLocation(Entity);
|
||||
if (!locationResult.IsSuccess)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var location = locationResult.Value;
|
||||
ref var archetype = ref World.ComponentManager.GetArchetypeReference(location.archetypeID);
|
||||
|
||||
var it = archetype._signature.GetIterator();
|
||||
while (it.Next(out var componentID))
|
||||
{
|
||||
if (ComponentRegistry.s_runtimeIDToType.TryGetValue(componentID, out var type))
|
||||
{
|
||||
var compInfo = ComponentRegistry.GetComponentInfo(new Ghost.Core.Identifier<IComponent>(componentID));
|
||||
if (compInfo.isCleanup)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var compDescriptor = Inspector.ComponentDescriptor.Create(type);
|
||||
Components.Add(new ComponentNode(World, this, type, compDescriptor));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override IconSource? CreateIcon()
|
||||
@@ -27,11 +63,54 @@ public sealed partial class EntityNode : SceneGraphNode
|
||||
|
||||
public override UIElement? CreateHeader()
|
||||
{
|
||||
return null;
|
||||
var root = new Grid
|
||||
{
|
||||
ColumnSpacing = 8,
|
||||
};
|
||||
|
||||
root.ColumnDefinitions.Add(new ColumnDefinition
|
||||
{
|
||||
Width = new GridLength(1, GridUnitType.Star)
|
||||
});
|
||||
root.ColumnDefinitions.Add(new ColumnDefinition
|
||||
{
|
||||
Width = GridLength.Auto,
|
||||
MinWidth = 20
|
||||
});
|
||||
|
||||
var nameBox = new TextBox
|
||||
{
|
||||
Text = Name,
|
||||
FontSize = 14,
|
||||
FontWeight = Microsoft.UI.Text.FontWeights.SemiBold,
|
||||
};
|
||||
|
||||
nameBox.SetBinding(TextBox.TextProperty, new Microsoft.UI.Xaml.Data.Binding
|
||||
{
|
||||
Source = this,
|
||||
Path = new PropertyPath(nameof(Name)),
|
||||
Mode = Microsoft.UI.Xaml.Data.BindingMode.TwoWay,
|
||||
UpdateSourceTrigger = Microsoft.UI.Xaml.Data.UpdateSourceTrigger.PropertyChanged
|
||||
});
|
||||
|
||||
var entityBlock = new TextBlock
|
||||
{
|
||||
Text = $"{Entity.ID}:{Entity.Generation}",
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
HorizontalAlignment = HorizontalAlignment.Right,
|
||||
};
|
||||
|
||||
Grid.SetColumn(nameBox, 0);
|
||||
Grid.SetColumn(entityBlock, 1);
|
||||
|
||||
root.Children.Add(nameBox);
|
||||
root.Children.Add(entityBlock);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
public override UIElement? CreateInspector()
|
||||
public override IInspectorModel CreateInspectorModel()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return new Inspector.EntityInspectorModel(World, Entity);
|
||||
}
|
||||
}
|
||||
|
||||
134
src/Editor/Ghost.Editor.Core/SceneGraph/HandlePropertyNode.cs
Normal file
134
src/Editor/Ghost.Editor.Core/SceneGraph/HandlePropertyNode.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Editor.Core.Inspector;
|
||||
using Ghost.Engine.Streaming;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace Ghost.Editor.Core.SceneGraph;
|
||||
|
||||
public class HandlePropertyNode<T> : PropertyNode<Handle<T>> where T : unmanaged
|
||||
{
|
||||
public Guid AssetGuid { get; private set; } = Guid.Empty;
|
||||
public long ExpectedHandleValue { get; private set; }
|
||||
|
||||
public HandlePropertyNode(PropertyDescriptor descriptor, ComponentNode parent)
|
||||
: base(descriptor, parent)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetHandleFromAsset(Guid assetGuid)
|
||||
{
|
||||
var assetManager = EditorApplication.GetService<AssetManager>();
|
||||
|
||||
MethodInfo? resolveMethod = null;
|
||||
if (typeof(T).Name == "GPUTexture")
|
||||
resolveMethod = typeof(AssetManager).GetMethod("ResolveTexture", BindingFlags.Public | BindingFlags.Instance);
|
||||
else if (typeof(T).Name == "Mesh")
|
||||
resolveMethod = typeof(AssetManager).GetMethod("ResolveMesh", BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
Handle<T> handle = default;
|
||||
if (resolveMethod != null && assetManager != null)
|
||||
{
|
||||
var res = resolveMethod.Invoke(assetManager, new object[] { assetGuid });
|
||||
if (res != null)
|
||||
{
|
||||
handle = (Handle<T>)res;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Error($"No resolve method found for type {typeof(T).Name}");
|
||||
}
|
||||
|
||||
AssetGuid = assetGuid;
|
||||
ExpectedHandleValue = UnsafeGetHandleValue(handle);
|
||||
SetValueFromUI(handle);
|
||||
}
|
||||
|
||||
public void ClearHandle()
|
||||
{
|
||||
AssetGuid = Guid.Empty;
|
||||
ExpectedHandleValue = 0;
|
||||
SetValueFromUI(default);
|
||||
}
|
||||
|
||||
private static long UnsafeGetHandleValue(Handle<T> handle)
|
||||
{
|
||||
return System.Runtime.CompilerServices.Unsafe.As<Handle<T>, long>(ref handle);
|
||||
}
|
||||
|
||||
public override void SerializeOverride(JsonObject jsonRoot, object boxedComponent)
|
||||
{
|
||||
if (AssetGuid != Guid.Empty)
|
||||
{
|
||||
var camelCaseName = char.ToLowerInvariant(Descriptor.Name[0]) + Descriptor.Name.Substring(1);
|
||||
if (jsonRoot.ContainsKey(camelCaseName))
|
||||
jsonRoot[camelCaseName] = AssetGuid.ToString();
|
||||
else
|
||||
jsonRoot[Descriptor.Name] = AssetGuid.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public override void DeserializeOverride(JsonElement jsonRoot, object boxedComponent)
|
||||
{
|
||||
var camelCaseName = char.ToLowerInvariant(Descriptor.Name[0]) + Descriptor.Name.Substring(1);
|
||||
|
||||
if (jsonRoot.TryGetProperty(camelCaseName, out var propElement) || jsonRoot.TryGetProperty(Descriptor.Name, out propElement))
|
||||
{
|
||||
if (propElement.ValueKind == JsonValueKind.String && Guid.TryParse(propElement.GetString(), out var guid) && guid != Guid.Empty)
|
||||
{
|
||||
var assetManager = EditorApplication.GetService<AssetManager>();
|
||||
|
||||
MethodInfo? resolveMethod = null;
|
||||
if (typeof(T).Name == "GPUTexture")
|
||||
resolveMethod = typeof(AssetManager).GetMethod("ResolveTexture", BindingFlags.Public | BindingFlags.Instance);
|
||||
else if (typeof(T).Name == "Mesh")
|
||||
resolveMethod = typeof(AssetManager).GetMethod("ResolveMesh", BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
if (resolveMethod != null && assetManager != null)
|
||||
{
|
||||
var handleObj = resolveMethod.Invoke(assetManager, new object[] { guid });
|
||||
if (handleObj != null)
|
||||
{
|
||||
var fieldInfo = boxedComponent.GetType().GetField(Descriptor.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
if (fieldInfo != null)
|
||||
{
|
||||
fieldInfo.SetValue(boxedComponent, handleObj);
|
||||
}
|
||||
|
||||
var handle = (Handle<T>)handleObj;
|
||||
var handleValue = System.Runtime.CompilerServices.Unsafe.As<Handle<T>, long>(ref handle);
|
||||
|
||||
AssetGuid = guid;
|
||||
ExpectedHandleValue = handleValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Validate(object boxedComponent)
|
||||
{
|
||||
if (AssetGuid != Guid.Empty)
|
||||
{
|
||||
var fieldInfo = boxedComponent.GetType().GetField(Descriptor.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
if (fieldInfo != null)
|
||||
{
|
||||
var val = fieldInfo.GetValue(boxedComponent);
|
||||
if (val != null)
|
||||
{
|
||||
var handle = (Handle<T>)val;
|
||||
var currentVal = System.Runtime.CompilerServices.Unsafe.As<Handle<T>, long>(ref handle);
|
||||
|
||||
if (currentVal != ExpectedHandleValue)
|
||||
{
|
||||
Logger.Error($"Handle field '{Descriptor.Name}' was modified externally. Guid tracking cleared.");
|
||||
AssetGuid = Guid.Empty;
|
||||
ExpectedHandleValue = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
86
src/Editor/Ghost.Editor.Core/SceneGraph/PropertyNode.cs
Normal file
86
src/Editor/Ghost.Editor.Core/SceneGraph/PropertyNode.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using Ghost.Editor.Core.Inspector;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace Ghost.Editor.Core.SceneGraph;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a single property/field within a ComponentNode.
|
||||
/// Handles ECS reading/writing as well as serialization overrides (like Guid metadata).
|
||||
/// </summary>
|
||||
public abstract class PropertyNode
|
||||
{
|
||||
public PropertyDescriptor Descriptor { get; }
|
||||
public ComponentNode ComponentNode { get; }
|
||||
public PropertyNode[]? Children { get; protected set; }
|
||||
|
||||
protected PropertyNode(PropertyDescriptor descriptor, ComponentNode parent)
|
||||
{
|
||||
Descriptor = descriptor;
|
||||
ComponentNode = parent;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Synchronize the cached value from the ECS backend.
|
||||
/// </summary>
|
||||
public abstract void Sync();
|
||||
|
||||
public virtual void SerializeOverride(JsonObject jsonRoot, object boxedComponent)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void DeserializeOverride(JsonElement jsonRoot, object boxedComponent)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Validate(object boxedComponent)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class PropertyNode<T> : PropertyNode
|
||||
where T : unmanaged
|
||||
{
|
||||
private T _value;
|
||||
public T Value => _value;
|
||||
|
||||
/// <summary>
|
||||
/// Event fired when the value is updated from ECS. UI controls bind to this.
|
||||
/// </summary>
|
||||
public event Action<T>? OnValueChanged;
|
||||
|
||||
public PropertyNode(PropertyDescriptor descriptor, ComponentNode parent, PropertyNode[]? children = null)
|
||||
: base(descriptor, parent)
|
||||
{
|
||||
_value = parent.GetPropertyValue<T>(descriptor);
|
||||
Children = children;
|
||||
}
|
||||
|
||||
public override void Sync()
|
||||
{
|
||||
var newValue = ComponentNode.GetPropertyValue<T>(Descriptor);
|
||||
|
||||
if (!EqualityComparer<T>.Default.Equals(_value, newValue))
|
||||
{
|
||||
_value = newValue;
|
||||
OnValueChanged?.Invoke(newValue);
|
||||
}
|
||||
|
||||
if (Children != null)
|
||||
{
|
||||
foreach (var child in Children)
|
||||
{
|
||||
child.Sync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called by the UI when the user edits the value.
|
||||
/// </summary>
|
||||
public void SetValueFromUI(T newValue)
|
||||
{
|
||||
_value = newValue;
|
||||
ComponentNode.SetPropertyValue(Descriptor, newValue);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using Ghost.Engine.Components;
|
||||
using Ghost.Engine.Core;
|
||||
using Ghost.Entities;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ghost.Editor.Core.SceneGraph;
|
||||
|
||||
@@ -84,7 +83,7 @@ public static class SceneGraphBuilder
|
||||
foreach (var rootEntity in roots)
|
||||
{
|
||||
var name = initialNames != null && initialNames.TryGetValue(rootEntity, out var n) ? n : "Entity";
|
||||
var entityNode = new EntityNode(parentNode.World, rootEntity, name);
|
||||
var entityNode = new EntityNode(parentNode.World, rootEntity, name, parentNode.GetOwningSceneNode());
|
||||
parentNode.Children.Add(entityNode);
|
||||
BuildSubtree(entityNode, childrenByParent, initialNames);
|
||||
}
|
||||
@@ -103,7 +102,7 @@ public static class SceneGraphBuilder
|
||||
foreach (var childEntity in childList)
|
||||
{
|
||||
var name = initialNames != null && initialNames.TryGetValue(childEntity, out var n) ? n : "Entity";
|
||||
var childNode = new EntityNode(parentNode.World, childEntity, name);
|
||||
var childNode = new EntityNode(parentNode.World, childEntity, name, parentNode.SceneNode);
|
||||
parentNode.Children.Add(childNode);
|
||||
BuildSubtree(childNode, childrenByParent, initialNames);
|
||||
}
|
||||
@@ -117,7 +116,7 @@ public static class SceneGraphBuilder
|
||||
if (childList.Contains(sibling))
|
||||
{
|
||||
var name = initialNames != null && initialNames.TryGetValue(sibling, out var n) ? n : "Entity";
|
||||
var childNode = new EntityNode(parentNode.World, sibling, name);
|
||||
var childNode = new EntityNode(parentNode.World, sibling, name, parentNode.SceneNode);
|
||||
parentNode.Children.Add(childNode);
|
||||
BuildSubtree(childNode, childrenByParent, initialNames);
|
||||
}
|
||||
@@ -142,6 +141,10 @@ public static class SceneGraphBuilder
|
||||
|
||||
ref var archetype = ref world.ComponentManager.GetArchetypeReference(location.Value.archetypeID);
|
||||
var hierarchyID = ComponentTypeID<Hierarchy>.Value;
|
||||
if (!archetype.HasComponent(hierarchyID))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var pData = archetype.GetComponentData(location.Value.chunkIndex, location.Value.rowIndex, hierarchyID);
|
||||
if (pData == null)
|
||||
{
|
||||
|
||||
@@ -4,10 +4,12 @@ using Ghost.Entities;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using System.Collections.ObjectModel;
|
||||
using Ghost.Editor.Core.Services;
|
||||
|
||||
namespace Ghost.Editor.Core.SceneGraph;
|
||||
|
||||
public abstract partial class SceneGraphNode : ObservableObject, IInspectable
|
||||
[ObservableObject]
|
||||
public abstract partial class SceneGraphNode : GhostObject, IInspectable
|
||||
{
|
||||
[ObservableProperty]
|
||||
public partial string Name
|
||||
@@ -20,6 +22,11 @@ public abstract partial class SceneGraphNode : ObservableObject, IInspectable
|
||||
get;
|
||||
}
|
||||
|
||||
public SceneGraphNode? Parent
|
||||
{
|
||||
get; internal set;
|
||||
}
|
||||
|
||||
public ObservableCollection<SceneGraphNode> Children
|
||||
{
|
||||
get;
|
||||
@@ -29,9 +36,70 @@ public abstract partial class SceneGraphNode : ObservableObject, IInspectable
|
||||
{
|
||||
World = world;
|
||||
Name = name;
|
||||
Children.CollectionChanged += OnChildrenChanged;
|
||||
}
|
||||
|
||||
public abstract IconSource? CreateIcon();
|
||||
public abstract UIElement? CreateHeader();
|
||||
public abstract UIElement? CreateInspector();
|
||||
private void OnChildrenChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
if (e.OldItems != null)
|
||||
{
|
||||
foreach (SceneGraphNode oldItem in e.OldItems)
|
||||
{
|
||||
if (oldItem.Parent == this)
|
||||
{
|
||||
oldItem.Parent = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (e.NewItems != null)
|
||||
{
|
||||
foreach (SceneGraphNode newItem in e.NewItems)
|
||||
{
|
||||
newItem.Parent = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual SceneNode? GetOwningSceneNode()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public override void Modify()
|
||||
{
|
||||
base.Modify(); // Marks this node dirty via base GhostObject logic
|
||||
|
||||
var sceneNode = GetOwningSceneNode();
|
||||
if (sceneNode != null)
|
||||
{
|
||||
var worldService = EditorApplication.GetService<IEditorWorldService>();
|
||||
var sceneAsset = worldService.GetAssetForScene(sceneNode.Scene.ID);
|
||||
if (sceneAsset != null)
|
||||
{
|
||||
EditorApplication.GetService<IDirtyTrackerService>().MarkDirty(sceneAsset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void SerializeState(BinaryWriter writer)
|
||||
{
|
||||
writer.Write(Name);
|
||||
}
|
||||
|
||||
public override void DeserializeState(BinaryReader reader)
|
||||
{
|
||||
Name = reader.ReadString();
|
||||
}
|
||||
|
||||
public virtual IconSource? CreateIcon()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual UIElement? CreateHeader()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract IInspectorModel CreateInspectorModel();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Engine.Core;
|
||||
using Ghost.Entities;
|
||||
using Microsoft.UI.Xaml;
|
||||
@@ -12,12 +13,14 @@ public sealed partial class SceneNode : SceneGraphNode
|
||||
get;
|
||||
}
|
||||
|
||||
public SceneNode(World world, Scene scene, string name)
|
||||
internal SceneNode(World world, Scene scene, string name)
|
||||
: base(world, name)
|
||||
{
|
||||
Scene = scene;
|
||||
}
|
||||
|
||||
public override SceneNode? GetOwningSceneNode() => this;
|
||||
|
||||
public override IconSource? CreateIcon()
|
||||
{
|
||||
return new FontIconSource
|
||||
@@ -31,8 +34,8 @@ public sealed partial class SceneNode : SceneGraphNode
|
||||
return null;
|
||||
}
|
||||
|
||||
public override UIElement? CreateInspector()
|
||||
public override IInspectorModel CreateInspectorModel()
|
||||
{
|
||||
return null;
|
||||
return null!;
|
||||
}
|
||||
}
|
||||
@@ -113,7 +113,7 @@ public sealed partial class AssetCatalog
|
||||
{
|
||||
return Path.GetFullPath(path).Replace('\\', '/');
|
||||
}
|
||||
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ public sealed partial class AssetCatalog
|
||||
{
|
||||
using var connection = OpenConnection();
|
||||
using var cmd = connection.CreateCommand();
|
||||
|
||||
|
||||
cmd.CommandText = SqlGetGuid;
|
||||
cmd.Parameters.AddWithValue("@path", ToUniversalPath(sourcePath));
|
||||
var result = cmd.ExecuteScalar();
|
||||
@@ -171,7 +171,7 @@ public sealed partial class AssetCatalog
|
||||
|
||||
using var connection = OpenConnection();
|
||||
using var cmd = connection.CreateCommand();
|
||||
|
||||
|
||||
cmd.CommandText = SqlDelete;
|
||||
cmd.Parameters.AddWithValue("@guid", guid.ToByteArray());
|
||||
return cmd.ExecuteNonQuery() > 0;
|
||||
@@ -181,10 +181,10 @@ public sealed partial class AssetCatalog
|
||||
{
|
||||
using var connection = OpenConnection();
|
||||
using var cmd = connection.CreateCommand();
|
||||
|
||||
|
||||
cmd.CommandText = SqlGetAssetTypeId;
|
||||
cmd.Parameters.AddWithValue("@guid", guid.ToByteArray());
|
||||
|
||||
|
||||
var result = cmd.ExecuteScalar();
|
||||
return result is byte[] bytes ? new Guid(bytes) : Guid.Empty;
|
||||
}
|
||||
@@ -193,10 +193,10 @@ public sealed partial class AssetCatalog
|
||||
{
|
||||
using var connection = OpenConnection();
|
||||
using var cmd = connection.CreateCommand();
|
||||
|
||||
|
||||
cmd.CommandText = SqlGetImportedAt;
|
||||
cmd.Parameters.AddWithValue("@guid", guid.ToByteArray());
|
||||
|
||||
|
||||
var result = cmd.ExecuteScalar();
|
||||
return result is long ticks ? new DateTime(ticks, DateTimeKind.Utc) : null;
|
||||
}
|
||||
@@ -237,10 +237,10 @@ public sealed partial class AssetCatalog
|
||||
{
|
||||
using var connection = OpenConnection();
|
||||
using var cmd = connection.CreateCommand();
|
||||
|
||||
|
||||
cmd.CommandText = SqlGetReferencers;
|
||||
cmd.Parameters.AddWithValue("@guid", guid.ToByteArray());
|
||||
|
||||
|
||||
using var reader = cmd.ExecuteReader();
|
||||
var list = new List<Guid>();
|
||||
while (reader.Read())
|
||||
@@ -255,10 +255,10 @@ public sealed partial class AssetCatalog
|
||||
{
|
||||
using var connection = OpenConnection();
|
||||
using var cmd = connection.CreateCommand();
|
||||
|
||||
|
||||
cmd.CommandText = SqlGetDependencies;
|
||||
cmd.Parameters.AddWithValue("@guid", guid.ToByteArray());
|
||||
|
||||
|
||||
using var reader = cmd.ExecuteReader();
|
||||
var list = new List<Guid>();
|
||||
while (reader.Read())
|
||||
@@ -293,9 +293,9 @@ public sealed partial class AssetCatalog
|
||||
using var cmd = connection.CreateCommand();
|
||||
|
||||
var parameterNames = new List<string>(assetTypeIds.Length);
|
||||
for (int i = 0; i < assetTypeIds.Length; i++)
|
||||
for (var i = 0; i < assetTypeIds.Length; i++)
|
||||
{
|
||||
string paramName = $"@typeId{i}";
|
||||
var paramName = $"@typeId{i}";
|
||||
parameterNames.Add(paramName);
|
||||
cmd.Parameters.AddWithValue(paramName, assetTypeIds[i].ToByteArray());
|
||||
}
|
||||
@@ -313,10 +313,10 @@ public sealed partial class AssetCatalog
|
||||
{
|
||||
using var connection = OpenConnection();
|
||||
using var cmd = connection.CreateCommand();
|
||||
|
||||
|
||||
cmd.CommandText = SqlEnumerateSubAssets;
|
||||
cmd.Parameters.AddWithValue("@parent_guid", parentGuid.ToByteArray());
|
||||
|
||||
|
||||
using var reader = cmd.ExecuteReader();
|
||||
var list = new List<SubAssetInfo>();
|
||||
while (reader.Read())
|
||||
|
||||
@@ -2,7 +2,9 @@ using Ghost.Core;
|
||||
using Ghost.Core.Utilities;
|
||||
using Ghost.Editor.Core.Assets;
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Ghost.Editor.Core.Services;
|
||||
|
||||
@@ -188,7 +190,7 @@ internal sealed class AssetRegistry : IAssetRegistry, IDisposable
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex);
|
||||
Logger.Warning($"FileSystemEvent exception: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -421,6 +423,37 @@ internal sealed class AssetRegistry : IAssetRegistry, IDisposable
|
||||
return await Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
public Task<Result> OpenAssetAsync(Guid id)
|
||||
{
|
||||
var path = GetAssetPath(id);
|
||||
if (path == null)
|
||||
{
|
||||
return Task.FromResult(Result.Failure("Asset not found."));
|
||||
}
|
||||
|
||||
return OpenAssetAsync(path);
|
||||
}
|
||||
|
||||
public Task<Result> OpenAssetAsync(string assetPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var method = TypeCache.GetMethodsWithAttribute<AssetOpenHandlerAttribute>()?
|
||||
.FirstOrDefault(m => m.GetCustomAttribute<AssetOpenHandlerAttribute>()?.Extensions.Contains(Path.GetExtension(assetPath)) ?? false);
|
||||
|
||||
if (method == null)
|
||||
{
|
||||
return Task.FromResult(Result.Failure("No handler for this asset type."));
|
||||
}
|
||||
|
||||
return (Task<Result>)method.Invoke(null, new object[] { assetPath })!;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Task.FromResult(Result.Failure($"Failed to open asset: {ex.Message}"));
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_watcher.Dispose();
|
||||
|
||||
77
src/Editor/Ghost.Editor.Core/Services/DirtyTrackerService.cs
Normal file
77
src/Editor/Ghost.Editor.Core/Services/DirtyTrackerService.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Editor.Core.Assets;
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
|
||||
namespace Ghost.Editor.Core.Services;
|
||||
|
||||
internal class DirtyTrackerService : IDirtyTrackerService
|
||||
{
|
||||
private readonly IUndoService _undoService;
|
||||
private readonly Dictionary<Guid, int> _cleanVersions = new();
|
||||
private readonly HashSet<GhostObject> _trackedObjects = new();
|
||||
|
||||
public DirtyTrackerService(IUndoService undoService)
|
||||
{
|
||||
_undoService = undoService;
|
||||
}
|
||||
|
||||
public void MarkDirty(GhostObject obj)
|
||||
{
|
||||
// When marked dirty, we just ensure it is tracked.
|
||||
// Its "clean version" remains whatever it was (or 0 if it was never saved).
|
||||
// If it was never saved and just got modified, its clean version is assumed to be 0 (or something that won't match GlobalVersion).
|
||||
|
||||
if (!_cleanVersions.ContainsKey(obj.InstanceID))
|
||||
{
|
||||
// If we've never seen it, and it's being marked dirty,
|
||||
// its "clean state" is whatever state existed BEFORE this edit (which caused GlobalVersion to increment).
|
||||
// Actually, if it's a brand new edit, UndoService will push an operation and increment GlobalVersion.
|
||||
// If the object was clean at the *current* version before the edit, we should record its clean version as (GlobalVersion - 1),
|
||||
// but since UndoService.RecordObject increments GlobalVersion, the timing matters.
|
||||
// Let's just say its clean version is 0. If GlobalVersion is > 0, it will be dirty.
|
||||
_cleanVersions[obj.InstanceID] = _undoService.GlobalVersion - 1;
|
||||
}
|
||||
|
||||
_trackedObjects.Add(obj);
|
||||
|
||||
if (obj is IAsset asset)
|
||||
{
|
||||
EditorApplication.GetService<IAssetRegistry>().SetAssetDirty(asset.ID);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsDirty(GhostObject obj)
|
||||
{
|
||||
if (_cleanVersions.TryGetValue(obj.InstanceID, out var cleanVersion))
|
||||
{
|
||||
return cleanVersion != _undoService.GlobalVersion;
|
||||
}
|
||||
|
||||
// If it's not tracked, it's clean.
|
||||
return false;
|
||||
}
|
||||
|
||||
public void MarkClean(GhostObject obj)
|
||||
{
|
||||
_cleanVersions[obj.InstanceID] = _undoService.GlobalVersion;
|
||||
_trackedObjects.Add(obj);
|
||||
}
|
||||
|
||||
public IReadOnlyList<GhostObject> GetDirtyObjects()
|
||||
{
|
||||
var dirtyObjects = new List<GhostObject>();
|
||||
|
||||
// Remove dead references
|
||||
_trackedObjects.RemoveWhere(obj => GhostObject.Find(obj.InstanceID) == null);
|
||||
|
||||
foreach (var obj in _trackedObjects)
|
||||
{
|
||||
if (IsDirty(obj))
|
||||
{
|
||||
dirtyObjects.Add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
return dirtyObjects;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ using Ghost.Core.Graphics;
|
||||
using Ghost.Editor.Core.Assets;
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Ghost.Graphics.Core;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using System.Collections.Concurrent;
|
||||
@@ -24,11 +23,11 @@ internal sealed class EditorShaderCompilerBridge : IShaderCompilationBridge
|
||||
public event ShaderVariantCompiledHandler? OnShaderVariantCompiled;
|
||||
public event Action<ulong>? OnShaderInvalidated;
|
||||
|
||||
public EditorShaderCompilerBridge(IAssetRegistry assetRegistry, IServiceProvider serviceProvider)
|
||||
public EditorShaderCompilerBridge(IAssetRegistry assetRegistry, IServiceProvider serviceProvider, IShaderCompiler shaderCompiler)
|
||||
{
|
||||
_assetRegistry = assetRegistry;
|
||||
_serviceProvider = serviceProvider;
|
||||
_compiler = new DXCShaderCompiler();
|
||||
_compiler = shaderCompiler;
|
||||
|
||||
_assetRegistry.OnAssetImported += OnAssetImported;
|
||||
}
|
||||
@@ -263,9 +262,20 @@ internal sealed class EditorShaderCompilerBridge : IShaderCompilationBridge
|
||||
using var compiled = compileResult.Value;
|
||||
|
||||
var stageCount = 0;
|
||||
if (compiled.asResult.IsCreated) stageCount++;
|
||||
if (compiled.msResult.IsCreated) stageCount++;
|
||||
if (compiled.psResult.IsCreated) stageCount++;
|
||||
if (compiled.asResult.IsCreated)
|
||||
{
|
||||
stageCount++;
|
||||
}
|
||||
|
||||
if (compiled.msResult.IsCreated)
|
||||
{
|
||||
stageCount++;
|
||||
}
|
||||
|
||||
if (compiled.psResult.IsCreated)
|
||||
{
|
||||
stageCount++;
|
||||
}
|
||||
|
||||
var byteCodes = stackalloc ShaderByteCode[stageCount];
|
||||
var idx = 0;
|
||||
|
||||
89
src/Editor/Ghost.Editor.Core/Services/EditorTickEngine.cs
Normal file
89
src/Editor/Ghost.Editor.Core/Services/EditorTickEngine.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Ghost.Entities;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Ghost.Editor.Core.Services;
|
||||
|
||||
public sealed class EditorTickEngine : IDisposable
|
||||
{
|
||||
private readonly IEditorWorldService _worldService;
|
||||
private readonly DispatcherQueueTimer _timer;
|
||||
private bool _isStarted;
|
||||
|
||||
// Time data
|
||||
private TimeData _timeData;
|
||||
private long _startTimestamp;
|
||||
private long _lastFrameTimestamp;
|
||||
|
||||
public event Action? OnSafeZone;
|
||||
public event Action? OnSystemUpdate;
|
||||
public event Action? OnInspectorSync;
|
||||
public event Action? OnFireEvents;
|
||||
|
||||
public EditorTickEngine(IEditorWorldService worldService)
|
||||
{
|
||||
_worldService = worldService;
|
||||
|
||||
_timer = EditorApplication.DispatcherQueue.CreateTimer();
|
||||
_timer.Interval = TimeSpan.FromMilliseconds(16); // ~60Hz
|
||||
_timer.Tick += OnTick;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
if (_isStarted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_startTimestamp = Stopwatch.GetTimestamp();
|
||||
_lastFrameTimestamp = _startTimestamp;
|
||||
_timeData = new TimeData();
|
||||
|
||||
_timer.Start();
|
||||
_isStarted = true;
|
||||
}
|
||||
|
||||
private void OnTick(DispatcherQueueTimer sender, object args)
|
||||
{
|
||||
var now = Stopwatch.GetTimestamp();
|
||||
var dt = (float)(now - _lastFrameTimestamp) / Stopwatch.Frequency;
|
||||
var elapsed = (double)(now - _startTimestamp) / Stopwatch.Frequency;
|
||||
|
||||
_timeData = new TimeData
|
||||
{
|
||||
FrameCount = _timeData.FrameCount + 1,
|
||||
DeltaTime = dt,
|
||||
ElapsedTime = elapsed
|
||||
};
|
||||
|
||||
_lastFrameTimestamp = now;
|
||||
|
||||
// Safe Zone (Drain Commands & ECB)
|
||||
_worldService.FlushCommands();
|
||||
OnSafeZone?.Invoke();
|
||||
|
||||
// Editor Systems
|
||||
_worldService.EditorWorld.SystemManager.UpdateAll(_timeData);
|
||||
OnSystemUpdate?.Invoke();
|
||||
|
||||
// Inspector Sync
|
||||
OnInspectorSync?.Invoke();
|
||||
|
||||
// Fire Events
|
||||
_worldService.FirePendingEvents();
|
||||
OnFireEvents?.Invoke();
|
||||
|
||||
_worldService.EditorWorld.AdvanceVersion();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_isStarted && _timer != null)
|
||||
{
|
||||
_timer.Stop();
|
||||
_timer.Tick -= OnTick;
|
||||
_isStarted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,47 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Editor.Core.Assets;
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Ghost.Entities;
|
||||
using Ghost.Engine;
|
||||
using Ghost.Engine.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Ghost.Entities;
|
||||
using Misaki.HighPerformance.Jobs;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Ghost.Editor.Core.Services;
|
||||
|
||||
public class EditorWorldService : IDisposable
|
||||
public interface IEditorWorldService : IDisposable
|
||||
{
|
||||
private const int DEFAULT_ENTITY_CAPACITY = 1024;
|
||||
World EditorWorld { get; }
|
||||
ObservableCollection<SceneNode> RootNodes { get; }
|
||||
|
||||
event Action<Entity, string, ushort>? EntityCreated;
|
||||
event Action<Entity>? EntityDestroyed;
|
||||
event Action<Entity, Entity, Entity>? EntityParentChanged;
|
||||
event Action<Entity, string>? EntityNameChanged;
|
||||
event Action? SceneGraphRebuilt;
|
||||
|
||||
void ChangeEntityScene(Entity entity, ushort sceneID);
|
||||
void CreateDefaultScene();
|
||||
void CreateEntity(string name, ushort sceneID, Entity parent = default);
|
||||
void Defer(Action action);
|
||||
void DestroyEntity(Entity entity);
|
||||
void FirePendingEvents();
|
||||
void FlushCommands();
|
||||
ushort GetEntitySceneID(Entity entity);
|
||||
SceneAsset? GetAssetForScene(ushort sceneID);
|
||||
void RegisterSceneAsset(ushort sceneID, SceneAsset asset);
|
||||
void RebuildSceneGraph(Dictionary<Entity, string>? initialNames = null);
|
||||
Error RemoveParent(Entity child);
|
||||
void RenameEntity(Entity entity, string newName);
|
||||
Error SetParent(Entity child, Entity parent);
|
||||
}
|
||||
|
||||
internal class EditorWorldService : IEditorWorldService
|
||||
{
|
||||
private readonly ConcurrentQueue<Action> _deferredActions = new();
|
||||
private readonly ConcurrentQueue<Action> _pendingEvents = new();
|
||||
private readonly ConcurrentDictionary<ushort, SceneAsset> _sceneAssetMap = new();
|
||||
|
||||
public World EditorWorld
|
||||
{
|
||||
@@ -22,58 +52,80 @@ public class EditorWorldService : IDisposable
|
||||
{
|
||||
get;
|
||||
} = new();
|
||||
|
||||
public event Action<Entity, string, ushort>? EntityCreated;
|
||||
public event Action<Entity>? EntityDestroyed;
|
||||
public event Action<Entity, Entity, Entity>? EntityParentChanged; // (child, oldParent, newParent)
|
||||
public event Action<Entity, string>? EntityNameChanged;
|
||||
public event Action? SceneGraphRebuilt;
|
||||
|
||||
public EditorWorldService()
|
||||
public EditorWorldService(JobScheduler? jobScheduler = null)
|
||||
{
|
||||
EditorWorld = World.Create(entityCapacity: DEFAULT_ENTITY_CAPACITY);
|
||||
EditorWorld = World.Create(jobScheduler, 1024);
|
||||
}
|
||||
|
||||
public Entity CreateEntity(string name, ushort sceneID, Entity parent = default)
|
||||
public void Defer(Action action)
|
||||
{
|
||||
var entity = EditorWorld.EntityManager.CreateEntity();
|
||||
_deferredActions.Enqueue(action);
|
||||
}
|
||||
|
||||
EditorWorld.EntityManager.AddComponent(entity, new Engine.Components.Hierarchy
|
||||
public void FlushCommands()
|
||||
{
|
||||
while (_deferredActions.TryDequeue(out var action))
|
||||
{
|
||||
parent = Entity.Invalid,
|
||||
firstChild = Entity.Invalid,
|
||||
nextSibling = Entity.Invalid
|
||||
});
|
||||
|
||||
EditorWorld.EntityManager.AddSharedComponent(entity, new Engine.Components.SceneID
|
||||
{
|
||||
value = sceneID
|
||||
});
|
||||
|
||||
if (parent.IsValid)
|
||||
{
|
||||
HierarchyUtility.SetParent(EditorWorld, entity, parent);
|
||||
action();
|
||||
}
|
||||
}
|
||||
|
||||
EditorWorld.AdvanceVersion();
|
||||
EntityCreated?.Invoke(entity, name, sceneID);
|
||||
|
||||
if (parent.IsValid)
|
||||
public void FirePendingEvents()
|
||||
{
|
||||
while (_pendingEvents.TryDequeue(out var evt))
|
||||
{
|
||||
EntityParentChanged?.Invoke(entity, Entity.Invalid, parent);
|
||||
evt();
|
||||
}
|
||||
}
|
||||
|
||||
return entity;
|
||||
public void CreateEntity(string name, ushort sceneID, Entity parent = default)
|
||||
{
|
||||
Defer(() =>
|
||||
{
|
||||
var entity = EditorWorld.EntityManager.CreateEntity();
|
||||
|
||||
EditorWorld.EntityManager.AddComponent(entity, new Engine.Components.Hierarchy
|
||||
{
|
||||
parent = Entity.Invalid,
|
||||
firstChild = Entity.Invalid,
|
||||
nextSibling = Entity.Invalid
|
||||
});
|
||||
|
||||
EditorWorld.EntityManager.AddSharedComponent(entity, new Engine.Components.SceneID
|
||||
{
|
||||
value = sceneID
|
||||
});
|
||||
|
||||
if (parent.IsValid)
|
||||
{
|
||||
HierarchyUtility.SetParent(EditorWorld, entity, parent);
|
||||
}
|
||||
|
||||
_pendingEvents.Enqueue(() =>
|
||||
{
|
||||
EntityCreated?.Invoke(entity, name, sceneID);
|
||||
if (parent.IsValid)
|
||||
{
|
||||
EntityParentChanged?.Invoke(entity, Entity.Invalid, parent);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public void DestroyEntity(Entity entity)
|
||||
{
|
||||
if (!entity.IsValid)
|
||||
Defer(() =>
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
DestroyEntityRecursive(entity);
|
||||
EditorWorld.AdvanceVersion();
|
||||
if (!entity.IsValid) return;
|
||||
DestroyEntityRecursive(entity);
|
||||
});
|
||||
}
|
||||
|
||||
private void DestroyEntityRecursive(Entity entity)
|
||||
@@ -93,7 +145,7 @@ public class EditorWorldService : IDisposable
|
||||
|
||||
HierarchyUtility.RemoveParent(EditorWorld, entity);
|
||||
EditorWorld.EntityManager.DestroyEntity(entity);
|
||||
EntityDestroyed?.Invoke(entity);
|
||||
_pendingEvents.Enqueue(() => EntityDestroyed?.Invoke(entity));
|
||||
}
|
||||
|
||||
private void UpdateSceneIDRecursive(Entity entity, ushort sceneID)
|
||||
@@ -119,41 +171,54 @@ public class EditorWorldService : IDisposable
|
||||
|
||||
public void ChangeEntityScene(Entity entity, ushort sceneID)
|
||||
{
|
||||
if (!entity.IsValid)
|
||||
Defer(() =>
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!entity.IsValid) return;
|
||||
|
||||
UpdateSceneIDRecursive(entity, sceneID);
|
||||
EditorWorld.AdvanceVersion();
|
||||
EntityParentChanged?.Invoke(entity, Entity.Invalid, Entity.Invalid);
|
||||
UpdateSceneIDRecursive(entity, sceneID);
|
||||
_pendingEvents.Enqueue(() => EntityParentChanged?.Invoke(entity, Entity.Invalid, Entity.Invalid));
|
||||
});
|
||||
}
|
||||
|
||||
public Error SetParent(Entity child, Entity parent)
|
||||
{
|
||||
if (!child.IsValid)
|
||||
{
|
||||
return Error.InvalidArgument;
|
||||
}
|
||||
if (!child.IsValid) return Error.InvalidArgument;
|
||||
|
||||
Entity oldParent = Entity.Invalid;
|
||||
if (EditorWorld.EntityManager.HasComponent<Engine.Components.Hierarchy>(child))
|
||||
{
|
||||
oldParent = EditorWorld.EntityManager.GetComponent<Engine.Components.Hierarchy>(child).parent;
|
||||
}
|
||||
|
||||
Error err;
|
||||
Error err = Error.None;
|
||||
if (parent.IsValid)
|
||||
{
|
||||
err = HierarchyUtility.SetParent(EditorWorld, child, parent);
|
||||
err = HierarchyUtility.IsValidParent(EditorWorld, child, parent);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = HierarchyUtility.RemoveParent(EditorWorld, child);
|
||||
if (!EditorWorld.EntityManager.HasComponent<Engine.Components.Hierarchy>(child))
|
||||
{
|
||||
err = Error.NotFound;
|
||||
}
|
||||
}
|
||||
|
||||
if (err == Error.None)
|
||||
if (err != Error.None)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
Defer(() =>
|
||||
{
|
||||
var oldParent = Entity.Invalid;
|
||||
if (EditorWorld.EntityManager.HasComponent<Engine.Components.Hierarchy>(child))
|
||||
{
|
||||
oldParent = EditorWorld.EntityManager.GetComponent<Engine.Components.Hierarchy>(child).parent;
|
||||
}
|
||||
|
||||
if (parent.IsValid)
|
||||
{
|
||||
HierarchyUtility.SetParent(EditorWorld, child, parent);
|
||||
}
|
||||
else
|
||||
{
|
||||
HierarchyUtility.RemoveParent(EditorWorld, child);
|
||||
}
|
||||
|
||||
if (parent.IsValid && EditorWorld.EntityManager.HasComponent<Engine.Components.SceneID>(parent))
|
||||
{
|
||||
var locRes = EditorWorld.EntityManager.GetEntityLocation(parent);
|
||||
@@ -167,11 +232,10 @@ public class EditorWorldService : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
EditorWorld.AdvanceVersion();
|
||||
EntityParentChanged?.Invoke(child, oldParent, parent);
|
||||
}
|
||||
_pendingEvents.Enqueue(() => EntityParentChanged?.Invoke(child, oldParent, parent));
|
||||
});
|
||||
|
||||
return err;
|
||||
return Error.None;
|
||||
}
|
||||
|
||||
public Error RemoveParent(Entity child)
|
||||
@@ -201,14 +265,24 @@ public class EditorWorldService : IDisposable
|
||||
return Scene.INVALID_ID;
|
||||
}
|
||||
|
||||
public SceneAsset? GetAssetForScene(ushort sceneID)
|
||||
{
|
||||
_sceneAssetMap.TryGetValue(sceneID, out var asset);
|
||||
return asset;
|
||||
}
|
||||
|
||||
public void RegisterSceneAsset(ushort sceneID, SceneAsset asset)
|
||||
{
|
||||
_sceneAssetMap[sceneID] = asset;
|
||||
}
|
||||
|
||||
public void RenameEntity(Entity entity, string newName)
|
||||
{
|
||||
if (!entity.IsValid)
|
||||
Defer(() =>
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EntityNameChanged?.Invoke(entity, newName);
|
||||
if (!entity.IsValid) return;
|
||||
_pendingEvents.Enqueue(() => EntityNameChanged?.Invoke(entity, newName));
|
||||
});
|
||||
}
|
||||
|
||||
public void CreateDefaultScene()
|
||||
@@ -218,13 +292,19 @@ public class EditorWorldService : IDisposable
|
||||
}
|
||||
public void RebuildSceneGraph(Dictionary<Entity, string>? initialNames = null)
|
||||
{
|
||||
RootNodes.Clear();
|
||||
var sceneNodes = SceneGraphBuilder.Build(EditorWorld, initialNames);
|
||||
foreach (var node in sceneNodes)
|
||||
Defer(() =>
|
||||
{
|
||||
RootNodes.Add(node);
|
||||
}
|
||||
SceneGraphRebuilt?.Invoke();
|
||||
var sceneNodes = SceneGraphBuilder.Build(EditorWorld, initialNames);
|
||||
_pendingEvents.Enqueue(() =>
|
||||
{
|
||||
RootNodes.Clear();
|
||||
foreach (var node in sceneNodes)
|
||||
{
|
||||
RootNodes.Add(node);
|
||||
}
|
||||
SceneGraphRebuilt?.Invoke();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -56,7 +56,14 @@ internal sealed partial class ImportCoordinator : IDisposable
|
||||
|
||||
public ValueTask EnqueueAsync(ImportJob job, CancellationToken token = default)
|
||||
{
|
||||
return _importChannel.Writer.WriteAsync(job, token);
|
||||
try
|
||||
{
|
||||
return _importChannel.Writer.WriteAsync(job, token);
|
||||
}
|
||||
catch (ChannelClosedException)
|
||||
{
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task WorkerLoop(CancellationToken token)
|
||||
@@ -209,6 +216,15 @@ internal sealed partial class ImportCoordinator : IDisposable
|
||||
{
|
||||
_importChannel.Writer.TryComplete();
|
||||
_cts.Cancel();
|
||||
|
||||
try
|
||||
{
|
||||
Task.WaitAll(_workers);
|
||||
}
|
||||
catch (AggregateException)
|
||||
{
|
||||
}
|
||||
|
||||
_cts.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
|
||||
namespace Ghost.Editor.Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Syncs the inspector model from ECS data on every editor tick (Phase 3).
|
||||
/// </summary>
|
||||
public sealed class InspectorSyncService : IDisposable
|
||||
{
|
||||
private readonly EditorTickEngine _tickEngine;
|
||||
private ISyncableInspectorModel? _activeModel;
|
||||
private bool _isStarted;
|
||||
|
||||
public InspectorSyncService(EditorTickEngine tickEngine)
|
||||
{
|
||||
_tickEngine = tickEngine;
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
if (_isStarted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_tickEngine.OnInspectorSync += OnInspectorSync;
|
||||
_isStarted = true;
|
||||
}
|
||||
|
||||
public void Bind(ISyncableInspectorModel model)
|
||||
{
|
||||
_activeModel = model;
|
||||
}
|
||||
|
||||
public void Unbind()
|
||||
{
|
||||
_activeModel = null;
|
||||
}
|
||||
|
||||
private void OnInspectorSync()
|
||||
{
|
||||
if (_activeModel == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_activeModel.Sync();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_isStarted)
|
||||
{
|
||||
_tickEngine.OnInspectorSync -= OnInspectorSync;
|
||||
_isStarted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,17 +2,15 @@ using Ghost.Editor.Core.SceneGraph;
|
||||
using Ghost.Engine.Components;
|
||||
using Ghost.Engine.Core;
|
||||
using Ghost.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ghost.Editor.Core.Services;
|
||||
|
||||
public class SceneGraphSyncService : IDisposable
|
||||
internal class SceneGraphSyncService : IDisposable
|
||||
{
|
||||
private readonly EditorWorldService _worldService;
|
||||
private readonly IEditorWorldService _worldService;
|
||||
private readonly Dictionary<Entity, EntityNode> _nodeMap = new();
|
||||
|
||||
public SceneGraphSyncService(EditorWorldService worldService)
|
||||
public SceneGraphSyncService(IEditorWorldService worldService)
|
||||
{
|
||||
_worldService = worldService;
|
||||
|
||||
@@ -31,12 +29,6 @@ public class SceneGraphSyncService : IDisposable
|
||||
return _nodeMap.TryGetValue(entity, out node!);
|
||||
}
|
||||
|
||||
// Keep Tick as an empty stub returning false so we don't break Hierarchy.xaml.cs before we update it
|
||||
public bool Tick()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_worldService.EntityCreated -= OnEntityCreated;
|
||||
@@ -75,11 +67,12 @@ public class SceneGraphSyncService : IDisposable
|
||||
return;
|
||||
}
|
||||
|
||||
var node = new EntityNode(_worldService.EditorWorld, entity, name);
|
||||
_nodeMap[entity] = node;
|
||||
|
||||
// By default, add to the scene's root collection
|
||||
var sceneNode = FindOrCreateSceneNode(sceneID);
|
||||
|
||||
var node = new EntityNode(_worldService.EditorWorld, entity, name, sceneNode);
|
||||
_nodeMap[entity] = node;
|
||||
|
||||
sceneNode.Children.Add(node);
|
||||
}
|
||||
|
||||
@@ -113,7 +106,7 @@ public class SceneGraphSyncService : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private bool RemoveNodeFromChildrenRecursive(System.Collections.ObjectModel.ObservableCollection<SceneGraphNode> children, EntityNode target)
|
||||
private static bool RemoveNodeFromChildrenRecursive(System.Collections.ObjectModel.ObservableCollection<SceneGraphNode> children, EntityNode target)
|
||||
{
|
||||
foreach (var child in children)
|
||||
{
|
||||
|
||||
@@ -69,11 +69,11 @@ internal class SceneSerializationService : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private readonly EditorWorldService _worldService;
|
||||
private readonly IEditorWorldService _worldService;
|
||||
private readonly IAssetRegistry _assetRegistry;
|
||||
private readonly SceneGraphSyncService _syncService;
|
||||
|
||||
public SceneSerializationService(EditorWorldService worldService, IAssetRegistry assetRegistry, SceneGraphSyncService syncService)
|
||||
public SceneSerializationService(IEditorWorldService worldService, IAssetRegistry assetRegistry, SceneGraphSyncService syncService)
|
||||
{
|
||||
_worldService = worldService;
|
||||
_assetRegistry = assetRegistry;
|
||||
@@ -111,7 +111,7 @@ internal class SceneSerializationService : IDisposable
|
||||
return fields;
|
||||
}
|
||||
|
||||
private static object RemapEntityFieldsToLocal(object boxed, Type type, Dictionary<Entity, int> reverseMap)
|
||||
private static void RemapEntityFieldsToLocal(object boxed, Type type, Dictionary<Entity, int> reverseMap)
|
||||
{
|
||||
var entityFields = GetEntityFields(type);
|
||||
foreach (var field in entityFields)
|
||||
@@ -120,11 +120,9 @@ internal class SceneSerializationService : IDisposable
|
||||
var localIndex = FileLocalIndexOf(reverseMap, entity);
|
||||
field.SetValue(boxed, new Entity(localIndex, localIndex >= 0 ? 0 : -1));
|
||||
}
|
||||
|
||||
return boxed;
|
||||
}
|
||||
|
||||
private static object RemapLocalFieldsToEntity(object boxed, Type type, Dictionary<int, Entity> forwardMap)
|
||||
private static void RemapLocalFieldsToEntity(object boxed, Type type, Dictionary<int, Entity> forwardMap)
|
||||
{
|
||||
var entityFields = GetEntityFields(type);
|
||||
foreach (var field in entityFields)
|
||||
@@ -138,8 +136,6 @@ internal class SceneSerializationService : IDisposable
|
||||
|
||||
field.SetValue(boxed, entity);
|
||||
}
|
||||
|
||||
return boxed;
|
||||
}
|
||||
|
||||
#region Binary Serialization
|
||||
@@ -292,131 +288,149 @@ internal class SceneSerializationService : IDisposable
|
||||
|
||||
#region Load Scene into Editor World
|
||||
|
||||
public unsafe Result<Scene> LoadSceneIntoEditorWorld(SceneSaveData data, SceneLoadingType loadingType = SceneLoadingType.Single)
|
||||
public unsafe void LoadSceneIntoEditorWorld(SceneSaveData data, SceneLoadingType loadingType = SceneLoadingType.Single, Action<Scene>? onComplete = null)
|
||||
{
|
||||
if (loadingType == SceneLoadingType.Single)
|
||||
_worldService.Defer(() =>
|
||||
{
|
||||
_worldService.EditorWorld.Reset();
|
||||
}
|
||||
|
||||
var world = _worldService.EditorWorld;
|
||||
var activeScene = SceneManager.CreateScene();
|
||||
|
||||
var entityCount = data.Entities.Count;
|
||||
var forwardMap = new Dictionary<int, Entity>(entityCount);
|
||||
if (entityCount == 0)
|
||||
{
|
||||
goto RebuildAndReturn;
|
||||
}
|
||||
|
||||
var scope = AllocationManager.CreateStackScope();
|
||||
var typeIds = new UnsafeArray<UnsafeList<Identifier<IComponent>>>(entityCount, scope.AllocationHandle);
|
||||
for (var i = 0; i < typeIds.Length; i++)
|
||||
{
|
||||
typeIds[i] = new UnsafeList<Identifier<IComponent>>(16, scope.AllocationHandle);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
for (var fileIndex = 0; fileIndex < entityCount; fileIndex++)
|
||||
if (loadingType == SceneLoadingType.Single)
|
||||
{
|
||||
var entityData = data.Entities[fileIndex];
|
||||
ref var list = ref typeIds[fileIndex];
|
||||
|
||||
list.Add(ComponentRegistry.GetOrRegisterComponentID<SceneID>());
|
||||
|
||||
foreach (var (typeName, _) in entityData.Components)
|
||||
{
|
||||
var compId = ComponentRegistry.GetComponentIDByName(typeName);
|
||||
if (compId.IsInvalid)
|
||||
{
|
||||
var type = TypeCache.GetTypes().FirstOrDefault(t => t.FullName == typeName);
|
||||
if (type == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
compId = RegisterComponentByType(type);
|
||||
}
|
||||
|
||||
list.Add(compId);
|
||||
}
|
||||
|
||||
var componentSet = new ComponentSetView(list);
|
||||
var entity = world.EntityManager.CreateEntity(componentSet);
|
||||
forwardMap[fileIndex] = entity;
|
||||
_worldService.EditorWorld.Reset();
|
||||
}
|
||||
|
||||
using var buffer = new MemoryBlock(1024, 16, scope.AllocationHandle);
|
||||
for (var fileIndex = 0; fileIndex < entityCount; fileIndex++)
|
||||
var world = _worldService.EditorWorld;
|
||||
var activeScene = SceneManager.CreateScene();
|
||||
|
||||
var entityCount = data.Entities.Count;
|
||||
var forwardMap = new Dictionary<int, Entity>(entityCount);
|
||||
if (entityCount == 0)
|
||||
{
|
||||
if (!forwardMap.TryGetValue(fileIndex, out var entity))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
world.EntityManager.SetSharedComponent(entity, new SceneID { value = activeScene.ID });
|
||||
|
||||
var entityData = data.Entities[fileIndex];
|
||||
|
||||
foreach (var (typeName, componentElement) in entityData.Components)
|
||||
{
|
||||
var compId = ComponentRegistry.GetComponentIDByName(typeName);
|
||||
if (compId.IsInvalid)
|
||||
{
|
||||
var type = TypeCache.GetTypes().FirstOrDefault(t => t.FullName == typeName);
|
||||
if (type == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
compId = ComponentRegistry.GetComponentIDByName(typeName);
|
||||
}
|
||||
|
||||
if (compId.IsInvalid)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var componentType = ComponentRegistry.s_runtimeIDToType[compId];
|
||||
|
||||
var boxed = componentElement.Deserialize(componentType, s_jsonOptions);
|
||||
if (boxed == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
boxed = RemapLocalFieldsToEntity(boxed, componentType, forwardMap);
|
||||
|
||||
Marshal.StructureToPtr(boxed, (nint)buffer.GetUnsafePtr(), false);
|
||||
|
||||
world.EntityManager.SetComponent(entity, compId, buffer.GetUnsafePtr());
|
||||
}
|
||||
goto RebuildAndReturn;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
scope.Dispose();
|
||||
|
||||
var scope = AllocationManager.CreateStackScope();
|
||||
var typeIds = new UnsafeArray<UnsafeList<Identifier<IComponent>>>(entityCount, scope.AllocationHandle);
|
||||
for (var i = 0; i < typeIds.Length; i++)
|
||||
{
|
||||
typeIds[i].Dispose();
|
||||
typeIds[i] = new UnsafeList<Identifier<IComponent>>(16, scope.AllocationHandle);
|
||||
}
|
||||
|
||||
typeIds.Dispose();
|
||||
}
|
||||
|
||||
RebuildAndReturn:
|
||||
var initialNames = new Dictionary<Entity, string>();
|
||||
for (var fileIndex = 0; fileIndex < entityCount; fileIndex++)
|
||||
{
|
||||
if (forwardMap.TryGetValue(fileIndex, out var entity))
|
||||
try
|
||||
{
|
||||
initialNames[entity] = data.Entities[fileIndex].Name;
|
||||
for (var fileIndex = 0; fileIndex < entityCount; fileIndex++)
|
||||
{
|
||||
var entityData = data.Entities[fileIndex];
|
||||
ref var list = ref typeIds[fileIndex];
|
||||
|
||||
list.Add(ComponentRegistry.GetOrRegisterComponentID<SceneID>());
|
||||
|
||||
foreach (var (typeName, _) in entityData.Components)
|
||||
{
|
||||
var compId = ComponentRegistry.GetComponentIDByName(typeName);
|
||||
if (compId.IsInvalid)
|
||||
{
|
||||
var type = TypeCache.GetTypes().FirstOrDefault(t => t.FullName == typeName);
|
||||
if (type == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
compId = RegisterComponentByType(type);
|
||||
}
|
||||
|
||||
list.Add(compId);
|
||||
}
|
||||
|
||||
var componentSet = new ComponentSetView(list);
|
||||
var entity = world.EntityManager.CreateEntity(componentSet);
|
||||
forwardMap[fileIndex] = entity;
|
||||
}
|
||||
|
||||
using var buffer = new MemoryBlock(1024, 16, scope.AllocationHandle);
|
||||
for (var fileIndex = 0; fileIndex < entityCount; fileIndex++)
|
||||
{
|
||||
if (!forwardMap.TryGetValue(fileIndex, out var entity))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
world.EntityManager.SetSharedComponent(entity, new SceneID { value = activeScene.ID });
|
||||
|
||||
var entityData = data.Entities[fileIndex];
|
||||
|
||||
foreach (var (typeName, componentElement) in entityData.Components)
|
||||
{
|
||||
var compId = ComponentRegistry.GetComponentIDByName(typeName);
|
||||
if (compId.IsInvalid)
|
||||
{
|
||||
var type = TypeCache.GetTypes().FirstOrDefault(t => t.FullName == typeName);
|
||||
if (type == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
compId = ComponentRegistry.GetComponentIDByName(typeName);
|
||||
}
|
||||
|
||||
if (compId.IsInvalid)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var componentType = ComponentRegistry.s_runtimeIDToType[compId];
|
||||
|
||||
if (_syncService.TryGetNode(entity, out var node))
|
||||
{
|
||||
node.BuildComponents();
|
||||
var compNode = node.Components.FirstOrDefault(c => c.ComponentType == componentType);
|
||||
if (compNode != null)
|
||||
{
|
||||
compNode.Deserialize(componentElement, s_jsonOptions, (boxed) =>
|
||||
{
|
||||
RemapLocalFieldsToEntity(boxed, componentType, forwardMap);
|
||||
});
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to direct deserialization
|
||||
var boxedLegacy = componentElement.Deserialize(componentType, s_jsonOptions);
|
||||
if (boxedLegacy == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
RemapLocalFieldsToEntity(boxedLegacy, componentType, forwardMap);
|
||||
|
||||
Marshal.StructureToPtr(boxedLegacy, (nint)buffer.GetUnsafePtr(), false);
|
||||
|
||||
world.EntityManager.SetComponent(entity, compId, buffer.GetUnsafePtr());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_worldService.RebuildSceneGraph(initialNames);
|
||||
return activeScene;
|
||||
finally
|
||||
{
|
||||
scope.Dispose();
|
||||
|
||||
for (var i = 0; i < typeIds.Length; i++)
|
||||
{
|
||||
typeIds[i].Dispose();
|
||||
}
|
||||
|
||||
typeIds.Dispose();
|
||||
}
|
||||
|
||||
RebuildAndReturn:
|
||||
var initialNames = new Dictionary<Entity, string>();
|
||||
for (var fileIndex = 0; fileIndex < entityCount; fileIndex++)
|
||||
{
|
||||
if (forwardMap.TryGetValue(fileIndex, out var entity))
|
||||
{
|
||||
initialNames[entity] = data.Entities[fileIndex].Name;
|
||||
}
|
||||
}
|
||||
_worldService.RebuildSceneGraph(initialNames);
|
||||
onComplete?.Invoke(activeScene);
|
||||
});
|
||||
}
|
||||
|
||||
private static Identifier<IComponent> RegisterComponentByType(Type type)
|
||||
@@ -444,19 +458,19 @@ internal class SceneSerializationService : IDisposable
|
||||
#region Save Scene from Editor World
|
||||
|
||||
public unsafe void SaveSceneFromEditorWorld(string filePath, Scene scene)
|
||||
{
|
||||
var bytes = SerializeSceneToMemory(scene);
|
||||
File.WriteAllBytes(filePath, bytes);
|
||||
}
|
||||
|
||||
public unsafe byte[] SerializeSceneToMemory(Scene scene)
|
||||
{
|
||||
var world = _worldService.EditorWorld;
|
||||
|
||||
using var scope = AllocationManager.CreateStackScope();
|
||||
using var sceneEntities = SceneManager.GetSceneEntities(world, scene, scope.AllocationHandle);
|
||||
using var entities = SceneManager.GetSceneEntities(world, scene, scope.AllocationHandle);
|
||||
|
||||
var entities = new List<Entity>(sceneEntities.Count);
|
||||
for (var i = 0; i < sceneEntities.Count; i++)
|
||||
{
|
||||
entities.Add(sceneEntities[i]);
|
||||
}
|
||||
|
||||
var sorted = SortEntitiesByHierarchy(world, entities);
|
||||
using var sorted = SortEntitiesByHierarchy(world, entities, scope.AllocationHandle);
|
||||
|
||||
var reverseMap = new Dictionary<Entity, int>();
|
||||
for (var i = 0; i < sorted.Count; i++)
|
||||
@@ -490,7 +504,8 @@ internal class SceneSerializationService : IDisposable
|
||||
writer.WriteStartObject();
|
||||
|
||||
var entityName = "Entity";
|
||||
if (_syncService != null && _syncService.TryGetNode(entity, out var node))
|
||||
SceneGraph.EntityNode? node = null;
|
||||
if (_syncService != null && _syncService.TryGetNode(entity, out node))
|
||||
{
|
||||
entityName = node.Name;
|
||||
}
|
||||
@@ -498,33 +513,51 @@ internal class SceneSerializationService : IDisposable
|
||||
|
||||
writer.WriteStartObject("components");
|
||||
|
||||
foreach (var layout in archetype._layouts)
|
||||
if (node != null)
|
||||
{
|
||||
var type = ComponentRegistry.s_runtimeIDToType[layout.componentID];
|
||||
if (type == typeof(SceneID))
|
||||
node.BuildComponents(); // Ensure latest
|
||||
|
||||
foreach (var compNode in node.Components)
|
||||
{
|
||||
continue;
|
||||
var type = compNode.ComponentType;
|
||||
var fullName = type.FullName ?? type.Name;
|
||||
writer.WritePropertyName(fullName);
|
||||
compNode.Serialize(writer, s_jsonOptions, (boxed) =>
|
||||
{
|
||||
RemapEntityFieldsToLocal(boxed, type, reverseMap);
|
||||
});
|
||||
}
|
||||
|
||||
var fullName = type.FullName ?? type.Name;
|
||||
var compInfo = ComponentRegistry.GetComponentInfo(layout.componentID);
|
||||
|
||||
var pData = archetype.GetComponentData(location.chunkIndex, location.rowIndex, layout.componentID);
|
||||
if (pData == null)
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var layout in archetype._layouts)
|
||||
{
|
||||
continue;
|
||||
var type = ComponentRegistry.s_runtimeIDToType[layout.componentID];
|
||||
if (type == typeof(SceneID))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var fullName = type.FullName ?? type.Name;
|
||||
var compInfo = ComponentRegistry.GetComponentInfo(layout.componentID);
|
||||
|
||||
var pData = archetype.GetComponentData(location.chunkIndex, location.rowIndex, layout.componentID);
|
||||
if (pData == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var boxed = Marshal.PtrToStructure((nint)pData, type);
|
||||
if (boxed == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
RemapEntityFieldsToLocal(boxed, type, reverseMap);
|
||||
|
||||
writer.WritePropertyName(fullName);
|
||||
JsonSerializer.Serialize(writer, boxed, type, s_jsonOptions);
|
||||
}
|
||||
|
||||
var boxed = Marshal.PtrToStructure((nint)pData, type);
|
||||
if (boxed == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
boxed = RemapEntityFieldsToLocal(boxed, type, reverseMap);
|
||||
|
||||
writer.WritePropertyName(fullName);
|
||||
JsonSerializer.Serialize(writer, boxed, type, s_jsonOptions);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
@@ -535,57 +568,71 @@ internal class SceneSerializationService : IDisposable
|
||||
writer.WriteEndObject();
|
||||
writer.Flush();
|
||||
|
||||
File.WriteAllBytes(filePath, stream.ToArray());
|
||||
return stream.ToArray();
|
||||
}
|
||||
|
||||
private static List<Entity> SortEntitiesByHierarchy(World world, List<Entity> entities)
|
||||
private static UnsafeList<Entity> SortEntitiesByHierarchy(World world, ReadOnlySpan<Entity> entities, AllocationHandle allocationHandle)
|
||||
{
|
||||
var entitySet = new HashSet<Entity>(entities);
|
||||
var roots = new List<Entity>();
|
||||
var childrenMap = new Dictionary<Entity, List<Entity>>();
|
||||
using var scope = AllocationManager.CreateStackScope();
|
||||
|
||||
foreach (var entity in entities)
|
||||
using var entitySet = new UnsafeHashSet<Entity>(entities.Length, scope.AllocationHandle);
|
||||
using var roots = new UnsafeList<Entity>(32, scope.AllocationHandle);
|
||||
var childrenMap = new UnsafeHashMap<Entity, UnsafeList<Entity>>(32, scope.AllocationHandle);
|
||||
|
||||
try
|
||||
{
|
||||
if (!world.EntityManager.HasComponent<Hierarchy>(entity))
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
roots.Add(entity);
|
||||
continue;
|
||||
}
|
||||
|
||||
ref var hierarchy = ref world.EntityManager.GetComponent<Hierarchy>(entity);
|
||||
if (hierarchy.parent.IsValid && entitySet.Contains(hierarchy.parent))
|
||||
{
|
||||
if (!childrenMap.TryGetValue(hierarchy.parent, out var list))
|
||||
if (!world.EntityManager.HasComponent<Hierarchy>(entity))
|
||||
{
|
||||
list = new List<Entity>();
|
||||
childrenMap[hierarchy.parent] = list;
|
||||
roots.Add(entity);
|
||||
continue;
|
||||
}
|
||||
|
||||
list.Add(entity);
|
||||
ref var hierarchy = ref world.EntityManager.GetComponent<Hierarchy>(entity);
|
||||
if (hierarchy.parent.IsValid && entitySet.Contains(hierarchy.parent))
|
||||
{
|
||||
ref var list = ref childrenMap.GetValueRefOrAddDefault(hierarchy.parent, out var exist);
|
||||
if (!exist)
|
||||
{
|
||||
list = new UnsafeList<Entity>(4, allocationHandle);
|
||||
}
|
||||
|
||||
list.Add(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
roots.Add(entity);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
var sorted = new UnsafeList<Entity>(entities.Length, allocationHandle);
|
||||
foreach (var root in roots)
|
||||
{
|
||||
roots.Add(entity);
|
||||
AddEntityAndDescendants(ref sorted, root, in childrenMap);
|
||||
}
|
||||
}
|
||||
|
||||
var sorted = new List<Entity>(entities.Count);
|
||||
foreach (var root in roots)
|
||||
return sorted;
|
||||
}
|
||||
finally
|
||||
{
|
||||
AddEntityAndDescendants(sorted, root, childrenMap);
|
||||
}
|
||||
foreach (var kvp in childrenMap)
|
||||
{
|
||||
kvp.Value.Dispose();
|
||||
}
|
||||
|
||||
return sorted;
|
||||
childrenMap.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddEntityAndDescendants(List<Entity> sorted, Entity entity, Dictionary<Entity, List<Entity>> childrenMap)
|
||||
private static void AddEntityAndDescendants(ref UnsafeList<Entity> sorted, Entity entity, ref readonly UnsafeHashMap<Entity, UnsafeList<Entity>> childrenMap)
|
||||
{
|
||||
sorted.Add(entity);
|
||||
if (childrenMap.TryGetValue(entity, out var children))
|
||||
{
|
||||
foreach (var child in children)
|
||||
{
|
||||
AddEntityAndDescendants(sorted, child, childrenMap);
|
||||
AddEntityAndDescendants(ref sorted, child, in childrenMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
567
src/Editor/Ghost.Editor.Core/Services/UndoService.cs
Normal file
567
src/Editor/Ghost.Editor.Core/Services/UndoService.cs
Normal file
@@ -0,0 +1,567 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Core.Collections;
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Ghost.Entities;
|
||||
|
||||
namespace Ghost.Editor.Core.Services;
|
||||
|
||||
public enum LifecycleEvent { Created, Destroyed }
|
||||
|
||||
public interface IUndoService
|
||||
{
|
||||
IEnumerable<UndoOperation> UndoOperations { get; }
|
||||
IEnumerable<UndoOperation> RedoOperations { get; }
|
||||
|
||||
int GlobalVersion { get; }
|
||||
|
||||
event Action? UndoRedoPerformed;
|
||||
|
||||
void RecordObject(GhostObject obj, string actionName);
|
||||
void RecordEntityComponent(ComponentNode node, string actionName);
|
||||
void RecordEntityStructure(EntityNode node, string actionName);
|
||||
void RecordEntityLifecycle(EntityNode node, LifecycleEvent type);
|
||||
|
||||
void BeginTransaction(string name);
|
||||
void EndTransaction();
|
||||
void PerformUndo();
|
||||
void PerformRedo();
|
||||
}
|
||||
|
||||
public abstract class UndoOperation
|
||||
{
|
||||
public int GroupId { get; set; }
|
||||
public string ActionName { get; set; } = string.Empty;
|
||||
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
|
||||
|
||||
// Creates an operation that holds the *current* state, so it can be pushed to Redo.
|
||||
public abstract UndoOperation CreateReciprocal(IEditorWorldService worldService);
|
||||
public abstract void Revert(IEditorWorldService worldService);
|
||||
|
||||
public virtual bool CanMerge(UndoOperation other) => false;
|
||||
}
|
||||
|
||||
public class ObjectStateOperation : UndoOperation
|
||||
{
|
||||
public Guid InstanceID { get; set; }
|
||||
public byte[] State { get; set; } = Array.Empty<byte>();
|
||||
|
||||
public override UndoOperation CreateReciprocal(IEditorWorldService worldService)
|
||||
{
|
||||
var obj = GhostObject.Find(InstanceID);
|
||||
var reciprocal = new ObjectStateOperation { GroupId = GroupId, ActionName = ActionName, InstanceID = InstanceID };
|
||||
if (obj != null)
|
||||
{
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new BinaryWriter(ms);
|
||||
obj.SerializeState(writer);
|
||||
reciprocal.State = ms.ToArray();
|
||||
}
|
||||
return reciprocal;
|
||||
}
|
||||
|
||||
public override void Revert(IEditorWorldService worldService)
|
||||
{
|
||||
var obj = GhostObject.Find(InstanceID);
|
||||
if (obj != null)
|
||||
{
|
||||
using var ms = new MemoryStream(State);
|
||||
using var reader = new BinaryReader(ms);
|
||||
obj.DeserializeState(reader);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanMerge(UndoOperation other)
|
||||
{
|
||||
if (other is ObjectStateOperation op)
|
||||
{
|
||||
return op.InstanceID == InstanceID && op.GroupId == GroupId;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class EntityComponentOperation : UndoOperation
|
||||
{
|
||||
public Guid InstanceID { get; set; }
|
||||
public Entity Entity { get; set; }
|
||||
public int ComponentId { get; set; }
|
||||
public byte[] ComponentData { get; set; } = Array.Empty<byte>();
|
||||
|
||||
public unsafe override UndoOperation CreateReciprocal(IEditorWorldService worldService)
|
||||
{
|
||||
var node = GhostObject.Find(InstanceID) as EntityNode;
|
||||
var targetEntity = node?.Entity ?? Entity;
|
||||
|
||||
var reciprocal = new EntityComponentOperation { GroupId = GroupId, ActionName = ActionName, Entity = targetEntity, InstanceID = InstanceID, ComponentId = ComponentId };
|
||||
var pComp = worldService.EditorWorld.EntityManager.GetComponent(targetEntity, new Identifier<IComponent>(ComponentId));
|
||||
if (pComp != null)
|
||||
{
|
||||
var size = ComponentRegistry.GetComponentInfo(new Identifier<IComponent>(ComponentId)).size;
|
||||
var data = new byte[size];
|
||||
fixed (byte* pDst = data)
|
||||
{
|
||||
Buffer.MemoryCopy(pComp, pDst, size, size);
|
||||
}
|
||||
reciprocal.ComponentData = data;
|
||||
}
|
||||
|
||||
return reciprocal;
|
||||
}
|
||||
|
||||
public override void Revert(IEditorWorldService worldService)
|
||||
{
|
||||
var cId = ComponentId;
|
||||
var data = ComponentData;
|
||||
var instId = InstanceID;
|
||||
var fallbackEntity = Entity;
|
||||
|
||||
worldService.Defer(() =>
|
||||
{
|
||||
var node = GhostObject.Find(instId) as EntityNode;
|
||||
var targetEntity = node?.Entity ?? fallbackEntity;
|
||||
|
||||
unsafe
|
||||
{
|
||||
var pComp = worldService.EditorWorld.EntityManager.GetComponent(targetEntity, new Identifier<IComponent>(cId));
|
||||
if (pComp != null)
|
||||
{
|
||||
fixed (byte* pSrc = data)
|
||||
{
|
||||
Buffer.MemoryCopy(pSrc, pComp, data.Length, data.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public override bool CanMerge(UndoOperation other)
|
||||
{
|
||||
if (other is EntityComponentOperation op)
|
||||
{
|
||||
if (op.Entity == Entity && op.ComponentId == ComponentId)
|
||||
{
|
||||
// Explicit transaction merge
|
||||
if (op.GroupId != 0 && op.GroupId == GroupId)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Time-based merge fallback for non-transactional continuous edits (e.g. 500ms)
|
||||
if (op.GroupId == 0)
|
||||
{
|
||||
return Math.Abs((op.Timestamp - Timestamp).TotalMilliseconds) < 500;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class EntityStructureOperation : UndoOperation
|
||||
{
|
||||
public Guid InstanceID { get; set; }
|
||||
public Entity Entity { get; set; }
|
||||
public int ArchetypeID { get; set; }
|
||||
public byte[] ComponentData { get; set; } = Array.Empty<byte>();
|
||||
public byte[] SharedData { get; set; } = Array.Empty<byte>();
|
||||
public int SharedDataHash { get; set; }
|
||||
|
||||
public unsafe static EntityStructureOperation Capture(IEditorWorldService worldService, EntityNode node)
|
||||
{
|
||||
var entity = node.Entity;
|
||||
var op = new EntityStructureOperation { Entity = entity, InstanceID = node.InstanceID };
|
||||
var locRes = worldService.EditorWorld.EntityManager.GetEntityLocation(entity);
|
||||
if (locRes.IsSuccess)
|
||||
{
|
||||
op.ArchetypeID = locRes.Value.archetypeID;
|
||||
ref var archetype = ref worldService.EditorWorld.ComponentManager.GetArchetypeReference(op.ArchetypeID);
|
||||
ref var chunk = ref archetype.GetChunkReference(locRes.Value.chunkIndex);
|
||||
|
||||
// Compute size of all unmanaged components
|
||||
var totalSize = 0;
|
||||
for (var i = 0; i < archetype._layouts.Count; i++)
|
||||
{
|
||||
totalSize += archetype._layouts[i].size;
|
||||
}
|
||||
|
||||
var data = new byte[totalSize];
|
||||
fixed (byte* pDst = data)
|
||||
{
|
||||
var offset = 0;
|
||||
for (var i = 0; i < archetype._layouts.Count; i++)
|
||||
{
|
||||
var layout = archetype._layouts[i];
|
||||
var pSrc = chunk.GetUnsafePtr() + layout.offset + (layout.size * locRes.Value.rowIndex);
|
||||
Buffer.MemoryCopy(pSrc, pDst + offset, layout.size, layout.size);
|
||||
offset += layout.size;
|
||||
}
|
||||
}
|
||||
op.ComponentData = data;
|
||||
|
||||
if (chunk._groupIndex >= 0 && chunk._groupIndex < archetype._chunkGroups.Count)
|
||||
{
|
||||
var group = archetype._chunkGroups[chunk._groupIndex];
|
||||
op.SharedData = group.sharedData.AsSpan().ToArray();
|
||||
op.SharedDataHash = group.sharedDataHash;
|
||||
}
|
||||
}
|
||||
return op;
|
||||
}
|
||||
|
||||
public override UndoOperation CreateReciprocal(IEditorWorldService worldService)
|
||||
{
|
||||
if (GhostObject.Find(InstanceID) is not EntityNode node)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var reciprocal = Capture(worldService, node);
|
||||
reciprocal.GroupId = GroupId;
|
||||
reciprocal.ActionName = ActionName;
|
||||
return reciprocal;
|
||||
}
|
||||
|
||||
public unsafe override void Revert(IEditorWorldService worldService)
|
||||
{
|
||||
var instId = InstanceID;
|
||||
var fallbackEntity = Entity;
|
||||
var archId = ArchetypeID;
|
||||
var compData = ComponentData;
|
||||
var sharedData = SharedData;
|
||||
var sharedHash = SharedDataHash;
|
||||
|
||||
worldService.Defer(() =>
|
||||
{
|
||||
var node = GhostObject.Find(instId) as EntityNode;
|
||||
var targetEntity = node?.Entity ?? fallbackEntity;
|
||||
|
||||
var world = worldService.EditorWorld;
|
||||
var locRes = world.EntityManager.GetEntityLocation(targetEntity);
|
||||
if (!locRes.IsSuccess)
|
||||
{
|
||||
return; // Entity destroyed? Should use Lifecycle undo for that.
|
||||
}
|
||||
|
||||
if (locRes.Value.archetypeID != archId)
|
||||
{
|
||||
ref var targetArchetype = ref world.ComponentManager.GetArchetypeReference(archId);
|
||||
|
||||
// Build ComponentSetView from the target archetype
|
||||
var it = targetArchetype._signature.GetIterator();
|
||||
var components = new List<Identifier<IComponent>>();
|
||||
while (it.Next(out var compId))
|
||||
{
|
||||
components.Add(new Identifier<IComponent>(compId));
|
||||
}
|
||||
|
||||
var set = new ComponentSetView(components.ToArray(), sharedData ?? Array.Empty<byte>());
|
||||
world.EntityManager.MigrateEntity(targetEntity, set);
|
||||
}
|
||||
|
||||
|
||||
// Overwrite unmanaged memory
|
||||
locRes = world.EntityManager.GetEntityLocation(targetEntity);
|
||||
if (locRes.IsSuccess)
|
||||
{
|
||||
ref var archetype = ref world.ComponentManager.GetArchetypeReference(locRes.Value.archetypeID);
|
||||
ref var chunk = ref archetype.GetChunkReference(locRes.Value.chunkIndex);
|
||||
|
||||
fixed (byte* pSrcBase = compData)
|
||||
{
|
||||
var offset = 0;
|
||||
for (var i = 0; i < archetype._layouts.Count; i++)
|
||||
{
|
||||
var layout = archetype._layouts[i];
|
||||
var pDst = chunk.GetUnsafePtr() + layout.offset + (layout.size * locRes.Value.rowIndex);
|
||||
Buffer.MemoryCopy(pSrcBase + offset, pDst, layout.size, layout.size);
|
||||
offset += layout.size;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public override bool CanMerge(UndoOperation other)
|
||||
{
|
||||
if (other is EntityStructureOperation op)
|
||||
{
|
||||
return op.Entity == Entity && op.GroupId == GroupId;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class EntityLifecycleOperation : UndoOperation
|
||||
{
|
||||
public Entity Entity { get; set; }
|
||||
public Guid InstanceID { get; set; }
|
||||
public LifecycleEvent EventType { get; set; }
|
||||
|
||||
// State for destruction
|
||||
public int ArchetypeID { get; set; }
|
||||
public byte[] ComponentData { get; set; } = Array.Empty<byte>();
|
||||
public byte[] SharedData { get; set; } = Array.Empty<byte>();
|
||||
public int SharedDataHash { get; set; }
|
||||
|
||||
public override UndoOperation CreateReciprocal(IEditorWorldService worldService)
|
||||
{
|
||||
var reciprocal = new EntityLifecycleOperation
|
||||
{
|
||||
GroupId = GroupId,
|
||||
ActionName = ActionName,
|
||||
Entity = Entity,
|
||||
InstanceID = InstanceID,
|
||||
EventType = EventType == LifecycleEvent.Created ? LifecycleEvent.Destroyed : LifecycleEvent.Created,
|
||||
ArchetypeID = ArchetypeID,
|
||||
ComponentData = ComponentData,
|
||||
SharedData = SharedData,
|
||||
SharedDataHash = SharedDataHash
|
||||
};
|
||||
return reciprocal;
|
||||
}
|
||||
|
||||
public override void Revert(IEditorWorldService worldService)
|
||||
{
|
||||
worldService.Defer(() =>
|
||||
{
|
||||
if (EventType == LifecycleEvent.Created)
|
||||
{
|
||||
// Revert a Creation = Destroy
|
||||
var node = GhostObject.Find(InstanceID) as EntityNode;
|
||||
var targetEntity = node?.Entity ?? Entity;
|
||||
worldService.EditorWorld.EntityManager.DestroyEntity(targetEntity);
|
||||
// The InstanceID GhostObject will be naturally unlinked, handles become null
|
||||
}
|
||||
else
|
||||
{
|
||||
// Revert a Destruction = Recreate
|
||||
var newEntity = worldService.EditorWorld.EntityManager.CreateEntity();
|
||||
|
||||
// TODO: Apply the ArchetypeID, ComponentData, SharedData to the newEntity.
|
||||
// We'd add the components using the archetype signature, then memcopy the bytes.
|
||||
|
||||
// Fix the Node reference
|
||||
if (GhostObject.Find(InstanceID) is not EntityNode node)
|
||||
{
|
||||
node = new EntityNode(worldService.EditorWorld, newEntity, "Resurrected", null);
|
||||
// Force the InstanceID using backing field
|
||||
var backingField = typeof(EntityNode).GetField("<InstanceID>k__BackingField", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
|
||||
?? typeof(SceneGraphNode).GetField("<InstanceID>k__BackingField", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
||||
backingField?.SetValue(node, InstanceID);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Update the entity property of the existing node (using reflection since it's init/readonly)
|
||||
var entityField = typeof(EntityNode).GetField("<Entity>k__BackingField", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
||||
entityField?.SetValue(node, newEntity);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
internal class UndoService : IUndoService
|
||||
{
|
||||
public event Action? UndoRedoPerformed;
|
||||
|
||||
private readonly IEditorWorldService _worldService;
|
||||
private readonly RingBuffer<UndoOperation> _undoStack = new(50);
|
||||
private readonly Stack<UndoOperation> _redoStack = new();
|
||||
|
||||
private int _nextGroupId = 1;
|
||||
private int _activeGroupId = 0;
|
||||
|
||||
public int GlobalVersion { get; private set; } = 0;
|
||||
|
||||
public IEnumerable<UndoOperation> UndoOperations => _undoStack;
|
||||
public IEnumerable<UndoOperation> RedoOperations => _redoStack;
|
||||
|
||||
public UndoService(IEditorWorldService worldService)
|
||||
{
|
||||
_worldService = worldService;
|
||||
}
|
||||
|
||||
public void BeginTransaction(string name)
|
||||
{
|
||||
_activeGroupId = _nextGroupId++;
|
||||
}
|
||||
|
||||
public void EndTransaction()
|
||||
{
|
||||
_activeGroupId = 0;
|
||||
}
|
||||
|
||||
private void PushOperation(UndoOperation op)
|
||||
{
|
||||
bool isTransaction = _activeGroupId != 0;
|
||||
op.GroupId = isTransaction ? _activeGroupId : 0;
|
||||
|
||||
UndoOperation? top = _undoStack.Count > 0 ? _undoStack.Peek() : null;
|
||||
if (top != null && op.CanMerge(top))
|
||||
{
|
||||
// Extend the merge window by updating the timestamp
|
||||
top.Timestamp = op.Timestamp;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isTransaction)
|
||||
{
|
||||
op.GroupId = _nextGroupId++;
|
||||
}
|
||||
|
||||
_undoStack.Push(op);
|
||||
_redoStack.Clear(); // Any new action clears the redo stack
|
||||
GlobalVersion++;
|
||||
}
|
||||
|
||||
public void RecordObject(GhostObject obj, string actionName)
|
||||
{
|
||||
var op = new ObjectStateOperation()
|
||||
{
|
||||
ActionName = actionName,
|
||||
InstanceID = obj.InstanceID
|
||||
};
|
||||
using var ms = new MemoryStream();
|
||||
using var writer = new BinaryWriter(ms);
|
||||
obj.SerializeState(writer);
|
||||
op.State = ms.ToArray();
|
||||
PushOperation(op);
|
||||
}
|
||||
|
||||
// TODO: We may want to have unified api RecordObject that can handle everything.
|
||||
public unsafe void RecordEntityComponent(ComponentNode node, string actionName)
|
||||
{
|
||||
var op = new EntityComponentOperation
|
||||
{
|
||||
ActionName = actionName,
|
||||
Entity = node.EntityNode.Entity,
|
||||
ComponentId = node.Descriptor.ComponentId,
|
||||
InstanceID = node.EntityNode.InstanceID
|
||||
};
|
||||
|
||||
var pComp = node.GetComponentPointer();
|
||||
var size = node.Descriptor.Size;
|
||||
var data = new byte[size];
|
||||
|
||||
fixed (byte* pDst = data)
|
||||
{
|
||||
Buffer.MemoryCopy(pComp, pDst, size, size);
|
||||
}
|
||||
|
||||
op.ComponentData = data;
|
||||
|
||||
PushOperation(op);
|
||||
}
|
||||
|
||||
public void RecordEntityStructure(EntityNode node, string actionName)
|
||||
{
|
||||
var op = EntityStructureOperation.Capture(_worldService, node);
|
||||
op.ActionName = actionName;
|
||||
PushOperation(op);
|
||||
}
|
||||
|
||||
public void RecordEntityLifecycle(EntityNode node, LifecycleEvent type)
|
||||
{
|
||||
var op = new EntityLifecycleOperation
|
||||
{
|
||||
ActionName = type == LifecycleEvent.Created ? "Create Entity" : "Destroy Entity",
|
||||
Entity = node.Entity,
|
||||
InstanceID = node.InstanceID,
|
||||
EventType = type
|
||||
};
|
||||
|
||||
if (type == LifecycleEvent.Destroyed)
|
||||
{
|
||||
// Capture state before destruction
|
||||
var structure = EntityStructureOperation.Capture(_worldService, node);
|
||||
op.ArchetypeID = structure.ArchetypeID;
|
||||
op.ComponentData = structure.ComponentData;
|
||||
op.SharedData = structure.SharedData;
|
||||
op.SharedDataHash = structure.SharedDataHash;
|
||||
}
|
||||
|
||||
PushOperation(op);
|
||||
}
|
||||
|
||||
public void PerformUndo()
|
||||
{
|
||||
if (_undoStack.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var targetGroup = _undoStack.Peek().GroupId;
|
||||
var toUndo = new List<UndoOperation>();
|
||||
|
||||
while (_undoStack.Count > 0 && _undoStack.Peek().GroupId == targetGroup)
|
||||
{
|
||||
toUndo.Add(_undoStack.Pop());
|
||||
}
|
||||
|
||||
var toRedo = new List<UndoOperation>();
|
||||
|
||||
// Revert in reverse order (which is standard for stack pop, but we popped them into a list)
|
||||
// Wait, the list has them in reverse chronological order (newest at index 0).
|
||||
// We should execute them in that order.
|
||||
foreach (var op in toUndo)
|
||||
{
|
||||
// Snapshot current state for Redo BEFORE reverting
|
||||
var reciprocal = op.CreateReciprocal(_worldService);
|
||||
toRedo.Add(reciprocal);
|
||||
|
||||
op.Revert(_worldService);
|
||||
}
|
||||
|
||||
// Push to Redo stack (we push the oldest action first so it comes off last on redo)
|
||||
toRedo.Reverse();
|
||||
foreach (var op in toRedo)
|
||||
{
|
||||
_redoStack.Push(op);
|
||||
}
|
||||
|
||||
GlobalVersion--;
|
||||
|
||||
// Flush ECS commands before UI updates
|
||||
_worldService.FlushCommands();
|
||||
|
||||
UndoRedoPerformed?.Invoke();
|
||||
}
|
||||
|
||||
public void PerformRedo()
|
||||
{
|
||||
if (_redoStack.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var targetGroup = _redoStack.Peek().GroupId;
|
||||
var toRedo = new List<UndoOperation>();
|
||||
|
||||
while (_redoStack.Count > 0 && _redoStack.Peek().GroupId == targetGroup)
|
||||
{
|
||||
toRedo.Add(_redoStack.Pop());
|
||||
}
|
||||
|
||||
toRedo.Reverse();
|
||||
|
||||
var toUndo = new List<UndoOperation>();
|
||||
|
||||
foreach (var op in toRedo)
|
||||
{
|
||||
var reciprocal = op.CreateReciprocal(_worldService);
|
||||
toUndo.Add(reciprocal);
|
||||
op.Revert(_worldService); // Revert actually means Apply in this symmetric design
|
||||
}
|
||||
|
||||
foreach (var op in toUndo)
|
||||
{
|
||||
_undoStack.Push(op);
|
||||
}
|
||||
|
||||
GlobalVersion++;
|
||||
|
||||
_worldService.FlushCommands();
|
||||
|
||||
UndoRedoPerformed?.Invoke();
|
||||
}
|
||||
}
|
||||
61
src/Editor/Ghost.Editor.Core/Utilities/BindingUtility.cs
Normal file
61
src/Editor/Ghost.Editor.Core/Utilities/BindingUtility.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Ghost.Editor.Core.Controls;
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
namespace Ghost.Editor.Core.Utilities;
|
||||
|
||||
public static class BindingUtility
|
||||
{
|
||||
public static void BindTwoWay<T>(this INotifyValueChanged<T> control, PropertyNode<T> node)
|
||||
where T : unmanaged
|
||||
{
|
||||
control.SetValueWithoutNotify(node.Value);
|
||||
control.OnValueChanged += (s, e) =>
|
||||
{
|
||||
node.ComponentNode.EntityNode.Modify();
|
||||
node.SetValueFromUI(e.NewValue);
|
||||
};
|
||||
node.OnValueChanged += control.SetValueWithoutNotify;
|
||||
}
|
||||
|
||||
public static void BindTwoWay<T, U>(this INotifyValueChanged<T> control, PropertyNode<U> node, Func<PropertyNode<U>, T> getter, Action<PropertyNode<U>, T> setter)
|
||||
where U : unmanaged
|
||||
{
|
||||
control.SetValueWithoutNotify(getter(node));
|
||||
control.OnValueChanged += (_, args) =>
|
||||
{
|
||||
node.ComponentNode.EntityNode.Modify();
|
||||
setter(node, args.NewValue);
|
||||
};
|
||||
|
||||
node.OnValueChanged += (newVal) =>
|
||||
{
|
||||
control.SetValueWithoutNotify(getter(node));
|
||||
};
|
||||
}
|
||||
|
||||
public static void BindOneWay<T>(this INotifyValueChanged<T> control, PropertyNode<T> node)
|
||||
where T : unmanaged
|
||||
{
|
||||
control.SetValueWithoutNotify(node.Value);
|
||||
node.OnValueChanged += control.SetValueWithoutNotify;
|
||||
}
|
||||
|
||||
public static void BindOneWay<T, U>(this INotifyValueChanged<T> control, PropertyNode<U> node, Func<PropertyNode<U>, T> getter)
|
||||
where U : unmanaged
|
||||
{
|
||||
node.OnValueChanged += (newVal) =>
|
||||
{
|
||||
control.SetValueWithoutNotify(getter(node));
|
||||
};
|
||||
}
|
||||
|
||||
public static void BindOneWay<T>(this FrameworkElement element, DependencyProperty dp, PropertyNode<T> node)
|
||||
where T : unmanaged
|
||||
{
|
||||
node.OnValueChanged += (newVal) =>
|
||||
{
|
||||
element.SetValue(dp, newVal);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -140,9 +140,9 @@ internal static class ShaderCompilerUtility
|
||||
options = additionalConfig.options,
|
||||
stage = ShaderStage.ComputeShader,
|
||||
};
|
||||
|
||||
|
||||
var compiled = new UnsafeArray<UnsafeArray<byte>>(descriptor.ShaderCodes.Length, allocationHandle);
|
||||
for (int i = 0; i < descriptor.ShaderCodes.Length; i++)
|
||||
for (var i = 0; i < descriptor.ShaderCodes.Length; i++)
|
||||
{
|
||||
config.shaderCode = descriptor.ShaderCodes[i].code;
|
||||
config.entryPoint = descriptor.ShaderCodes[i].entryPoint;
|
||||
@@ -150,7 +150,7 @@ internal static class ShaderCompilerUtility
|
||||
var result = shaderCompiler.Compile(ref config, allocationHandle);
|
||||
if (result.IsFailure)
|
||||
{
|
||||
for (int j = 0; j < i; j++)
|
||||
for (var j = 0; j < i; j++)
|
||||
{
|
||||
compiled[j].Dispose();
|
||||
}
|
||||
|
||||
@@ -65,9 +65,9 @@ public static class TypeCache
|
||||
private static Dictionary<nint, List<int>> FindTypesWithAttribute()
|
||||
{
|
||||
var dict = new Dictionary<nint, List<int>>();
|
||||
for (int i = 0; i < s_types.Length; i++)
|
||||
for (var i = 0; i < s_types.Length; i++)
|
||||
{
|
||||
TypeInfo? type = s_types[i];
|
||||
var type = s_types[i];
|
||||
var attrs = type.GetCustomAttributes<DiscoverableAttributeBase>(false);
|
||||
foreach (var attr in attrs)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Ghost.Editor.Core.Services;
|
||||
using Ghost.Editor.Models;
|
||||
using Ghost.Engine;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
@@ -58,10 +58,10 @@ internal static class ActivationHandler
|
||||
var opts = new AllocationManagerDesc
|
||||
{
|
||||
ArenaCapacity = 1024 * 1024 * 1024, // 1 GB. Arena using virtual memory, so this is just a reservation and won't actually consume physical memory until used.
|
||||
StackCapacity = 1024 * 1024 * 64, // 64 MB. Stack using virtual memory, so this is just a reservation and won't actually consume physical memory until used.
|
||||
StackCapacity = 64 * 1024 * 1024, // 64 MB. Stack using virtual memory, so this is just a reservation and won't actually consume physical memory until used.
|
||||
FreeListChunkSize = 64 * 1024,
|
||||
FreeListDefaultAlignment = 8,
|
||||
TLSFInitialChunkSize = 64 * 1024,
|
||||
TLSFInitialChunkSize = 32 * 1024 * 1024,
|
||||
TLSFAlignment = 8,
|
||||
};
|
||||
|
||||
@@ -69,6 +69,9 @@ internal static class ActivationHandler
|
||||
|
||||
var assetRegistry = App.GetService<IAssetRegistry>();
|
||||
var engineCore = App.GetService<EngineCore>();
|
||||
var editorTick = App.GetService<EditorTickEngine>();
|
||||
|
||||
editorTick.Start();
|
||||
|
||||
assetRegistry.OnAssetImported += (sender, e) =>
|
||||
{
|
||||
|
||||
@@ -2,12 +2,12 @@ using Ghost.Core;
|
||||
using Ghost.Editor.Core;
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Editor.Core.Services;
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Ghost.Editor.ViewModels.Controls;
|
||||
using Ghost.Editor.ViewModels.Windows;
|
||||
using Ghost.Editor.Views.Windows;
|
||||
using Ghost.Engine;
|
||||
using Ghost.Engine.Streaming;
|
||||
using Ghost.Graphics.Core;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
@@ -65,8 +65,13 @@ public partial class App : Application
|
||||
services.AddSingleton<IInspectorService, InspectorService>();
|
||||
services.AddSingleton<IPreviewService, PreviewService>();
|
||||
services.AddSingleton<IAssetRegistry, AssetRegistry>();
|
||||
services.AddSingleton<IShaderCompiler, DXCShaderCompiler>();
|
||||
services.AddSingleton<IEditorWorldService, EditorWorldService>();
|
||||
services.AddSingleton<IUndoService, UndoService>();
|
||||
services.AddSingleton<IDirtyTrackerService, DirtyTrackerService>();
|
||||
|
||||
services.AddSingleton<EditorWorldService>();
|
||||
services.AddSingleton<InspectorSyncService>();
|
||||
services.AddSingleton<EditorTickEngine>();
|
||||
services.AddSingleton<SceneSerializationService>();
|
||||
services.AddSingleton<SceneGraphSyncService>();
|
||||
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
using Ghost.Editor.Core.Inspector;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
namespace Ghost.Editor.Components;
|
||||
|
||||
//[CustomEditor(typeof(Hierarchy))]
|
||||
internal class HierarchyEditor : ComponentEditor
|
||||
{
|
||||
public void Create(ComponentObject componentObject, StackPanel container)
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(ComponentObject componentObject)
|
||||
{
|
||||
}
|
||||
|
||||
public void Destroy(ComponentObject componentObject)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using Ghost.Editor.Core;
|
||||
using Ghost.Editor.Core.Controls;
|
||||
using Ghost.Editor.Core.Inspector;
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Ghost.Engine.Components;
|
||||
using Ghost.Engine.Utilities;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
@@ -15,52 +17,60 @@ internal class LocalToWorldEditor : ComponentEditor
|
||||
private Float3Field _rotationField = null!;
|
||||
private Float3Field _scaleField = null!;
|
||||
|
||||
public override void Create(StackPanel container)
|
||||
public override void Create(Panel root, ComponentNode componentNode)
|
||||
{
|
||||
_translationField = new Float3Field();
|
||||
_rotationField = new Float3Field();
|
||||
_scaleField = new Float3Field();
|
||||
|
||||
_translationField.OnValueChanged += (s, e) =>
|
||||
{
|
||||
ref var data = ref ComponentObject.GetData<LocalToWorld>();
|
||||
data.matrix.c3.xyz = e.NewValue;
|
||||
};
|
||||
root.Children.Add(new PropertyField() { Label = "Position", Content = _translationField });
|
||||
root.Children.Add(new PropertyField() { Label = "Rotation", Content = _rotationField });
|
||||
root.Children.Add(new PropertyField() { Label = "Scale", Content = _scaleField });
|
||||
|
||||
_rotationField.OnValueChanged += (s, e) =>
|
||||
{
|
||||
ref var data = ref ComponentObject.GetData<LocalToWorld>();
|
||||
var newRotation = quaternion.EulerXYZ(e.NewValue * math.TORADIANS);
|
||||
var property = componentNode.GetProperty<float4x4>(nameof(LocalToWorld.matrix));
|
||||
|
||||
data.matrix.GetTRS(out var oldTranslation, out var _, out var oldScale);
|
||||
data.matrix = float4x4.TRS(oldTranslation, newRotation, oldScale);
|
||||
};
|
||||
_translationField.BindTwoWay(property,
|
||||
getter: node =>
|
||||
{
|
||||
return node.Value.c3.xyz;
|
||||
},
|
||||
setter: (node, val) =>
|
||||
{
|
||||
var data = node.Value;
|
||||
data.c3.xyz = val;
|
||||
node.SetValueFromUI(data);
|
||||
});
|
||||
|
||||
_scaleField.OnValueChanged += (s, e) =>
|
||||
{
|
||||
ref var data = ref ComponentObject.GetData<LocalToWorld>();
|
||||
var newScale = e.NewValue;
|
||||
_rotationField.BindTwoWay(property,
|
||||
getter: node =>
|
||||
{
|
||||
node.Value.GetTRS(out _, out var rotation, out _);
|
||||
return math.degrees(math.EulerXYZ(rotation));
|
||||
},
|
||||
setter: (node, val) =>
|
||||
{
|
||||
var data = node.Value;
|
||||
var newRotation = quaternion.EulerXYZ(val * math.TORADIANS);
|
||||
data.GetTRS(out var oldTranslation, out _, out var oldScale);
|
||||
data = float4x4.TRS(oldTranslation, newRotation, oldScale);
|
||||
node.SetValueFromUI(data);
|
||||
});
|
||||
|
||||
data.matrix.GetTRS(out var oldTranslation, out var oldRotation, out var _);
|
||||
data.matrix = float4x4.TRS(oldTranslation, oldRotation, newScale);
|
||||
};
|
||||
|
||||
container.Children.Add(new PropertyField() { Label = "Position", Content = _translationField });
|
||||
container.Children.Add(new PropertyField() { Label = "Rotation", Content = _rotationField });
|
||||
container.Children.Add(new PropertyField() { Label = "Scale", Content = _scaleField });
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
var data = ComponentObject.GetData<LocalToWorld>();
|
||||
data.matrix.GetTRS(out var position, out var rotation, out var scale);
|
||||
|
||||
_translationField.Value = position;
|
||||
_rotationField.Value = math.degrees(math.EulerXYZ(rotation));
|
||||
_scaleField.Value = scale;
|
||||
}
|
||||
|
||||
public override void Destroy()
|
||||
{
|
||||
_scaleField.BindTwoWay(property,
|
||||
getter: node =>
|
||||
{
|
||||
var matrix = node.Value;
|
||||
var scaleX = math.length(matrix.c0.xyz);
|
||||
var scaleY = math.length(matrix.c1.xyz);
|
||||
var scaleZ = math.length(matrix.c2.xyz);
|
||||
return new float3(scaleX, scaleY, scaleZ);
|
||||
},
|
||||
setter: (node, val) =>
|
||||
{
|
||||
var data = node.Value;
|
||||
data.GetTRS(out var oldTranslation, out var oldRotation, out _);
|
||||
data = float4x4.TRS(oldTranslation, oldRotation, val);
|
||||
node.SetValueFromUI(data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
using Ghost.Editor.Core;
|
||||
using Ghost.Editor.Core.Services;
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Ghost.Editor.Views.Controls;
|
||||
using Ghost.Engine.Core;
|
||||
|
||||
namespace Ghost.Editor.Views.Controls;
|
||||
namespace Ghost.Editor.ContextMenu;
|
||||
|
||||
internal partial class ContentBrowser
|
||||
internal static class ContentBrowserContextMenu
|
||||
{
|
||||
[ContextMenuItem("project-browser", "Show in Explorer")]
|
||||
private static void ShowInExplorer()
|
||||
{
|
||||
var path = LastFocused?.ViewModel.CurrentDirectoryPath;
|
||||
var path = ContentBrowser.LastFocused?.ViewModel.CurrentDirectoryPath;
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
return;
|
||||
@@ -29,7 +30,7 @@ internal partial class ContentBrowser
|
||||
{
|
||||
// TODO: Use AssetService
|
||||
|
||||
var viewModel = LastFocused?.ViewModel;
|
||||
var viewModel = ContentBrowser.LastFocused?.ViewModel;
|
||||
if (viewModel is null)
|
||||
{
|
||||
return;
|
||||
@@ -57,7 +58,7 @@ internal partial class ContentBrowser
|
||||
[ContextMenuItem("project-browser", "Create/Asset/Scene")]
|
||||
private static void CreateSceneAsset()
|
||||
{
|
||||
var viewModel = LastFocused?.ViewModel;
|
||||
var viewModel = ContentBrowser.LastFocused?.ViewModel;
|
||||
if (viewModel is null)
|
||||
{
|
||||
return;
|
||||
@@ -75,6 +76,6 @@ internal partial class ContentBrowser
|
||||
var sceneSerializationService = App.GetService<SceneSerializationService>();
|
||||
sceneSerializationService.SaveSceneFromEditorWorld(newScenePath, tempScene);
|
||||
|
||||
SceneManager.DestroyScene(tempScene, App.GetService<EditorWorldService>().EditorWorld);
|
||||
SceneManager.DestroyScene(tempScene, App.GetService<IEditorWorldService>().EditorWorld);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/Editor/Ghost.Editor/ContextMenu/EditPageMenu.cs
Normal file
34
src/Editor/Ghost.Editor/ContextMenu/EditPageMenu.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Editor.Core;
|
||||
using Ghost.Editor.Core.Services;
|
||||
using Windows.System;
|
||||
|
||||
namespace Ghost.Editor.ContextMenu;
|
||||
|
||||
internal static class EditPageContextMenu
|
||||
{
|
||||
[Shortcut(VirtualKey.S, VirtualKeyModifiers.Control)]
|
||||
[ContextMenuItem("edit-page-menu", "File/Save")]
|
||||
private static async void MenuBar_Save()
|
||||
{
|
||||
if (EditorApplication.State != EditorState.Idle)
|
||||
{
|
||||
Logger.Warning("Cannot save while the editor is busy.");
|
||||
return;
|
||||
}
|
||||
|
||||
await App.GetService<Ghost.Editor.Core.Contracts.IAssetRegistry>().SaveDirtyAssetsAsync();
|
||||
}
|
||||
|
||||
[ContextMenuItem("edit-page-menu", "Edit/Undo", priority: 1, group: 1)]
|
||||
private static void MenuBar_Undo()
|
||||
{
|
||||
App.GetService<IUndoService>().PerformUndo();
|
||||
}
|
||||
|
||||
[ContextMenuItem("edit-page-menu", "Edit/Redo", priority: 0, group: 1)]
|
||||
private static void MenuBar_Redo()
|
||||
{
|
||||
App.GetService<IUndoService>().PerformRedo();
|
||||
}
|
||||
}
|
||||
@@ -203,7 +203,6 @@
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="ContextMenu\" />
|
||||
<Folder Include="ViewModels\Pages\" />
|
||||
<Folder Include="Views\Pages\" />
|
||||
</ItemGroup>
|
||||
@@ -235,8 +234,28 @@
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release_Editor|x64'">
|
||||
<Optimize>True</Optimize>
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release_Editor|ARM64'">
|
||||
<Optimize>True</Optimize>
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug_Editor|x64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug_Editor|ARM64'">
|
||||
<DebugType>embedded</DebugType>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
||||
@@ -4,7 +4,6 @@ using Ghost.Editor.Core.Assets;
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Editor.Core.Utilities;
|
||||
using Ghost.Editor.Models;
|
||||
using Ghost.Engine;
|
||||
using Ghost.Engine.Streaming;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using System.Collections.ObjectModel;
|
||||
@@ -157,7 +156,7 @@ internal partial class ContentBrowserViewModel : ObservableObject
|
||||
CurrentDirectoryPath = Path.GetFullPath(path);
|
||||
}
|
||||
|
||||
internal (ExplorerItem?, int) OpenSelected()
|
||||
internal async ValueTask<(ExplorerItem?, int)> OpenSelected()
|
||||
{
|
||||
if (SelectedItem == null)
|
||||
{
|
||||
@@ -172,7 +171,12 @@ internal partial class ContentBrowserViewModel : ObservableObject
|
||||
}
|
||||
else
|
||||
{
|
||||
// _assetRegistry.OpenAsset(SelectedItem.FullName);
|
||||
var result = await _assetRegistry.OpenAssetAsync(SelectedItem.Path);
|
||||
if (result.IsFailure)
|
||||
{
|
||||
return (null, -1);
|
||||
}
|
||||
|
||||
return (null, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@
|
||||
</DataTemplate>
|
||||
</GridView.ItemTemplate>
|
||||
<GridView.ContextFlyout>
|
||||
<ghost:ContextFlyout Tag="project-browser" />
|
||||
<ghost:ContextFlyout ContextMenuTag="project-browser" />
|
||||
</GridView.ContextFlyout>
|
||||
</GridView>
|
||||
|
||||
|
||||
@@ -127,7 +127,7 @@ internal sealed partial class ContentBrowser : UserControl
|
||||
|
||||
ViewModel.SelectedItem = selectedItem;
|
||||
|
||||
var navigatedItem = ViewModel.OpenSelected();
|
||||
var navigatedItem = await ViewModel.OpenSelected();
|
||||
if (navigatedItem.Item1 != null)
|
||||
{
|
||||
if (navigatedItem.Item2 == 0)
|
||||
|
||||
@@ -5,15 +5,15 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="using:Ghost.Editor.Views.Controls"
|
||||
xmlns:sg="using:Ghost.Editor.Core.SceneGraph"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:sg="using:Ghost.Editor.Core.SceneGraph"
|
||||
mc:Ignorable="d">
|
||||
|
||||
<UserControl.Resources>
|
||||
<local:SceneGraphTemplateSelector
|
||||
x:Key="SceneGraphTemplateSelector"
|
||||
SceneNodeTemplate="{StaticResource SceneNodeTemplate}"
|
||||
EntityNodeTemplate="{StaticResource EntityNodeTemplate}" />
|
||||
EntityNodeTemplate="{StaticResource EntityNodeTemplate}"
|
||||
SceneNodeTemplate="{StaticResource SceneNodeTemplate}" />
|
||||
|
||||
<DataTemplate x:Key="SceneNodeTemplate" x:DataType="sg:SceneNode">
|
||||
<TreeViewItem
|
||||
@@ -22,7 +22,7 @@
|
||||
ItemsSource="{x:Bind Children, Mode=OneWay}">
|
||||
<TreeViewItem.ContextFlyout>
|
||||
<MenuFlyout>
|
||||
<MenuFlyoutItem Text="Create Entity" Click="OnCreateEntityClick" />
|
||||
<MenuFlyoutItem Click="OnCreateEntityClick" Text="Create Entity" />
|
||||
</MenuFlyout>
|
||||
</TreeViewItem.ContextFlyout>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
@@ -33,13 +33,11 @@
|
||||
</DataTemplate>
|
||||
|
||||
<DataTemplate x:Key="EntityNodeTemplate" x:DataType="sg:EntityNode">
|
||||
<TreeViewItem
|
||||
AutomationProperties.Name="{x:Bind Name, Mode=OneWay}"
|
||||
ItemsSource="{x:Bind Children, Mode=OneWay}">
|
||||
<TreeViewItem AutomationProperties.Name="{x:Bind Name, Mode=OneWay}" ItemsSource="{x:Bind Children, Mode=OneWay}">
|
||||
<TreeViewItem.ContextFlyout>
|
||||
<MenuFlyout>
|
||||
<MenuFlyoutItem Text="Create Child" Click="OnCreateChildClick" />
|
||||
<MenuFlyoutItem Text="Delete" Click="OnDeleteEntityClick" />
|
||||
<MenuFlyoutItem Click="OnCreateChildClick" Text="Create Child" />
|
||||
<MenuFlyoutItem Click="OnDeleteEntityClick" Text="Delete" />
|
||||
</MenuFlyout>
|
||||
</TreeViewItem.ContextFlyout>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
@@ -101,15 +99,15 @@
|
||||
<TreeView
|
||||
x:Name="SceneTreeView"
|
||||
Grid.Row="1"
|
||||
Padding="4,2,0,2"
|
||||
ItemTemplateSelector="{StaticResource SceneGraphTemplateSelector}"
|
||||
SelectionMode="Single"
|
||||
Margin="4,2,0,2"
|
||||
AllowDrop="True"
|
||||
CanDrag="True"
|
||||
CanDragItems="True"
|
||||
CanReorderItems="True"
|
||||
AllowDrop="True"
|
||||
DragItemsCompleted="OnTreeViewDragItemsCompleted"
|
||||
DragItemsStarting="OnTreeViewDragItemsStarting"
|
||||
DragOver="OnTreeViewDragOver"
|
||||
Drop="OnTreeViewDrop"
|
||||
KeyDown="OnTreeViewKeyDown" />
|
||||
ItemTemplateSelector="{StaticResource SceneGraphTemplateSelector}"
|
||||
KeyDown="OnTreeViewKeyDown"
|
||||
SelectionMode="Single" />
|
||||
</Grid>
|
||||
</UserControl>
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Editor.Core.Services;
|
||||
using Ghost.Editor.Core.SceneGraph;
|
||||
using Ghost.Entities;
|
||||
using Ghost.Editor.Core.Services;
|
||||
using Ghost.Core;
|
||||
using Ghost.Engine;
|
||||
using Ghost.Entities;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Input;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using System;
|
||||
|
||||
namespace Ghost.Editor.Views.Controls;
|
||||
|
||||
public sealed partial class Hierarchy : UserControl
|
||||
{
|
||||
private readonly IInspectorService _inspectorService;
|
||||
private readonly IEditorWorldService _worldService;
|
||||
private readonly SceneGraphSyncService _syncService;
|
||||
private readonly EditorWorldService _worldService;
|
||||
private EntityNode? _draggedNode;
|
||||
|
||||
public Hierarchy()
|
||||
@@ -23,8 +22,12 @@ public sealed partial class Hierarchy : UserControl
|
||||
InitializeComponent();
|
||||
|
||||
_inspectorService = App.GetService<IInspectorService>();
|
||||
|
||||
// We resolve SceneGraphSyncService here to force the DI container to instantiate it.
|
||||
// This ensures the singleton hooks into EditorWorldService events and starts populating RootNodes.
|
||||
_syncService = App.GetService<SceneGraphSyncService>();
|
||||
_worldService = App.GetService<EditorWorldService>();
|
||||
|
||||
_worldService = App.GetService<IEditorWorldService>();
|
||||
|
||||
SceneTreeView.ItemsSource = _worldService.RootNodes;
|
||||
|
||||
@@ -70,80 +73,87 @@ public sealed partial class Hierarchy : UserControl
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTreeViewDragOver(object sender, DragEventArgs e)
|
||||
private void OnTreeViewDragItemsCompleted(TreeView sender, TreeViewDragItemsCompletedEventArgs args)
|
||||
{
|
||||
e.AcceptedOperation = global::Windows.ApplicationModel.DataTransfer.DataPackageOperation.None;
|
||||
var entityNode = args.Items.Count > 0 ? args.Items[0] as EntityNode : _draggedNode;
|
||||
_draggedNode = null;
|
||||
|
||||
if (_draggedNode == null)
|
||||
if (entityNode == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var targetItem = GetAncestorTreeViewItem(e.OriginalSource as DependencyObject);
|
||||
if (targetItem == null)
|
||||
if (args.DropResult != global::Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move)
|
||||
{
|
||||
RebuildSceneGraphFromECS();
|
||||
return;
|
||||
}
|
||||
|
||||
var targetNode = targetItem.DataContext as SceneGraphNode;
|
||||
if (targetNode == null)
|
||||
if (args.NewParentItem is not SceneGraphNode newParent)
|
||||
{
|
||||
RebuildSceneGraphFromECS();
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. Can't drag onto itself
|
||||
if (_draggedNode == targetNode)
|
||||
if (newParent == entityNode)
|
||||
{
|
||||
RebuildSceneGraphFromECS();
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Can't drag onto a child of itself (cycle checking)
|
||||
if (targetNode is EntityNode targetEntityNode)
|
||||
var result = Error.None;
|
||||
|
||||
if (newParent is EntityNode parentEntityNode)
|
||||
{
|
||||
if (HierarchyUtility.IsAncestor(_worldService.EditorWorld, targetEntityNode.Entity, _draggedNode.Entity))
|
||||
if (HierarchyUtility.IsAncestor(_worldService.EditorWorld, parentEntityNode.Entity, entityNode.Entity))
|
||||
{
|
||||
RebuildSceneGraphFromECS();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
e.AcceptedOperation = global::Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
|
||||
}
|
||||
|
||||
private void OnTreeViewDrop(object sender, DragEventArgs e)
|
||||
{
|
||||
if (_draggedNode == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var targetItem = GetAncestorTreeViewItem(e.OriginalSource as DependencyObject);
|
||||
if (targetItem == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var targetNode = targetItem.DataContext as SceneGraphNode;
|
||||
if (targetNode == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_draggedNode == targetNode)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (targetNode is EntityNode targetEntityNode)
|
||||
{
|
||||
if (!HierarchyUtility.IsAncestor(_worldService.EditorWorld, targetEntityNode.Entity, _draggedNode.Entity))
|
||||
var currentParent = GetCurrentParent(entityNode);
|
||||
if (currentParent == parentEntityNode.Entity)
|
||||
{
|
||||
_worldService.SetParent(_draggedNode.Entity, targetEntityNode.Entity);
|
||||
RebuildSceneGraphFromECS();
|
||||
return;
|
||||
}
|
||||
|
||||
result = _worldService.SetParent(entityNode.Entity, parentEntityNode.Entity);
|
||||
}
|
||||
else if (newParent is SceneNode sceneNode)
|
||||
{
|
||||
var currentParent = GetCurrentParent(entityNode);
|
||||
var sceneChanged = _worldService.GetEntitySceneID(entityNode.Entity) != sceneNode.Scene.ID;
|
||||
if (!currentParent.IsValid && !sceneChanged)
|
||||
{
|
||||
RebuildSceneGraphFromECS();
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentParent.IsValid)
|
||||
{
|
||||
result = _worldService.RemoveParent(entityNode.Entity);
|
||||
if (result != Error.None)
|
||||
{
|
||||
RebuildSceneGraphFromECS();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (sceneChanged)
|
||||
{
|
||||
_worldService.ChangeEntityScene(entityNode.Entity, sceneNode.Scene.ID);
|
||||
}
|
||||
}
|
||||
else if (targetNode is SceneNode sceneNode)
|
||||
else
|
||||
{
|
||||
_worldService.RemoveParent(_draggedNode.Entity);
|
||||
_worldService.ChangeEntityScene(_draggedNode.Entity, sceneNode.Scene.ID);
|
||||
RebuildSceneGraphFromECS();
|
||||
return;
|
||||
}
|
||||
|
||||
if (result != Error.None)
|
||||
{
|
||||
RebuildSceneGraphFromECS();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +170,7 @@ public sealed partial class Hierarchy : UserControl
|
||||
if (sender is MenuFlyoutItem menuItem && menuItem.DataContext is EntityNode entityNode)
|
||||
{
|
||||
var sceneID = _worldService.GetEntitySceneID(entityNode.Entity);
|
||||
if (sceneID != Ghost.Engine.Core.Scene.INVALID_ID)
|
||||
if (sceneID != Engine.Core.Scene.INVALID_ID)
|
||||
{
|
||||
_worldService.CreateEntity("Entity", sceneID, parent: entityNode.Entity);
|
||||
}
|
||||
@@ -175,17 +185,38 @@ public sealed partial class Hierarchy : UserControl
|
||||
}
|
||||
}
|
||||
|
||||
private TreeViewItem? GetAncestorTreeViewItem(DependencyObject? current)
|
||||
private Entity GetCurrentParent(EntityNode entityNode)
|
||||
{
|
||||
while (current != null)
|
||||
if (!_worldService.EditorWorld.EntityManager.HasComponent<Ghost.Engine.Components.Hierarchy>(entityNode.Entity))
|
||||
{
|
||||
if (current is TreeViewItem item)
|
||||
{
|
||||
return item;
|
||||
}
|
||||
current = VisualTreeHelper.GetParent(current);
|
||||
return Entity.Invalid;
|
||||
}
|
||||
|
||||
return _worldService.EditorWorld.EntityManager.GetComponent<Ghost.Engine.Components.Hierarchy>(entityNode.Entity).parent;
|
||||
}
|
||||
|
||||
private void RebuildSceneGraphFromECS()
|
||||
{
|
||||
var names = new Dictionary<Entity, string>();
|
||||
foreach (var sceneNode in _worldService.RootNodes)
|
||||
{
|
||||
CaptureEntityNames(sceneNode, names);
|
||||
}
|
||||
|
||||
_worldService.RebuildSceneGraph(names);
|
||||
}
|
||||
|
||||
private static void CaptureEntityNames(SceneGraphNode node, Dictionary<Entity, string> names)
|
||||
{
|
||||
if (node is EntityNode entityNode)
|
||||
{
|
||||
names[entityNode.Entity] = entityNode.Name;
|
||||
}
|
||||
|
||||
foreach (var child in node.Children)
|
||||
{
|
||||
CaptureEntityNames(child, names);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void OnUnloaded(object sender, RoutedEventArgs e)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:inspector="using:Ghost.Editor.Core.Inspector"
|
||||
xmlns:local="using:Ghost.Editor.Views.Controls"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d">
|
||||
@@ -28,14 +29,20 @@
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<FontIcon
|
||||
<ContentControl
|
||||
x:Name="IconPresenter"
|
||||
Grid.Column="0"
|
||||
FontSize="18"
|
||||
Glyph="" />
|
||||
<TextBox
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Center"
|
||||
HorizontalContentAlignment="Stretch" />
|
||||
|
||||
<ContentControl
|
||||
x:Name="HeaderPresenter"
|
||||
Grid.Column="1"
|
||||
FontSize="14"
|
||||
Text="Name" />
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalAlignment="Center"
|
||||
HorizontalContentAlignment="Stretch" />
|
||||
|
||||
<DropDownButton
|
||||
Grid.Column="2"
|
||||
Padding="2"
|
||||
@@ -43,8 +50,6 @@
|
||||
<DropDownButton.Flyout>
|
||||
<MenuFlyout Placement="Bottom">
|
||||
<MenuFlyoutItem Text="Send" />
|
||||
<MenuFlyoutItem Text="Reply" />
|
||||
<MenuFlyoutItem Text="Reply All" />
|
||||
</MenuFlyout>
|
||||
</DropDownButton.Flyout>
|
||||
<FontIcon FontSize="12" Glyph="" />
|
||||
@@ -52,56 +57,12 @@
|
||||
</Grid>
|
||||
|
||||
<!-- Content -->
|
||||
<Grid Grid.Row="1">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" MaxHeight="150" />
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Grid Grid.Row="0" Padding="8,2">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBlock
|
||||
Grid.Column="0"
|
||||
VerticalAlignment="Center"
|
||||
Style="{StaticResource BodyStrongTextBlockStyle}"
|
||||
Text="Components" />
|
||||
<Button Grid.Column="1" Style="{ThemeResource ToolbarButton}">
|
||||
<FontIcon FontSize="{StaticResource ToolbarFontIconFontSize}" Glyph="" />
|
||||
</Button>
|
||||
<Button Grid.Column="2" Style="{ThemeResource ToolbarButton}">
|
||||
<FontIcon FontSize="{StaticResource ToolbarFontIconFontSize}" Glyph="" />
|
||||
</Button>
|
||||
</Grid>
|
||||
|
||||
<AutoSuggestBox
|
||||
Grid.Row="1"
|
||||
Margin="8,0"
|
||||
PlaceholderText="Search components..." />
|
||||
|
||||
<!-- Components List -->
|
||||
<ListView
|
||||
Grid.Row="2"
|
||||
Padding="4,2,0,2"
|
||||
SelectionMode="Extended">
|
||||
<TextBlock Text="Test" />
|
||||
<TextBlock Text="Test" />
|
||||
<TextBlock Text="Test" />
|
||||
</ListView>
|
||||
|
||||
<!-- Component Properties for Selected Component -->
|
||||
<ScrollView
|
||||
Grid.Row="3"
|
||||
BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
|
||||
BorderThickness="0,1,0,0">
|
||||
<ItemsRepeater />
|
||||
</ScrollView>
|
||||
</Grid>
|
||||
<ScrollView x:Name="ContentScrollView" Grid.Row="1">
|
||||
<StackPanel
|
||||
x:Name="InspectorContentContainer"
|
||||
Padding="0,4,0,12"
|
||||
Orientation="Vertical"
|
||||
Spacing="4" />
|
||||
</ScrollView>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
|
||||
@@ -1,27 +1,89 @@
|
||||
using Ghost.Editor.Core.Contracts;
|
||||
using Ghost.Editor.Core.Services;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Controls.Primitives;
|
||||
using Microsoft.UI.Xaml.Data;
|
||||
using Microsoft.UI.Xaml.Input;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Microsoft.UI.Xaml.Navigation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Windows.Foundation;
|
||||
using Windows.Foundation.Collections;
|
||||
|
||||
// To learn more about WinUI, the WinUI project structure,
|
||||
// and more about our project templates, see: http://aka.ms/winui-project-info.
|
||||
|
||||
namespace Ghost.Editor.Views.Controls;
|
||||
|
||||
public sealed partial class Inspector : UserControl
|
||||
{
|
||||
private readonly IInspectorService _inspectorService;
|
||||
private readonly InspectorSyncService _syncService;
|
||||
|
||||
private IInspectorModel? _currentModel;
|
||||
|
||||
public Inspector()
|
||||
{
|
||||
InitializeComponent();
|
||||
_inspectorService = App.GetService<IInspectorService>();
|
||||
_syncService = App.GetService<InspectorSyncService>();
|
||||
|
||||
_inspectorService.OnSelectionChanged += InspectorService_OnSelectionChanged;
|
||||
Loaded += Inspector_Loaded;
|
||||
Unloaded += Inspector_Unloaded;
|
||||
|
||||
if (_inspectorService.Selected != null)
|
||||
{
|
||||
BuildInspector(_inspectorService.Selected);
|
||||
}
|
||||
}
|
||||
|
||||
private void Inspector_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_syncService.Start();
|
||||
}
|
||||
|
||||
private void Inspector_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_syncService.Unbind();
|
||||
_currentModel?.Dispose();
|
||||
_currentModel = null;
|
||||
}
|
||||
|
||||
private void InspectorService_OnSelectionChanged(object? sender, InspectorSelectionChangedEventArgs e)
|
||||
{
|
||||
BuildInspector(e.Selected);
|
||||
}
|
||||
|
||||
private void BuildInspector(IInspectable? inspectable)
|
||||
{
|
||||
// Cleanup old
|
||||
_syncService.Unbind();
|
||||
_currentModel?.Dispose();
|
||||
_currentModel = null;
|
||||
InspectorContentContainer.Children.Clear();
|
||||
|
||||
if (inspectable == null)
|
||||
{
|
||||
IconPresenter.Content = null;
|
||||
HeaderPresenter.Content = null;
|
||||
return;
|
||||
}
|
||||
|
||||
// Set header
|
||||
var icon = inspectable.CreateIcon();
|
||||
if (icon != null)
|
||||
{
|
||||
IconPresenter.Content = new IconSourceElement { IconSource = icon };
|
||||
}
|
||||
else
|
||||
{
|
||||
IconPresenter.Content = new FontIcon { Glyph = "\uF158", FontSize = 18 };
|
||||
}
|
||||
|
||||
HeaderPresenter.Content = inspectable.CreateHeader();
|
||||
|
||||
// Build body
|
||||
_currentModel = inspectable.CreateInspectorModel();
|
||||
if (_currentModel != null)
|
||||
{
|
||||
InspectorContentContainer.Children.Add(_currentModel.BuildUI());
|
||||
|
||||
if (_currentModel is ISyncableInspectorModel syncableModel)
|
||||
{
|
||||
_syncService.Bind(syncableModel);
|
||||
syncableModel.Sync(); // Initial sync
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:controls="using:Ghost.Editor.Views.Controls"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:ghost="using:Ghost.Editor.Core.Controls"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
Background="{ThemeResource LayerFillColorDefaultBrush}"
|
||||
NavigationCacheMode="Enabled"
|
||||
@@ -48,7 +49,7 @@
|
||||
|
||||
<Border Height="12" Style="{StaticResource VerticalDivider}" />
|
||||
|
||||
<MenuBar>
|
||||
<!--<MenuBar x:Name="TopMenuBar">
|
||||
<MenuBarItem Title="File">
|
||||
<MenuFlyoutItem Text="New" />
|
||||
<MenuFlyoutItem Text="Open..." />
|
||||
@@ -58,6 +59,7 @@
|
||||
|
||||
<MenuBarItem Title="Edit">
|
||||
<MenuFlyoutItem Text="Undo" />
|
||||
<MenuFlyoutItem Text="Redo" />
|
||||
<MenuFlyoutItem Text="Cut" />
|
||||
<MenuFlyoutItem Text="Copy" />
|
||||
<MenuFlyoutItem Text="Paste" />
|
||||
@@ -66,7 +68,8 @@
|
||||
<MenuBarItem Title="Help">
|
||||
<MenuFlyoutItem Text="About" />
|
||||
</MenuBarItem>
|
||||
</MenuBar>
|
||||
</MenuBar>-->
|
||||
<ghost:MenuContextBar ContextMenuTag="edit-page-menu" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Ghost.Editor.Views.Controls;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Ghost.Editor.Views.Pages;
|
||||
|
||||
@@ -15,6 +16,17 @@ public sealed partial class EditPage : Page
|
||||
ContentBrowserPresenter.Content = GetContentBrowser();
|
||||
}
|
||||
|
||||
private static MenuFlyoutItem CreateNewMenuItem(string name, MethodInfo methodInfo)
|
||||
{
|
||||
var menuItem = new MenuFlyoutItem { Text = name };
|
||||
menuItem.Click += (s, e) =>
|
||||
{
|
||||
methodInfo.Invoke(null, null);
|
||||
};
|
||||
|
||||
return menuItem;
|
||||
}
|
||||
|
||||
private ContentBrowser GetContentBrowser()
|
||||
{
|
||||
return _contentBrowser ??= new ContentBrowser();
|
||||
|
||||
@@ -46,15 +46,7 @@
|
||||
<Project Path="Runtime/Ghost.Graphics/Ghost.Graphics.csproj" />
|
||||
</Folder>
|
||||
<Folder Name="/Test/">
|
||||
<Project Path="Test/Ghost.Entities.Test/Ghost.Entities.Test.csproj" />
|
||||
<Project Path="Test/Ghost.Graphics.Test/Ghost.Graphics.Test.csproj">
|
||||
<Platform Solution="*|ARM64" Project="ARM64" />
|
||||
<Platform Solution="*|x64" Project="x64" />
|
||||
<Platform Solution="*|x86" Project="x86" />
|
||||
<Deploy />
|
||||
</Project>
|
||||
<Project Path="Test/Ghost.MicroTest/Ghost.MicroTest.csproj" Id="8c8ffa4b-e1e4-46a1-9221-7b508a109edd" />
|
||||
<Project Path="Test/Ghost.Shader.Test/Ghost.Shader.Test.csproj" />
|
||||
<Project Path="Test/Ghost.TestCore/Ghost.TestCore.csproj" />
|
||||
<Project Path="Test/Ghost.UnitTest/Ghost.UnitTest.csproj" Id="4da45668-456b-4dcc-acd8-6bfe154e6837">
|
||||
<Platform Solution="*|ARM64" Project="ARM64" />
|
||||
|
||||
45
src/Runtime/Ghost.Core/Attributes/InspectorAttributes.cs
Normal file
45
src/Runtime/Ghost.Core/Attributes/InspectorAttributes.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace Ghost.Core.Attributes;
|
||||
|
||||
/// <summary>
|
||||
/// Marks a field as read-only in the inspector.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public sealed class ReadOnlyInInspectorAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hides a field from the inspector entirely.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public sealed class HideInInspectorAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the display name for a field in the inspector.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public sealed class InspectorNameAttribute : Attribute
|
||||
{
|
||||
public string Name { get; }
|
||||
|
||||
public InspectorNameAttribute(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Groups fields under a collapsible header in the inspector.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public sealed class InspectorGroupAttribute : Attribute
|
||||
{
|
||||
public string GroupName { get; }
|
||||
|
||||
public InspectorGroupAttribute(string groupName)
|
||||
{
|
||||
GroupName = groupName;
|
||||
}
|
||||
}
|
||||
123
src/Runtime/Ghost.Core/Collections/RingBuffer.cs
Normal file
123
src/Runtime/Ghost.Core/Collections/RingBuffer.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Ghost.Core.Collections;
|
||||
|
||||
public class RingBuffer<T> : IEnumerable<T>
|
||||
{
|
||||
public struct Enumerator : IEnumerator<T>
|
||||
{
|
||||
private readonly RingBuffer<T> _ringBuffer;
|
||||
private int _index;
|
||||
public Enumerator(RingBuffer<T> ringBuffer)
|
||||
{
|
||||
_ringBuffer = ringBuffer;
|
||||
_index = -1;
|
||||
}
|
||||
|
||||
public readonly T Current => _ringBuffer._buffer[(_ringBuffer._head + _index) % _ringBuffer._buffer.Length];
|
||||
readonly object? IEnumerator.Current => Current;
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (_index + 1 >= _ringBuffer._count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_index++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_index = -1;
|
||||
}
|
||||
|
||||
public readonly void Dispose()
|
||||
{
|
||||
// No resources to dispose
|
||||
}
|
||||
}
|
||||
|
||||
private readonly T[] _buffer;
|
||||
private int _head;
|
||||
private int _count;
|
||||
|
||||
public int Count => _count;
|
||||
|
||||
public RingBuffer(int capacity)
|
||||
{
|
||||
_buffer = new T[capacity];
|
||||
}
|
||||
|
||||
public void Push(T item)
|
||||
{
|
||||
if (_count < _buffer.Length)
|
||||
{
|
||||
_buffer[(_head + _count) % _buffer.Length] = item;
|
||||
_count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
_buffer[_head] = item;
|
||||
_head = (_head + 1) % _buffer.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public T Pop()
|
||||
{
|
||||
if (_count == 0) throw new InvalidOperationException("Ring buffer is empty.");
|
||||
_count--;
|
||||
var item = _buffer[(_head + _count) % _buffer.Length];
|
||||
_buffer[(_head + _count) % _buffer.Length] = default!; // Clear reference
|
||||
return item;
|
||||
}
|
||||
|
||||
public bool TryPop(out T? item)
|
||||
{
|
||||
if (_count == 0)
|
||||
{
|
||||
item = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
_count--;
|
||||
item = _buffer[(_head + _count) % _buffer.Length];
|
||||
_buffer[(_head + _count) % _buffer.Length] = default!; // Clear reference
|
||||
return true;
|
||||
}
|
||||
|
||||
public T Peek()
|
||||
{
|
||||
if (_count == 0) throw new InvalidOperationException("Ring buffer is empty.");
|
||||
return _buffer[(_head + _count - 1) % _buffer.Length];
|
||||
}
|
||||
|
||||
public bool TryPeek(out T? item)
|
||||
{
|
||||
if (_count == 0)
|
||||
{
|
||||
item = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
item = _buffer[(_head + _count - 1) % _buffer.Length];
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_head = 0;
|
||||
_count = 0;
|
||||
Array.Clear(_buffer, 0, _buffer.Length);
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return new Enumerator(this);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
}
|
||||
@@ -17,10 +17,12 @@
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Release_Editor'">
|
||||
<DefineConstants>$(DefineConstants);MHP_ENABLE_SAFETY_CHECKS</DefineConstants>
|
||||
<Optimize>True</Optimize>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)'=='Release_Dev'">
|
||||
<DefineConstants>$(DefineConstants);MHP_ENABLE_SAFETY_CHECKS</DefineConstants>
|
||||
<Optimize>True</Optimize>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Ghost.Core;
|
||||
|
||||
@@ -217,7 +216,7 @@ public static class Logger
|
||||
var messageStr = message?.ToString() ?? "null";
|
||||
s_logger.Log(messageStr, LogLevel.Error);
|
||||
#if DEBUG
|
||||
System.Diagnostics.Debug.Fail(messageStr);
|
||||
throw new Exception(messageStr);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -227,7 +226,7 @@ public static class Logger
|
||||
{
|
||||
s_logger.Log(message, LogLevel.Error);
|
||||
#if DEBUG
|
||||
System.Diagnostics.Debug.Fail(message);
|
||||
throw new Exception(message);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -238,7 +237,7 @@ public static class Logger
|
||||
var message = string.Format(format, args);
|
||||
s_logger.Log(message, LogLevel.Error);
|
||||
#if DEBUG
|
||||
System.Diagnostics.Debug.Fail(message);
|
||||
throw new Exception(message);
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -249,7 +248,7 @@ public static class Logger
|
||||
{
|
||||
s_logger.Log(ex);
|
||||
#if DEBUG
|
||||
System.Diagnostics.Debug.Fail(ex.Message);
|
||||
throw ex;
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -298,12 +297,7 @@ public static class Logger
|
||||
#if DEBUG
|
||||
if (!condition)
|
||||
{
|
||||
System.Diagnostics.Debug.Fail(message ?? "Assertion failed.");
|
||||
}
|
||||
#elif GHOST_EDITOR
|
||||
if (!condition)
|
||||
{
|
||||
throw new InvalidOperationException(message ?? "Assertion failed.");
|
||||
throw new Exception(message ?? "Assertion failed.");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
|
||||
@@ -90,7 +90,7 @@ public sealed class CastMemoryManager<TFrom, TTo> : MemoryManager<TTo>
|
||||
|
||||
unsafe
|
||||
{
|
||||
int byteOffset = elementIndex * Unsafe.SizeOf<TTo>();
|
||||
var byteOffset = elementIndex * Unsafe.SizeOf<TTo>();
|
||||
void* pointer = (byte*)_innerHandle.Pointer + byteOffset;
|
||||
|
||||
return new MemoryHandle(pointer, default, this);
|
||||
|
||||
@@ -112,9 +112,9 @@ public static class StreamUtility
|
||||
return reader.BaseStream.ReadMemory(reader.BaseStream.Length - reader.BaseStream.Position, allocationHandle);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ReadSpan<T>(this BinaryReader reader, Span<T> data)
|
||||
where T : struct
|
||||
where T : struct
|
||||
{
|
||||
reader.ReadExactly(MemoryMarshal.AsBytes(data));
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ namespace Ghost.Engine;
|
||||
|
||||
public enum SceneLoadingType
|
||||
{
|
||||
Single = 0,
|
||||
Additive = 1,
|
||||
Single = 0,
|
||||
Additive = 1,
|
||||
}
|
||||
|
||||
public enum ShadowCastingMode : uint
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Ghost.Core.Attributes;
|
||||
using Ghost.Engine.Editor;
|
||||
using Ghost.Entities;
|
||||
using System.Runtime.CompilerServices;
|
||||
@@ -7,8 +8,11 @@ namespace Ghost.Engine.Components;
|
||||
[HideEditor]
|
||||
public struct Hierarchy : IComponentData
|
||||
{
|
||||
[ReadOnlyInInspector]
|
||||
public Entity parent;
|
||||
[ReadOnlyInInspector]
|
||||
public Entity firstChild;
|
||||
[ReadOnlyInInspector]
|
||||
public Entity nextSibling;
|
||||
|
||||
public static Hierarchy Root
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using Ghost.Core.Attributes;
|
||||
using Ghost.Entities;
|
||||
|
||||
namespace Ghost.Engine.Components;
|
||||
|
||||
public struct SceneID : ISharedComponent
|
||||
{
|
||||
[ReadOnlyInInspector]
|
||||
public ushort value;
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user