add pausing and stepping
This commit is contained in:
@ -42,7 +42,9 @@ namespace Hazel
|
||||
m_CheckerBoardTexture = Texture2D::Create("assets/textures/Checkerboard.png");
|
||||
|
||||
m_IconPlay = Texture2D::Create("Resources/Icons/PlayButton.png");
|
||||
m_IconStop = Texture2D::Create("Resources/Icons/PauseButton.png");
|
||||
m_IconPause = Texture2D::Create("Resources/Icons/PauseButton.png");
|
||||
m_IconStep = Texture2D::Create("Resources/Icons/StepButton.png");
|
||||
m_IconStop = Texture2D::Create("Resources/Icons/StopButton.png");
|
||||
m_IconSimulate = Texture2D::Create("Resources/Icons/SimulateButton.png");
|
||||
|
||||
m_EditorScene = CreateRef<Scene>();
|
||||
@ -76,12 +78,13 @@ namespace Hazel
|
||||
// reset Renderer Draw Stats
|
||||
Renderer2D::ResetStats();
|
||||
|
||||
m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y);
|
||||
|
||||
if (const auto& spec = m_FrameBuffer->GetSpecification();
|
||||
spec.Width != m_ViewPortSize.x || spec.Height != m_ViewPortSize.y)
|
||||
{
|
||||
m_FrameBuffer->Resize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y);
|
||||
m_CameraController.OnResize(m_ViewPortSize.x, m_ViewPortSize.y);
|
||||
|
||||
m_EditorCamera.SetViewPortSize(m_ViewPortSize.x, m_ViewPortSize.y);
|
||||
}
|
||||
|
||||
@ -594,6 +597,10 @@ namespace Hazel
|
||||
m_SceneHierachyPanel.SetContext(m_ActiveScene);
|
||||
}
|
||||
|
||||
void EditorLayer::OnScenePause()
|
||||
{
|
||||
}
|
||||
|
||||
void EditorLayer::OnSceneSimulation()
|
||||
{
|
||||
if (m_SceneState == SceneState::Simulate)
|
||||
@ -684,17 +691,27 @@ namespace Hazel
|
||||
ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x * 0.5f - size * 0.5f);
|
||||
ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x * 0.5f - (size * 0.5f));
|
||||
|
||||
Ref<Texture2D> icon = (m_SceneState == SceneState::Edit || m_SceneState == SceneState::Simulate)? m_IconPlay : m_IconStop;
|
||||
if (ImGui::ImageButton("toolbar-play-edit", icon->GetRendererID(), ImVec2{size, size}, ImVec2{0, 0}, ImVec2{1, 1}))
|
||||
bool hasPlayButton = m_SceneState == SceneState::Play || m_SceneState == SceneState::Edit;
|
||||
bool hasSimulateButton = m_SceneState == SceneState::Simulate || m_SceneState == SceneState::Edit;
|
||||
bool hasPauseButton = m_SceneState != SceneState::Edit;
|
||||
|
||||
if (hasPlayButton)
|
||||
{
|
||||
if (m_SceneState == SceneState::Edit || m_SceneState == SceneState::Simulate)
|
||||
OnScenePlay();
|
||||
else if (m_SceneState == SceneState::Play)
|
||||
OnSceneStop();
|
||||
Ref<Texture2D> icon = (m_SceneState == SceneState::Edit || m_SceneState == SceneState::Simulate)? m_IconPlay : m_IconStop;
|
||||
if (ImGui::ImageButton("toolbar-play-edit", icon->GetRendererID(), ImVec2{size, size}, ImVec2{0, 0}, ImVec2{1, 1}))
|
||||
{
|
||||
if (m_SceneState == SceneState::Edit || m_SceneState == SceneState::Simulate)
|
||||
OnScenePlay();
|
||||
else if (m_SceneState == SceneState::Play)
|
||||
OnSceneStop();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::SameLine();
|
||||
if (hasSimulateButton)
|
||||
{
|
||||
if (hasPlayButton)
|
||||
ImGui::SameLine();
|
||||
|
||||
Ref<Texture2D> icon = (m_SceneState == SceneState::Edit || m_SceneState == SceneState::Play)? m_IconSimulate : m_IconStop;
|
||||
if (ImGui::ImageButton("toolbar-simulate", icon->GetRendererID(), ImVec2{size, size}, ImVec2{0, 0}, ImVec2{1, 1}))
|
||||
{
|
||||
@ -705,6 +722,32 @@ namespace Hazel
|
||||
}
|
||||
}
|
||||
|
||||
if (hasPauseButton)
|
||||
{
|
||||
bool isPaused = m_ActiveScene->IsPaused();
|
||||
ImGui::SameLine();
|
||||
{
|
||||
Ref<Texture2D> icon = m_IconPause;
|
||||
if (ImGui::ImageButton("toolbar-pause", icon->GetRendererID(), ImVec2{size, size}, ImVec2{0, 0}, ImVec2{1, 1}))
|
||||
{
|
||||
m_ActiveScene->SetPaused(!isPaused);
|
||||
}
|
||||
}
|
||||
|
||||
if (isPaused)
|
||||
{
|
||||
ImGui::SameLine();
|
||||
{
|
||||
Ref<Texture2D> icon = m_IconStep;
|
||||
if (ImGui::ImageButton("toolbar-step", icon->GetRendererID(), ImVec2{size, size}, ImVec2{0, 0}, ImVec2{1, 1}))
|
||||
{
|
||||
m_ActiveScene->Step();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::PopStyleVar(2);
|
||||
ImGui::PopStyleColor(3);
|
||||
ImGui::End();
|
||||
|
||||
@ -29,6 +29,7 @@ namespace Hazel
|
||||
void OnScenePlay();
|
||||
void OnSceneSimulation();
|
||||
void OnSceneStop();
|
||||
void OnScenePause();
|
||||
|
||||
|
||||
void SaveSceneAs() const;
|
||||
@ -84,7 +85,7 @@ namespace Hazel
|
||||
|
||||
SceneState m_SceneState = SceneState::Edit;
|
||||
|
||||
Ref<Texture2D> m_IconPlay, m_IconStop, m_IconSimulate;
|
||||
Ref<Texture2D> m_IconPlay, m_IconPause, m_IconStep, m_IconStop, m_IconSimulate;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -143,6 +143,11 @@ namespace Hazel
|
||||
}
|
||||
|
||||
|
||||
void Scene::Step(const int frames)
|
||||
{
|
||||
m_StepFrame = frames;
|
||||
}
|
||||
|
||||
void Scene::OnPhysics2DStart()
|
||||
{
|
||||
b2WorldDef worldDef = b2DefaultWorldDef();
|
||||
@ -285,49 +290,52 @@ namespace Hazel
|
||||
|
||||
void Scene::OnUpdateRuntime(TimeStep& ts)
|
||||
{
|
||||
// Update scripts
|
||||
if (!m_IsPaused || m_StepFrame-- > 0)
|
||||
{
|
||||
// C# Script Update
|
||||
auto view = m_Registry.view<ScriptComponent>();
|
||||
for (auto e : view)
|
||||
// Update scripts
|
||||
{
|
||||
Entity entity = {e, this};
|
||||
ScriptEngine::OnUpdateEntity(entity, ts);
|
||||
}
|
||||
|
||||
//
|
||||
m_Registry.view<NativeScriptComponent>().each([=, this](auto entity, auto& nsc)
|
||||
{
|
||||
// TODO: move to Scene::OnScenePlay
|
||||
if (!nsc.Instance)
|
||||
// C# Script Update
|
||||
auto view = m_Registry.view<ScriptComponent>();
|
||||
for (auto e : view)
|
||||
{
|
||||
nsc.Instance = nsc.InstantiateScript();
|
||||
nsc.Instance->m_Entity = Entity{entity, this};
|
||||
nsc.Instance->OnCreate();
|
||||
Entity entity = {e, this};
|
||||
ScriptEngine::OnUpdateEntity(entity, ts);
|
||||
}
|
||||
|
||||
nsc.Instance->OnUpdate(ts);
|
||||
});
|
||||
}
|
||||
//
|
||||
m_Registry.view<NativeScriptComponent>().each([=, this](auto entity, auto& nsc)
|
||||
{
|
||||
// TODO: move to Scene::OnScenePlay
|
||||
if (!nsc.Instance)
|
||||
{
|
||||
nsc.Instance = nsc.InstantiateScript();
|
||||
nsc.Instance->m_Entity = Entity{entity, this};
|
||||
nsc.Instance->OnCreate();
|
||||
}
|
||||
|
||||
// Physics
|
||||
{
|
||||
b2World_Step(*m_PhysicsWorld, ts, 4);
|
||||
nsc.Instance->OnUpdate(ts);
|
||||
});
|
||||
}
|
||||
|
||||
auto view = m_Registry.view<RigidBody2DComponent>();
|
||||
for (auto e : view)
|
||||
// Physics
|
||||
{
|
||||
Entity entity = {e, this};
|
||||
auto& transform = entity.GetComponent<TransformComponent>();
|
||||
auto& rb2D = entity.GetComponent<RigidBody2DComponent>();
|
||||
b2World_Step(*m_PhysicsWorld, ts, 4);
|
||||
|
||||
b2BodyId& body = rb2D.RuntimeBody;
|
||||
auto view = m_Registry.view<RigidBody2DComponent>();
|
||||
for (auto e : view)
|
||||
{
|
||||
Entity entity = {e, this};
|
||||
auto& transform = entity.GetComponent<TransformComponent>();
|
||||
auto& rb2D = entity.GetComponent<RigidBody2DComponent>();
|
||||
|
||||
const auto& position = b2Body_GetPosition(body);
|
||||
transform.Translation.x = position.x;
|
||||
transform.Translation.y = position.y;
|
||||
const float angle = b2Rot_GetAngle(b2Body_GetRotation(body));
|
||||
transform.Rotation.z = angle;
|
||||
b2BodyId& body = rb2D.RuntimeBody;
|
||||
|
||||
const auto& position = b2Body_GetPosition(body);
|
||||
transform.Translation.x = position.x;
|
||||
transform.Translation.y = position.y;
|
||||
const float angle = b2Rot_GetAngle(b2Body_GetRotation(body));
|
||||
transform.Rotation.z = angle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -380,24 +388,27 @@ namespace Hazel
|
||||
|
||||
void Scene::OnUpdateSimulation(TimeStep& ts, const EditorCamera& camera)
|
||||
{
|
||||
// Physics
|
||||
if (!m_IsPaused || m_StepFrame-- > 0)
|
||||
{
|
||||
b2World_Step(*m_PhysicsWorld, ts, 4);
|
||||
|
||||
auto view = m_Registry.view<RigidBody2DComponent>();
|
||||
for (auto e : view)
|
||||
// Physics
|
||||
{
|
||||
Entity entity = {e, this};
|
||||
auto& transform = entity.GetComponent<TransformComponent>();
|
||||
auto& rb2D = entity.GetComponent<RigidBody2DComponent>();
|
||||
b2World_Step(*m_PhysicsWorld, ts, 4);
|
||||
|
||||
b2BodyId& body = rb2D.RuntimeBody;
|
||||
auto view = m_Registry.view<RigidBody2DComponent>();
|
||||
for (auto e : view)
|
||||
{
|
||||
Entity entity = {e, this};
|
||||
auto& transform = entity.GetComponent<TransformComponent>();
|
||||
auto& rb2D = entity.GetComponent<RigidBody2DComponent>();
|
||||
|
||||
const auto& position = b2Body_GetPosition(body);
|
||||
transform.Translation.x = position.x;
|
||||
transform.Translation.y = position.y;
|
||||
const float angle = b2Rot_GetAngle(b2Body_GetRotation(body));
|
||||
transform.Rotation.z = angle;
|
||||
b2BodyId& body = rb2D.RuntimeBody;
|
||||
|
||||
const auto& position = b2Body_GetPosition(body);
|
||||
transform.Translation.x = position.x;
|
||||
transform.Translation.y = position.y;
|
||||
const float angle = b2Rot_GetAngle(b2Body_GetRotation(body));
|
||||
transform.Rotation.z = angle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -49,6 +49,10 @@ namespace Hazel
|
||||
Entity GetEntityByUUID(UUID uuid);
|
||||
|
||||
bool IsRunning() const {return m_IsRunning; }
|
||||
bool IsPaused() const {return m_IsPaused; }
|
||||
|
||||
void SetPaused(const bool pause) {m_IsPaused = pause; }
|
||||
void Step(int frames = 1);
|
||||
|
||||
template<typename... Components>
|
||||
auto GetAllEntitiesWith()
|
||||
@ -73,6 +77,8 @@ namespace Hazel
|
||||
b2WorldId* m_PhysicsWorld = nullptr;
|
||||
|
||||
bool m_IsRunning = false;
|
||||
bool m_IsPaused = false;
|
||||
int m_StepFrame = 0;
|
||||
|
||||
std::unordered_map<UUID, entt::entity> m_EnttMap;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user