添加场景加载与保存功能
This commit is contained in:
@ -5,21 +5,20 @@
|
||||
#include "EditorLayer.h"
|
||||
|
||||
#include <imgui.h>
|
||||
#include <iostream>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "glm/ext/matrix_clip_space.hpp"
|
||||
#include <Hazel/Scene/SceneSerializer.h>
|
||||
#include <Hazel/Utils/PlatformUtils.h>
|
||||
|
||||
namespace Hazel
|
||||
{
|
||||
EditorLayer::EditorLayer()
|
||||
: Layer("HazelEditor"), m_CameraController((float)Application::Get().GetWindow().GetWidth() / (float)Application::Get().GetWindow().GetHeight())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void EditorLayer::OnAttach()
|
||||
{
|
||||
|
||||
FrameBufferSpecification spec;
|
||||
spec.Width = Application::Get().GetWindow().GetWidth();
|
||||
spec.Height = Application::Get().GetWindow().GetHeight();
|
||||
@ -40,8 +39,6 @@ namespace Hazel
|
||||
redSquare.AddComponent<SpriteRendererComponent>(glm::vec4{1.0f, 0.0f, 0.0f, 1.0f});
|
||||
|
||||
|
||||
|
||||
|
||||
m_CameraEntity = m_ActiveScene->CreateEntity("Camera A");
|
||||
m_CameraEntity.AddComponent<CameraComponent>();
|
||||
m_PrimaryCamera = true;
|
||||
@ -55,10 +52,10 @@ namespace Hazel
|
||||
class CameraController : public ScriptableEntity
|
||||
{
|
||||
public:
|
||||
void OnUpdate(const TimeStep ts)
|
||||
{
|
||||
void OnUpdate(const TimeStep ts) override
|
||||
{
|
||||
auto& translation = GetComponent<TransformComponent>().Translation;
|
||||
static const auto state = SDL_GetKeyboardState(nullptr);
|
||||
const auto state = SDL_GetKeyboardState(nullptr);
|
||||
if (state[SDL_SCANCODE_A])
|
||||
translation.x -= ts * speed;
|
||||
if (state[SDL_SCANCODE_D])
|
||||
@ -77,6 +74,8 @@ namespace Hazel
|
||||
|
||||
|
||||
m_SceneHierachyPanel.SetContext(m_ActiveScene);
|
||||
|
||||
m_State = SDL_GetKeyboardState(nullptr);
|
||||
}
|
||||
|
||||
void EditorLayer::OnDetech()
|
||||
@ -110,7 +109,7 @@ namespace Hazel
|
||||
RendererCommand::Clear();
|
||||
|
||||
|
||||
// Renderer2D::BeginScene(m_cameraController.GetCamera());
|
||||
// Renderer2D::BeginScene(m_CameraController.GetCamera());
|
||||
|
||||
// update Scene
|
||||
m_ActiveScene->OnUpdate(ts);
|
||||
@ -192,6 +191,22 @@ namespace Hazel
|
||||
// ImGui::MenuItem("Fullscreen", NULL, &opt_fullscreen);
|
||||
// ImGui::MenuItem("Padding", NULL, &opt_padding);
|
||||
// ImGui::Separator();
|
||||
if (ImGui::MenuItem("New", "Ctrl+N"))
|
||||
{
|
||||
NewScene();
|
||||
}
|
||||
|
||||
if (ImGui::MenuItem("Open...", "Ctrl+O"))
|
||||
{
|
||||
OpenScene();
|
||||
}
|
||||
|
||||
if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S"))
|
||||
{
|
||||
SaveScene();
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
if (ImGui::MenuItem("Exit")) { Hazel::Application::Get().Close(); }
|
||||
|
||||
@ -256,5 +271,59 @@ namespace Hazel
|
||||
{
|
||||
m_CameraController.OnEvent(e);
|
||||
}
|
||||
|
||||
#define SHORTCUT_NEW (SDL_KMOD_CTRL | SDLK_N)
|
||||
#define SHORTCUT_OPEN (SDL_KMOD_CTRL | SDLK_O)
|
||||
#define SHORTCUT_SAVE_ALL (SDL_KMOD_CTRL | SDL_KMOD_SHIFT | SDLK_S)
|
||||
|
||||
const auto mod = SDL_GetModState();
|
||||
const auto ctrl = (mod & SDL_KMOD_CTRL) ? SDL_KMOD_CTRL : 0;
|
||||
const auto shift = (mod & SDL_KMOD_SHIFT) ? SDL_KMOD_SHIFT : 0;
|
||||
|
||||
switch (ctrl | shift | e.key.key)
|
||||
{
|
||||
case SHORTCUT_NEW:
|
||||
NewScene();
|
||||
break;
|
||||
case SHORTCUT_OPEN:
|
||||
OpenScene();
|
||||
break;
|
||||
case SHORTCUT_SAVE_ALL:
|
||||
SaveScene();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void EditorLayer::SaveScene()
|
||||
{
|
||||
std::string filepath = FileDiaglogs::SaveFile("Hazel Scene (*.scene)\0*.scene\0");
|
||||
if (!filepath.empty())
|
||||
{
|
||||
SceneSerializer serializer(m_ActiveScene);
|
||||
serializer.Serialize(filepath);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorLayer::OpenScene()
|
||||
{
|
||||
std::string filepath = FileDiaglogs::OpenFile("Hazel Scene (*.scene)\0*.scene\0");
|
||||
if (!filepath.empty())
|
||||
{
|
||||
m_ActiveScene = CreateRef<Scene>();
|
||||
m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y);
|
||||
m_SceneHierachyPanel.SetContext(m_ActiveScene);
|
||||
|
||||
SceneSerializer serializer(m_ActiveScene);
|
||||
serializer.Deserialize(filepath);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorLayer::NewScene()
|
||||
{
|
||||
m_ActiveScene = CreateRef<Scene>();
|
||||
m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y);
|
||||
m_SceneHierachyPanel.SetContext(m_ActiveScene);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user