2025-07-11 02:35:28 +03:30
|
|
|
#include <app/application.hpp>
|
2025-07-10 13:29:03 +03:30
|
|
|
#include <asset_manager/asset_manager.hpp>
|
2025-07-10 13:55:18 +03:30
|
|
|
#include <camera/component.hpp>
|
2025-07-11 01:16:52 +03:30
|
|
|
#include <ecs/components.hpp>
|
|
|
|
#include <ecs/scene.hpp>
|
2025-07-11 02:18:27 +03:30
|
|
|
#include <ecs/serializer.hpp>
|
2025-07-11 01:42:56 +03:30
|
|
|
#include <input/input.hpp>
|
2025-07-10 13:29:03 +03:30
|
|
|
#include <input/key_codes.hpp>
|
2025-07-17 10:44:00 +03:30
|
|
|
#include <math/vec4.hpp>
|
2025-07-20 04:46:15 +03:30
|
|
|
#include <mirror/layers/editor_layer.hpp>
|
2025-07-11 02:35:28 +03:30
|
|
|
#include <renderer/framebuffer.hpp>
|
|
|
|
#include <renderer/graphics_context.hpp>
|
|
|
|
#include <renderer/texture.hpp>
|
2025-07-10 13:29:03 +03:30
|
|
|
#include <ui/ui.hpp>
|
2021-10-05 13:44:32 +03:30
|
|
|
|
2025-07-11 00:05:48 +03:30
|
|
|
namespace lt {
|
2021-08-14 16:17:33 +04:30
|
|
|
|
2025-07-06 15:10:34 +03:30
|
|
|
EditorLayer::EditorLayer(const std::string &name)
|
|
|
|
: Layer(name)
|
|
|
|
, m_scene_dir("")
|
|
|
|
, m_direction { 0.0, 0.0 }
|
2022-03-04 22:40:20 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
m_scene = create_ref<Scene>();
|
2021-08-16 16:21:03 +04:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
m_properties_panel = create_ref<PropertiesPanel>();
|
|
|
|
m_sceneHierarchyPanel = create_ref<SceneHierarchyPanel>(m_scene, m_properties_panel);
|
|
|
|
m_content_browser_panel = create_ref<AssetBrowserPanel>(m_scene);
|
2021-09-11 11:29:44 +04:30
|
|
|
|
2025-07-06 15:10:34 +03:30
|
|
|
m_framebuffer = Framebuffer::create(
|
|
|
|
{
|
|
|
|
.width = 1,
|
|
|
|
.height = 1,
|
|
|
|
.samples = 1,
|
|
|
|
},
|
|
|
|
GraphicsContext::get_shared_context()
|
|
|
|
);
|
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 15:36:53 +03:30
|
|
|
m_camera_entity = m_scene->create_entity("Camera");
|
2025-07-06 14:02:50 +03:30
|
|
|
m_camera_entity.add_component<CameraComponent>(SceneCamera(), true);
|
2021-10-08 22:59:47 +03:30
|
|
|
|
2025-07-10 13:29:03 +03:30
|
|
|
AssetManager::load_texture("Awesomeface", "data/assets/textures/awesomeface.asset");
|
2025-07-06 14:02:50 +03:30
|
|
|
|
|
|
|
auto entity = Entity { m_scene->create_entity("Awesomeface", {}) };
|
|
|
|
entity.add_component<SpriteRendererComponent>(
|
2025-07-10 13:29:03 +03:30
|
|
|
AssetManager::get_texture("Awesomeface"),
|
2025-07-17 10:44:00 +03:30
|
|
|
math::vec4 { 0.0f, 1.0f, 1.0f, 1.0f }
|
2025-07-05 13:28:41 +03:30
|
|
|
);
|
2022-03-04 22:40:20 +03:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-07-06 14:02:50 +03:30
|
|
|
auto serializer = SceneSerializer { m_scene };
|
2025-07-11 02:12:55 +03:30
|
|
|
ensure(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-06 14:02:50 +03:30
|
|
|
auto serializer = SceneSerializer { m_scene };
|
2025-07-05 15:36:53 +03:30
|
|
|
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
|
|
|
|
2025-07-06 15:10:34 +03:30
|
|
|
void EditorLayer::on_update(float delta_time)
|
2022-03-04 22:40:20 +03:30
|
|
|
{
|
2025-07-06 15:10:34 +03:30
|
|
|
m_scene->on_update(delta_time);
|
2021-08-14 16:17:33 +04:30
|
|
|
|
2025-07-06 15:10:34 +03:30
|
|
|
if (Input::get_keyboard_key(Key::A))
|
|
|
|
{
|
|
|
|
m_direction.x = -1.0;
|
|
|
|
}
|
|
|
|
else if (Input::get_keyboard_key(Key::D))
|
|
|
|
{
|
|
|
|
m_direction.x = 1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_direction.x = 0.0;
|
|
|
|
}
|
2021-10-23 19:04:29 +03:30
|
|
|
|
2025-07-06 15:10:34 +03:30
|
|
|
if (Input::get_keyboard_key(Key::S))
|
|
|
|
{
|
|
|
|
m_direction.y = -1.0;
|
|
|
|
}
|
|
|
|
else if (Input::get_keyboard_key(Key::W))
|
|
|
|
{
|
|
|
|
m_direction.y = 1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_direction.y = 0.0;
|
|
|
|
}
|
2021-08-16 16:21:03 +04:30
|
|
|
|
2025-07-06 15:10:34 +03:30
|
|
|
auto &translation = m_camera_entity.get_component<TransformComponent>().translation;
|
2025-07-17 10:44:00 +03:30
|
|
|
auto velocity = m_direction * m_speed * delta_time;
|
|
|
|
translation = translation * math::vec3 { velocity.x, velocity.y, 0.0f };
|
2021-10-02 08:51:54 +03:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
if (Input::get_keyboard_key(Key::Escape))
|
2025-07-06 15:10:34 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
Application::quit();
|
2025-07-06 15:10:34 +03:30
|
|
|
}
|
2022-03-04 22:40:20 +03:30
|
|
|
}
|
2021-10-23 19:04:29 +03:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void EditorLayer::on_render()
|
2022-03-04 22:40:20 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
m_scene->on_render(m_framebuffer);
|
2022-03-04 22:40:20 +03:30
|
|
|
}
|
2021-10-23 19:04:29 +03:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void EditorLayer::on_user_interface_update()
|
2022-03-04 22:40:20 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
UserInterface::dockspace_begin();
|
2022-03-04 22:40:20 +03:30
|
|
|
ImGui::ShowDemoWindow();
|
|
|
|
|
|
|
|
if (ImGui::Begin("Game"))
|
2021-08-14 16:17:33 +04:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
Input::receive_game_events(ImGui::IsWindowFocused());
|
2025-07-06 15:10:34 +03:30
|
|
|
auto available_region = ImGui::GetContentRegionAvail();
|
2021-08-14 16:17:33 +04:30
|
|
|
|
2025-07-16 14:42:50 +03:30
|
|
|
if (m_available_content_region_prev.x != available_region.x
|
|
|
|
|| m_available_content_region_prev.y != available_region.y)
|
2021-08-14 16:17:33 +04:30
|
|
|
{
|
2025-07-17 10:44:00 +03:30
|
|
|
m_framebuffer->resize(
|
|
|
|
math::uvec2 {
|
|
|
|
static_cast<uint32_t>(available_region.x),
|
|
|
|
static_cast<uint32_t>(available_region.y),
|
|
|
|
}
|
|
|
|
);
|
2025-07-06 14:02:50 +03:30
|
|
|
auto &camera = m_camera_entity.get_component<CameraComponent>().camera;
|
2025-07-06 15:10:34 +03:30
|
|
|
camera.set_viewport_size(
|
|
|
|
static_cast<uint32_t>(available_region.x),
|
|
|
|
static_cast<uint32_t>(available_region.y)
|
|
|
|
);
|
2022-03-04 22:40:20 +03:30
|
|
|
|
2025-07-06 15:10:34 +03:30
|
|
|
m_available_content_region_prev = available_region;
|
2022-03-04 22:40:20 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
if (GraphicsContext::get_graphics_api() == GraphicsAPI::DirectX)
|
2025-07-06 15:10:34 +03:30
|
|
|
{
|
|
|
|
ImGui::Image(m_framebuffer->get_color_attachment(), available_region);
|
|
|
|
}
|
2022-03-04 22:40:20 +03:30
|
|
|
else
|
2025-07-06 15:10:34 +03:30
|
|
|
{
|
2025-07-05 13:28:41 +03:30
|
|
|
ImGui::Image(
|
2025-07-06 14:02:50 +03:30
|
|
|
m_framebuffer->get_color_attachment(),
|
2025-07-06 15:10:34 +03:30
|
|
|
available_region,
|
2025-07-05 13:28:41 +03:30
|
|
|
ImVec2(0, 1),
|
|
|
|
ImVec2(1, 0)
|
|
|
|
);
|
2025-07-06 15:10:34 +03:30
|
|
|
}
|
2021-08-14 16:17:33 +04:30
|
|
|
}
|
2022-03-04 22:40:20 +03:30
|
|
|
ImGui::End();
|
|
|
|
|
|
|
|
// Panels
|
2025-07-05 15:36:53 +03:30
|
|
|
m_sceneHierarchyPanel->on_user_interface_update();
|
|
|
|
m_properties_panel->on_user_interface_update();
|
|
|
|
m_content_browser_panel->on_user_interface_update();
|
2022-03-04 22:40:20 +03:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
UserInterface::dockspace_end();
|
2022-03-04 22:40:20 +03:30
|
|
|
}
|
2021-10-23 19:04:29 +03:30
|
|
|
|
2025-07-11 00:05:48 +03:30
|
|
|
} // namespace lt
|