Refactor project structure and improve performance
Changed the `ProjectRepository` class to be static for easier usage. Changed `ProjectService` constants to public properties for accessibility. Changed `App.xaml` to consolidate theme resources into `Override.xaml`. Changed `App.xaml.cs` to implement an `AppStateMachine` for better state management. Changed `ConsolePage` and `HierarchyPage` to utilize the new ViewModel structure. Changed `ProjectPage` to use the `ExplorerItem` model for asset display. Changed `Entity` and `EntityManager` to enhance component management with a new `IComponentData` interface. Changed the `Logger` class to introduce structured logging functionality. Changed the system architecture to support dependency management for better organization. Changed the `QueryEnumerable` class to allow for more flexible entity queries. Changed the `TypeHandle` class to improve efficiency in retrieving type handles. Changed the `World` class to support robust world management and multiple worlds. Updated the `Test` class to demonstrate the new entity and component management system.
This commit is contained in:
@@ -1,19 +1,11 @@
|
|||||||
using Ghost.Data.Models;
|
using Ghost.Data.Models;
|
||||||
using System.Data;
|
using Ghost.Data.Resources;
|
||||||
using System.Data.SQLite;
|
using System.Data.SQLite;
|
||||||
|
|
||||||
namespace Ghost.Data.Repository;
|
namespace Ghost.Data.Repository;
|
||||||
|
|
||||||
internal class ProjectRepository : IDisposable
|
internal static class ProjectRepository
|
||||||
{
|
{
|
||||||
private readonly SQLiteConnection _connection;
|
|
||||||
|
|
||||||
public ProjectRepository(string sourceDirectory)
|
|
||||||
{
|
|
||||||
_connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, sourceDirectory));
|
|
||||||
_connection.Open();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Command
|
private static class Command
|
||||||
{
|
{
|
||||||
public const string CONNECTION_STRING = "Data Source={0}\\projects.db;Version=3;";
|
public const string CONNECTION_STRING = "Data Source={0}\\projects.db;Version=3;";
|
||||||
@@ -24,18 +16,21 @@ internal class ProjectRepository : IDisposable
|
|||||||
public const string UPDATE_PROJECT_STRING = "UPDATE Projects SET Name = @Name, MetadataPath = @MetadataPath WHERE ID = @ID;";
|
public const string UPDATE_PROJECT_STRING = "UPDATE Projects SET Name = @Name, MetadataPath = @MetadataPath WHERE ID = @ID;";
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task EnsureTableCreatedAsync()
|
private static async Task EnsureTableCreatedAsync(SQLiteConnection connection)
|
||||||
{
|
{
|
||||||
using var createCommand = _connection.CreateCommand();
|
using var createCommand = connection.CreateCommand();
|
||||||
createCommand.CommandText = Command.CREATE_PROJECT_TABLE_STRING;
|
createCommand.CommandText = Command.CREATE_PROJECT_TABLE_STRING;
|
||||||
await createCommand.ExecuteNonQueryAsync();
|
await createCommand.ExecuteNonQueryAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async IAsyncEnumerable<ProjectInfo> LoadProjectsAsync()
|
public static async IAsyncEnumerable<ProjectInfo> LoadProjectsAsync()
|
||||||
{
|
{
|
||||||
await EnsureTableCreatedAsync();
|
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
using var command = _connection.CreateCommand();
|
await EnsureTableCreatedAsync(connection);
|
||||||
|
|
||||||
|
using var command = connection.CreateCommand();
|
||||||
command.CommandText = Command.SELECT_PROJECT_STRING;
|
command.CommandText = Command.SELECT_PROJECT_STRING;
|
||||||
|
|
||||||
using var reader = command.ExecuteReader();
|
using var reader = command.ExecuteReader();
|
||||||
@@ -52,11 +47,14 @@ internal class ProjectRepository : IDisposable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task AddProjectAsync(ProjectInfo project)
|
public static async Task AddProjectAsync(ProjectInfo project)
|
||||||
{
|
{
|
||||||
await EnsureTableCreatedAsync();
|
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
using var command = _connection.CreateCommand();
|
await EnsureTableCreatedAsync(connection);
|
||||||
|
|
||||||
|
using var command = connection.CreateCommand();
|
||||||
command.CommandText = Command.INSERT_PROJECT_STRING;
|
command.CommandText = Command.INSERT_PROJECT_STRING;
|
||||||
|
|
||||||
command.Parameters.AddWithValue("@Name", project.Name);
|
command.Parameters.AddWithValue("@Name", project.Name);
|
||||||
@@ -65,9 +63,12 @@ internal class ProjectRepository : IDisposable
|
|||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task RemoveProjectAsync(ProjectInfo project)
|
public static async Task RemoveProjectAsync(ProjectInfo project)
|
||||||
{
|
{
|
||||||
using var command = _connection.CreateCommand();
|
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
using var command = connection.CreateCommand();
|
||||||
command.CommandText = Command.REMOVE_PROJECT_STRING;
|
command.CommandText = Command.REMOVE_PROJECT_STRING;
|
||||||
|
|
||||||
command.Parameters.AddWithValue("@ID", project.ID);
|
command.Parameters.AddWithValue("@ID", project.ID);
|
||||||
@@ -75,9 +76,12 @@ internal class ProjectRepository : IDisposable
|
|||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateProjectAsync(ProjectInfo project)
|
public static async Task UpdateProjectAsync(ProjectInfo project)
|
||||||
{
|
{
|
||||||
using var command = _connection.CreateCommand();
|
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
using var command = connection.CreateCommand();
|
||||||
command.CommandText = Command.UPDATE_PROJECT_STRING;
|
command.CommandText = Command.UPDATE_PROJECT_STRING;
|
||||||
|
|
||||||
command.Parameters.AddWithValue("@Name", project.Name);
|
command.Parameters.AddWithValue("@Name", project.Name);
|
||||||
@@ -86,14 +90,4 @@ internal class ProjectRepository : IDisposable
|
|||||||
|
|
||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
if (_connection.State == ConnectionState.Open)
|
|
||||||
{
|
|
||||||
_connection.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
_connection.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -8,10 +8,11 @@ namespace Ghost.Data.Services;
|
|||||||
|
|
||||||
internal partial class ProjectService
|
internal partial class ProjectService
|
||||||
{
|
{
|
||||||
private const string _ASSETS_FOLDER = "Assets";
|
|
||||||
private const string _CONFIG_FOLDER = "ProjectConfig";
|
|
||||||
private const string _TEMPLATE_CONTENT_FILE = "content.zip";
|
private const string _TEMPLATE_CONTENT_FILE = "content.zip";
|
||||||
|
|
||||||
|
public const string ASSETS_FOLDER = "Assets";
|
||||||
|
public const string CONFIG_FOLDER = "ProjectConfig";
|
||||||
|
|
||||||
public static void EnsureDefaultTemplate()
|
public static void EnsureDefaultTemplate()
|
||||||
{
|
{
|
||||||
var templates = Directory.GetFiles(DataPath.s_projectTemplateFolder, "template.json", SearchOption.AllDirectories);
|
var templates = Directory.GetFiles(DataPath.s_projectTemplateFolder, "template.json", SearchOption.AllDirectories);
|
||||||
@@ -70,8 +71,8 @@ internal partial class ProjectService
|
|||||||
return Result<ProjectMetadataInfo>.Error("Project directory is invalid or does not exist.");
|
return Result<ProjectMetadataInfo>.Error("Project directory is invalid or does not exist.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var projectAssetsPath = Path.Combine(projectDirectory, _ASSETS_FOLDER);
|
var projectAssetsPath = Path.Combine(projectDirectory, ASSETS_FOLDER);
|
||||||
var projectConfigPath = Path.Combine(projectDirectory, _CONFIG_FOLDER);
|
var projectConfigPath = Path.Combine(projectDirectory, CONFIG_FOLDER);
|
||||||
if (!Directory.Exists(projectAssetsPath) || !Directory.Exists(projectConfigPath))
|
if (!Directory.Exists(projectAssetsPath) || !Directory.Exists(projectConfigPath))
|
||||||
{
|
{
|
||||||
return Result<ProjectMetadataInfo>.Error("Project folder structure is invalid.");
|
return Result<ProjectMetadataInfo>.Error("Project folder structure is invalid.");
|
||||||
@@ -94,8 +95,8 @@ internal partial class ProjectService
|
|||||||
|
|
||||||
private static async ValueTask SetupRequestFolderAsync(string projectDirectory, string templateDirectory)
|
private static async ValueTask SetupRequestFolderAsync(string projectDirectory, string templateDirectory)
|
||||||
{
|
{
|
||||||
var projectAssetsPath = Path.Combine(projectDirectory, _ASSETS_FOLDER);
|
var projectAssetsPath = Path.Combine(projectDirectory, ASSETS_FOLDER);
|
||||||
var projectConfigPath = Path.Combine(projectDirectory, _CONFIG_FOLDER);
|
var projectConfigPath = Path.Combine(projectDirectory, CONFIG_FOLDER);
|
||||||
var templateContentPath = Path.Combine(templateDirectory, _TEMPLATE_CONTENT_FILE);
|
var templateContentPath = Path.Combine(templateDirectory, _TEMPLATE_CONTENT_FILE);
|
||||||
|
|
||||||
Directory.CreateDirectory(projectAssetsPath);
|
Directory.CreateDirectory(projectAssetsPath);
|
||||||
@@ -111,13 +112,17 @@ internal partial class ProjectService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal partial class ProjectService : IDisposable
|
internal partial class ProjectService
|
||||||
{
|
{
|
||||||
private readonly ProjectRepository _repository = new(DataPath.s_applicationDataFolder);
|
public static ProjectMetadataInfo CurrentProject
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
public Task AddProjectAsync(ProjectInfo project)
|
public Task AddProjectAsync(ProjectInfo project)
|
||||||
{
|
{
|
||||||
return _repository.AddProjectAsync(project);
|
return ProjectRepository.AddProjectAsync(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ProjectInfo> AddProjectAsync(string name, string path)
|
public async Task<ProjectInfo> AddProjectAsync(string name, string path)
|
||||||
@@ -127,27 +132,27 @@ internal partial class ProjectService : IDisposable
|
|||||||
Name = name,
|
Name = name,
|
||||||
MetadataPath = path,
|
MetadataPath = path,
|
||||||
};
|
};
|
||||||
await _repository.AddProjectAsync(project);
|
await ProjectRepository.AddProjectAsync(project);
|
||||||
|
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task RemoveProjectAsync(ProjectInfo project)
|
public Task RemoveProjectAsync(ProjectInfo project)
|
||||||
{
|
{
|
||||||
return _repository.RemoveProjectAsync(project);
|
return ProjectRepository.RemoveProjectAsync(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task UpdateProjectAsync(ProjectInfo project)
|
public Task UpdateProjectAsync(ProjectInfo project)
|
||||||
{
|
{
|
||||||
return _repository.UpdateProjectAsync(project);
|
return ProjectRepository.UpdateProjectAsync(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IAsyncEnumerable<ProjectInfo> LoadAllProjectAsync()
|
public IAsyncEnumerable<ProjectInfo> LoadAllProjectAsync()
|
||||||
{
|
{
|
||||||
return _repository.LoadProjectsAsync();
|
return ProjectRepository.LoadProjectsAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result<ProjectInfo>> CreateProjectAsync(string projectName, string projectDirectory, Version engineVersion, string templatePath)
|
public async Task<Result<ProjectMetadataInfo>> CreateProjectAsync(string projectName, string projectDirectory, Version engineVersion, string templatePath)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -161,7 +166,7 @@ internal partial class ProjectService : IDisposable
|
|||||||
// Check if folder is empty
|
// Check if folder is empty
|
||||||
if (Directory.EnumerateFiles(projectPath, "*", SearchOption.AllDirectories).Any())
|
if (Directory.EnumerateFiles(projectPath, "*", SearchOption.AllDirectories).Any())
|
||||||
{
|
{
|
||||||
return new(false, null, "Directory is not empty");
|
return new(false, default, "Directory is not empty");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,11 +176,11 @@ internal partial class ProjectService : IDisposable
|
|||||||
await SetupRequestFolderAsync(projectPath, templatePath);
|
await SetupRequestFolderAsync(projectPath, templatePath);
|
||||||
|
|
||||||
var info = await AddProjectAsync(projectName, metadataPath);
|
var info = await AddProjectAsync(projectName, metadataPath);
|
||||||
return new(true, info);
|
return new(true, new(metadataPath, metadata));
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return Result<ProjectInfo>.Error($"Failed to create project: {e.Message}");
|
return Result<ProjectMetadataInfo>.Error($"Failed to create project: {e.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,9 +194,4 @@ internal partial class ProjectService : IDisposable
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_repository.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -9,10 +9,8 @@
|
|||||||
<ResourceDictionary.MergedDictionaries>
|
<ResourceDictionary.MergedDictionaries>
|
||||||
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
|
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
|
||||||
<XamlControlsResources Source="/Controls/EditorControls.xaml" />
|
<XamlControlsResources Source="/Controls/EditorControls.xaml" />
|
||||||
<ResourceDictionary Source="/Themes/Dark.xaml" />
|
<ResourceDictionary Source="/Themes/Override.xaml" />
|
||||||
<ResourceDictionary Source="/Themes/Light.xaml" />
|
|
||||||
</ResourceDictionary.MergedDictionaries>
|
</ResourceDictionary.MergedDictionaries>
|
||||||
<!-- Other app resources here -->
|
|
||||||
</ResourceDictionary>
|
</ResourceDictionary>
|
||||||
</Application.Resources>
|
</Application.Resources>
|
||||||
</Application>
|
</Application>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
using Ghost.Editor.AppStates;
|
using Ghost.Editor.Helpers;
|
||||||
using Ghost.Editor.Helpers;
|
using Ghost.Editor.Infrastructures.AppState;
|
||||||
using Ghost.Editor.Services;
|
using Ghost.Editor.Services;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
@@ -20,7 +20,7 @@ namespace Ghost.Editor
|
|||||||
|
|
||||||
internal static Window? Window
|
internal static Window? Window
|
||||||
{
|
{
|
||||||
get => (Current as App)?._window;
|
get => (Current as App)!._window;
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (Current is App app)
|
if (Current is App app)
|
||||||
@@ -48,16 +48,10 @@ namespace Ghost.Editor
|
|||||||
UseContentRoot(AppContext.BaseDirectory).
|
UseContentRoot(AppContext.BaseDirectory).
|
||||||
ConfigureServices((context, services) =>
|
ConfigureServices((context, services) =>
|
||||||
{
|
{
|
||||||
services.AddSingleton(sp =>
|
|
||||||
{
|
|
||||||
return new AppStateService(
|
|
||||||
new LandingState(),
|
|
||||||
new EditorState());
|
|
||||||
});
|
|
||||||
|
|
||||||
HostHelper.AddLandingScope(context, services);
|
HostHelper.AddLandingScope(context, services);
|
||||||
HostHelper.AddEngineScope(context, services);
|
HostHelper.AddEngineScope(context, services);
|
||||||
|
|
||||||
|
services.AddSingleton<AppStateMachine>();
|
||||||
services.AddSingleton<StackedNotificationService>();
|
services.AddSingleton<StackedNotificationService>();
|
||||||
})
|
})
|
||||||
.Build();
|
.Build();
|
||||||
@@ -92,7 +86,11 @@ namespace Ghost.Editor
|
|||||||
|
|
||||||
Host.Start();
|
Host.Start();
|
||||||
|
|
||||||
await GetService<AppStateService>().TransitionToAsync(StateKey.Landing);
|
var stateMachine = GetService<AppStateMachine>();
|
||||||
|
stateMachine.RegisterState(StateKey.Landing, () => new LandingState());
|
||||||
|
stateMachine.RegisterState(StateKey.EngineEditor, () => new EditorState());
|
||||||
|
|
||||||
|
await stateMachine.TransitionToAsync(StateKey.Landing);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
|
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
|
||||||
|
|||||||
38
Ghost.Editor/Controls/ViewModelPage.cs
Normal file
38
Ghost.Editor/Controls/ViewModelPage.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using Ghost.Editor.Contracts;
|
||||||
|
using Microsoft.UI.Xaml.Controls;
|
||||||
|
using Microsoft.UI.Xaml.Navigation;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.Controls;
|
||||||
|
|
||||||
|
public abstract partial class ViewModelPage<VM> : Page
|
||||||
|
where VM : ObservableObject
|
||||||
|
{
|
||||||
|
public VM ViewModel
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ViewModelPage(VM viewModel)
|
||||||
|
{
|
||||||
|
ViewModel = viewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnNavigatedTo(NavigationEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnNavigatedTo(e);
|
||||||
|
if (ViewModel is INavigationAware navigationAware)
|
||||||
|
{
|
||||||
|
navigationAware.OnNavigatedTo(e.Parameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnNavigatedFrom(NavigationEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnNavigatedFrom(e);
|
||||||
|
if (ViewModel is INavigationAware navigationAware)
|
||||||
|
{
|
||||||
|
navigationAware.OnNavigatedFrom();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,9 +44,8 @@
|
|||||||
<None Remove="Controls\EditorControls.xaml" />
|
<None Remove="Controls\EditorControls.xaml" />
|
||||||
<None Remove="Controls\Internal\InspectorView.xaml" />
|
<None Remove="Controls\Internal\InspectorView.xaml" />
|
||||||
<None Remove="Controls\Internal\InternalControls.xaml" />
|
<None Remove="Controls\Internal\InternalControls.xaml" />
|
||||||
<None Remove="Themes\Dark.xaml" />
|
|
||||||
<None Remove="Themes\Light.xaml" />
|
|
||||||
<None Remove="View\Pages\EngineEditor\ConsolePage.xaml" />
|
<None Remove="View\Pages\EngineEditor\ConsolePage.xaml" />
|
||||||
|
<None Remove="View\Pages\EngineEditor\HierarchyPage.xaml" />
|
||||||
<None Remove="View\Pages\EngineEditor\ProjectPage.xaml" />
|
<None Remove="View\Pages\EngineEditor\ProjectPage.xaml" />
|
||||||
<None Remove="View\Pages\Landing\CreateProjectPage.xaml" />
|
<None Remove="View\Pages\Landing\CreateProjectPage.xaml" />
|
||||||
<None Remove="View\Pages\Landing\OpenProjectPage.xaml" />
|
<None Remove="View\Pages\Landing\OpenProjectPage.xaml" />
|
||||||
@@ -105,7 +104,7 @@
|
|||||||
</Page>
|
</Page>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Helpers\Converters\" />
|
<Folder Include="AppStates\" />
|
||||||
<Folder Include="Resources\" />
|
<Folder Include="Resources\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -113,6 +112,11 @@
|
|||||||
<HintPath>..\..\Class\Misaki.HighPerformance\Misaki.HighPerformance.Unsafe\bin\Release\net9.0\Misaki.HighPerformance.Unsafe.dll</HintPath>
|
<HintPath>..\..\Class\Misaki.HighPerformance\Misaki.HighPerformance.Unsafe\bin\Release\net9.0\Misaki.HighPerformance.Unsafe.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Page Update="View\Pages\EngineEditor\HierarchyPage.xaml">
|
||||||
|
<Generator>MSBuild:Compile</Generator>
|
||||||
|
</Page>
|
||||||
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Page Update="View\Pages\EngineEditor\ProjectPage.xaml">
|
<Page Update="View\Pages\EngineEditor\ProjectPage.xaml">
|
||||||
<Generator>MSBuild:Compile</Generator>
|
<Generator>MSBuild:Compile</Generator>
|
||||||
@@ -124,12 +128,7 @@
|
|||||||
</Page>
|
</Page>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Page Update="Themes\Light.xaml">
|
<Page Update="Themes\Override.xaml">
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="Themes\Dark.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
<Generator>MSBuild:Compile</Generator>
|
||||||
</Page>
|
</Page>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
40
Ghost.Editor/Helpers/Converters/AssetPathToGlyphConverter.cs
Normal file
40
Ghost.Editor/Helpers/Converters/AssetPathToGlyphConverter.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using Microsoft.UI.Xaml.Data;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.Helpers.Converters;
|
||||||
|
|
||||||
|
public partial class AssetPathToGlyphConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object? Convert(object value, Type targetType, object parameter, string language)
|
||||||
|
{
|
||||||
|
if (value is not string path)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Directory.Exists(path))
|
||||||
|
{
|
||||||
|
return "\uE8B7";
|
||||||
|
}
|
||||||
|
|
||||||
|
var extension = Path.GetExtension(path).ToLowerInvariant();
|
||||||
|
|
||||||
|
// TODO: Use resource dictionary for icons.
|
||||||
|
return extension switch
|
||||||
|
{
|
||||||
|
".fbx" or ".obj" => "\uF158",
|
||||||
|
".png" or ".jpg" or ".jpeg" or ".gif" or ".bmp" => "\uE91B", // Image icon
|
||||||
|
".mp3" or ".wav" or ".ogg" => "\uE767", // Audio icon
|
||||||
|
".mp4" or ".avi" or ".mkv" => "\uE714", // Video icon
|
||||||
|
".txt" or ".md" => "\uF000", // Text file icon
|
||||||
|
".cs" or ".hlsl" => "\uE943", // Code file icon
|
||||||
|
_ => "\uE8A5", // Default file icon
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public object ConvertBack(object value, Type targetType, object parameter, string language)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
using Ghost.Data.Services;
|
using Ghost.Data.Services;
|
||||||
|
using Ghost.Editor.View.Pages.EngineEditor;
|
||||||
using Ghost.Editor.View.Pages.Landing;
|
using Ghost.Editor.View.Pages.Landing;
|
||||||
using Ghost.Editor.View.Windows;
|
using Ghost.Editor.View.Windows;
|
||||||
using Ghost.Editor.ViewModel.Pages.Landing;
|
using Ghost.Editor.ViewModels.Pages.EngineEditor;
|
||||||
using Ghost.Editor.ViewModel.Windows;
|
using Ghost.Editor.ViewModels.Pages.Landing;
|
||||||
|
using Ghost.Editor.ViewModels.Windows;
|
||||||
|
using Ghost.Engine;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
@@ -12,19 +15,31 @@ internal static partial class HostHelper
|
|||||||
{
|
{
|
||||||
public static void AddLandingScope(HostBuilderContext context, IServiceCollection services)
|
public static void AddLandingScope(HostBuilderContext context, IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddSingleton<LandingWindow>();
|
services.AddTransient<LandingWindow>();
|
||||||
|
|
||||||
services.AddTransient<CreateProjectPage>();
|
services.AddTransient<CreateProjectPage>();
|
||||||
services.AddTransient<CreateProjectViewModel>();
|
services.AddTransient<CreateProjectViewModel>();
|
||||||
|
|
||||||
services.AddTransient<OpenProjectPage>();
|
services.AddTransient<OpenProjectPage>();
|
||||||
|
services.AddTransient<OpenProjectViewModel>();
|
||||||
|
|
||||||
services.AddTransient<ProjectService>();
|
services.AddTransient<ProjectService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void AddEngineScope(HostBuilderContext context, IServiceCollection services)
|
public static void AddEngineScope(HostBuilderContext context, IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddSingleton<EngineEditorWindow>();
|
services.AddSingleton<EngineCore>();
|
||||||
services.AddSingleton<EngineEditorViewModel>();
|
|
||||||
|
services.AddTransient<EngineEditorWindow>();
|
||||||
|
services.AddTransient<EngineEditorViewModel>();
|
||||||
|
|
||||||
|
services.AddTransient<HierarchyPage>();
|
||||||
|
services.AddTransient<HierarchyViewModel>();
|
||||||
|
|
||||||
|
services.AddTransient<ProjectPage>();
|
||||||
|
services.AddTransient<ProjectViewModel>();
|
||||||
|
|
||||||
|
services.AddTransient<ConsolePage>();
|
||||||
|
services.AddTransient<ConsoleViewModel>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,20 +1,23 @@
|
|||||||
using Ghost.Editor.AppStates;
|
using System;
|
||||||
using Ghost.Editor.Contracts;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Ghost.Editor.Services;
|
namespace Ghost.Editor.Infrastructures.AppState;
|
||||||
|
|
||||||
internal class AppStateService(params IEnumerable<IAppState<StateKey>> states)
|
internal class AppStateMachine
|
||||||
{
|
{
|
||||||
private readonly Dictionary<StateKey, IAppState<StateKey>> _states = states.ToDictionary(s => s.StateKy, s => s);
|
private Dictionary<StateKey, Lazy<IAppState>> s_states = new();
|
||||||
private IAppState<StateKey>? _current;
|
private IAppState? s_current;
|
||||||
|
|
||||||
|
public void RegisterState(StateKey key, Func<IAppState> stateFactory)
|
||||||
|
{
|
||||||
|
s_states[key] = new(stateFactory);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task TransitionToAsync(StateKey stateKey, object? parameter = null)
|
public async Task TransitionToAsync(StateKey stateKey, object? parameter = null)
|
||||||
{
|
{
|
||||||
var previous = _current;
|
var previous = s_current;
|
||||||
var next = _states[stateKey];
|
var next = s_states[stateKey].Value;
|
||||||
|
|
||||||
if (previous != null)
|
if (previous != null)
|
||||||
{
|
{
|
||||||
@@ -30,6 +33,6 @@ internal class AppStateService(params IEnumerable<IAppState<StateKey>> states)
|
|||||||
|
|
||||||
await next.OnEnteredAsync(parameter);
|
await next.OnEnteredAsync(parameter);
|
||||||
|
|
||||||
_current = next;
|
s_current = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
using Ghost.Data.Models;
|
using Ghost.Data.Models;
|
||||||
using Ghost.Editor.Contracts;
|
using Ghost.Data.Services;
|
||||||
using Ghost.Editor.View.Windows;
|
using Ghost.Editor.View.Windows;
|
||||||
|
using Ghost.Engine;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Ghost.Editor.AppStates;
|
namespace Ghost.Editor.Infrastructures.AppState;
|
||||||
|
|
||||||
internal class EditorState : IAppState<StateKey>
|
internal class EditorState : IAppState
|
||||||
{
|
{
|
||||||
private EngineEditorWindow? _window;
|
private EngineEditorWindow? _window;
|
||||||
|
private EngineCore? _engineCore;
|
||||||
public StateKey StateKy => StateKey.EngineEditor;
|
|
||||||
|
|
||||||
public Task OnExitingAsync()
|
public Task OnExitingAsync()
|
||||||
{
|
{
|
||||||
@@ -20,23 +20,31 @@ internal class EditorState : IAppState<StateKey>
|
|||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task OnEnteringAsync(object? parameter)
|
public async Task OnEnteringAsync(object? parameter)
|
||||||
{
|
{
|
||||||
if (parameter is not ProjectMetadata metadata)
|
if (parameter is not ProjectMetadataInfo metadataInfo)
|
||||||
{
|
{
|
||||||
throw new System.ArgumentException("Parameter must be of type ProjectMetadata.", nameof(parameter));
|
throw new System.ArgumentException("Parameter must be of type ProjectMetadata.", nameof(parameter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ProjectService.CurrentProject = metadataInfo;
|
||||||
|
|
||||||
|
_engineCore = App.GetService<EngineCore>();
|
||||||
|
await _engineCore.StartAsync(new Engine.Models.LaunchArgument());
|
||||||
|
|
||||||
_window = App.GetService<EngineEditorWindow>();
|
_window = App.GetService<EngineEditorWindow>();
|
||||||
_window.ViewModel.CurrentProject = metadata;
|
|
||||||
_window.Activate();
|
_window.Activate();
|
||||||
|
|
||||||
App.Window = _window;
|
App.Window = _window;
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task OnExitedAsync()
|
public async Task OnExitedAsync()
|
||||||
{
|
{
|
||||||
|
if (_engineCore != null)
|
||||||
|
{
|
||||||
|
await _engineCore.ShutDownAsync();
|
||||||
|
}
|
||||||
|
|
||||||
if (App.Window == _window)
|
if (App.Window == _window)
|
||||||
{
|
{
|
||||||
App.Window = null;
|
App.Window = null;
|
||||||
@@ -44,7 +52,6 @@ internal class EditorState : IAppState<StateKey>
|
|||||||
|
|
||||||
_window?.Close();
|
_window?.Close();
|
||||||
_window = null;
|
_window = null;
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task OnEnteredAsync(object? parameter)
|
public Task OnEnteredAsync(object? parameter)
|
||||||
@@ -1,14 +1,9 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Ghost.Editor.Contracts;
|
namespace Ghost.Editor.Infrastructures.AppState;
|
||||||
|
|
||||||
internal interface IAppState<Key>
|
internal interface IAppState
|
||||||
{
|
{
|
||||||
public Key StateKy
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called when exiting the state.
|
/// Called when exiting the state.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -1,15 +1,12 @@
|
|||||||
using Ghost.Editor.Contracts;
|
using Ghost.Editor.View.Windows;
|
||||||
using Ghost.Editor.View.Windows;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Ghost.Editor.AppStates;
|
namespace Ghost.Editor.Infrastructures.AppState;
|
||||||
|
|
||||||
internal class LandingState : IAppState<StateKey>
|
internal class LandingState : IAppState
|
||||||
{
|
{
|
||||||
private LandingWindow? _window;
|
private LandingWindow? _window;
|
||||||
|
|
||||||
public StateKey StateKy => StateKey.Landing;
|
|
||||||
|
|
||||||
public Task OnExitingAsync()
|
public Task OnExitingAsync()
|
||||||
{
|
{
|
||||||
if (App.Window == _window)
|
if (App.Window == _window)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace Ghost.Editor.AppStates;
|
namespace Ghost.Editor.Infrastructures.AppState;
|
||||||
|
|
||||||
internal enum StateKey
|
internal enum StateKey
|
||||||
{
|
{
|
||||||
17
Ghost.Editor/Infrastructures/SceneGraph/EntityNode.cs
Normal file
17
Ghost.Editor/Infrastructures/SceneGraph/EntityNode.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using Ghost.Entities;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.Infrastructures.SceneGraph;
|
||||||
|
|
||||||
|
public partial class EntityNode : SceneGraphNode
|
||||||
|
{
|
||||||
|
private readonly Entity _entity;
|
||||||
|
|
||||||
|
public Entity Entity => _entity;
|
||||||
|
public override NodeType Type => NodeType.Entity;
|
||||||
|
|
||||||
|
public EntityNode(Entity entity, string name)
|
||||||
|
{
|
||||||
|
_entity = entity;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
112
Ghost.Editor/Infrastructures/SceneGraph/SceneGraphHelpers.cs
Normal file
112
Ghost.Editor/Infrastructures/SceneGraph/SceneGraphHelpers.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
using Ghost.Engine.Components;
|
||||||
|
using Ghost.Entities;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.Infrastructures.SceneGraph;
|
||||||
|
|
||||||
|
internal class SceneGraphHelpers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="EntityNode"/> entity with default components.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="world">The world context where the entity will be created.</param>
|
||||||
|
/// <param name="entity">The entity to be wrapped in the <see cref="EntityNode"/>.</param>
|
||||||
|
public static EntityNode CreateEntityNode(World world, Entity entity, string name)
|
||||||
|
{
|
||||||
|
world.EntityManager.AddComponent(entity, LocalToWorld.Identity);
|
||||||
|
world.EntityManager.AddComponent(entity, Hierarchy.Root);
|
||||||
|
return new EntityNode(entity, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="Entity"/> and <see cref="EntityNode"/> entity with default components.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="world">The world context where the entity will be created.</param>
|
||||||
|
public static EntityNode CreateEntityNode(World world, string name)
|
||||||
|
{
|
||||||
|
var entity = world.EntityManager.CreateEntity();
|
||||||
|
return CreateEntityNode(world, entity, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attaches childEntity to parentEntity in the scene graph.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="world">The world context where the entities exist.</param>
|
||||||
|
/// <param name="parentNode">The parent entity to which the child will be attached.</param>
|
||||||
|
/// <param name="childNode">The child entity to be attached.</param>
|
||||||
|
public static void AttachChild(SceneNode scene, EntityNode parentNode, EntityNode childNode)
|
||||||
|
{
|
||||||
|
// 1) If the child already has a parent, detach it first
|
||||||
|
var childHierarchy = scene.World.EntityManager.GetComponent<Hierarchy>(childNode.Entity);
|
||||||
|
if (childHierarchy.ValueRO.parent != Entity.Invalid)
|
||||||
|
{
|
||||||
|
DetachFromParent(scene, childNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2) Link child to new parent
|
||||||
|
childHierarchy.ValueRW.parent = parentNode.Entity;
|
||||||
|
|
||||||
|
// 3) Insert child at the head of parent's child list
|
||||||
|
var parentHierarchy = scene.World.EntityManager.GetComponent<Hierarchy>(parentNode.Entity);
|
||||||
|
|
||||||
|
childHierarchy.ValueRW.nextSibling = parentHierarchy.ValueRO.firstChild;
|
||||||
|
parentHierarchy.ValueRW.firstChild = childNode.Entity;
|
||||||
|
|
||||||
|
// 4) Write back
|
||||||
|
scene.World.EntityManager.SetComponent(parentNode.Entity, in parentHierarchy.ValueRO);
|
||||||
|
scene.World.EntityManager.SetComponent(childNode.Entity, in childHierarchy.ValueRO);
|
||||||
|
|
||||||
|
// 5) Update children list in parent node
|
||||||
|
parentNode.Children.Add(childNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Detaches the specified entity from its parent in the scene graph.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="world">The world context where the entities exist.</param>
|
||||||
|
/// <param name="node">The entity to detach from its parent.</param>
|
||||||
|
public static void DetachFromParent(SceneNode scene, EntityNode node)
|
||||||
|
{
|
||||||
|
var hierarchy = scene.World.EntityManager.GetComponent<Hierarchy>(node.Entity);
|
||||||
|
var parent = hierarchy.ValueRO.parent;
|
||||||
|
if (parent == Entity.Invalid)
|
||||||
|
{
|
||||||
|
return; // already root
|
||||||
|
}
|
||||||
|
|
||||||
|
var parentHierarchy = scene.World.EntityManager.GetComponent<Hierarchy>(parent);
|
||||||
|
|
||||||
|
// If entity is the first child, simply move head
|
||||||
|
if (parentHierarchy.ValueRO.firstChild == node.Entity)
|
||||||
|
{
|
||||||
|
parentHierarchy.ValueRW.firstChild = hierarchy.ValueRO.nextSibling;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Otherwise, find the previous sibling in the linked list
|
||||||
|
var prevSibling = parentHierarchy.ValueRO.firstChild;
|
||||||
|
while (prevSibling != Entity.Invalid)
|
||||||
|
{
|
||||||
|
var prevHierarchy = scene.World.EntityManager.GetComponent<Hierarchy>(prevSibling);
|
||||||
|
if (prevHierarchy.ValueRW.nextSibling == node.Entity)
|
||||||
|
{
|
||||||
|
prevHierarchy.ValueRW.nextSibling = hierarchy.ValueRO.nextSibling;
|
||||||
|
scene.World.EntityManager.SetComponent(prevSibling, in prevHierarchy.ValueRO);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
prevSibling = prevHierarchy.ValueRO.nextSibling;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear child's references
|
||||||
|
hierarchy.ValueRW.parent = Entity.Invalid;
|
||||||
|
hierarchy.ValueRW.nextSibling = Entity.Invalid;
|
||||||
|
|
||||||
|
// Write back
|
||||||
|
scene.World.EntityManager.SetComponent(parent, in parentHierarchy.ValueRO);
|
||||||
|
scene.World.EntityManager.SetComponent(node.Entity, in hierarchy.ValueRO);
|
||||||
|
|
||||||
|
// Remove from parent's children list
|
||||||
|
scene.EntityNodeLookup[parent].Children.Remove(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
Ghost.Editor/Infrastructures/SceneGraph/SceneGraphNode.cs
Normal file
32
Ghost.Editor/Infrastructures/SceneGraph/SceneGraphNode.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.Infrastructures.SceneGraph;
|
||||||
|
|
||||||
|
public abstract partial class SceneGraphNode : ObservableObject
|
||||||
|
{
|
||||||
|
public enum NodeType
|
||||||
|
{
|
||||||
|
Scene,
|
||||||
|
Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract NodeType Type
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial string Name
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Will the new collection allocated if ui bind to this property?
|
||||||
|
private ObservableCollection<EntityNode>? _children;
|
||||||
|
public ObservableCollection<EntityNode> Children
|
||||||
|
{
|
||||||
|
get => _children ??= new();
|
||||||
|
}
|
||||||
|
}
|
||||||
58
Ghost.Editor/Infrastructures/SceneGraph/SceneNode.cs
Normal file
58
Ghost.Editor/Infrastructures/SceneGraph/SceneNode.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
using Ghost.Engine.Components;
|
||||||
|
using Ghost.Entities;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.Infrastructures.SceneGraph;
|
||||||
|
|
||||||
|
public partial class SceneNode : SceneGraphNode
|
||||||
|
{
|
||||||
|
private readonly World _world;
|
||||||
|
private Dictionary<Entity, EntityNode> _entityNodeLookup = new();
|
||||||
|
|
||||||
|
public World World => _world;
|
||||||
|
public Dictionary<Entity, EntityNode> EntityNodeLookup => _entityNodeLookup;
|
||||||
|
|
||||||
|
public override NodeType Type => NodeType.Scene;
|
||||||
|
|
||||||
|
public SceneNode(World world, string name)
|
||||||
|
{
|
||||||
|
_world = world;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private EntityNode BuildNodeRecursive(Entity entity, World world)
|
||||||
|
{
|
||||||
|
// TODO: Node serialization.
|
||||||
|
var node = new EntityNode(entity, "New Entity");
|
||||||
|
_entityNodeLookup[entity] = node;
|
||||||
|
|
||||||
|
var hc = world.EntityManager.GetComponent<Hierarchy>(entity);
|
||||||
|
var child = hc.ValueRO.firstChild;
|
||||||
|
|
||||||
|
while (child != Entity.Invalid)
|
||||||
|
{
|
||||||
|
node.Children.Add(BuildNodeRecursive(child, world));
|
||||||
|
var childHC = world.EntityManager.GetComponent<Hierarchy>(child);
|
||||||
|
child = childHC.ValueRO.nextSibling;
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void BuildGraph()
|
||||||
|
{
|
||||||
|
foreach (var (entity, hierarchy) in _world.Query<Hierarchy>())
|
||||||
|
{
|
||||||
|
if (hierarchy.ValueRO.parent == Entity.Invalid)
|
||||||
|
{
|
||||||
|
var node = BuildNodeRecursive(entity, _world);
|
||||||
|
Children.Add(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Load()
|
||||||
|
{
|
||||||
|
BuildGraph();
|
||||||
|
}
|
||||||
|
}
|
||||||
19
Ghost.Editor/Models/AssetItem.cs
Normal file
19
Ghost.Editor/Models/AssetItem.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
namespace Ghost.Editor.Models;
|
||||||
|
|
||||||
|
internal struct AssetItem()
|
||||||
|
{
|
||||||
|
public string AssetPath
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
} = string.Empty;
|
||||||
|
|
||||||
|
public string AssetName
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
} = string.Empty;
|
||||||
|
|
||||||
|
public string IconGlyph
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
} = string.Empty;
|
||||||
|
}
|
||||||
27
Ghost.Editor/Models/ExplorerItem.cs
Normal file
27
Ghost.Editor/Models/ExplorerItem.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.Models;
|
||||||
|
|
||||||
|
internal class ExplorerItem(string name, string path, bool isDirectory)
|
||||||
|
{
|
||||||
|
public string Name
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
} = name;
|
||||||
|
|
||||||
|
public string Path
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
} = path;
|
||||||
|
|
||||||
|
public bool IsDirectory
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
} = isDirectory;
|
||||||
|
|
||||||
|
public ObservableCollection<ExplorerItem>? Children
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,269 +0,0 @@
|
|||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
|
||||||
using Ghost.Entities;
|
|
||||||
using Ghost.Entities.Helpers;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.ObjectModel;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Models;
|
|
||||||
|
|
||||||
public partial class GameObject : ObservableObject
|
|
||||||
{
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial bool IsActive
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial bool IsActiveHierarchy
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Entity Entity
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Scene Scene
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
internal set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameObject? Parent
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
internal set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Name
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial ObservableCollection<IComponentData>? Components
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
private set;
|
|
||||||
}
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial IEnumerable<ScriptComponent>? ScriptComponents
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
private set;
|
|
||||||
}
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial ObservableCollection<GameObject>? Children
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
private set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameObject(Scene scene, string name)
|
|
||||||
{
|
|
||||||
Entity = scene.World.EntityManager.CreateEntity();
|
|
||||||
Scene = scene;
|
|
||||||
Name = name;
|
|
||||||
IsActive = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
partial void OnIsActiveChanged(bool value)
|
|
||||||
{
|
|
||||||
IsActiveHierarchy = value && (Parent?.IsActiveHierarchy ?? true);
|
|
||||||
HandleActiveStateChanged();
|
|
||||||
|
|
||||||
if (Children != null)
|
|
||||||
{
|
|
||||||
foreach (var child in Children)
|
|
||||||
{
|
|
||||||
child.IsActiveHierarchy = value && IsActiveHierarchy;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
partial void OnIsActiveHierarchyChanged(bool value)
|
|
||||||
{
|
|
||||||
HandleActiveStateChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void HandleActiveStateChanged()
|
|
||||||
{
|
|
||||||
if (IsActive && IsActiveHierarchy)
|
|
||||||
{
|
|
||||||
OnEnable();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
OnDisable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void OnEnable()
|
|
||||||
{
|
|
||||||
if (ScriptComponents != null)
|
|
||||||
{
|
|
||||||
foreach (var script in ScriptComponents)
|
|
||||||
{
|
|
||||||
if (!script.Enable)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
script.OnEnable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void OnDisable()
|
|
||||||
{
|
|
||||||
if (ScriptComponents != null)
|
|
||||||
{
|
|
||||||
foreach (var script in ScriptComponents)
|
|
||||||
{
|
|
||||||
if (!script.Enable)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
script.OnDisable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddChild(GameObject child)
|
|
||||||
{
|
|
||||||
if (child.Scene != Scene)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Child GameObject must belong to the same Scene.");
|
|
||||||
}
|
|
||||||
|
|
||||||
Children ??= new();
|
|
||||||
Children.Add(child);
|
|
||||||
child.Parent = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool RemoveChild(GameObject child)
|
|
||||||
{
|
|
||||||
if (Children is null)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Children.Remove(child))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
child.Parent = null;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Destroy()
|
|
||||||
{
|
|
||||||
if (ScriptComponents != null)
|
|
||||||
{
|
|
||||||
foreach (var component in ScriptComponents)
|
|
||||||
{
|
|
||||||
if (!component.Enable)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
component.OnDestroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Children != null)
|
|
||||||
{
|
|
||||||
foreach (var child in Children)
|
|
||||||
{
|
|
||||||
child.Destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
Children.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
Parent?.Children?.Remove(this);
|
|
||||||
Entity.Destroy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public partial class GameObject
|
|
||||||
{
|
|
||||||
// TODO: Implement a more efficient synchronization mechanism for components
|
|
||||||
internal void SyncComponents()
|
|
||||||
{
|
|
||||||
foreach (var (typeHandle, mask) in Scene.World.ComponentStorage.ComponentEntityMasks)
|
|
||||||
{
|
|
||||||
if (!mask.IsSet(Entity.ID))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var pool = Scene.World.ComponentStorage.ComponentPools[typeHandle];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void SyncScripts()
|
|
||||||
{
|
|
||||||
var scriptsPool = Scene.World.ComponentStorage.ScriptComponentPool.ScriptComponents;
|
|
||||||
if (scriptsPool == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
scriptsPool.TryGetValue(Entity, out var scripts);
|
|
||||||
ScriptComponents = scripts;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddComponent<T>(T component)
|
|
||||||
where T : struct, IComponentData
|
|
||||||
{
|
|
||||||
Entity.AddComponent<T>(component);
|
|
||||||
SyncComponents();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool RemoveComponent<T>()
|
|
||||||
where T : struct, IComponentData
|
|
||||||
{
|
|
||||||
var result = Entity.RemoveComponent<T>();
|
|
||||||
SyncComponents();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddScript<T>()
|
|
||||||
where T : ScriptComponent, new()
|
|
||||||
{
|
|
||||||
Entity.AddScript<T>();
|
|
||||||
SyncScripts();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddScript(Type type)
|
|
||||||
{
|
|
||||||
Entity.AddScript(type);
|
|
||||||
SyncScripts();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool RemoveScript<T>()
|
|
||||||
where T : ScriptComponent
|
|
||||||
{
|
|
||||||
var result = Scene.World.EntityManager.RemoveScript<T>(Entity);
|
|
||||||
SyncScripts();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool RemoveScriptAt(int index)
|
|
||||||
{
|
|
||||||
var result = Scene.World.EntityManager.RemoveScriptAt(Entity, index);
|
|
||||||
SyncScripts();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
using Ghost.Entities;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Models;
|
|
||||||
|
|
||||||
public class Scene
|
|
||||||
{
|
|
||||||
private readonly HashSet<GameObject> _rootObjects = new();
|
|
||||||
private readonly World _world = World.Create();
|
|
||||||
|
|
||||||
public IEnumerable<GameObject> RootObjects => _rootObjects;
|
|
||||||
public World World => _world;
|
|
||||||
|
|
||||||
internal Scene()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void Load()
|
|
||||||
{
|
|
||||||
foreach (var gameObject in _rootObjects)
|
|
||||||
{
|
|
||||||
gameObject.OnEnable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void Unload()
|
|
||||||
{
|
|
||||||
foreach (var gameObject in _rootObjects)
|
|
||||||
{
|
|
||||||
gameObject.OnDisable();
|
|
||||||
gameObject.Destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
_rootObjects.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +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">
|
|
||||||
<ResourceDictionary.ThemeDictionaries>
|
|
||||||
<ResourceDictionary x:Key="Dark">
|
|
||||||
<StaticResource x:Key="TabViewItemHeaderBackgroundSelected" ResourceKey="ControlFillColorSecondaryBrush" />
|
|
||||||
</ResourceDictionary>
|
|
||||||
</ResourceDictionary.ThemeDictionaries>
|
|
||||||
</ResourceDictionary>
|
|
||||||
@@ -1,8 +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">
|
|
||||||
<ResourceDictionary.ThemeDictionaries>
|
|
||||||
<ResourceDictionary x:Key="Light">
|
|
||||||
<StaticResource x:Key="TabViewItemHeaderBackgroundSelected" ResourceKey="ControlFillColorSecondaryBrush" />
|
|
||||||
</ResourceDictionary>
|
|
||||||
</ResourceDictionary.ThemeDictionaries>
|
|
||||||
</ResourceDictionary>
|
|
||||||
14
Ghost.Editor/Themes/Override.xaml
Normal file
14
Ghost.Editor/Themes/Override.xaml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?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:controls="using:Microsoft.UI.Xaml.Controls">
|
||||||
|
<ResourceDictionary.ThemeDictionaries>
|
||||||
|
<ResourceDictionary x:Key="Dark">
|
||||||
|
<StaticResource x:Key="TabViewItemHeaderBackgroundSelected" ResourceKey="ControlFillColorSecondaryBrush" />
|
||||||
|
</ResourceDictionary>
|
||||||
|
<ResourceDictionary x:Key="Light">
|
||||||
|
<StaticResource x:Key="TabViewItemHeaderBackgroundSelected" ResourceKey="ControlFillColorSecondaryBrush" />
|
||||||
|
</ResourceDictionary>
|
||||||
|
</ResourceDictionary.ThemeDictionaries>
|
||||||
|
</ResourceDictionary>
|
||||||
@@ -21,19 +21,19 @@
|
|||||||
BorderThickness="0,0,0,1">
|
BorderThickness="0,0,0,1">
|
||||||
<CommandBar Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}" DefaultLabelPosition="Collapsed">
|
<CommandBar Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}" DefaultLabelPosition="Collapsed">
|
||||||
<CommandBar.PrimaryCommands>
|
<CommandBar.PrimaryCommands>
|
||||||
<AppBarButton Content="Clear" />
|
<AppBarButton Command="{x:Bind ViewModel.ClearLogsCommand}" Content="Clear" />
|
||||||
<AppBarSeparator />
|
<AppBarSeparator />
|
||||||
<AppBarToggleButton Width="45">
|
<AppBarToggleButton Width="45" IsChecked="{x:Bind ViewModel.ShowInfo, Mode=TwoWay}">
|
||||||
<AppBarToggleButton.Icon>
|
<AppBarToggleButton.Icon>
|
||||||
<FontIcon Glyph="" />
|
<FontIcon Glyph="" />
|
||||||
</AppBarToggleButton.Icon>
|
</AppBarToggleButton.Icon>
|
||||||
</AppBarToggleButton>
|
</AppBarToggleButton>
|
||||||
<AppBarToggleButton Width="45">
|
<AppBarToggleButton Width="45" IsChecked="{x:Bind ViewModel.ShowWarning, Mode=TwoWay}">
|
||||||
<AppBarToggleButton.Icon>
|
<AppBarToggleButton.Icon>
|
||||||
<FontIcon Glyph="" />
|
<FontIcon Glyph="" />
|
||||||
</AppBarToggleButton.Icon>
|
</AppBarToggleButton.Icon>
|
||||||
</AppBarToggleButton>
|
</AppBarToggleButton>
|
||||||
<AppBarToggleButton Width="45">
|
<AppBarToggleButton Width="45" IsChecked="{x:Bind ViewModel.ShowError, Mode=TwoWay}">
|
||||||
<AppBarToggleButton.Icon>
|
<AppBarToggleButton.Icon>
|
||||||
<FontIcon Glyph="" />
|
<FontIcon Glyph="" />
|
||||||
</AppBarToggleButton.Icon>
|
</AppBarToggleButton.Icon>
|
||||||
@@ -42,7 +42,10 @@
|
|||||||
|
|
||||||
<CommandBar.SecondaryCommands>
|
<CommandBar.SecondaryCommands>
|
||||||
<AppBarToggleButton BorderThickness="0" Label="Clear On Play" />
|
<AppBarToggleButton BorderThickness="0" Label="Clear On Play" />
|
||||||
<AppBarToggleButton BorderThickness="0" Label="Show Stack Trace" />
|
<AppBarToggleButton
|
||||||
|
BorderThickness="0"
|
||||||
|
IsChecked="{x:Bind ViewModel.ShowStackTrace, Mode=TwoWay}"
|
||||||
|
Label="Show Stack Trace" />
|
||||||
</CommandBar.SecondaryCommands>
|
</CommandBar.SecondaryCommands>
|
||||||
</CommandBar>
|
</CommandBar>
|
||||||
</Grid>
|
</Grid>
|
||||||
@@ -54,7 +57,11 @@
|
|||||||
<RowDefinition Height="100" />
|
<RowDefinition Height="100" />
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
|
|
||||||
<ListView Grid.Row="0" />
|
<ListView
|
||||||
|
x:Name="LogListView"
|
||||||
|
Grid.Row="0"
|
||||||
|
ItemsSource="{x:Bind ViewModel.Logs, Mode=OneWay}"
|
||||||
|
SelectedItem="{x:Bind ViewModel.SelectedLog, Mode=TwoWay}" />
|
||||||
<Grid
|
<Grid
|
||||||
Grid.Row="1"
|
Grid.Row="1"
|
||||||
Padding="4"
|
Padding="4"
|
||||||
@@ -64,7 +71,7 @@
|
|||||||
<TextBlock
|
<TextBlock
|
||||||
IsTextSelectionEnabled="True"
|
IsTextSelectionEnabled="True"
|
||||||
Style="{StaticResource CaptionTextBlockStyle}"
|
Style="{StaticResource CaptionTextBlockStyle}"
|
||||||
Text="Test Log"
|
Text="{x:Bind ViewModel.SelectedLog.ToStringWithStackTrace(), Mode=OneWay}"
|
||||||
TextWrapping="Wrap" />
|
TextWrapping="Wrap" />
|
||||||
</ScrollViewer>
|
</ScrollViewer>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|||||||
@@ -1,11 +1,19 @@
|
|||||||
|
using Ghost.Editor.ViewModels.Pages.EngineEditor;
|
||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
|
|
||||||
namespace Ghost.Editor.View.Pages.EngineEditor;
|
namespace Ghost.Editor.View.Pages.EngineEditor;
|
||||||
|
|
||||||
public sealed partial class ConsolePage : Page
|
internal sealed partial class ConsolePage : Page
|
||||||
{
|
{
|
||||||
|
public ConsoleViewModel ViewModel
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
public ConsolePage()
|
public ConsolePage()
|
||||||
{
|
{
|
||||||
|
ViewModel = App.GetService<ConsoleViewModel>();
|
||||||
|
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
43
Ghost.Editor/View/Pages/EngineEditor/HierarchyPage.xaml
Normal file
43
Ghost.Editor/View/Pages/EngineEditor/HierarchyPage.xaml
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<Page
|
||||||
|
x:Class="Ghost.Editor.View.Pages.EngineEditor.HierarchyPage"
|
||||||
|
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:local="using:Ghost.Editor.View.Pages.EngineEditor"
|
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
xmlns:sg="using:Ghost.Editor.Infrastructures.SceneGraph"
|
||||||
|
mc:Ignorable="d">
|
||||||
|
|
||||||
|
<Page.Resources>
|
||||||
|
<DataTemplate x:Key="SceneTemplate" x:DataType="sg:SceneGraphNode">
|
||||||
|
<TreeViewItem
|
||||||
|
AutomationProperties.Name="{x:Bind Name}"
|
||||||
|
Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}"
|
||||||
|
IsExpanded="True"
|
||||||
|
ItemsSource="{x:Bind Children}">
|
||||||
|
<StackPanel Orientation="Horizontal">
|
||||||
|
<FontIcon FontSize="14" Glyph="" />
|
||||||
|
<TextBlock Margin="10,0" Text="{x:Bind Name}" />
|
||||||
|
</StackPanel>
|
||||||
|
</TreeViewItem>
|
||||||
|
</DataTemplate>
|
||||||
|
|
||||||
|
<DataTemplate x:Key="EntityTemplate" x:DataType="sg:SceneGraphNode">
|
||||||
|
<TreeViewItem AutomationProperties.Name="{x:Bind Name}" ItemsSource="{x:Bind Children}">
|
||||||
|
<StackPanel Orientation="Horizontal">
|
||||||
|
<FontIcon FontSize="14" Glyph="" />
|
||||||
|
<TextBlock Margin="10,0" Text="{x:Bind Name}" />
|
||||||
|
</StackPanel>
|
||||||
|
</TreeViewItem>
|
||||||
|
</DataTemplate>
|
||||||
|
</Page.Resources>
|
||||||
|
|
||||||
|
<Grid Background="{ThemeResource LayerFillColorDefaultBrush}">
|
||||||
|
<TreeView ItemsSource="{x:Bind ViewModel.SceneList}">
|
||||||
|
<TreeView.ItemTemplateSelector>
|
||||||
|
<local:HierarchyTemplateSector EntityTemplate="{StaticResource EntityTemplate}" SceneTemplate="{StaticResource SceneTemplate}" />
|
||||||
|
</TreeView.ItemTemplateSelector>
|
||||||
|
</TreeView>
|
||||||
|
</Grid>
|
||||||
|
</Page>
|
||||||
57
Ghost.Editor/View/Pages/EngineEditor/HierarchyPage.xaml.cs
Normal file
57
Ghost.Editor/View/Pages/EngineEditor/HierarchyPage.xaml.cs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
using Ghost.Editor.Infrastructures.SceneGraph;
|
||||||
|
using Ghost.Editor.ViewModels.Pages.EngineEditor;
|
||||||
|
using Microsoft.UI.Xaml;
|
||||||
|
using Microsoft.UI.Xaml.Controls;
|
||||||
|
|
||||||
|
// 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.View.Pages.EngineEditor;
|
||||||
|
/// <summary>
|
||||||
|
/// An empty page that can be used on its own or navigated to within a Frame.
|
||||||
|
/// </summary>
|
||||||
|
internal sealed partial class HierarchyPage : Page
|
||||||
|
{
|
||||||
|
public HierarchyViewModel ViewModel
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HierarchyPage()
|
||||||
|
{
|
||||||
|
ViewModel = App.GetService<HierarchyViewModel>();
|
||||||
|
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal partial class HierarchyTemplateSector : DataTemplateSelector
|
||||||
|
{
|
||||||
|
public DataTemplate? SceneTemplate
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataTemplate? EntityTemplate
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override DataTemplate SelectTemplateCore(object item)
|
||||||
|
{
|
||||||
|
if (SceneTemplate == null || EntityTemplate == null)
|
||||||
|
{
|
||||||
|
return base.SelectTemplateCore(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
var node = (SceneGraphNode)item;
|
||||||
|
return node.Type switch
|
||||||
|
{
|
||||||
|
SceneGraphNode.NodeType.Scene => SceneTemplate,
|
||||||
|
SceneGraphNode.NodeType.Entity => EntityTemplate,
|
||||||
|
_ => base.SelectTemplateCore(item)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,11 +3,17 @@
|
|||||||
x:Class="Ghost.Editor.View.Pages.EngineEditor.ProjectPage"
|
x:Class="Ghost.Editor.View.Pages.EngineEditor.ProjectPage"
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:converter="using:Ghost.Editor.Helpers.Converters"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:local="using:Ghost.Editor.View.Pages.EngineEditor"
|
xmlns:local="using:Ghost.Editor.View.Pages.EngineEditor"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
xmlns:model="using:Ghost.Editor.Models"
|
||||||
mc:Ignorable="d">
|
mc:Ignorable="d">
|
||||||
|
|
||||||
|
<Page.Resources>
|
||||||
|
<converter:AssetPathToGlyphConverter x:Key="AssetPathToGlyphConverter" />
|
||||||
|
</Page.Resources>
|
||||||
|
|
||||||
<Grid Background="{ThemeResource LayerFillColorDefaultBrush}">
|
<Grid Background="{ThemeResource LayerFillColorDefaultBrush}">
|
||||||
<Grid.ColumnDefinitions>
|
<Grid.ColumnDefinitions>
|
||||||
<ColumnDefinition Width="250" />
|
<ColumnDefinition Width="250" />
|
||||||
@@ -17,55 +23,116 @@
|
|||||||
<!-- Folder Tree View -->
|
<!-- Folder Tree View -->
|
||||||
<Grid
|
<Grid
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
|
Padding="4"
|
||||||
|
Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}"
|
||||||
BorderBrush="{ThemeResource CardStrokeColorDefaultSolid}"
|
BorderBrush="{ThemeResource CardStrokeColorDefaultSolid}"
|
||||||
BorderThickness="0,0,1,0">
|
BorderThickness="0,0,1,0">
|
||||||
<TreeView
|
<TreeView
|
||||||
HorizontalAlignment="Stretch"
|
HorizontalAlignment="Stretch"
|
||||||
VerticalAlignment="Stretch"
|
VerticalAlignment="Stretch"
|
||||||
Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}"
|
ItemsSource="{x:Bind ViewModel.SubDirectories}"
|
||||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
|
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
|
||||||
ScrollViewer.VerticalScrollBarVisibility="Auto">
|
ScrollViewer.VerticalScrollBarVisibility="Auto"
|
||||||
<TreeView.ItemContainerStyle>
|
SelectedItem="{x:Bind ViewModel.SelectedDirectory, Mode=TwoWay}">
|
||||||
<Style TargetType="TreeViewItem">
|
<TreeView.ItemTemplate>
|
||||||
<Setter Property="Padding" Value="4,2" />
|
<DataTemplate x:DataType="model:ExplorerItem">
|
||||||
<Setter Property="Margin" Value="0,0,0,2" />
|
<TreeViewItem ItemsSource="{x:Bind Children}">
|
||||||
</Style>
|
<StackPanel Orientation="Horizontal">
|
||||||
</TreeView.ItemContainerStyle>
|
<FontIcon
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
FontSize="14"
|
||||||
|
Glyph="" />
|
||||||
|
<TextBlock
|
||||||
|
Margin="8,0,0,0"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
Style="{StaticResource CaptionTextBlockStyle}"
|
||||||
|
Text="{x:Bind Name}"
|
||||||
|
TextTrimming="CharacterEllipsis" />
|
||||||
|
</StackPanel>
|
||||||
|
</TreeViewItem>
|
||||||
|
</DataTemplate>
|
||||||
|
</TreeView.ItemTemplate>
|
||||||
</TreeView>
|
</TreeView>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
<!-- Files -->
|
<!-- Files -->
|
||||||
<ScrollViewer
|
<Grid Grid.Column="1">
|
||||||
Grid.Column="1"
|
<Grid.RowDefinitions>
|
||||||
HorizontalScrollBarVisibility="Auto"
|
<RowDefinition Height="Auto" />
|
||||||
VerticalScrollBarVisibility="Auto">
|
<RowDefinition Height="*" />
|
||||||
<GridView HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
|
<RowDefinition Height="Auto" />
|
||||||
<GridView.ItemContainerStyle>
|
</Grid.RowDefinitions>
|
||||||
<Style BasedOn="{StaticResource DefaultGridViewItemStyle}" TargetType="GridViewItem">
|
|
||||||
<Setter Property="Margin" Value="5,5,5,5" />
|
|
||||||
</Style>
|
|
||||||
</GridView.ItemContainerStyle>
|
|
||||||
|
|
||||||
<GridView.ItemTemplate>
|
<Grid
|
||||||
<DataTemplate>
|
Grid.Row="0"
|
||||||
<Grid>
|
Padding="4"
|
||||||
<Grid.RowDefinitions>
|
HorizontalAlignment="Stretch"
|
||||||
<RowDefinition Height="*" />
|
VerticalAlignment="Stretch"
|
||||||
<RowDefinition Height="0.2*" />
|
Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}"
|
||||||
</Grid.RowDefinitions>
|
BorderBrush="{ThemeResource CardStrokeColorDefaultSolid}"
|
||||||
<ImageIcon
|
BorderThickness="0,0,0,1">
|
||||||
Grid.Row="0"
|
<BreadcrumbBar Height="15" />
|
||||||
Width="24"
|
</Grid>
|
||||||
Height="24" />
|
|
||||||
<TextBlock
|
<ScrollViewer
|
||||||
Grid.Row="1"
|
Grid.Row="1"
|
||||||
Margin="8,0"
|
Padding="8"
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Stretch"
|
||||||
TextTrimming="CharacterEllipsis" />
|
HorizontalScrollBarVisibility="Auto"
|
||||||
</Grid>
|
VerticalScrollBarVisibility="Auto">
|
||||||
</DataTemplate>
|
<GridView
|
||||||
</GridView.ItemTemplate>
|
HorizontalAlignment="Stretch"
|
||||||
</GridView>
|
VerticalAlignment="Stretch"
|
||||||
</ScrollViewer>
|
ItemsSource="{x:Bind ViewModel.DirectoryAssets, Mode=OneWay}"
|
||||||
|
SelectedItem="{x:Bind ViewModel.SelectedAsset, Mode=TwoWay}">
|
||||||
|
<GridView.ItemContainerStyle>
|
||||||
|
<Style BasedOn="{StaticResource DefaultGridViewItemStyle}" TargetType="GridViewItem">
|
||||||
|
<Setter Property="Margin" Value="2" />
|
||||||
|
</Style>
|
||||||
|
</GridView.ItemContainerStyle>
|
||||||
|
|
||||||
|
<GridView.ItemTemplate>
|
||||||
|
<DataTemplate x:DataType="model:ExplorerItem">
|
||||||
|
<Grid
|
||||||
|
Width="100"
|
||||||
|
Height="100"
|
||||||
|
Padding="8"
|
||||||
|
DoubleTapped="GridViewItem_DoubleTapped"
|
||||||
|
IsDoubleTapEnabled="True">
|
||||||
|
<Grid.RowDefinitions>
|
||||||
|
<RowDefinition Height="*" />
|
||||||
|
<RowDefinition Height="0.25*" />
|
||||||
|
</Grid.RowDefinitions>
|
||||||
|
<FontIcon FontSize="42" Glyph="{x:Bind Path, Converter={StaticResource AssetPathToGlyphConverter}}" />
|
||||||
|
<TextBlock
|
||||||
|
Grid.Row="1"
|
||||||
|
Margin="8,0"
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
Style="{StaticResource CaptionTextBlockStyle}"
|
||||||
|
Text="{x:Bind Name}"
|
||||||
|
TextTrimming="CharacterEllipsis" />
|
||||||
|
</Grid>
|
||||||
|
</DataTemplate>
|
||||||
|
</GridView.ItemTemplate>
|
||||||
|
</GridView>
|
||||||
|
</ScrollViewer>
|
||||||
|
|
||||||
|
<Grid
|
||||||
|
Grid.Row="2"
|
||||||
|
Padding="4"
|
||||||
|
HorizontalAlignment="Stretch"
|
||||||
|
VerticalAlignment="Stretch"
|
||||||
|
Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}"
|
||||||
|
BorderBrush="{ThemeResource CardStrokeColorDefaultSolid}"
|
||||||
|
BorderThickness="0,1,0,0">
|
||||||
|
<TextBlock
|
||||||
|
VerticalAlignment="Center"
|
||||||
|
HorizontalTextAlignment="Left"
|
||||||
|
Style="{StaticResource CaptionTextBlockStyle}"
|
||||||
|
Text="{x:Bind ViewModel.SelectedAsset.Path, Mode=OneWay}"
|
||||||
|
TextTrimming="CharacterEllipsis" />
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Page>
|
</Page>
|
||||||
|
|||||||
@@ -1,30 +1,25 @@
|
|||||||
using System;
|
using Ghost.Editor.ViewModels.Pages.EngineEditor;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Runtime.InteropServices.WindowsRuntime;
|
|
||||||
using Windows.Foundation;
|
|
||||||
using Windows.Foundation.Collections;
|
|
||||||
using Microsoft.UI.Xaml;
|
|
||||||
using Microsoft.UI.Xaml.Controls;
|
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.Input;
|
||||||
using Microsoft.UI.Xaml.Media;
|
|
||||||
using Microsoft.UI.Xaml.Navigation;
|
|
||||||
|
|
||||||
// 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.View.Pages.EngineEditor;
|
namespace Ghost.Editor.View.Pages.EngineEditor;
|
||||||
|
|
||||||
/// <summary>
|
internal sealed partial class ProjectPage : Page
|
||||||
/// An empty page that can be used on its own or navigated to within a Frame.
|
|
||||||
/// </summary>
|
|
||||||
public sealed partial class ProjectPage : Page
|
|
||||||
{
|
{
|
||||||
|
public ProjectViewModel ViewModel
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
public ProjectPage()
|
public ProjectPage()
|
||||||
{
|
{
|
||||||
|
ViewModel = App.GetService<ProjectViewModel>();
|
||||||
|
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void GridViewItem_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
|
||||||
|
{
|
||||||
|
ViewModel.NavigateToSelected();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using Ghost.Editor.ViewModel.Pages.Landing;
|
using Ghost.Editor.ViewModels.Pages.Landing;
|
||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
using Microsoft.UI.Xaml.Navigation;
|
using Microsoft.UI.Xaml.Navigation;
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
xmlns:converters="using:Ghost.Editor.Helpers.Converters"
|
xmlns:converters="using:Ghost.Editor.Helpers.Converters"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:data="using:Ghost.Data.Models"
|
xmlns:data="using:Ghost.Data.Models"
|
||||||
|
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
|
||||||
xmlns:local="using:Ghost.Editor.View.Pages.Landing"
|
xmlns:local="using:Ghost.Editor.View.Pages.Landing"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
NavigationCacheMode="Enabled"
|
NavigationCacheMode="Enabled"
|
||||||
@@ -73,7 +74,7 @@
|
|||||||
CornerRadius="{StaticResource OverlayCornerRadius}"
|
CornerRadius="{StaticResource OverlayCornerRadius}"
|
||||||
IsItemClickEnabled="True"
|
IsItemClickEnabled="True"
|
||||||
ItemClick="ListView_ItemClick"
|
ItemClick="ListView_ItemClick"
|
||||||
ItemsSource="{x:Bind projects}"
|
ItemsSource="{x:Bind ViewModel.projects}"
|
||||||
SelectionMode="None">
|
SelectionMode="None">
|
||||||
<ListView.ItemTemplate>
|
<ListView.ItemTemplate>
|
||||||
<DataTemplate x:DataType="data:ProjectMetadataInfo">
|
<DataTemplate x:DataType="data:ProjectMetadataInfo">
|
||||||
|
|||||||
@@ -1,68 +1,47 @@
|
|||||||
using Ghost.Data.Models;
|
using Ghost.Data.Models;
|
||||||
using Ghost.Data.Services;
|
using Ghost.Editor.ViewModels.Pages.Landing;
|
||||||
using Ghost.Editor.AppStates;
|
|
||||||
using Ghost.Editor.Services;
|
|
||||||
using Microsoft.UI.Xaml;
|
using Microsoft.UI.Xaml;
|
||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
using Microsoft.UI.Xaml.Navigation;
|
using Microsoft.UI.Xaml.Navigation;
|
||||||
using System;
|
|
||||||
using System.Collections.ObjectModel;
|
|
||||||
using System.Linq;
|
|
||||||
using Windows.ApplicationModel.DataTransfer;
|
using Windows.ApplicationModel.DataTransfer;
|
||||||
using Windows.Storage;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.View.Pages.Landing;
|
namespace Ghost.Editor.View.Pages.Landing;
|
||||||
|
|
||||||
internal sealed partial class OpenProjectPage : Page
|
internal sealed partial class OpenProjectPage : Page
|
||||||
{
|
{
|
||||||
private readonly ProjectService _projectService;
|
public OpenProjectViewModel ViewModel
|
||||||
private readonly StackedNotificationService _notificationService;
|
{
|
||||||
private readonly AppStateService _stateService;
|
get;
|
||||||
|
}
|
||||||
public readonly ObservableCollection<ProjectMetadataInfo> projects = new();
|
|
||||||
|
|
||||||
public OpenProjectPage()
|
public OpenProjectPage()
|
||||||
{
|
{
|
||||||
_notificationService = App.GetService<StackedNotificationService>();
|
ViewModel = App.GetService<OpenProjectViewModel>();
|
||||||
_projectService = App.GetService<ProjectService>();
|
|
||||||
_stateService = App.GetService<AppStateService>();
|
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateEmptyPlaceHolderVisibility()
|
protected override void OnNavigatedTo(NavigationEventArgs e)
|
||||||
{
|
|
||||||
EmptyPlaceHolder.Visibility = projects.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override async void OnNavigatedTo(NavigationEventArgs e)
|
|
||||||
{
|
{
|
||||||
base.OnNavigatedTo(e);
|
base.OnNavigatedTo(e);
|
||||||
|
ViewModel.OnNavigatedTo(e.Parameter);
|
||||||
|
}
|
||||||
|
|
||||||
projects.Clear();
|
override protected void OnNavigatedFrom(NavigationEventArgs e)
|
||||||
await foreach (var projectInfo in _projectService.LoadAllProjectAsync())
|
{
|
||||||
{
|
base.OnNavigatedFrom(e);
|
||||||
var metadata = await ProjectService.LoadMetadataAsync(projectInfo.MetadataPath);
|
ViewModel.OnNavigatedFrom();
|
||||||
if (metadata == null)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
projects.Add(new(projectInfo.MetadataPath, metadata));
|
|
||||||
}
|
|
||||||
|
|
||||||
UpdateEmptyPlaceHolderVisibility();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProjectContainer_DragEnter(object sender, DragEventArgs e)
|
private void ProjectContainer_DragEnter(object sender, DragEventArgs e)
|
||||||
{
|
{
|
||||||
DragVisual.Visibility = Visibility.Visible;
|
ViewModel.DragVisibility = Visibility.Visible;
|
||||||
EmptyPlaceHolder.Visibility = Visibility.Collapsed;
|
ViewModel.EmptyVisibility = Visibility.Collapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProjectContainer_DragLeave(object sender, DragEventArgs e)
|
private void ProjectContainer_DragLeave(object sender, DragEventArgs e)
|
||||||
{
|
{
|
||||||
DragVisual.Visibility = Visibility.Collapsed;
|
ViewModel.DragVisibility = Visibility.Collapsed;
|
||||||
UpdateEmptyPlaceHolderVisibility();
|
ViewModel.UpdateEmptyPlaceHolderVisibility();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProjectContainer_DragOver(object sender, DragEventArgs e)
|
private void ProjectContainer_DragOver(object sender, DragEventArgs e)
|
||||||
@@ -79,55 +58,14 @@ internal sealed partial class OpenProjectPage : Page
|
|||||||
|
|
||||||
private async void ProjectContainer_Drop(object sender, DragEventArgs e)
|
private async void ProjectContainer_Drop(object sender, DragEventArgs e)
|
||||||
{
|
{
|
||||||
var errorMessage = string.Empty;
|
await ViewModel.ContentDrop(e.DataView);
|
||||||
if (e.DataView.Contains(StandardDataFormats.StorageItems))
|
|
||||||
{
|
|
||||||
var items = await e.DataView.GetStorageItemsAsync();
|
|
||||||
var rootFolder = items.OfType<StorageFolder>().FirstOrDefault();
|
|
||||||
if (rootFolder != null)
|
|
||||||
{
|
|
||||||
var result = await _projectService.AddProjectFromDirectoryAsync(rootFolder.Path);
|
|
||||||
if (result.success)
|
|
||||||
{
|
|
||||||
projects.Add(result.data);
|
|
||||||
DragVisual.Visibility = Visibility.Collapsed;
|
|
||||||
goto CloseDropPanel;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
errorMessage = result.message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
errorMessage = "Unsupported data format. Please drop a folder containing a project.";
|
|
||||||
}
|
|
||||||
|
|
||||||
_notificationService.ShowNotification(errorMessage, InfoBarSeverity.Error);
|
|
||||||
|
|
||||||
CloseDropPanel:
|
|
||||||
DragVisual.Visibility = Visibility.Collapsed;
|
|
||||||
UpdateEmptyPlaceHolderVisibility();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void ListView_ItemClick(object sender, ItemClickEventArgs e)
|
private async void ListView_ItemClick(object sender, ItemClickEventArgs e)
|
||||||
{
|
{
|
||||||
if (e.ClickedItem is not ProjectMetadataInfo project)
|
if (e.ClickedItem is ProjectMetadataInfo project)
|
||||||
{
|
{
|
||||||
return;
|
await ViewModel.LoadProject(project);
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
project.Metadata.LastOpened = DateTime.Now;
|
|
||||||
await ProjectService.CreateMetadataFileAsync(project.Path, project.Metadata);
|
|
||||||
|
|
||||||
await _stateService.TransitionToAsync(StateKey.EngineEditor, project.Metadata);
|
|
||||||
}
|
|
||||||
catch (Exception exp)
|
|
||||||
{
|
|
||||||
_notificationService.ShowNotification($"Failed to load project: {exp.Message}", InfoBarSeverity.Error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,7 +43,7 @@
|
|||||||
Margin="8,0,0,0"
|
Margin="8,0,0,0"
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
Style="{StaticResource CaptionTextBlockStyle}"
|
Style="{StaticResource CaptionTextBlockStyle}"
|
||||||
Text="{x:Bind ViewModel.CurrentProject.Name, Mode=OneWay}" />
|
Text="{x:Bind ViewModel.CurrentProject.Metadata.Name, Mode=OneWay}" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
<!-- Toolbar -->
|
<!-- Toolbar -->
|
||||||
@@ -83,13 +83,26 @@
|
|||||||
<ColumnDefinition Width="Auto" />
|
<ColumnDefinition Width="Auto" />
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
<Grid
|
<TabView
|
||||||
Grid.Column="0"
|
Grid.Column="0"
|
||||||
Width="350"
|
Width="350"
|
||||||
Background="Aquamarine" />
|
HorizontalAlignment="Stretch"
|
||||||
|
VerticalAlignment="Stretch"
|
||||||
|
TabWidthMode="Compact">
|
||||||
|
<TabView.TabItems>
|
||||||
|
<TabViewItem Header="Hierarchy">
|
||||||
|
<TabViewItem.IconSource>
|
||||||
|
<FontIconSource Glyph="" />
|
||||||
|
</TabViewItem.IconSource>
|
||||||
|
<ee:HierarchyPage />
|
||||||
|
</TabViewItem>
|
||||||
|
</TabView.TabItems>
|
||||||
|
</TabView>
|
||||||
|
|
||||||
<Grid Grid.Column="1">
|
<Grid Grid.Column="1">
|
||||||
<Image Source="C:\Users\Misaki\OneDrive\Pictures\Screenshots\Screenshot 2024-07-20 021657.png" Stretch="UniformToFill" />
|
<Image Source="C:\Users\Misaki\OneDrive\Pictures\Screenshots\Screenshot 2024-07-20 021657.png" Stretch="UniformToFill" />
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
<Grid
|
<Grid
|
||||||
Grid.Column="2"
|
Grid.Column="2"
|
||||||
Width="350"
|
Width="350"
|
||||||
@@ -103,7 +116,7 @@
|
|||||||
<TabView.TabItems>
|
<TabView.TabItems>
|
||||||
<TabViewItem Header="Project">
|
<TabViewItem Header="Project">
|
||||||
<TabViewItem.IconSource>
|
<TabViewItem.IconSource>
|
||||||
<FontIconSource Glyph="" />
|
<FontIconSource Glyph="" />
|
||||||
</TabViewItem.IconSource>
|
</TabViewItem.IconSource>
|
||||||
<ee:ProjectPage />
|
<ee:ProjectPage />
|
||||||
</TabViewItem>
|
</TabViewItem>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
using Ghost.Data.Resources;
|
using Ghost.Data.Resources;
|
||||||
using Ghost.Editor.ViewModel.Windows;
|
using Ghost.Editor.ViewModels.Windows;
|
||||||
using Ghost.Engine.Resources;
|
using Ghost.Engine.Resources;
|
||||||
using WinUIEx;
|
using WinUIEx;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
namespace Ghost.Editor.ViewModel.Pages.EngineEditor;
|
|
||||||
|
|
||||||
internal class ProjectViewModel
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using CommunityToolkit.Mvvm.Input;
|
||||||
|
using Ghost.Engine.Models;
|
||||||
|
using Ghost.Engine.Services;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.ViewModels.Pages.EngineEditor;
|
||||||
|
|
||||||
|
internal partial class ConsoleViewModel : ObservableObject
|
||||||
|
{
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial ObservableCollection<LogMessage> Logs
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
} = new();
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial bool ShowInfo
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
} = true;
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial bool ShowWarning
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
} = true;
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial bool ShowError
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
} = true;
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial bool ShowStackTrace
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
} = false;
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial LogMessage? SelectedLog
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConsoleViewModel()
|
||||||
|
{
|
||||||
|
foreach (var log in Logger.Logs)
|
||||||
|
{
|
||||||
|
Logs.Add(log);
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.OnLogsUpdate += UpdateLogs;
|
||||||
|
}
|
||||||
|
|
||||||
|
~ConsoleViewModel()
|
||||||
|
{
|
||||||
|
Logger.OnLogsUpdate -= UpdateLogs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateLogs(LogChangeType updateType)
|
||||||
|
{
|
||||||
|
switch (updateType)
|
||||||
|
{
|
||||||
|
case LogChangeType.LogAdded:
|
||||||
|
Logs.Add(Logger.Logs[^1]);
|
||||||
|
break;
|
||||||
|
case LogChangeType.LogRemoved:
|
||||||
|
if (Logs.Count > 0)
|
||||||
|
{
|
||||||
|
Logs.RemoveAt(0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case LogChangeType.LogsCleared:
|
||||||
|
Logs.Clear();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
partial void OnShowStackTraceChanged(bool value)
|
||||||
|
{
|
||||||
|
Logger.HasStackTrace = value;
|
||||||
|
Logger.LogInfo($"Stack trace visibility set to {value}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
private void ClearLogs()
|
||||||
|
{
|
||||||
|
Logger.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using Ghost.Editor.Infrastructures.SceneGraph;
|
||||||
|
using Ghost.Entities;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.ViewModels.Pages.EngineEditor;
|
||||||
|
|
||||||
|
internal partial class HierarchyViewModel : ObservableObject
|
||||||
|
{
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial ObservableCollection<SceneNode> SceneList
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
private set;
|
||||||
|
} = new();
|
||||||
|
|
||||||
|
public HierarchyViewModel()
|
||||||
|
{
|
||||||
|
// Test only
|
||||||
|
var testWorld = World.Create();
|
||||||
|
var entity1 = SceneGraphHelpers.CreateEntityNode(testWorld, "entity 1");
|
||||||
|
var entity2 = SceneGraphHelpers.CreateEntityNode(testWorld, "entity 3");
|
||||||
|
var entity3 = SceneGraphHelpers.CreateEntityNode(testWorld, "entity 4");
|
||||||
|
var entity4 = SceneGraphHelpers.CreateEntityNode(testWorld, "entity 5");
|
||||||
|
var entity5 = SceneGraphHelpers.CreateEntityNode(testWorld, "entity 2");
|
||||||
|
|
||||||
|
var testScene = new SceneNode(testWorld, "Test Scene");
|
||||||
|
|
||||||
|
SceneGraphHelpers.AttachChild(testScene, entity1, entity2);
|
||||||
|
SceneGraphHelpers.AttachChild(testScene, entity1, entity3);
|
||||||
|
SceneGraphHelpers.AttachChild(testScene, entity2, entity4);
|
||||||
|
|
||||||
|
testScene.Children.Add(entity1);
|
||||||
|
testScene.Children.Add(entity5);
|
||||||
|
|
||||||
|
SceneList.Add(testScene);
|
||||||
|
}
|
||||||
|
}
|
||||||
142
Ghost.Editor/ViewModels/Pages/EngineEditor/ProjectViewModel.cs
Normal file
142
Ghost.Editor/ViewModels/Pages/EngineEditor/ProjectViewModel.cs
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using Ghost.Data.Services;
|
||||||
|
using Ghost.Editor.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.ViewModels.Pages.EngineEditor;
|
||||||
|
|
||||||
|
internal partial class ProjectViewModel : ObservableObject
|
||||||
|
{
|
||||||
|
public ObservableCollection<ExplorerItem> SubDirectories
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
} = new();
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial ObservableCollection<ExplorerItem> DirectoryAssets
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
} = new();
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial ExplorerItem? SelectedDirectory
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial ExplorerItem? SelectedAsset
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProjectViewModel()
|
||||||
|
{
|
||||||
|
if (ProjectService.CurrentProject.Metadata == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Current project is not set.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var assetsItem = new ExplorerItem("Assets", Path.Combine(Path.GetDirectoryName(ProjectService.CurrentProject.Path)!, ProjectService.ASSETS_FOLDER), true);
|
||||||
|
LoadSubFolderRecursive(ref assetsItem);
|
||||||
|
|
||||||
|
SubDirectories.Add(assetsItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LoadSubFolderRecursive(ref ExplorerItem parentItem)
|
||||||
|
{
|
||||||
|
foreach (var directory in Directory.EnumerateDirectories(parentItem.Path))
|
||||||
|
{
|
||||||
|
var item = new ExplorerItem(Path.GetFileName(directory), directory, true);
|
||||||
|
LoadSubFolderRecursive(ref item);
|
||||||
|
|
||||||
|
parentItem.Children ??= new();
|
||||||
|
parentItem.Children.Add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task<ExplorerItem?> FindNodeIterative(ExplorerItem root, Func<ExplorerItem, bool> predicate)
|
||||||
|
{
|
||||||
|
var stack = new Stack<ExplorerItem>();
|
||||||
|
stack.Push(root);
|
||||||
|
|
||||||
|
return Task.Run(() =>
|
||||||
|
{
|
||||||
|
while (stack.Count > 0)
|
||||||
|
{
|
||||||
|
var node = stack.Pop();
|
||||||
|
if (predicate(node))
|
||||||
|
{
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.Children == null || node.Children.Count == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = node.Children.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
stack.Push(node.Children[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void NavigateToDirectory(string? path)
|
||||||
|
{
|
||||||
|
App.Window?.DispatcherQueue.TryEnqueue(async () =>
|
||||||
|
{
|
||||||
|
DirectoryAssets.Clear();
|
||||||
|
|
||||||
|
if (!Directory.Exists(path))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var directory in Directory.EnumerateDirectories(path))
|
||||||
|
{
|
||||||
|
var directoryItem = new ExplorerItem(Path.GetFileName(directory), directory, true);
|
||||||
|
DirectoryAssets.Add(directoryItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var file in Directory.EnumerateFiles(path))
|
||||||
|
{
|
||||||
|
var fileItem = new ExplorerItem(Path.GetFileName(file), file, false);
|
||||||
|
DirectoryAssets.Add(fileItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
SelectedDirectory = await FindNodeIterative(SubDirectories[0], x => x.Path == path);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void NavigateToSelected()
|
||||||
|
{
|
||||||
|
if (SelectedAsset == null || !SelectedAsset.IsDirectory)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NavigateToDirectory(SelectedAsset.Path);
|
||||||
|
}
|
||||||
|
|
||||||
|
partial void OnSelectedDirectoryChanged(ExplorerItem? value)
|
||||||
|
{
|
||||||
|
DirectoryAssets.Clear();
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
NavigateToDirectory(value.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,9 @@
|
|||||||
using CommunityToolkit.Mvvm.Input;
|
using CommunityToolkit.Mvvm.Input;
|
||||||
using Ghost.Data.Models;
|
using Ghost.Data.Models;
|
||||||
using Ghost.Data.Services;
|
using Ghost.Data.Services;
|
||||||
using Ghost.Editor.AppStates;
|
|
||||||
using Ghost.Editor.Contracts;
|
using Ghost.Editor.Contracts;
|
||||||
using Ghost.Editor.Helpers;
|
using Ghost.Editor.Helpers;
|
||||||
|
using Ghost.Editor.Infrastructures.AppState;
|
||||||
using Ghost.Editor.Services;
|
using Ghost.Editor.Services;
|
||||||
using Ghost.Engine.Resources;
|
using Ghost.Engine.Resources;
|
||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
@@ -13,9 +13,9 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Ghost.Editor.ViewModel.Pages.Landing;
|
namespace Ghost.Editor.ViewModels.Pages.Landing;
|
||||||
|
|
||||||
internal partial class CreateProjectViewModel(StackedNotificationService notificationService, ProjectService projectService, AppStateService stateService) : ObservableRecipient, INavigationAware
|
internal partial class CreateProjectViewModel(StackedNotificationService notificationService, ProjectService projectService, AppStateMachine stateService) : ObservableObject, INavigationAware
|
||||||
{
|
{
|
||||||
public ObservableCollection<TemplateData> templates = new();
|
public ObservableCollection<TemplateData> templates = new();
|
||||||
|
|
||||||
@@ -77,16 +77,15 @@ internal partial class CreateProjectViewModel(StackedNotificationService notific
|
|||||||
}
|
}
|
||||||
|
|
||||||
var result = await projectService.CreateProjectAsync(ProjectName, ProjectLocation, EngineData.s_engineVersion, SelectedTemplate.Value.directory);
|
var result = await projectService.CreateProjectAsync(ProjectName, ProjectLocation, EngineData.s_engineVersion, SelectedTemplate.Value.directory);
|
||||||
if (!result.success || result.data == null)
|
if (!result.success)
|
||||||
{
|
{
|
||||||
notificationService.ShowNotification(result.message, InfoBarSeverity.Error);
|
notificationService.ShowNotification(result.message, InfoBarSeverity.Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var metadata = await ProjectService.LoadMetadataAsync(result.data.MetadataPath); // Metadata should not be null here if create project succeeded
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await stateService.TransitionToAsync(StateKey.EngineEditor, metadata);
|
await stateService.TransitionToAsync(StateKey.EngineEditor, result.data);
|
||||||
}
|
}
|
||||||
catch (System.Exception e)
|
catch (System.Exception e)
|
||||||
{
|
{
|
||||||
109
Ghost.Editor/ViewModels/Pages/Landing/OpenProjectViewModel.cs
Normal file
109
Ghost.Editor/ViewModels/Pages/Landing/OpenProjectViewModel.cs
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using Ghost.Data.Models;
|
||||||
|
using Ghost.Data.Services;
|
||||||
|
using Ghost.Editor.Contracts;
|
||||||
|
using Ghost.Editor.Infrastructures.AppState;
|
||||||
|
using Ghost.Editor.Services;
|
||||||
|
using Microsoft.UI.Xaml;
|
||||||
|
using Microsoft.UI.Xaml.Controls;
|
||||||
|
using System;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Windows.ApplicationModel.DataTransfer;
|
||||||
|
using Windows.Storage;
|
||||||
|
|
||||||
|
namespace Ghost.Editor.ViewModels.Pages.Landing;
|
||||||
|
|
||||||
|
internal partial class OpenProjectViewModel(ProjectService projectService, StackedNotificationService _notificationService, AppStateMachine _stateService) : ObservableObject, INavigationAware
|
||||||
|
{
|
||||||
|
public readonly ObservableCollection<ProjectMetadataInfo> projects = new();
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial Visibility EmptyVisibility
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
public partial Visibility DragVisibility
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateEmptyPlaceHolderVisibility()
|
||||||
|
{
|
||||||
|
EmptyVisibility = projects.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void OnNavigatedTo(object? parameter)
|
||||||
|
{
|
||||||
|
projects.Clear();
|
||||||
|
await foreach (var projectInfo in projectService.LoadAllProjectAsync())
|
||||||
|
{
|
||||||
|
var metadata = await ProjectService.LoadMetadataAsync(projectInfo.MetadataPath);
|
||||||
|
if (metadata == null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
projects.Add(new(projectInfo.MetadataPath, metadata));
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateEmptyPlaceHolderVisibility();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnNavigatedFrom()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task ContentDrop(DataPackageView dataView)
|
||||||
|
{
|
||||||
|
var errorMessage = string.Empty;
|
||||||
|
if (dataView.Contains(StandardDataFormats.StorageItems))
|
||||||
|
{
|
||||||
|
var items = await dataView.GetStorageItemsAsync();
|
||||||
|
var rootFolder = items.OfType<StorageFolder>().FirstOrDefault();
|
||||||
|
if (rootFolder != null)
|
||||||
|
{
|
||||||
|
var result = await projectService.AddProjectFromDirectoryAsync(rootFolder.Path);
|
||||||
|
if (result.success)
|
||||||
|
{
|
||||||
|
projects.Add(result.data);
|
||||||
|
goto CloseDropPanel;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
errorMessage = result.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
errorMessage = "Unsupported data format. Please drop a folder containing a project.";
|
||||||
|
}
|
||||||
|
|
||||||
|
_notificationService.ShowNotification(errorMessage, InfoBarSeverity.Error);
|
||||||
|
|
||||||
|
CloseDropPanel:
|
||||||
|
DragVisibility = Visibility.Collapsed;
|
||||||
|
UpdateEmptyPlaceHolderVisibility();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task LoadProject(ProjectMetadataInfo project)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
project.Metadata.LastOpened = DateTime.Now;
|
||||||
|
await ProjectService.CreateMetadataFileAsync(project.Path, project.Metadata);
|
||||||
|
|
||||||
|
await _stateService.TransitionToAsync(StateKey.EngineEditor, project);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_notificationService.ShowNotification($"Failed to load project: {e.Message}", InfoBarSeverity.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,13 @@
|
|||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
using Ghost.Data.Models;
|
using Ghost.Data.Models;
|
||||||
|
using Ghost.Data.Services;
|
||||||
using Ghost.Engine.Resources;
|
using Ghost.Engine.Resources;
|
||||||
|
|
||||||
namespace Ghost.Editor.ViewModel.Windows;
|
namespace Ghost.Editor.ViewModels.Windows;
|
||||||
|
|
||||||
internal partial class EngineEditorViewModel : ObservableRecipient
|
internal partial class EngineEditorViewModel : ObservableRecipient
|
||||||
{
|
{
|
||||||
public string engineVersionDescriptor = $"{EngineData.ENGINE_NAME} - {EngineData.s_engineVersion}";
|
public string engineVersionDescriptor = $"{EngineData.ENGINE_NAME} - {EngineData.s_engineVersion}";
|
||||||
|
|
||||||
[ObservableProperty]
|
public ProjectMetadataInfo CurrentProject => ProjectService.CurrentProject;
|
||||||
public partial ProjectMetadata CurrentProject
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
24
Ghost.Engine/Components/Hierarchy.cs
Normal file
24
Ghost.Engine/Components/Hierarchy.cs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
using Ghost.Entities;
|
||||||
|
using Ghost.Entities.Components;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace Ghost.Engine.Components;
|
||||||
|
|
||||||
|
[SkipLocalsInit]
|
||||||
|
public struct Hierarchy : IComponentData
|
||||||
|
{
|
||||||
|
public Entity parent;
|
||||||
|
public Entity firstChild;
|
||||||
|
public Entity nextSibling;
|
||||||
|
|
||||||
|
public static Hierarchy Root
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => new()
|
||||||
|
{
|
||||||
|
parent = Entity.Invalid,
|
||||||
|
firstChild = Entity.Invalid,
|
||||||
|
nextSibling = Entity.Invalid
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
21
Ghost.Engine/Components/LocalToWorld.cs
Normal file
21
Ghost.Engine/Components/LocalToWorld.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using Ghost.Engine.Helpers;
|
||||||
|
using Ghost.Entities.Components;
|
||||||
|
using System.Numerics;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace Ghost.Engine.Components;
|
||||||
|
|
||||||
|
[SkipLocalsInit]
|
||||||
|
public struct LocalToWorld : IComponentData
|
||||||
|
{
|
||||||
|
public Matrix4x4 matrix;
|
||||||
|
|
||||||
|
public static LocalToWorld Identity
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => new()
|
||||||
|
{
|
||||||
|
matrix = MatrixHelpers.CreateTRS(Vector3.Zero, Quaternion.Identity, Vector3.One)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
using Ghost.Engine.Helpers;
|
|
||||||
using Ghost.Entities;
|
|
||||||
using System.Numerics;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Ghost.Engine.Components;
|
|
||||||
|
|
||||||
public struct Transform : IComponentData
|
|
||||||
{
|
|
||||||
private Vector3 _position = Vector3.Zero;
|
|
||||||
public Vector3 Position
|
|
||||||
{
|
|
||||||
get => _position;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_position = value;
|
|
||||||
hasChanged = true;
|
|
||||||
UpdateMatrices();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Quaternion _rotation = Quaternion.Identity;
|
|
||||||
public Quaternion Rotation
|
|
||||||
{
|
|
||||||
get => _rotation;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_rotation = value;
|
|
||||||
hasChanged = true;
|
|
||||||
UpdateMatrices();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Vector3 _scale = Vector3.One;
|
|
||||||
public Vector3 Scale
|
|
||||||
{
|
|
||||||
get => _scale;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
_scale = value;
|
|
||||||
hasChanged = true;
|
|
||||||
UpdateMatrices();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool hasChanged;
|
|
||||||
|
|
||||||
private Matrix4x4 _localToWorldMatrix;
|
|
||||||
private Matrix4x4 _worldToLocalMatrix;
|
|
||||||
|
|
||||||
public readonly Matrix4x4 LocalToWorldMatrix => _localToWorldMatrix;
|
|
||||||
public readonly Matrix4x4 WorldToLocalMatrix => _worldToLocalMatrix;
|
|
||||||
|
|
||||||
public static Transform Default
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get => new(Vector3.Zero, Quaternion.Identity, Vector3.One);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Transform(Vector3 position, Quaternion rotation, Vector3 scale)
|
|
||||||
{
|
|
||||||
_position = position;
|
|
||||||
_rotation = rotation;
|
|
||||||
_scale = scale;
|
|
||||||
hasChanged = false;
|
|
||||||
_localToWorldMatrix = Matrix4x4.Identity;
|
|
||||||
_worldToLocalMatrix = Matrix4x4.Identity;
|
|
||||||
|
|
||||||
UpdateMatrices();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void UpdateMatrices()
|
|
||||||
{
|
|
||||||
_localToWorldMatrix = MatrixHelpers.CreateTRS(_position, _rotation, _scale);
|
|
||||||
Matrix4x4.Invert(_localToWorldMatrix, out _worldToLocalMatrix);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,25 +1,16 @@
|
|||||||
using Ghost.Engine.Models;
|
using Ghost.Engine.Models;
|
||||||
|
using Ghost.Engine.Services;
|
||||||
|
|
||||||
namespace Ghost.Engine;
|
namespace Ghost.Engine;
|
||||||
|
|
||||||
internal class EngineCore
|
internal class EngineCore : IDisposable, IAsyncDisposable
|
||||||
{
|
{
|
||||||
public static EngineCore? Current
|
public async Task StartAsync(LaunchArgument args)
|
||||||
{
|
{
|
||||||
get;
|
|
||||||
private set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task StartAsync(LaunchArgument args)
|
|
||||||
{
|
|
||||||
if (Current != null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Current = new EngineCore();
|
|
||||||
|
|
||||||
ActivationHandler.Handle(args);
|
ActivationHandler.Handle(args);
|
||||||
|
|
||||||
|
Logger.LogInfo("Engine started successfully.");
|
||||||
|
|
||||||
await Task.CompletedTask;
|
await Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,4 +18,14 @@ internal class EngineCore
|
|||||||
{
|
{
|
||||||
await Task.CompletedTask;
|
await Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
ShutDownAsync().GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask DisposeAsync()
|
||||||
|
{
|
||||||
|
await ShutDownAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
56
Ghost.Engine/Models/LogMessage.cs
Normal file
56
Ghost.Engine/Models/LogMessage.cs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace Ghost.Engine.Models;
|
||||||
|
|
||||||
|
public enum LogLevel
|
||||||
|
{
|
||||||
|
Info,
|
||||||
|
Warning,
|
||||||
|
Error
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class LogMessage
|
||||||
|
{
|
||||||
|
public LogLevel Level
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Message
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StackTrace? StackTrace
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTime Timestamp
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LogMessage(LogLevel level, string message, StackTrace? stackTrace = null)
|
||||||
|
{
|
||||||
|
Level = level;
|
||||||
|
Message = message;
|
||||||
|
StackTrace = stackTrace;
|
||||||
|
Timestamp = DateTime.Now;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"{Timestamp:HH:mm:ss} [{Level}] {Message}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ToStringWithStackTrace()
|
||||||
|
{
|
||||||
|
if (StackTrace == null)
|
||||||
|
{
|
||||||
|
return ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $"{ToString()}\n{StackTrace}";
|
||||||
|
}
|
||||||
|
}
|
||||||
73
Ghost.Engine/Services/Logger.cs
Normal file
73
Ghost.Engine/Services/Logger.cs
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
using Ghost.Engine.Models;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace Ghost.Engine.Services;
|
||||||
|
|
||||||
|
internal enum LogChangeType
|
||||||
|
{
|
||||||
|
LogAdded,
|
||||||
|
LogRemoved,
|
||||||
|
LogsCleared
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Logger
|
||||||
|
{
|
||||||
|
|
||||||
|
private const int _MAX_LOGS = 4096;
|
||||||
|
|
||||||
|
private static readonly List<LogMessage> _logs = new();
|
||||||
|
internal static List<LogMessage> Logs => _logs;
|
||||||
|
|
||||||
|
internal static event Action<LogChangeType>? OnLogsUpdate;
|
||||||
|
|
||||||
|
internal static bool HasStackTrace
|
||||||
|
{
|
||||||
|
get; set;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void LogInternal(LogLevel level, string message, int skipFrame)
|
||||||
|
{
|
||||||
|
if (_logs.Count >= _MAX_LOGS)
|
||||||
|
{
|
||||||
|
_logs.RemoveAt(0);
|
||||||
|
OnLogsUpdate?.Invoke(LogChangeType.LogRemoved);
|
||||||
|
}
|
||||||
|
|
||||||
|
StackTrace? stackTrace = null;
|
||||||
|
if (HasStackTrace)
|
||||||
|
{
|
||||||
|
stackTrace = new StackTrace(skipFrame, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
var logMessage = new LogMessage(level, message, stackTrace);
|
||||||
|
_logs.Add(logMessage);
|
||||||
|
|
||||||
|
OnLogsUpdate?.Invoke(LogChangeType.LogAdded);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Log(LogLevel level, string message)
|
||||||
|
{
|
||||||
|
LogInternal(level, message, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void LogInfo(string message)
|
||||||
|
{
|
||||||
|
LogInternal(LogLevel.Info, message, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void LogWarning(string message)
|
||||||
|
{
|
||||||
|
LogInternal(LogLevel.Warning, message, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void LogError(string message)
|
||||||
|
{
|
||||||
|
LogInternal(LogLevel.Error, message, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void Clear()
|
||||||
|
{
|
||||||
|
_logs.Clear();
|
||||||
|
OnLogsUpdate?.Invoke(LogChangeType.LogsCleared);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
global using EntityID = System.Int32;
|
global using EntityID = System.Int32;
|
||||||
global using GenerationID = System.UInt16;
|
global using GenerationID = System.Byte;
|
||||||
global using WorldID = System.UInt16;
|
global using WorldID = System.UInt16;
|
||||||
|
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|||||||
@@ -3,12 +3,7 @@ using Misaki.HighPerformance.Unsafe.Collections;
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace Ghost.Entities;
|
namespace Ghost.Entities.Components;
|
||||||
|
|
||||||
public interface IComponentData
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static class SingletonContainer<T>
|
internal static class SingletonContainer<T>
|
||||||
where T : struct, IComponentData
|
where T : struct, IComponentData
|
||||||
@@ -50,7 +45,7 @@ internal class ComponentPool<T> : IComponentPool<T>
|
|||||||
|
|
||||||
public EntityID Count => _nextId;
|
public EntityID Count => _nextId;
|
||||||
|
|
||||||
public ComponentPool(int initialSize = 16)
|
public ComponentPool(int initialSize)
|
||||||
{
|
{
|
||||||
_nextId = 0;
|
_nextId = 0;
|
||||||
_capacity = initialSize;
|
_capacity = initialSize;
|
||||||
@@ -144,7 +139,7 @@ internal class ComponentPool<T> : IComponentPool<T>
|
|||||||
return index != Entity.INVALID_ID && _components[index].owner.Generation == entity.Generation;
|
return index != Entity.INVALID_ID && _components[index].owner.Generation == entity.Generation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Set(Entity entity, T component)
|
public void Set(Entity entity, in T component)
|
||||||
{
|
{
|
||||||
if (!entity.IsValid || entity.ID >= _lookup.Length || GetComponentIndex(entity) == Entity.INVALID_ID)
|
if (!entity.IsValid || entity.ID >= _lookup.Length || GetComponentIndex(entity) == Entity.INVALID_ID)
|
||||||
{
|
{
|
||||||
@@ -334,7 +329,8 @@ internal class ScriptComponentPool : IComponentPool<ScriptComponent>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class ComponentStorage : IDisposable
|
[SkipLocalsInit]
|
||||||
|
internal readonly struct ComponentStorage : IDisposable
|
||||||
{
|
{
|
||||||
private readonly Dictionary<nint, IComponentPool> _componentPools = new();
|
private readonly Dictionary<nint, IComponentPool> _componentPools = new();
|
||||||
private readonly Dictionary<nint, BitSet> _componentEntityMasks = new();
|
private readonly Dictionary<nint, BitSet> _componentEntityMasks = new();
|
||||||
@@ -360,25 +356,25 @@ internal class ComponentStorage : IDisposable
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public bool TryGetPool(Type type, [MaybeNullWhen(false)] out IComponentPool pool)
|
public bool TryGetPool(Type type, [MaybeNullWhen(false)] out IComponentPool pool)
|
||||||
{
|
{
|
||||||
return TryGetPool(type.TypeHandle.Value, out pool);
|
return TryGetPool(TypeHandle.Get(type), out pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public bool TryGetPool<T>([MaybeNullWhen(false)] out ComponentPool<T> pool)
|
public bool TryGetPool<T>([MaybeNullWhen(false)] out ComponentPool<T> pool)
|
||||||
where T : struct, IComponentData
|
where T : struct, IComponentData
|
||||||
{
|
{
|
||||||
var result = TryGetPool(TypeHandle<T>.Value, out var obj);
|
var result = TryGetPool(TypeHandle.Get<T>(), out var obj);
|
||||||
pool = (ComponentPool<T>?)obj;
|
pool = (ComponentPool<T>?)obj ?? default;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ComponentPool<T> GetOrCreateComponentPool<T>()
|
public ComponentPool<T> GetOrCreateComponentPool<T>()
|
||||||
where T : struct, IComponentData
|
where T : struct, IComponentData
|
||||||
{
|
{
|
||||||
var key = TypeHandle<T>.Value;
|
var key = TypeHandle.Get<T>();
|
||||||
if (!_componentPools.TryGetValue(key, out var obj))
|
if (!_componentPools.TryGetValue(key, out var obj))
|
||||||
{
|
{
|
||||||
var pool = new ComponentPool<T>();
|
var pool = new ComponentPool<T>(16);
|
||||||
_componentPools[key] = pool;
|
_componentPools[key] = pool;
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
@@ -402,7 +398,7 @@ internal class ComponentStorage : IDisposable
|
|||||||
public bool TryGetMask<T>([MaybeNullWhen(false)] out BitSet bitSet)
|
public bool TryGetMask<T>([MaybeNullWhen(false)] out BitSet bitSet)
|
||||||
where T : struct, IComponentData
|
where T : struct, IComponentData
|
||||||
{
|
{
|
||||||
return TryGetMask(TypeHandle<T>.Value, out bitSet);
|
return TryGetMask(TypeHandle.Get<T>(), out bitSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BitSet GetOrCreateMask(nint typeHandle)
|
public BitSet GetOrCreateMask(nint typeHandle)
|
||||||
4
Ghost.Entities/Components/IComponentData.cs
Normal file
4
Ghost.Entities/Components/IComponentData.cs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
namespace Ghost.Entities.Components;
|
||||||
|
public interface IComponentData
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace Ghost.Entities;
|
namespace Ghost.Entities.Components;
|
||||||
|
|
||||||
public abstract class ScriptComponent : IComponentData
|
public abstract class ScriptComponent : IComponentData
|
||||||
{
|
{
|
||||||
@@ -1,18 +1,14 @@
|
|||||||
using Ghost.Entities.Query;
|
using System.Runtime.CompilerServices;
|
||||||
using Ghost.Entities.Utilities;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace Ghost.Entities;
|
namespace Ghost.Entities;
|
||||||
|
|
||||||
[SkipLocalsInit]
|
[SkipLocalsInit]
|
||||||
public struct Entity : IEquatable<Entity>, IComparable<Entity>
|
public struct Entity : IEquatable<Entity>, IComparable<Entity>
|
||||||
{
|
{
|
||||||
public const int WORLD_INDEX_BITS = 4;
|
// Is 256 generations enough? If not, increase the size of GenerationID or make generation as a separate int field.
|
||||||
public const int GENERATION_BITS = 8;
|
public const int GENERATION_BITS = sizeof(GenerationID) * 8;
|
||||||
public const int INDEX_BITS = sizeof(EntityID) * 8 - WORLD_INDEX_BITS - GENERATION_BITS;
|
public const int INDEX_BITS = sizeof(EntityID) * 8 - GENERATION_BITS;
|
||||||
|
|
||||||
private const int _WORLD_INDEX_MASK = (1 << WORLD_INDEX_BITS) - 1;
|
|
||||||
private const int _GENERATION_MASK = (1 << GENERATION_BITS) - 1;
|
private const int _GENERATION_MASK = (1 << GENERATION_BITS) - 1;
|
||||||
private const int _INDEX_MASK = (1 << INDEX_BITS) - 1;
|
private const int _INDEX_MASK = (1 << INDEX_BITS) - 1;
|
||||||
|
|
||||||
@@ -38,24 +34,18 @@ public struct Entity : IEquatable<Entity>, IComparable<Entity>
|
|||||||
get => (GenerationID)(_id >> INDEX_BITS & _GENERATION_MASK);
|
get => (GenerationID)(_id >> INDEX_BITS & _GENERATION_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly WorldID WorldID
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get => (WorldID)(_id >> (INDEX_BITS + GENERATION_BITS) & _WORLD_INDEX_MASK);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Entity Invalid
|
public static Entity Invalid
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get => new(INVALID_ID, 0, 0);
|
get => new(INVALID_ID, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal Entity(EntityID id, GenerationID generation, WorldID worldID)
|
internal Entity(EntityID id, GenerationID generation)
|
||||||
{
|
{
|
||||||
_id = worldID << (INDEX_BITS + GENERATION_BITS) | generation << INDEX_BITS | id;
|
_id = generation << INDEX_BITS | id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void IncrementGeneration()
|
internal void IncrementGeneration()
|
||||||
{
|
{
|
||||||
var generation = Generation + 1;
|
var generation = Generation + 1;
|
||||||
if (generation >= _GENERATION_MASK)
|
if (generation >= _GENERATION_MASK)
|
||||||
@@ -98,263 +88,6 @@ public struct Entity : IEquatable<Entity>, IComparable<Entity>
|
|||||||
|
|
||||||
public override readonly string ToString()
|
public override readonly string ToString()
|
||||||
{
|
{
|
||||||
return $"Entity {{ Index: {ID}, Generation: {Generation}, WorldIndex: {WorldID} }}";
|
return $"Entity {{ Index: {ID}, Generation: {Generation} }}";
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class EntityManager : IDisposable
|
|
||||||
{
|
|
||||||
private readonly List<Entity> _entities;
|
|
||||||
private readonly Queue<EntityID> _freeEntitySlots;
|
|
||||||
|
|
||||||
private readonly World _world;
|
|
||||||
|
|
||||||
public int EntityCount => _entities.Count;
|
|
||||||
public ReadOnlySpan<Entity> Entities => CollectionsMarshal.AsSpan(_entities);
|
|
||||||
|
|
||||||
public event Action<Entity, Type>? OnComponentAdded;
|
|
||||||
public event Action<Entity, Type>? OnComponentRemoved;
|
|
||||||
public event Action<Entity>? OnEntityCreated;
|
|
||||||
public event Action<EntityID>? OnEntityRemoved;
|
|
||||||
|
|
||||||
internal EntityManager(World world, int initialCapacity)
|
|
||||||
{
|
|
||||||
_entities = new(initialCapacity);
|
|
||||||
_freeEntitySlots = new(initialCapacity);
|
|
||||||
_world = world;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a new <see cref="Entity"/> to the world.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The created <see cref="Entity"/>.</returns>
|
|
||||||
public Entity CreateEntity()
|
|
||||||
{
|
|
||||||
Entity entity;
|
|
||||||
if (_freeEntitySlots.TryDequeue(out var id))
|
|
||||||
{
|
|
||||||
entity = _entities[id];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
id = _entities.Count;
|
|
||||||
entity = new Entity(id, 0, _world.ID);
|
|
||||||
_entities.Add(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
OnEntityCreated?.Invoke(entity);
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes the specified <see cref="Entity"/> from the world.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="entity"></param>
|
|
||||||
public void RemoveEntity(ref Entity entity)
|
|
||||||
{
|
|
||||||
if (entity.ID >= _entities.Count || _entities[entity.ID].Generation != entity.Generation)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_world.ComponentStorage.Remove(entity);
|
|
||||||
|
|
||||||
var slot = _entities[entity.ID];
|
|
||||||
slot.IncrementGeneration();
|
|
||||||
_entities[entity.ID] = slot;
|
|
||||||
_freeEntitySlots.Enqueue(entity.ID);
|
|
||||||
|
|
||||||
OnEntityRemoved?.Invoke(entity.ID);
|
|
||||||
entity = Entity.Invalid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if the given <see cref="Entity"/> is valid and belongs to this <see cref="World"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="entity">The entity to check.</param>
|
|
||||||
/// <returns>True if the entity is valid and belongs to this world; otherwise, false.</returns>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public bool HasEntity(Entity entity)
|
|
||||||
{
|
|
||||||
if (!entity.IsValid
|
|
||||||
|| entity.WorldID != _world.ID
|
|
||||||
|| entity.ID >= _entities.Count)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return _entities[entity.ID].Generation == entity.Generation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a component of type <typeparamref name="T"/> to the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the component to set.</typeparam>
|
|
||||||
/// <param name="entity">The entity for which the component is to be add.</param>
|
|
||||||
/// <param name="component">The component value to add.</param>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void AddComponent<T>(Entity entity, T component)
|
|
||||||
where T : struct, IComponentData
|
|
||||||
{
|
|
||||||
_world.ComponentStorage.GetOrCreateComponentPool<T>().Add(entity, component);
|
|
||||||
_world.ComponentStorage.GetOrCreateMask(TypeHandle<T>.Value).SetBit(entity.ID);
|
|
||||||
OnComponentAdded?.Invoke(entity, typeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes a component of type <typeparamref name="T"/> from the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the component to remove.</typeparam>
|
|
||||||
/// <param name="entity">The entity for which the component is to be remove.</param>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public bool RemoveComponent<T>(Entity entity)
|
|
||||||
where T : struct, IComponentData
|
|
||||||
{
|
|
||||||
if (!_world.ComponentStorage.TryGetPool<T>(out var pool) || !pool.Has(entity))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!pool.Remove(entity))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
_world.ComponentStorage.GetOrCreateMask(TypeHandle<T>.Value).ClearBit(entity.ID);
|
|
||||||
OnComponentRemoved?.Invoke(entity, typeof(T));
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sets a component of type <typeparamref name="T"/> for the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the component to set.</typeparam>
|
|
||||||
/// <param name="entity">The entity for which the component is to be set.</param>
|
|
||||||
/// <param name="component">The component value to set.</param>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void SetComponent<T>(Entity entity, T component)
|
|
||||||
where T : struct, IComponentData
|
|
||||||
{
|
|
||||||
_world.ComponentStorage.GetOrCreateComponentPool<T>().Set(entity, component);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if the given <see cref="Entity"/> has a component of the specified type.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="entity">The entity to check.</param>
|
|
||||||
/// <param name="typeHandle">The handle of the component type.</param>
|
|
||||||
/// <returns>True if the entity has the component; otherwise, false.</returns>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public bool HasComponent(Entity entity, nint typeHandle)
|
|
||||||
{
|
|
||||||
return _world.ComponentStorage.TryGetMask(typeHandle, out var bitSet) && bitSet.IsSet(entity.ID);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Retrieves a reference to a component of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the component to retrieve.</typeparam>
|
|
||||||
/// <param name="entity">The entity whose component is to be retrieved.</param>
|
|
||||||
/// <returns>A <see cref="Ref{T}"/> to the component, or a null reference if the component does not exist.</returns>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public Ref<T> GetComponent<T>(Entity entity)
|
|
||||||
where T : struct, IComponentData
|
|
||||||
{
|
|
||||||
if (_world.ComponentStorage.TryGetPool<T>(out var pool) && pool.Has(entity))
|
|
||||||
{
|
|
||||||
return new Ref<T>(ref pool.GetRef(entity));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return new Ref<T>(ref Unsafe.NullRef<T>(), false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a script of type <typeparamref name="T"/> to the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the script to add.</typeparam>
|
|
||||||
/// <param name="entity">The entity to which the script is to be added.</param>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void AddScript<T>(Entity entity)
|
|
||||||
where T : ScriptComponent, new()
|
|
||||||
{
|
|
||||||
_world.ComponentStorage.ScriptComponentPool.Add(entity, new T());
|
|
||||||
OnComponentAdded?.Invoke(entity, typeof(ScriptComponent));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a script of the specified type to the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="entity">The entity to which the script is to be added.</param>
|
|
||||||
/// <param name="type">The type of the script to add.</param>
|
|
||||||
/// <exception cref="ArgumentException">Thrown if the specified type does not inherit from <see cref="ScriptComponent"/>.</exception>
|
|
||||||
/// <exception cref="InvalidOperationException">Thrown if the script instance could not be created.</exception>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void AddScript(Entity entity, Type type)
|
|
||||||
{
|
|
||||||
if (!typeof(ScriptComponent).IsAssignableFrom(type))
|
|
||||||
{
|
|
||||||
throw new ArgumentException($"Type {type} must inherit from ScriptComponent.", nameof(type));
|
|
||||||
}
|
|
||||||
|
|
||||||
var instance = (ScriptComponent?)Activator.CreateInstance(type) ?? throw new InvalidOperationException($"Failed to create instance of {type}.");
|
|
||||||
_world.ComponentStorage.ScriptComponentPool.Add(entity, instance);
|
|
||||||
OnComponentAdded?.Invoke(entity, typeof(ScriptComponent));
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool RemoveScript<T>(Entity entity)
|
|
||||||
where T : ScriptComponent
|
|
||||||
{
|
|
||||||
if (!_world.ComponentStorage.ScriptComponentPool.Remove<T>(entity))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
OnComponentRemoved?.Invoke(entity, typeof(ScriptComponent));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool RemoveScriptAt(Entity entity, int index)
|
|
||||||
{
|
|
||||||
if (!_world.ComponentStorage.ScriptComponentPool.RemoveAt(entity, index))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
OnComponentRemoved?.Invoke(entity, typeof(ScriptComponent));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Retrieves the first script of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the script to retrieve.</typeparam>
|
|
||||||
/// <param name="entity">The entity whose script is to be retrieved.</param>
|
|
||||||
/// <returns>The script of type <typeparamref name="T"/>, or null if no such script exists.</returns>
|
|
||||||
public T? GetScript<T>(Entity entity)
|
|
||||||
where T : ScriptComponent
|
|
||||||
{
|
|
||||||
return (T?)_world.ComponentStorage.ScriptComponentPool.Get(entity)?
|
|
||||||
.FirstOrDefault(script => script is T tScript);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Retrieves all scripts of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the scripts to retrieve.</typeparam>
|
|
||||||
/// <param name="entity">The entity whose scripts are to be retrieved.</param>
|
|
||||||
/// <returns>An enumerable of scripts of type <typeparamref name="T"/>.</returns>
|
|
||||||
public IEnumerable<T> GetScripts<T>(Entity entity)
|
|
||||||
where T : ScriptComponent
|
|
||||||
{
|
|
||||||
return (IEnumerable<T>?)_world.ComponentStorage.ScriptComponentPool.Get(entity)?.Where(script => script is T tScript) ?? Enumerable.Empty<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_entities.Clear();
|
|
||||||
_freeEntitySlots.Clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
262
Ghost.Entities/EntityManager.cs
Normal file
262
Ghost.Entities/EntityManager.cs
Normal file
@@ -0,0 +1,262 @@
|
|||||||
|
using Ghost.Entities.Components;
|
||||||
|
using Ghost.Entities.Query;
|
||||||
|
using Ghost.Entities.Utilities;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace Ghost.Entities;
|
||||||
|
public struct EntityManager : IDisposable
|
||||||
|
{
|
||||||
|
private readonly List<Entity> _entities;
|
||||||
|
private readonly Queue<EntityID> _freeEntitySlots;
|
||||||
|
|
||||||
|
private readonly World _world;
|
||||||
|
|
||||||
|
public readonly int EntityCount => _entities.Count;
|
||||||
|
public readonly ReadOnlySpan<Entity> Entities => CollectionsMarshal.AsSpan(_entities);
|
||||||
|
|
||||||
|
public event Action<Entity>? OnEntityCreated;
|
||||||
|
public event Action<EntityID>? OnEntityRemoved;
|
||||||
|
public event Action<Entity, Type>? OnComponentAdded;
|
||||||
|
public event Action<Entity, Type>? OnComponentRemoved;
|
||||||
|
|
||||||
|
internal EntityManager(World world, int initialCapacity)
|
||||||
|
{
|
||||||
|
_entities = new(initialCapacity);
|
||||||
|
_freeEntitySlots = new(initialCapacity);
|
||||||
|
_world = world;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a new <see cref="Entity"/> to the world.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The created <see cref="Entity"/>.</returns>
|
||||||
|
public readonly Entity CreateEntity()
|
||||||
|
{
|
||||||
|
Entity entity;
|
||||||
|
if (_freeEntitySlots.TryDequeue(out var id))
|
||||||
|
{
|
||||||
|
entity = _entities[id];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
id = _entities.Count;
|
||||||
|
entity = new Entity(id, 0);
|
||||||
|
_entities.Add(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
OnEntityCreated?.Invoke(entity);
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the specified <see cref="Entity"/> from the world.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entity"></param>
|
||||||
|
public readonly void RemoveEntity(ref Entity entity)
|
||||||
|
{
|
||||||
|
if (entity.ID >= _entities.Count || _entities[entity.ID].Generation != entity.Generation)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_world.ComponentStorage.Remove(entity);
|
||||||
|
|
||||||
|
var slot = _entities[entity.ID];
|
||||||
|
slot.IncrementGeneration();
|
||||||
|
_entities[entity.ID] = slot;
|
||||||
|
_freeEntitySlots.Enqueue(entity.ID);
|
||||||
|
|
||||||
|
OnEntityRemoved?.Invoke(entity.ID);
|
||||||
|
entity = Entity.Invalid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the given <see cref="Entity"/> is valid and belongs to this <see cref="World"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entity">The entity to check.</param>
|
||||||
|
/// <returns>True if the entity is valid and belongs to this world; otherwise, false.</returns>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly bool HasEntity(Entity entity)
|
||||||
|
{
|
||||||
|
if (!entity.IsValid
|
||||||
|
|| entity.ID >= _entities.Count)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _entities[entity.ID].Generation == entity.Generation;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a component of type <typeparamref name="T"/> to the given <see cref="Entity"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the component to set.</typeparam>
|
||||||
|
/// <param name="entity">The entity for which the component is to be add.</param>
|
||||||
|
/// <param name="component">The component value to add.</param>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly void AddComponent<T>(Entity entity, T component)
|
||||||
|
where T : struct, IComponentData
|
||||||
|
{
|
||||||
|
_world.ComponentStorage.GetOrCreateComponentPool<T>().Add(entity, component);
|
||||||
|
_world.ComponentStorage.GetOrCreateMask(TypeHandle.Get<T>()).SetBit(entity.ID);
|
||||||
|
OnComponentAdded?.Invoke(entity, typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes a component of type <typeparamref name="T"/> from the given <see cref="Entity"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the component to remove.</typeparam>
|
||||||
|
/// <param name="entity">The entity for which the component is to be remove.</param>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly bool RemoveComponent<T>(Entity entity)
|
||||||
|
where T : struct, IComponentData
|
||||||
|
{
|
||||||
|
if (!_world.ComponentStorage.TryGetPool<T>(out var pool) || !pool.Has(entity))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pool.Remove(entity))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_world.ComponentStorage.GetOrCreateMask(TypeHandle.Get<T>()).ClearBit(entity.ID);
|
||||||
|
OnComponentRemoved?.Invoke(entity, typeof(T));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets a component of type <typeparamref name="T"/> for the given <see cref="Entity"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the component to set.</typeparam>
|
||||||
|
/// <param name="entity">The entity for which the component is to be set.</param>
|
||||||
|
/// <param name="component">The component value to set.</param>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly void SetComponent<T>(Entity entity, in T component)
|
||||||
|
where T : struct, IComponentData
|
||||||
|
{
|
||||||
|
_world.ComponentStorage.GetOrCreateComponentPool<T>().Set(entity, in component);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the given <see cref="Entity"/> has a component of the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entity">The entity to check.</param>
|
||||||
|
/// <param name="typeHandle">The handle of the component type.</param>
|
||||||
|
/// <returns>True if the entity has the component; otherwise, false.</returns>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly bool HasComponent(Entity entity, nint typeHandle)
|
||||||
|
{
|
||||||
|
return _world.ComponentStorage.TryGetMask(typeHandle, out var bitSet) && bitSet.IsSet(entity.ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves a reference to a component of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the component to retrieve.</typeparam>
|
||||||
|
/// <param name="entity">The entity whose component is to be retrieved.</param>
|
||||||
|
/// <returns>A <see cref="Ref{T}"/> to the component, or a null reference if the component does not exist.</returns>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly Ref<T> GetComponent<T>(Entity entity)
|
||||||
|
where T : struct, IComponentData
|
||||||
|
{
|
||||||
|
if (_world.ComponentStorage.TryGetPool<T>(out var pool) && pool.Has(entity))
|
||||||
|
{
|
||||||
|
return new Ref<T>(ref pool.GetRef(entity));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new Ref<T>(ref Unsafe.NullRef<T>(), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a script of type <typeparamref name="T"/> to the given <see cref="Entity"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the script to add.</typeparam>
|
||||||
|
/// <param name="entity">The entity to which the script is to be added.</param>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly void AddScript<T>(Entity entity)
|
||||||
|
where T : ScriptComponent, new()
|
||||||
|
{
|
||||||
|
_world.ComponentStorage.ScriptComponentPool.Add(entity, new T());
|
||||||
|
OnComponentAdded?.Invoke(entity, typeof(ScriptComponent));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a script of the specified type to the given <see cref="Entity"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entity">The entity to which the script is to be added.</param>
|
||||||
|
/// <param name="type">The type of the script to add.</param>
|
||||||
|
/// <exception cref="ArgumentException">Thrown if the specified type does not inherit from <see cref="ScriptComponent"/>.</exception>
|
||||||
|
/// <exception cref="InvalidOperationException">Thrown if the script instance could not be created.</exception>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly void AddScript(Entity entity, Type type)
|
||||||
|
{
|
||||||
|
if (!typeof(ScriptComponent).IsAssignableFrom(type))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"Type {type} must inherit from ScriptComponent.", nameof(type));
|
||||||
|
}
|
||||||
|
|
||||||
|
var instance = (ScriptComponent?)Activator.CreateInstance(type) ?? throw new InvalidOperationException($"Failed to create instance of {type}.");
|
||||||
|
_world.ComponentStorage.ScriptComponentPool.Add(entity, instance);
|
||||||
|
OnComponentAdded?.Invoke(entity, typeof(ScriptComponent));
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly bool RemoveScript<T>(Entity entity)
|
||||||
|
where T : ScriptComponent
|
||||||
|
{
|
||||||
|
if (!_world.ComponentStorage.ScriptComponentPool.Remove<T>(entity))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
OnComponentRemoved?.Invoke(entity, typeof(ScriptComponent));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly bool RemoveScriptAt(Entity entity, int index)
|
||||||
|
{
|
||||||
|
if (!_world.ComponentStorage.ScriptComponentPool.RemoveAt(entity, index))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
OnComponentRemoved?.Invoke(entity, typeof(ScriptComponent));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves the first script of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the script to retrieve.</typeparam>
|
||||||
|
/// <param name="entity">The entity whose script is to be retrieved.</param>
|
||||||
|
/// <returns>The script of type <typeparamref name="T"/>, or null if no such script exists.</returns>
|
||||||
|
public readonly T? GetScript<T>(Entity entity)
|
||||||
|
where T : ScriptComponent
|
||||||
|
{
|
||||||
|
return (T?)_world.ComponentStorage.ScriptComponentPool.Get(entity)?
|
||||||
|
.FirstOrDefault(script => script is T tScript);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves all scripts of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type of the scripts to retrieve.</typeparam>
|
||||||
|
/// <param name="entity">The entity whose scripts are to be retrieved.</param>
|
||||||
|
/// <returns>An enumerable of scripts of type <typeparamref name="T"/>.</returns>
|
||||||
|
public readonly IEnumerable<T> GetScripts<T>(Entity entity)
|
||||||
|
where T : ScriptComponent
|
||||||
|
{
|
||||||
|
return (IEnumerable<T>?)_world.ComponentStorage.ScriptComponentPool.Get(entity)?.Where(script => script is T tScript) ?? Enumerable.Empty<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly void Dispose()
|
||||||
|
{
|
||||||
|
_entities.Clear();
|
||||||
|
_freeEntitySlots.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,147 +0,0 @@
|
|||||||
using Ghost.Entities.Query;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Ghost.Entities.Helpers;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Provides extension methods for working with entities in the Ghost framework.
|
|
||||||
/// </summary>
|
|
||||||
public static class EntityHelpers
|
|
||||||
{
|
|
||||||
public static World GetWorld(this Entity entity)
|
|
||||||
{
|
|
||||||
return World.GetWorld(entity.WorldID);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a component of type <typeparamref name="T"/> to the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the component to set.</typeparam>
|
|
||||||
/// <param name="entity">The entity for which the component is to be add.</param>
|
|
||||||
/// <param name="component">The component value to add.</param>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static void AddComponent<T>(this Entity entity, T component)
|
|
||||||
where T : struct, IComponentData
|
|
||||||
{
|
|
||||||
var world = entity.GetWorld();
|
|
||||||
world.EntityManager.AddComponent<T>(entity, component);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Removes a component of type <typeparamref name="T"/> from the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the component to remove.</typeparam>
|
|
||||||
/// <param name="entity">The entity for which the component is to be remove.</param>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static bool RemoveComponent<T>(this Entity entity)
|
|
||||||
where T : struct, IComponentData
|
|
||||||
{
|
|
||||||
var world = entity.GetWorld();
|
|
||||||
return world.EntityManager.RemoveComponent<T>(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sets a component of type <typeparamref name="T"/> for the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the component to set.</typeparam>
|
|
||||||
/// <param name="entity">The entity for which the component is to be set.</param>
|
|
||||||
/// <param name="component">The component value to set.</param>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static void SetComponent<T>(this Entity entity, T component)
|
|
||||||
where T : struct, IComponentData
|
|
||||||
{
|
|
||||||
var world = entity.GetWorld();
|
|
||||||
world.EntityManager.SetComponent<T>(entity, component);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Checks if the given <see cref="Entity"/> has a component of the specified type.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="entity">The entity to check.</param>
|
|
||||||
/// <param name="typeHandle">The handle of the component type.</param>
|
|
||||||
/// <returns>True if the entity has the component; otherwise, false.</returns>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static bool HasComponent(this Entity entity, nint typeHandle)
|
|
||||||
{
|
|
||||||
var world = entity.GetWorld();
|
|
||||||
return world.EntityManager.HasComponent(entity, typeHandle);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Retrieves a reference to a component of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the component to retrieve.</typeparam>
|
|
||||||
/// <param name="entity">The entity whose component is to be retrieved.</param>
|
|
||||||
/// <returns>A <see cref="Ref{T}"/> to the component, or a null reference if the component does not exist.</returns>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static Ref<T> GetComponent<T>(this Entity entity)
|
|
||||||
where T : struct, IComponentData
|
|
||||||
{
|
|
||||||
var world = entity.GetWorld();
|
|
||||||
return world.EntityManager.GetComponent<T>(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a script of type <typeparamref name="T"/> to the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the script to add.</typeparam>
|
|
||||||
/// <param name="entity">The entity to which the script is to be added.</param>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static void AddScript<T>(this Entity entity)
|
|
||||||
where T : ScriptComponent, new()
|
|
||||||
{
|
|
||||||
var world = entity.GetWorld();
|
|
||||||
world.EntityManager.AddScript<T>(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a script of the specified type to the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="entity">The entity to which the script is to be added.</param>
|
|
||||||
/// <param name="type">The type of the script to add.</param>
|
|
||||||
/// <exception cref="ArgumentException">Thrown if the specified type does not inherit from <see cref="ScriptComponent"/>.</exception>
|
|
||||||
/// <exception cref="InvalidOperationException">Thrown if the script instance could not be created.</exception>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static void AddScript(this Entity entity, Type type)
|
|
||||||
{
|
|
||||||
var world = entity.GetWorld();
|
|
||||||
world.EntityManager.AddScript(entity, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Retrieves the first script of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the script to retrieve.</typeparam>
|
|
||||||
/// <param name="entity">The entity whose script is to be retrieved.</param>
|
|
||||||
/// <returns>The script of type <typeparamref name="T"/>, or null if no such script exists.</returns>
|
|
||||||
public static T? GetScript<T>(this Entity entity)
|
|
||||||
where T : ScriptComponent
|
|
||||||
{
|
|
||||||
var world = entity.GetWorld();
|
|
||||||
return world.EntityManager.GetScript<T>(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Retrieves all scripts of type <typeparamref name="T"/> associated with the given <see cref="Entity"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the scripts to retrieve.</typeparam>
|
|
||||||
/// <param name="entity">The entity whose scripts are to be retrieved.</param>
|
|
||||||
/// <returns>An enumerable of scripts of type <typeparamref name="T"/>.</returns>
|
|
||||||
public static IEnumerable<T> GetScripts<T>(this Entity entity)
|
|
||||||
where T : ScriptComponent
|
|
||||||
{
|
|
||||||
var world = entity.GetWorld();
|
|
||||||
return world.EntityManager.GetScripts<T>(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Destroys the given <see cref="Entity"/> by removing it from its associated <see cref="World"/>.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="entity">The entity to destroy.</param>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static void Destroy(this Entity entity)
|
|
||||||
{
|
|
||||||
var world = entity.GetWorld();
|
|
||||||
world.EntityManager.RemoveEntity(ref entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Runtime.CompilerServices;
|
using Ghost.Entities.Components;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
namespace Ghost.Entities.Query;
|
namespace Ghost.Entities.Query;
|
||||||
|
|
||||||
|
|||||||
@@ -1,135 +0,0 @@
|
|||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Ghost.Entities;
|
|
||||||
|
|
||||||
public abstract class SystemBase
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the execution order of the current operation or component.
|
|
||||||
/// </summary>
|
|
||||||
public virtual int ExecutionOrder => 0;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets a value indicating whether the feature is enabled.
|
|
||||||
/// </summary>
|
|
||||||
public virtual bool Enable
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = true;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The world that this system belongs to.
|
|
||||||
/// </summary>
|
|
||||||
public World World
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
internal set;
|
|
||||||
} = null!;
|
|
||||||
|
|
||||||
public virtual void OnCreate()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void OnUpdate()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void OnDestroy()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class SystemStorage : IDisposable
|
|
||||||
{
|
|
||||||
private readonly List<SystemBase> _systems = new();
|
|
||||||
private readonly List<SystemBase> _executionList = new();
|
|
||||||
|
|
||||||
private readonly World _world;
|
|
||||||
|
|
||||||
public event Action<SystemBase>? SystemAdded;
|
|
||||||
public event Action<SystemBase>? SystemRemoved;
|
|
||||||
|
|
||||||
internal SystemStorage(World world)
|
|
||||||
{
|
|
||||||
_world = world;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddSystem<T>(T system)
|
|
||||||
where T : SystemBase
|
|
||||||
{
|
|
||||||
_systems.Add(system);
|
|
||||||
system.World = _world;
|
|
||||||
if (system.Enable)
|
|
||||||
{
|
|
||||||
system.OnCreate();
|
|
||||||
}
|
|
||||||
|
|
||||||
SystemAdded?.Invoke(system);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void AddSystem<T>()
|
|
||||||
where T : SystemBase, new()
|
|
||||||
{
|
|
||||||
AddSystem(new T());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveSystem<T>(T system)
|
|
||||||
where T : SystemBase
|
|
||||||
{
|
|
||||||
system.World = null!;
|
|
||||||
_systems.Remove(system);
|
|
||||||
if (system.Enable)
|
|
||||||
{
|
|
||||||
system.OnDestroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
SystemRemoved?.Invoke(system);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public void RemoveSystem<T>()
|
|
||||||
where T : SystemBase, new()
|
|
||||||
{
|
|
||||||
var system = _systems.FirstOrDefault(s => s is T);
|
|
||||||
if (system != null)
|
|
||||||
{
|
|
||||||
RemoveSystem(system);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void RebuildExecutionList()
|
|
||||||
{
|
|
||||||
_executionList.Clear();
|
|
||||||
_executionList.AddRange(_systems.OrderBy(s => s.ExecutionOrder));
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void UpdateSystems()
|
|
||||||
{
|
|
||||||
foreach (var system in _systems)
|
|
||||||
{
|
|
||||||
if (!system.Enable)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
system.OnUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
foreach (var system in _systems)
|
|
||||||
{
|
|
||||||
if (!system.Enable)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
system.OnDestroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
_systems.Clear();
|
|
||||||
_executionList.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
27
Ghost.Entities/Systems/ISystem.cs
Normal file
27
Ghost.Entities/Systems/ISystem.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
namespace Ghost.Entities.Systems;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attribute to declare that a system depends on one or more other systems.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class, AllowMultiple = false)]
|
||||||
|
public class DependsOnAttribute : Attribute
|
||||||
|
{
|
||||||
|
public Type[] Prerequisites
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DependsOnAttribute(params Type[] prerequisites)
|
||||||
|
{
|
||||||
|
Prerequisites = prerequisites;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface ISystem
|
||||||
|
{
|
||||||
|
public void OnCreate(in SystemState state);
|
||||||
|
|
||||||
|
public void OnUpdate(in SystemState state);
|
||||||
|
|
||||||
|
public void OnDestroy(in SystemState state);
|
||||||
|
}
|
||||||
92
Ghost.Entities/Systems/SystemDependencyBuilder.cs
Normal file
92
Ghost.Entities/Systems/SystemDependencyBuilder.cs
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Ghost.Entities.Systems;
|
||||||
|
|
||||||
|
internal class SystemDependencyBuilder(List<Type> allSystemTypes)
|
||||||
|
{
|
||||||
|
private Dictionary<Type, List<Type>> _dependencies = new();
|
||||||
|
|
||||||
|
public void BuildDependencyGraph()
|
||||||
|
{
|
||||||
|
foreach (var systemType in allSystemTypes)
|
||||||
|
{
|
||||||
|
if (!typeof(ISystem).IsAssignableFrom(systemType) || systemType.IsAbstract || systemType.IsInterface)
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{systemType.Name} is not a concrete ISystem type.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var directDependencies = new List<Type>();
|
||||||
|
var dependsOnAttributes = systemType.GetCustomAttributes<DependsOnAttribute>(false);
|
||||||
|
foreach (var attr in dependsOnAttributes)
|
||||||
|
{
|
||||||
|
directDependencies.AddRange(attr.Prerequisites);
|
||||||
|
}
|
||||||
|
|
||||||
|
_dependencies[systemType] = directDependencies;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Visit(Type systemType, HashSet<Type> visited, HashSet<Type> permanentMark, List<Type> executionOrder)
|
||||||
|
{
|
||||||
|
if (permanentMark.Contains(systemType))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (visited.Contains(systemType))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Circular dependency detected involving system: {systemType.Name}");
|
||||||
|
}
|
||||||
|
|
||||||
|
visited.Add(systemType); // Mark as currently visiting
|
||||||
|
|
||||||
|
if (_dependencies.TryGetValue(systemType, out var directDependencies))
|
||||||
|
{
|
||||||
|
foreach (var dependencyType in directDependencies)
|
||||||
|
{
|
||||||
|
// Ensure the dependency is a registered system type
|
||||||
|
if (!allSystemTypes.Contains(dependencyType))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"System {systemType.Name} depends on unregistered system {dependencyType.Name}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
Visit(dependencyType, visited, permanentMark, executionOrder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
visited.Remove(systemType); // Done visiting this node in the current path
|
||||||
|
permanentMark.Add(systemType); // Mark as permanently processed
|
||||||
|
executionOrder.Add(systemType); // Add to the sorted list (this will be reversed later for correct order)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Builds the topological order of systems.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A list of system types in the order they should be executed.</returns>
|
||||||
|
/// <exception cref="InvalidOperationException">Thrown if a circular dependency is detected.</exception>"
|
||||||
|
public List<Type> BuildExecutionOrder()
|
||||||
|
{
|
||||||
|
var executionOrder = new List<Type>(allSystemTypes.Count);
|
||||||
|
var visited = new HashSet<Type>(); // Tracks visited nodes in the current DFS path (for cycle detection)
|
||||||
|
var permanentMark = new HashSet<Type>(); // Tracks nodes whose dependencies have been fully resolved
|
||||||
|
|
||||||
|
// Initialize dependencies for all registered systems, even those without explicit attributes
|
||||||
|
foreach (var sysType in allSystemTypes)
|
||||||
|
{
|
||||||
|
if (!_dependencies.ContainsKey(sysType))
|
||||||
|
{
|
||||||
|
_dependencies[sysType] = new();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var systemType in allSystemTypes)
|
||||||
|
{
|
||||||
|
if (!permanentMark.Contains(systemType))
|
||||||
|
{
|
||||||
|
Visit(systemType, visited, permanentMark, executionOrder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return executionOrder;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
Ghost.Entities/Systems/SystemState.cs
Normal file
10
Ghost.Entities/Systems/SystemState.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
namespace Ghost.Entities.Systems;
|
||||||
|
|
||||||
|
public struct SystemState
|
||||||
|
{
|
||||||
|
public World World
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
init;
|
||||||
|
}
|
||||||
|
}
|
||||||
95
Ghost.Entities/Systems/SystemStorage.cs
Normal file
95
Ghost.Entities/Systems/SystemStorage.cs
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace Ghost.Entities.Systems;
|
||||||
|
|
||||||
|
[SkipLocalsInit]
|
||||||
|
public struct SystemStorage
|
||||||
|
{
|
||||||
|
private readonly List<Type> _systems = new();
|
||||||
|
private readonly List<ISystem> _executionList = new();
|
||||||
|
|
||||||
|
private readonly World _world;
|
||||||
|
|
||||||
|
public event Action<Type>? SystemAdded;
|
||||||
|
public event Action<Type>? SystemRemoved;
|
||||||
|
|
||||||
|
internal SystemStorage(World world)
|
||||||
|
{
|
||||||
|
_world = world;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly void AddSystem(Type systemType)
|
||||||
|
{
|
||||||
|
_systems.Add(systemType);
|
||||||
|
SystemAdded?.Invoke(systemType);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly void AddSystem<T>()
|
||||||
|
where T : ISystem, new()
|
||||||
|
{
|
||||||
|
AddSystem(typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly void RemoveSystem(Type systemType)
|
||||||
|
{
|
||||||
|
_systems.Remove(systemType);
|
||||||
|
SystemRemoved?.Invoke(systemType);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public readonly void RemoveSystem<T>()
|
||||||
|
where T : ISystem, new()
|
||||||
|
{
|
||||||
|
RemoveSystem(typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void CreateSystems()
|
||||||
|
{
|
||||||
|
var builder = new SystemDependencyBuilder(_systems);
|
||||||
|
builder.BuildDependencyGraph();
|
||||||
|
var executionOrder = builder.BuildExecutionOrder();
|
||||||
|
|
||||||
|
var state = new SystemState()
|
||||||
|
{
|
||||||
|
World = _world,
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var systemType in executionOrder)
|
||||||
|
{
|
||||||
|
var system = (ISystem?)Activator.CreateInstance(systemType) ?? throw new InvalidOperationException($"Failed to create instance of system type {systemType.Name}.");
|
||||||
|
|
||||||
|
_executionList.Add(system);
|
||||||
|
system.OnCreate(in state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void UpdateSystems()
|
||||||
|
{
|
||||||
|
var state = new SystemState()
|
||||||
|
{
|
||||||
|
World = _world,
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var system in _executionList)
|
||||||
|
{
|
||||||
|
system.OnUpdate(in state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void Dispose()
|
||||||
|
{
|
||||||
|
var state = new SystemState()
|
||||||
|
{
|
||||||
|
World = _world,
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var system in _executionList)
|
||||||
|
{
|
||||||
|
system.OnDestroy(in state);
|
||||||
|
}
|
||||||
|
|
||||||
|
_systems.Clear();
|
||||||
|
_executionList.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -5,6 +5,7 @@
|
|||||||
<#@ import namespace="System.Linq" #>
|
<#@ import namespace="System.Linq" #>
|
||||||
<#@ include file="Helpers.ttinclude" #>
|
<#@ include file="Helpers.ttinclude" #>
|
||||||
|
|
||||||
|
using Ghost.Entities.Components;
|
||||||
using Ghost.Entities.Query;
|
using Ghost.Entities.Query;
|
||||||
using Ghost.Entities.Utilities;
|
using Ghost.Entities.Utilities;
|
||||||
using Misaki.HighPerformance.Unsafe.Collections;
|
using Misaki.HighPerformance.Unsafe.Collections;
|
||||||
@@ -47,11 +48,11 @@ public struct QueryEnumerable<<#= generics #>>
|
|||||||
|
|
||||||
_filters = new();
|
_filters = new();
|
||||||
<# for (int i = 0; i < arity; i++) {#>
|
<# for (int i = 0; i < arity; i++) {#>
|
||||||
_filters._all.Add(TypeHandle<T<#= i #>>.Value);
|
_filters._all.Add(TypeHandle.Get<T<#= i #>>());
|
||||||
<# } #>
|
<# } #>
|
||||||
}
|
}
|
||||||
|
|
||||||
public Enumerator GetEnumerator() => new Enumerator(_world, <#= constructorParams #>, _count, _filters);
|
public readonly Enumerator GetEnumerator() => new (_world, <#= constructorParams #>, _count, _filters);
|
||||||
|
|
||||||
public ref struct Enumerator
|
public ref struct Enumerator
|
||||||
{
|
{
|
||||||
@@ -110,7 +111,7 @@ var compRestrictions = AppendGenericRestrictions(i, "TComponent", "struct, IComp
|
|||||||
<#= compRestrictions #>
|
<#= compRestrictions #>
|
||||||
{
|
{
|
||||||
<# for (int j = 0; j < i; j++) {#>
|
<# for (int j = 0; j < i; j++) {#>
|
||||||
_filters._all.Add(TypeHandle<TComponent<#= j #>>.Value);
|
_filters._all.Add(TypeHandle.Get<TComponent<#= j #>>());
|
||||||
<# } #>
|
<# } #>
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -119,7 +120,7 @@ var compRestrictions = AppendGenericRestrictions(i, "TComponent", "struct, IComp
|
|||||||
<#= compRestrictions #>
|
<#= compRestrictions #>
|
||||||
{
|
{
|
||||||
<# for (int j = 0; j < i; j++) {#>
|
<# for (int j = 0; j < i; j++) {#>
|
||||||
_filters._any.Add(TypeHandle<TComponent<#= j #>>.Value);
|
_filters._any.Add(TypeHandle.Get<TComponent<#= j #>>());
|
||||||
<# } #>
|
<# } #>
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -128,7 +129,7 @@ var compRestrictions = AppendGenericRestrictions(i, "TComponent", "struct, IComp
|
|||||||
<#= compRestrictions #>
|
<#= compRestrictions #>
|
||||||
{
|
{
|
||||||
<# for (int j = 0; j < i; j++) {#>
|
<# for (int j = 0; j < i; j++) {#>
|
||||||
_filters._absent.Add(TypeHandle<TComponent<#= j #>>.Value);
|
_filters._absent.Add(TypeHandle.Get<TComponent<#= j #>>());
|
||||||
<# } #>
|
<# } #>
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -137,7 +138,7 @@ var compRestrictions = AppendGenericRestrictions(i, "TComponent", "struct, IComp
|
|||||||
<#= compRestrictions #>
|
<#= compRestrictions #>
|
||||||
{
|
{
|
||||||
<# for (int j = 0; j < i; j++) {#>
|
<# for (int j = 0; j < i; j++) {#>
|
||||||
_filters._disabled.Add(TypeHandle<TComponent<#= j #>>.Value);
|
_filters._disabled.Add(TypeHandle.Get<TComponent<#= j #>>());
|
||||||
<# } #>
|
<# } #>
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
|
|
||||||
|
using Ghost.Entities.Components;
|
||||||
using Ghost.Entities.Query;
|
using Ghost.Entities.Query;
|
||||||
|
|
||||||
namespace Ghost.Entities;
|
namespace Ghost.Entities;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
using Ghost.Entities.Components;
|
||||||
|
|
||||||
namespace Ghost.Entities;
|
namespace Ghost.Entities;
|
||||||
|
|
||||||
public delegate void QueryRefComponent<T0>(Entity entity, ref T0 t0Component)
|
public delegate void QueryRefComponent<T0>(Entity entity, ref T0 t0Component)
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
using Ghost.Entities.Components;
|
||||||
|
|
||||||
namespace Ghost.Entities;
|
namespace Ghost.Entities;
|
||||||
|
|
||||||
public partial class World
|
public partial class World
|
||||||
|
|||||||
@@ -1,6 +1,28 @@
|
|||||||
namespace Ghost.Entities.Utilities;
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
internal static class TypeHandle<T>
|
namespace Ghost.Entities.Utilities;
|
||||||
|
|
||||||
|
internal static class TypeHandle
|
||||||
{
|
{
|
||||||
public static nint Value => typeof(T).TypeHandle.Value;
|
/// <summary>
|
||||||
|
/// Gets the type handle for the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The type to get the handle for.</typeparam>
|
||||||
|
/// <returns>The type handle as a nint.</returns>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static nint Get<T>()
|
||||||
|
{
|
||||||
|
return typeof(T).TypeHandle.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type handle for the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">The type to get the handle for.</param>
|
||||||
|
/// <returns>The type handle as a nint.</returns>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static nint Get(Type type)
|
||||||
|
{
|
||||||
|
return type.TypeHandle.Value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
using Ghost.Entities.Query;
|
using Ghost.Entities.Components;
|
||||||
|
using Ghost.Entities.Query;
|
||||||
|
using Ghost.Entities.Systems;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
@@ -7,11 +9,9 @@ namespace Ghost.Entities;
|
|||||||
// TODO: Archetype system for better performance
|
// TODO: Archetype system for better performance
|
||||||
public partial class World
|
public partial class World
|
||||||
{
|
{
|
||||||
private static List<World> s_worlds = new(2);
|
private static List<World> s_worlds = new(4);
|
||||||
private static Queue<WorldID> s_freeWorldSlots = new();
|
private static Queue<WorldID> s_freeWorldSlots = new();
|
||||||
|
|
||||||
private static int s_maxWorldCount = (int)MathF.Pow(2, Entity.WORLD_INDEX_BITS);
|
|
||||||
|
|
||||||
public static int WorldCount => s_worlds.Count - s_freeWorldSlots.Count;
|
public static int WorldCount => s_worlds.Count - s_freeWorldSlots.Count;
|
||||||
|
|
||||||
public static World Create(int entityCapacity = 16)
|
public static World Create(int entityCapacity = 16)
|
||||||
@@ -24,7 +24,7 @@ public partial class World
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (s_worlds.Count >= s_maxWorldCount)
|
if (s_worlds.Count >= WorldID.MaxValue)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException("Maximum number of worlds reached");
|
throw new InvalidOperationException("Maximum number of worlds reached");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Ghost.Entities;
|
using Ghost.Entities;
|
||||||
using Ghost.Entities.Helpers;
|
using Ghost.Entities.Components;
|
||||||
|
using Ghost.Entities.Systems;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
var t = new Test();
|
var t = new Test();
|
||||||
@@ -15,17 +16,17 @@ public partial class Test
|
|||||||
var entity2 = world.EntityManager.CreateEntity();
|
var entity2 = world.EntityManager.CreateEntity();
|
||||||
var entity3 = world.EntityManager.CreateEntity();
|
var entity3 = world.EntityManager.CreateEntity();
|
||||||
|
|
||||||
entity1.AddComponent(new Transform { position = new Vector3(1, 2, 3) });
|
world.EntityManager.AddComponent(entity1, new Transform { position = new Vector3(1, 2, 3) });
|
||||||
entity1.AddComponent(new Mesh { index = 42 });
|
world.EntityManager.AddComponent(entity1, new Mesh { index = 42 });
|
||||||
entity1.AddScript<UIManager>();
|
world.EntityManager.AddScript<UIManager>(entity1);
|
||||||
entity1.AddScript<EventManager>();
|
world.EntityManager.AddScript<EventManager>(entity1);
|
||||||
|
|
||||||
entity2.AddComponent(new Transform { position = new Vector3(4, 5, 6) });
|
world.EntityManager.AddComponent(entity2, new Transform { position = new Vector3(4, 5, 6) });
|
||||||
entity2.AddComponent(new Mesh { index = 43 });
|
world.EntityManager.AddComponent(entity2, new Mesh { index = 43 });
|
||||||
entity2.AddScript<UserScript>();
|
world.EntityManager.AddScript<UserScript>(entity2);
|
||||||
|
|
||||||
entity3.AddComponent(new Transform { position = new Vector3(7, 8, 9) });
|
world.EntityManager.AddComponent(entity3, new Transform { position = new Vector3(7, 8, 9) });
|
||||||
entity3.AddScript<EventManager>();
|
world.EntityManager.AddScript<EventManager>(entity3);
|
||||||
|
|
||||||
foreach (var (_, transform) in world.Query<Transform>())
|
foreach (var (_, transform) in world.Query<Transform>())
|
||||||
{
|
{
|
||||||
@@ -40,62 +41,56 @@ public partial class Test
|
|||||||
world.EntityManager.RemoveEntity(ref entity2);
|
world.EntityManager.RemoveEntity(ref entity2);
|
||||||
|
|
||||||
var entity4 = world.EntityManager.CreateEntity();
|
var entity4 = world.EntityManager.CreateEntity();
|
||||||
entity4.AddComponent(new Transform { position = new Vector3(10, 11, 12) });
|
world.EntityManager.AddComponent(entity4, new Transform { position = new Vector3(10, 11, 12) });
|
||||||
entity4.AddComponent(new Mesh { index = 44 });
|
world.EntityManager.AddComponent(entity4, new Mesh { index = 44 });
|
||||||
entity4.AddScript<UserScript>();
|
world.EntityManager.AddScript<UserScript>(entity4);
|
||||||
|
|
||||||
|
world.SystemStorage.AddSystem<TestSystem2>();
|
||||||
world.SystemStorage.AddSystem<TestSystem>();
|
world.SystemStorage.AddSystem<TestSystem>();
|
||||||
|
world.SystemStorage.CreateSystems();
|
||||||
world.SystemStorage.UpdateSystems();
|
world.SystemStorage.UpdateSystems();
|
||||||
|
|
||||||
//world.SystemStorage.RebuildExecutionList();
|
|
||||||
//world.ComponentStorage.RebuildExecutionList();
|
|
||||||
|
|
||||||
//Console.WriteLine();
|
|
||||||
//Console.WriteLine("Starting scripts...");
|
|
||||||
//foreach (var component in world.QueryScript())
|
|
||||||
//{
|
|
||||||
// if (!component.Enable)
|
|
||||||
// {
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// component.Start();
|
|
||||||
//}
|
|
||||||
|
|
||||||
//Console.WriteLine();
|
|
||||||
//Console.WriteLine("Updating scripts...");
|
|
||||||
//foreach (var component in world.QueryScript())
|
|
||||||
//{
|
|
||||||
// if (!component.Enable)
|
|
||||||
// {
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// component.Update();
|
|
||||||
//}
|
|
||||||
|
|
||||||
//Console.WriteLine();
|
|
||||||
//Console.WriteLine("LateUpdating scripts...");
|
|
||||||
//foreach (var component in world.QueryScript())
|
|
||||||
//{
|
|
||||||
// if (!component.Enable)
|
|
||||||
// {
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
// component.LateUpdate();
|
|
||||||
//}
|
|
||||||
|
|
||||||
world.Dispose();
|
world.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class TestSystem : SystemBase
|
public class TestSystem : ISystem
|
||||||
{
|
{
|
||||||
public override void OnUpdate()
|
public void OnCreate(in SystemState state)
|
||||||
{
|
{
|
||||||
foreach (var (entity, transform) in World.Query<Transform>().WithAny<Mesh>())
|
}
|
||||||
|
|
||||||
|
public void OnUpdate(in SystemState state)
|
||||||
|
{
|
||||||
|
foreach (var (entity, transform) in state.World.Query<Transform>())
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Entity {entity.ID}: Transform Position = {transform.ValueRO.position}");
|
Console.WriteLine($"Entity {entity.ID}: Transform Position = {transform.ValueRO.position}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnDestroy(in SystemState state)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[DependsOn(typeof(TestSystem))]
|
||||||
|
public class TestSystem2 : ISystem
|
||||||
|
{
|
||||||
|
public void OnCreate(in SystemState state)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnUpdate(in SystemState state)
|
||||||
|
{
|
||||||
|
foreach (var (entity, mesh) in state.World.Query<Mesh>())
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Entity {entity.ID}: Mesh Index = {mesh.ValueRO.index}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnDestroy(in SystemState state)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct Transform : IComponentData
|
public struct Transform : IComponentData
|
||||||
|
|||||||
Reference in New Issue
Block a user