Simulation mode and pause/resume

This commit is contained in:
Simon Lübeß
2023-03-24 23:50:46 +01:00
parent f16fbc3984
commit d54cdf43ee
5 changed files with 150 additions and 18 deletions
Binary file not shown.
Binary file not shown.
+4
View File
@@ -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)
+128 -15
View File
@@ -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;
float size = ImGui.GetWindowHeight() - 2 * padding;
ImGui.SameLine((ImGui.GetContentRegionMax().x / 2 - size / 2));
float centerX = ImGui.GetContentRegionMax().x / 2;
if (ImGui.ImageButton(icon, .(size, size), .Zero, .Ones, 0))
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,9 +487,40 @@ 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;
@@ -417,6 +528,8 @@ namespace GlitchyEditor
_editor.CurrentScene = _activeScene;
_isPaused = false;
/*
* Update the viewport size because if the game windows size changed in
* "Game"-mode the updated aspect-rations will reset once we go back
+16 -1
View File
@@ -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,
}