diff --git a/GlitchyEditor/content/Textures/EditorIcons.dds b/GlitchyEditor/content/Textures/EditorIcons.dds index 70df89a..5245d70 100644 Binary files a/GlitchyEditor/content/Textures/EditorIcons.dds and b/GlitchyEditor/content/Textures/EditorIcons.dds differ diff --git a/GlitchyEditor/content/Textures/EditorIcons.psd b/GlitchyEditor/content/Textures/EditorIcons.psd index c6e8d27..c39d285 100644 Binary files a/GlitchyEditor/content/Textures/EditorIcons.psd and b/GlitchyEditor/content/Textures/EditorIcons.psd differ diff --git a/GlitchyEditor/src/EditorIcons.bf b/GlitchyEditor/src/EditorIcons.bf index a550f62..1f13efe 100644 --- a/GlitchyEditor/src/EditorIcons.bf +++ b/GlitchyEditor/src/EditorIcons.bf @@ -16,6 +16,8 @@ namespace GlitchyEditor public SubTexture2D File ~ _.ReleaseRef(); public SubTexture2D Play ~ _.ReleaseRef(); public SubTexture2D Stop ~ _.ReleaseRef(); + public SubTexture2D Simulate ~ _.ReleaseRef(); + public SubTexture2D Pause ~ _.ReleaseRef(); public SamplerState SamplerState { @@ -35,6 +37,8 @@ namespace GlitchyEditor File = GetNextGridTexture(ref pen, iconSize); Play = GetNextGridTexture(ref pen, iconSize); Stop = GetNextGridTexture(ref pen, iconSize); + Simulate = GetNextGridTexture(ref pen, iconSize); + Pause = GetNextGridTexture(ref pen, iconSize); } private SubTexture2D GetNextGridTexture(ref Vector2 pen, Vector2 iconSize) diff --git a/GlitchyEditor/src/EditorLayer.bf b/GlitchyEditor/src/EditorLayer.bf index 4ccb9e3..dba5fc2 100644 --- a/GlitchyEditor/src/EditorLayer.bf +++ b/GlitchyEditor/src/EditorLayer.bf @@ -69,11 +69,11 @@ namespace GlitchyEditor { Edit, Play, - // Pause, - // Simulate + Simulate } SceneState _sceneState = .Edit; + bool _isPaused = false; public this(EditorContentManager contentManager) : base("Example") { @@ -197,11 +197,25 @@ namespace GlitchyEditor { Debug.Profiler.ProfileFunction!(); - _camera.Update(gameTime); - _editor.CurrentScene = _activeScene; - - _activeScene.Update(gameTime, (_sceneState == .Edit) ? .Editor : .Runtime); + + Scene.UpdateMode updateMode; + + switch (_sceneState) + { + case .Edit: + updateMode = .Editor; + case .Play: + updateMode = .Runtime; + case .Simulate: + updateMode = .Physics; + } + + if (_sceneState != .Edit && _isPaused) + updateMode = .None; + + + _activeScene.Update(gameTime, updateMode); // Clear the swapchain-buffer RenderCommand.Clear(null, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0); @@ -211,6 +225,8 @@ namespace GlitchyEditor if (_editor.SceneViewportWindow.Visible) { + _camera.Update(gameTime); + _editorSceneRenderer.Scene = _activeScene; RenderCommand.Clear(_editorViewportTarget, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0); @@ -366,22 +382,86 @@ namespace GlitchyEditor ImGui.Begin("##toolbar", null, .NoDecoration | .NoScrollbar | .NoScrollWithMouse); - SubTexture2D icon = _sceneState == .Edit ? _editorIcons.Play : _editorIcons.Stop; + float padding = 2.0f; - float size = ImGui.GetWindowHeight() - 4.0f; - - ImGui.SameLine((ImGui.GetContentRegionMax().x / 2 - size / 2)); + float size = ImGui.GetWindowHeight() - 2 * padding; - if (ImGui.ImageButton(icon, .(size, size), .Zero, .Ones, 0)) + float centerX = ImGui.GetContentRegionMax().x / 2; + + + if (_sceneState == .Edit) + EditorButtons: { - if (_sceneState == .Edit) - { + // Display the buttons for edit state + + float totalWidth = size * 3 + padding * 4; + + float penX = centerX - totalWidth / 2; + + ImGui.SameLine(penX); + + if (ImGui.ImageButton(_editorIcons.Play, .(size, size), .Zero, .Ones, 0)) OnScenePlay(); - } - else if (_sceneState == .Play) + + ImGui.AttachTooltip("Play the game."); + + ImGui.SameLine(penX += size + 2 * padding); + + ImGui.PushID(1); + + if (ImGui.ImageButton(_editorIcons.Simulate, .(size, size), .Zero, .Ones, 0)) + OnSceneSimulate(); + + ImGui.PopID(); + + ImGui.AttachTooltip("Enter simulation mode.\nThis only runs the physics engine."); + + if (_isPaused) { - OnSceneStop(); + ImGui.PushStyleColor(.Button, *ImGui.GetStyleColorVec4(.ButtonActive)); + + defer:EditorButtons { ImGui.PopStyleColor(); } } + + ImGui.PushID(2); + + ImGui.SameLine(penX += size + 2 * padding); + + if (ImGui.ImageButton(_editorIcons.Pause, .(size, size), .Zero, .Ones, 0)) + _isPaused = !_isPaused; + + ImGui.PopID(); + + ImGui.AttachTooltip("If enabled the game or simulation will be started in paused state."); + + } + else + { + // Display the buttons for play/simulation state + + ImGui.SameLine(centerX - size - padding); + + SubTexture2D pauseButtonIcon = _isPaused ? _editorIcons.Play : _editorIcons.Pause; + + if (ImGui.ImageButton(pauseButtonIcon, .(size, size), .Zero, .Ones, 0)) + { + if (_isPaused) + OnSceneResume(); + else + OnScenePause(); + } + + ImGui.AttachTooltip(_isPaused ? "Resume" : "Pause"); + + ImGui.SameLine(centerX + padding); + ImGui.PushID(1); + + if (ImGui.ImageButton(_editorIcons.Stop, .(size, size), .Zero, .Ones, 0)) + OnSceneStop(); + + ImGui.PopID(); + + ImGui.AttachTooltip("Stop"); } ImGui.End(); @@ -407,15 +487,48 @@ namespace GlitchyEditor _editor.CurrentScene = _activeScene; } + private void OnSceneSimulate() + { + _editor.SceneViewportWindow.EditorMode = false; + _sceneState = .Simulate; + + using (Scene simulationScene = new Scene()) + { + _editorScene.CopyTo(simulationScene); + + simulationScene.OnSimulationStart(); + + SetReference!(_activeScene, simulationScene); + } + + _editor.CurrentScene = _activeScene; + } + + private void OnScenePause() + { + _isPaused = true; + } + + private void OnSceneResume() + { + _isPaused = false; + } + private void OnSceneStop() { - _activeScene.OnRuntimeStop(); + if (_sceneState == .Play) + _activeScene.OnRuntimeStop(); + else + _activeScene.OnSimulationStop(); + SetReference!(_activeScene, _editorScene); _editor.SceneViewportWindow.EditorMode = true; _sceneState = .Edit; _editor.CurrentScene = _activeScene; + + _isPaused = false; /* * Update the viewport size because if the game windows size changed in diff --git a/GlitchyEngine/src/World/Scene.bf b/GlitchyEngine/src/World/Scene.bf index cdf6417..be82ee6 100644 --- a/GlitchyEngine/src/World/Scene.bf +++ b/GlitchyEngine/src/World/Scene.bf @@ -125,6 +125,16 @@ namespace GlitchyEngine.World } public void OnRuntimeStart() + { + OnSimulationStart(); + } + + public void OnRuntimeStop() + { + OnSimulationStop(); + } + + public void OnSimulationStart() { _physicsWorld2D = Box2D.World.Create(ref _gravity2D); @@ -183,7 +193,7 @@ namespace GlitchyEngine.World } } - public void OnRuntimeStop() + public void OnSimulationStop() { Box2D.World.Delete(_physicsWorld2D); _physicsWorld2D = null; @@ -191,8 +201,13 @@ namespace GlitchyEngine.World public enum UpdateMode { + /// No special update configuration (this does NOT mean nothing will be updated!) + None = 0x00, + /// Update editor-specific stuff Editor = 0x01, + /// Update the physics related stuff Physics = 0x02, + /// Update the runtume related stuff (e.g. execute scripts). Also run physics! Runtime = 0x04 | Physics, }