light/modules/mirror/src/editor_layer.cpp

110 lines
3 KiB
C++
Raw Normal View History

2025-07-05 13:28:41 +03:30
#include <engine/utils/serializer.hpp>
#include <mirror/editor_layer.hpp>
namespace Light {
EditorLayer::EditorLayer(const std::string &name): Layer(name), m_scene_dir("")
2022-03-04 22:40:20 +03:30
{
m_scene = CreateRef<Scene>();
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
m_framebuffer = Framebuffer::Create({ 1, 1, 1 }, GraphicsContext::GetSharedContext());
if (m_scene_dir.empty())
2022-03-04 22:40:20 +03:30
{
m_camera_entity = m_scene->CreateEntity("Camera");
m_camera_entity.AddComponent<CameraComponent>(SceneCamera(), true);
2022-03-04 22:40:20 +03:30
ResourceManager::LoadTexture("Awesomeface", "Assets/Textures/awesomeface.png");
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
{
SceneSerializer serializer(m_scene);
ASSERT(serializer.Deserialize(m_scene_dir), "Failed to de-serialize: {}", m_scene_dir);
// m_camera_entity = m_scene->GetEntityByTag("Game Camera");
}
2022-03-04 22:40:20 +03:30
}
2022-03-04 22:40:20 +03:30
EditorLayer::~EditorLayer()
{
if (!m_scene_dir.empty())
{
SceneSerializer serializer(m_scene);
serializer.Serialize(m_scene_dir);
}
2022-03-04 22:40:20 +03:30
}
2022-03-04 22:40:20 +03:30
void EditorLayer::OnUpdate(float deltaTime)
{
m_scene->OnUpdate(deltaTime);
m_direction.x = Input::GetKeyboardKey(Key::A) ? -1.0f :
2022-03-04 22:40:20 +03:30
Input::GetKeyboardKey(Key::D) ? 1.0f :
0.0f;
m_direction.y = Input::GetKeyboardKey(Key::S) ? -1.0f :
2022-03-04 22:40:20 +03:30
Input::GetKeyboardKey(Key::W) ? 1.0f :
0.0f;
auto &cameraTranslation = m_camera_entity.GetComponent<TransformComponent>().translation;
cameraTranslation += glm::vec3(m_direction * m_speed * deltaTime, 0.0f);
2022-03-04 22:40:20 +03:30
if (Input::GetKeyboardKey(Key::Escape))
Application::Quit();
}
2022-03-04 22:40:20 +03:30
void EditorLayer::OnRender()
{
m_scene->OnRender(m_framebuffer);
2022-03-04 22:40:20 +03:30
}
2022-03-04 22:40:20 +03:30
void EditorLayer::OnUserInterfaceUpdate()
{
UserInterface::DockspaceBegin();
ImGui::ShowDemoWindow();
if (ImGui::Begin("Game"))
{
2022-03-04 22:40:20 +03:30
Input::ReceiveGameEvents(ImGui::IsWindowFocused());
ImVec2 regionAvail = ImGui::GetContentRegionAvail();
if (m_available_content_region_prev != regionAvail)
{
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);
m_available_content_region_prev = regionAvail;
2022-03-04 22:40:20 +03:30
}
if (GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX)
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(
m_framebuffer->GetColorAttachment(),
2025-07-05 13:28:41 +03:30
regionAvail,
ImVec2(0, 1),
ImVec2(1, 0)
);
}
2022-03-04 22:40:20 +03:30
ImGui::End();
// Panels
m_sceneHierarchyPanel->OnUserInterfaceUpdate();
m_properties_panel->OnUserInterfaceUpdate();
m_content_browser_panel->OnUserInterfaceUpdate();
2022-03-04 22:40:20 +03:30
UserInterface::DockspaceEnd();
}
2022-03-04 22:40:20 +03:30
} // namespace Light