添加场景加载与保存功能
This commit is contained in:
@ -1,53 +0,0 @@
|
||||
//
|
||||
// Created by sfd on 25-5-18.
|
||||
//
|
||||
|
||||
#include "GameLayer.h"
|
||||
|
||||
#include <Hazel/Core/Application.h>
|
||||
#include <Hazel/Renderer/Renderer2D.h>
|
||||
#include <Hazel/Renderer/RendererCommand.h>
|
||||
|
||||
GameLayer::GameLayer() : Layer("GameLayer") , m_cameraController((float)(Application::Get().GetWindow().GetWidth()) / (float)(Application::Get().GetWindow().GetHeight()))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void GameLayer::OnAttach()
|
||||
{
|
||||
}
|
||||
|
||||
void GameLayer::OnDetech()
|
||||
{
|
||||
}
|
||||
|
||||
void GameLayer::OnUpdate(TimeStep& ts)
|
||||
{
|
||||
auto mouseState = SDL_GetMouseState(NULL, NULL);
|
||||
static Uint32 prevMouseState = 0;
|
||||
m_cameraController.OnUpdate(ts);
|
||||
|
||||
RendererCommand::SetClearColor({0.2f, 0.2f, 0.2f, 1.0f});
|
||||
RendererCommand::Clear();
|
||||
|
||||
Renderer2D::BeginScene(m_cameraController.GetCamera());
|
||||
|
||||
Hazel::Renderer2D::DrawQuad({0.0f, 0.0f}, {1.0f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f});
|
||||
|
||||
Renderer2D::EndScene();
|
||||
|
||||
if ((mouseState & SDL_BUTTON_LMASK) && !(prevMouseState & SDL_BUTTON_LMASK))
|
||||
{
|
||||
HZ_CORE_INFO("LEFT Mouse Clicked!");
|
||||
}
|
||||
prevMouseState = mouseState;
|
||||
}
|
||||
|
||||
void GameLayer::OnEvent(SDL_Event& e)
|
||||
{
|
||||
m_cameraController.OnEvent(e);
|
||||
}
|
||||
|
||||
void GameLayer::OnImGuiRender()
|
||||
{
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
//
|
||||
// Created by sfd on 25-5-18.
|
||||
//
|
||||
|
||||
#ifndef GAMELAYER_H
|
||||
#define GAMELAYER_H
|
||||
#include <Hazel/Core/Layer.h>
|
||||
#include <Hazel/Renderer/OrthographicCameraController.h>
|
||||
|
||||
|
||||
using namespace Hazel;
|
||||
|
||||
class GameLayer : public Layer{
|
||||
public:
|
||||
GameLayer();
|
||||
~GameLayer() = default;
|
||||
|
||||
void OnAttach() override;
|
||||
void OnDetech() override;
|
||||
void OnUpdate(TimeStep& ts) override;
|
||||
void OnEvent(SDL_Event& e) override;
|
||||
void OnImGuiRender() override;
|
||||
|
||||
private:
|
||||
OrthographicCameraController m_cameraController;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //GAMELAYER_H
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,6 +22,10 @@ namespace Hazel
|
||||
virtual void OnUpdate(TimeStep& ts) override;
|
||||
virtual void OnImGuiRender() override;
|
||||
virtual void OnEvent(SDL_Event& e) override;
|
||||
private:
|
||||
void SaveScene();
|
||||
void OpenScene();
|
||||
void NewScene();
|
||||
|
||||
private:
|
||||
OrthographicCameraController m_CameraController;
|
||||
@ -45,6 +49,7 @@ namespace Hazel
|
||||
Ref<FrameBuffer> m_FrameBuffer;
|
||||
|
||||
SceneHierachyPanel m_SceneHierachyPanel;
|
||||
const bool* m_State = nullptr;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@ -18,16 +18,17 @@ namespace Hazel
|
||||
void SceneHierachyPanel::SetContext(const Ref<Scene>& context)
|
||||
{
|
||||
m_Context = context;
|
||||
m_SelectionContext = {};
|
||||
}
|
||||
|
||||
void SceneHierachyPanel::OnImGuiRender()
|
||||
{
|
||||
ImGui::Begin("Scene Hierachy");
|
||||
|
||||
for (const auto entityID : m_Context->m_Registry.view<entt::entity>())
|
||||
m_Context->m_Registry.view<entt::entity>().each([&](auto entityID)
|
||||
{
|
||||
DrawEntityNode({entityID, m_Context.get()});
|
||||
}
|
||||
});
|
||||
|
||||
if (ImGui::IsMouseDown(0) && ImGui::IsWindowHovered())
|
||||
{
|
||||
@ -185,6 +186,7 @@ namespace Hazel
|
||||
|
||||
if (entity.HasComponent<T>())
|
||||
{
|
||||
ImGui::PushID(&entity);
|
||||
auto& component = entity.GetComponent<T>();
|
||||
|
||||
ImVec2 contextReginAvail = ImGui::GetContentRegionAvail();
|
||||
@ -219,7 +221,8 @@ namespace Hazel
|
||||
}
|
||||
|
||||
if (removeComponent)
|
||||
entity.RemoveComponent<SpriteRendererComponent>();
|
||||
entity.RemoveComponent<T>();
|
||||
ImGui::PopID();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user