Copy scene when entering play mode

This commit is contained in:
Simon Lübeß
2023-03-18 23:30:57 +01:00
parent 793aa40975
commit c164aeecaa
14 changed files with 172 additions and 248 deletions
@@ -60,7 +60,7 @@ namespace GlitchyEditor.EditWindows
ShowComponentEditor<TransformComponent>("Transform", entity, => ShowTransformComponentEditor);
ShowComponentEditor<CameraComponent>("Camera", entity, => ShowCameraComponentEditor, => ShowComponentContextMenu<CameraComponent>);
ShowComponentEditor<SpriterRendererComponent>("Sprite Renderer", entity, => ShowSpriteRendererComponentEditor, => ShowComponentContextMenu<SpriterRendererComponent>);
ShowComponentEditor<SpriteRendererComponent>("Sprite Renderer", entity, => ShowSpriteRendererComponentEditor, => ShowComponentContextMenu<SpriteRendererComponent>);
ShowComponentEditor<MeshRendererComponent>("Mesh Renderer", entity, => ShowMeshRendererComponentEditor, => ShowComponentContextMenu<MeshRendererComponent>);
ShowComponentEditor<LightComponent>("Light", entity, => ShowLightComponentEditor, => ShowComponentContextMenu<LightComponent>);
ShowComponentEditor<MeshComponent>("Mesh", entity, => ShowMeshComponentEditor, => ShowComponentContextMenu<MeshComponent>);
@@ -117,18 +117,18 @@ namespace GlitchyEditor.EditWindows
private static void ShowNameComponentEditor(Entity entity)
{
if (!entity.HasComponent<DebugNameComponent>())
if (!entity.HasComponent<NameComponent>())
return;
char8[256] nameBuffer = default;
DebugNameComponent* component = entity.GetComponent<DebugNameComponent>();
NameComponent* component = entity.GetComponent<NameComponent>();
String name = null;
StringView name = null;
if(component != null)
{
name = component.DebugName;
name = component.Name;
}
else
{
@@ -142,11 +142,10 @@ namespace GlitchyEditor.EditWindows
{
if(component == null)
{
component = entity.AddComponent<DebugNameComponent>();
component = entity.AddComponent<NameComponent>();
}
component.DebugName.Clear();
component.DebugName.Append(&nameBuffer);
component.Name = StringView(&nameBuffer);
}
}
@@ -250,7 +249,7 @@ namespace GlitchyEditor.EditWindows
}
}
private static void ShowSpriteRendererComponentEditor(Entity entity, SpriterRendererComponent* spriteRendererComponent)
private static void ShowSpriteRendererComponentEditor(Entity entity, SpriteRendererComponent* spriteRendererComponent)
{
ColorRGBA spriteColor = ColorRGBA.LinearToSRGB(spriteRendererComponent.Color);
if (ImGui.ColorEdit4("Color", ref spriteColor))
@@ -548,7 +547,7 @@ namespace GlitchyEditor.EditWindows
ImGui.Separator();
ShowComponentButton<CameraComponent>("Camera");
ShowComponentButton<SpriterRendererComponent>("Sprite Renderer");
ShowComponentButton<SpriteRendererComponent>("Sprite Renderer");
ShowComponentButton<LightComponent>("Light");
ShowComponentButton<Rigidbody2DComponent>("Rigidbody 2D");
ShowComponentButton<BoxCollider2DComponent>("Box collider 2D");
@@ -301,11 +301,11 @@ namespace GlitchyEditor.EditWindows
{
String name = null;
var nameComponent = tree.Value.GetComponent<DebugNameComponent>();
var nameComponent = tree.Value.GetComponent<NameComponent>();
if(nameComponent != null)
{
name = nameComponent.DebugName;
name = scope:: .(nameComponent.Name);
}
else
{
@@ -501,13 +501,13 @@ namespace GlitchyEditor.EditWindows
{
Entity entity = .(entityId, _scene);
String name = null;
StringView name = null;
var nameComponent = entity.GetComponent<DebugNameComponent>();
var nameComponent = entity.GetComponent<NameComponent>();
if(nameComponent != null)
{
name = nameComponent.DebugName;
name = nameComponent.Name;
}
else
{
+3
View File
@@ -26,6 +26,9 @@ namespace GlitchyEditor
get => _scene;
set
{
if (_scene == value)
return;
_scene = value;
_entityHierarchyWindow.SetContext(_scene);
}
+49 -24
View File
@@ -27,7 +27,9 @@ namespace GlitchyEditor
BlendState _opaqueBlendState ~ _.ReleaseRef();
DepthStencilState _depthStencilState ~ _.ReleaseRef();
Scene _scene ~ delete _;
Scene _activeScene ~ _?.ReleaseRef();
Scene _editorScene ~ _?.ReleaseRef();
String _sceneFilePath = new String() ~ delete _;
public String SceneFilePath
@@ -78,7 +80,8 @@ namespace GlitchyEditor
InitGraphics();
_scene = new Scene();
_editorScene = new Scene();
SetReference!(_activeScene, _editorScene);
_camera = EditorCamera(Vector3(3.5f, 1.25f, 2.75f), Quaternion.FromEulerAngles(MathHelper.ToRadians(40), MathHelper.ToRadians(25), 0), MathHelper.ToRadians(75), 0.1f, 1);
_camera.RenderTarget = _cameraTarget;
@@ -163,7 +166,7 @@ namespace GlitchyEditor
private void InitEditor()
{
_editor = new Editor(_scene, _contentManager);
_editor = new Editor(_editorScene, _contentManager);
_editor.SceneViewportWindow.ViewportSizeChanged.Add(new (s, e) => ViewportSizeChanged(s, e));
_editor.CurrentCamera = &_camera;
@@ -176,6 +179,8 @@ namespace GlitchyEditor
{
_camera.Update(gameTime);
_editor.CurrentScene = _activeScene;
// Clear the swapchain-buffer
RenderCommand.Clear(null, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0);
@@ -185,9 +190,9 @@ namespace GlitchyEditor
RenderCommand.SetDepthStencilState(_depthStencilState);
if (_sceneState == .Edit)
_scene.UpdateEditor(gameTime, _camera, _viewportTarget, scope => DebugDraw3D, scope => DebugDraw2D);
_activeScene.UpdateEditor(gameTime, _camera, _viewportTarget, scope => DebugDraw3D, scope => DebugDraw2D);
else if (_sceneState == .Play)
_scene.UpdateRuntime(gameTime, _viewportTarget);
_activeScene.UpdateRuntime(gameTime, _viewportTarget);
RenderCommand.UnbindRenderTargets();
RenderCommand.SetRenderTarget(null, 0, true);
@@ -226,9 +231,9 @@ namespace GlitchyEditor
return Math.Clamp(1.5f - Vector3.Distance(_editor.CurrentCamera.Position, pos) / 50, 0, 1);
}
for (var (entity, transform, camera) in _scene.[Friend]_ecsWorld.Enumerate<TransformComponent, CameraComponent>())
for (var (entity, transform, camera) in _activeScene.[Friend]_ecsWorld.Enumerate<TransformComponent, CameraComponent>())
{
if (_editor.EntityHierarchyWindow.SelectedEntities.Contains(.(entity, _scene)))
if (_editor.EntityHierarchyWindow.SelectedEntities.Contains(.(entity, _activeScene)))
{
DebugRenderer.DrawViewFrustum(transform.WorldTransform, camera.Camera.Projection, _camera.Projection * _camera.View, .White);
}
@@ -240,9 +245,9 @@ namespace GlitchyEditor
//Renderer2D.DrawQuad(world, _iconCamera, .White, .(0, 0, 1, 1), entity.Index);
}
for (var (entity, transform, light) in _scene.[Friend]_ecsWorld.Enumerate<TransformComponent, LightComponent>())
for (var (entity, transform, light) in _activeScene.[Friend]_ecsWorld.Enumerate<TransformComponent, LightComponent>())
{
if (_editor.EntityHierarchyWindow.SelectedEntities.Contains(.(entity, _scene)))
if (_editor.EntityHierarchyWindow.SelectedEntities.Contains(.(entity, _activeScene)))
{
Renderer.DrawRay(.Zero, .(0, 0, 20), ColorRGBA(light.SceneLight.Color, 1.0f), transform.WorldTransform);
@@ -343,18 +348,30 @@ namespace GlitchyEditor
private void OnScenePlay()
{
_sceneState = .Play;
_editor.SceneViewportWindow.EditorMode = false;
_sceneState = .Play;
_scene.OnRuntimeStart();
using (Scene runtimeScene = new Scene())
{
_editorScene.CopyTo(runtimeScene);
runtimeScene.OnRuntimeStart();
SetReference!(_activeScene, runtimeScene);
}
_editor.CurrentScene = _activeScene;
}
private void OnSceneStop()
{
_scene.OnRuntimeStop();
_activeScene.OnRuntimeStop();
SetReference!(_activeScene, _editorScene);
_editor.SceneViewportWindow.EditorMode = true;
_sceneState = .Edit;
_editor.CurrentScene = _activeScene;
}
// Just for testing
@@ -488,13 +505,17 @@ namespace GlitchyEditor
/// Creates a new scene.
private void NewScene()
{
OnSceneStop();
SceneFilePath = null;
delete _scene;
_scene = new Scene();
_editor.CurrentScene = _scene;
_editorScene.ReleaseRef();
_editorScene = new Scene();
_editor.CurrentScene = _editorScene;
var vpSize = _editor.SceneViewportWindow.ViewportSize;
_scene.OnViewportResize((.)vpSize.X, (.)vpSize.Y);
_editorScene.OnViewportResize((.)vpSize.X, (.)vpSize.Y);
SetReference!(_activeScene, _editorScene);
_camera.Position = .(-1.5f, 1.5f, -2.5f);
_camera.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0);
@@ -530,7 +551,7 @@ namespace GlitchyEditor
return;
}
SceneSerializer serializer = scope .(_scene);
SceneSerializer serializer = scope .(_editorScene);
serializer.Serialize(SceneFilePath);
}
@@ -565,16 +586,20 @@ namespace GlitchyEditor
/// Loads the given scene file.
private void LoadSceneFile(StringView filename)
{
OnSceneStop();
SceneFilePath = scope String(filename);
delete _scene;
_scene = new Scene();
_editor.CurrentScene = _scene;
_editorScene.ReleaseRef();
_editorScene = new Scene();
_editor.CurrentScene = _editorScene;
var vpSize = _editor.SceneViewportWindow.ViewportSize;
_scene.OnViewportResize((.)vpSize.X, (.)vpSize.Y);
_editorScene.OnViewportResize((.)vpSize.X, (.)vpSize.Y);
SceneSerializer serializer = scope .(_scene);
SceneSerializer serializer = scope .(_editorScene);
serializer.Deserialize(SceneFilePath);
SetReference!(_activeScene, _editorScene);
}
private void DrawMainMenuBar()
@@ -648,7 +673,7 @@ namespace GlitchyEditor
_viewportTarget.Resize(sizeX, sizeY);
_cameraTarget.Resize(sizeX, sizeY);
_scene.OnViewportResize(sizeX, sizeY);
_activeScene.OnViewportResize(sizeX, sizeY);
_camera.OnViewportResize(sizeX, sizeY);
}