2025-07-05 13:28:41 +03:30
|
|
|
#include <engine/utils/serializer.hpp>
|
|
|
|
#include <mirror/editor_layer.hpp>
|
2021-10-05 13:44:32 +03:30
|
|
|
|
2021-08-14 16:17:33 +04:30
|
|
|
namespace Light {
|
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
EditorLayer::EditorLayer(const std::string &name): Layer(name), m_scene_dir("")
|
2022-03-04 22:40:20 +03:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
m_scene = CreateRef<Scene>();
|
2021-08-16 16:21:03 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
m_properties_panel = CreateRef<PropertiesPanel>();
|
|
|
|
m_sceneHierarchyPanel = CreateRef<SceneHierarchyPanel>(m_scene, m_properties_panel);
|
|
|
|
m_content_browser_panel = CreateRef<AssetBrowserPanel>(m_scene);
|
2021-09-11 11:29:44 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
m_framebuffer = Framebuffer::Create({ 1, 1, 1 }, GraphicsContext::GetSharedContext());
|
2021-10-08 22:59:47 +03:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_scene_dir.empty())
|
2022-03-04 22:40:20 +03:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
m_camera_entity = m_scene->CreateEntity("Camera");
|
|
|
|
m_camera_entity.AddComponent<CameraComponent>(SceneCamera(), true);
|
2021-10-08 22:59:47 +03:30
|
|
|
|
2022-03-04 22:40:20 +03:30
|
|
|
ResourceManager::LoadTexture("Awesomeface", "Assets/Textures/awesomeface.png");
|
2025-07-05 14:23:01 +03:30
|
|
|
Entity entity = m_scene->CreateEntity("Awesomeface", {});
|
2025-07-05 13:28:41 +03:30
|
|
|
entity.AddComponent<SpriteRendererComponent>(
|
|
|
|
ResourceManager::GetTexture("Awesomeface"),
|
|
|
|
glm::vec4 { 0.0f, 1.0f, 1.0f, 1.0f }
|
|
|
|
);
|
2022-03-04 22:40:20 +03:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
SceneSerializer serializer(m_scene);
|
|
|
|
ASSERT(serializer.Deserialize(m_scene_dir), "Failed to de-serialize: {}", m_scene_dir);
|
2021-10-07 01:13:18 +03:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
// m_camera_entity = m_scene->GetEntityByTag("Game Camera");
|
2021-08-16 16:21:03 +04:30
|
|
|
}
|
2022-03-04 22:40:20 +03:30
|
|
|
}
|
2021-10-07 03:26:36 +03:30
|
|
|
|
2022-03-04 22:40:20 +03:30
|
|
|
EditorLayer::~EditorLayer()
|
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
if (!m_scene_dir.empty())
|
2021-10-07 03:26:36 +03:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
SceneSerializer serializer(m_scene);
|
|
|
|
serializer.Serialize(m_scene_dir);
|
2021-10-07 03:26:36 +03:30
|
|
|
}
|
2022-03-04 22:40:20 +03:30
|
|
|
}
|
2021-10-23 19:04:29 +03:30
|
|
|
|
2022-03-04 22:40:20 +03:30
|
|
|
void EditorLayer::OnUpdate(float deltaTime)
|
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
m_scene->OnUpdate(deltaTime);
|
2021-08-14 16:17:33 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
m_direction.x = Input::GetKeyboardKey(Key::A) ? -1.0f :
|
2022-03-04 22:40:20 +03:30
|
|
|
Input::GetKeyboardKey(Key::D) ? 1.0f :
|
2025-07-05 11:33:43 +03:30
|
|
|
0.0f;
|
2021-10-23 19:04:29 +03:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
m_direction.y = Input::GetKeyboardKey(Key::S) ? -1.0f :
|
2022-03-04 22:40:20 +03:30
|
|
|
Input::GetKeyboardKey(Key::W) ? 1.0f :
|
2025-07-05 11:33:43 +03:30
|
|
|
0.0f;
|
2021-08-16 16:21:03 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
auto &cameraTranslation = m_camera_entity.GetComponent<TransformComponent>().translation;
|
|
|
|
cameraTranslation += glm::vec3(m_direction * m_speed * deltaTime, 0.0f);
|
2021-10-02 08:51:54 +03:30
|
|
|
|
2022-03-04 22:40:20 +03:30
|
|
|
if (Input::GetKeyboardKey(Key::Escape))
|
|
|
|
Application::Quit();
|
|
|
|
}
|
2021-10-23 19:04:29 +03:30
|
|
|
|
2022-03-04 22:40:20 +03:30
|
|
|
void EditorLayer::OnRender()
|
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
m_scene->OnRender(m_framebuffer);
|
2022-03-04 22:40:20 +03:30
|
|
|
}
|
2021-10-23 19:04:29 +03:30
|
|
|
|
2022-03-04 22:40:20 +03:30
|
|
|
void EditorLayer::OnUserInterfaceUpdate()
|
|
|
|
{
|
|
|
|
UserInterface::DockspaceBegin();
|
|
|
|
ImGui::ShowDemoWindow();
|
|
|
|
|
|
|
|
if (ImGui::Begin("Game"))
|
2021-08-14 16:17:33 +04:30
|
|
|
{
|
2022-03-04 22:40:20 +03:30
|
|
|
Input::ReceiveGameEvents(ImGui::IsWindowFocused());
|
|
|
|
ImVec2 regionAvail = ImGui::GetContentRegionAvail();
|
2021-08-14 16:17:33 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_available_content_region_prev != regionAvail)
|
2021-08-14 16:17:33 +04:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
m_framebuffer->Resize({ regionAvail.x, regionAvail.y });
|
|
|
|
auto &camera = m_camera_entity.GetComponent<CameraComponent>().camera;
|
2022-03-04 22:40:20 +03:30
|
|
|
camera.SetViewportSize(regionAvail.x, regionAvail.y);
|
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
m_available_content_region_prev = regionAvail;
|
2022-03-04 22:40:20 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
if (GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX)
|
2025-07-05 14:23:01 +03:30
|
|
|
ImGui::Image(m_framebuffer->GetColorAttachment(), regionAvail);
|
2022-03-04 22:40:20 +03:30
|
|
|
else
|
2025-07-05 13:28:41 +03:30
|
|
|
ImGui::Image(
|
2025-07-05 14:23:01 +03:30
|
|
|
m_framebuffer->GetColorAttachment(),
|
2025-07-05 13:28:41 +03:30
|
|
|
regionAvail,
|
|
|
|
ImVec2(0, 1),
|
|
|
|
ImVec2(1, 0)
|
|
|
|
);
|
2021-08-14 16:17:33 +04:30
|
|
|
}
|
2022-03-04 22:40:20 +03:30
|
|
|
ImGui::End();
|
|
|
|
|
|
|
|
// Panels
|
2025-07-05 14:23:01 +03:30
|
|
|
m_sceneHierarchyPanel->OnUserInterfaceUpdate();
|
|
|
|
m_properties_panel->OnUserInterfaceUpdate();
|
|
|
|
m_content_browser_panel->OnUserInterfaceUpdate();
|
2022-03-04 22:40:20 +03:30
|
|
|
|
|
|
|
UserInterface::DockspaceEnd();
|
|
|
|
}
|
2021-10-23 19:04:29 +03:30
|
|
|
|
2022-03-04 22:40:20 +03:30
|
|
|
} // namespace Light
|