Skip to content

More update around the editor #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 24, 2025
52 changes: 35 additions & 17 deletions Tetragrama/Components/DockspaceUIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,41 @@ namespace Tetragrama::Components
std::string DockspaceUIComponent::s_asset_importer_report_msg = "";
float DockspaceUIComponent::s_editor_scene_serializer_progress = 0.0f;

DockspaceUIComponent::DockspaceUIComponent() : m_asset_importer(CreateScope<Importers::AssimpImporter>()), m_editor_serializer(CreateScope<Serializers::EditorSceneSerializer>()) {}
DockspaceUIComponent::DockspaceUIComponent() {}

DockspaceUIComponent::~DockspaceUIComponent() {}

void DockspaceUIComponent::Initialize(Layers::ImguiLayer* parent, const char* name, bool visibility, bool closed)
{
UIComponent::Initialize(parent, name, visibility, closed);

m_dockspace_node_flag = ImGuiDockNodeFlags_NoWindowMenuButton | ImGuiDockNodeFlags_PassthruCentralNode;
m_window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
m_asset_importer = ZPushStructCtor(&(parent->LayerArena), Importers::AssimpImporter);
m_editor_serializer = ZPushStructCtor(&(parent->LayerArena), Serializers::EditorSceneSerializer);

auto context = reinterpret_cast<EditorContext*>(ParentLayer->ParentContext);
m_editor_serializer->Context = context;
m_asset_importer->Context = context;
m_editor_serializer->Initialize(&(parent->LayerArena));
m_asset_importer->Initialize(&(parent->LayerArena));

const auto& editor_config = *context->ConfigurationPtr;
m_editor_serializer->AssetImporter = m_asset_importer;

m_default_import_configuration = {.OutputModelFilePath = fmt::format("{0}/{1}", editor_config.WorkingSpacePath, editor_config.SceneDataPath), .OutputMeshFilePath = fmt::format("{0}/{1}", editor_config.WorkingSpacePath, editor_config.SceneDataPath), .OutputTextureFilesPath = fmt::format("{0}/{1}", editor_config.WorkingSpacePath, editor_config.DefaultImportTexturePath), .OutputMaterialsPath = fmt::format("{0}/{1}", editor_config.WorkingSpacePath, editor_config.SceneDataPath)};
m_dockspace_node_flag = ImGuiDockNodeFlags_NoWindowMenuButton | ImGuiDockNodeFlags_PassthruCentralNode;
m_window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;

auto context = reinterpret_cast<EditorContext*>(ParentLayer->ParentContext);
m_editor_serializer->Context = context;
m_asset_importer->Context = context;

const auto& editor_config = *context->ConfigurationPtr;

auto o_model_fpath = fmt::format("{0}/{1}", editor_config.WorkingSpacePath, editor_config.SceneDataPath);
auto o_mesh_fpath = fmt::format("{0}/{1}", editor_config.WorkingSpacePath, editor_config.SceneDataPath);
auto o_texture_fpath = fmt::format("{0}/{1}", editor_config.WorkingSpacePath, editor_config.DefaultImportTexturePath);
auto o_material_fpath = fmt::format("{0}/{1}", editor_config.WorkingSpacePath, editor_config.SceneDataPath);

m_default_import_configuration = {};
m_default_import_configuration.OutputModelFilePath.init(&(parent->LayerArena), o_model_fpath.c_str());
m_default_import_configuration.OutputMeshFilePath.init(&(parent->LayerArena), o_mesh_fpath.c_str());
m_default_import_configuration.OutputTextureFilesPath.init(&(parent->LayerArena), o_texture_fpath.c_str());
m_default_import_configuration.OutputMaterialsPath.init(&(parent->LayerArena), o_material_fpath.c_str());

#ifdef _WIN32
std::replace(m_default_import_configuration.OutputModelFilePath.begin(), m_default_import_configuration.OutputModelFilePath.end(), '/', '\\');
Expand Down Expand Up @@ -305,8 +322,8 @@ namespace Tetragrama::Components

if (ImGui::Button("Save", ImVec2(80, 0)) && is_save_button_enabled)
{
auto context = reinterpret_cast<EditorContext*>(ParentLayer->ParentContext);
ZEngine::Helpers::secure_strcpy(context->CurrentScenePtr->Name, 50, s_save_as_input_buffer);
auto context = reinterpret_cast<EditorContext*>(ParentLayer->ParentContext);
context->CurrentScenePtr->Name = s_save_as_input_buffer;
m_editor_serializer->Serialize(context->CurrentScenePtr);

m_open_save_scene_as = false;
Expand Down Expand Up @@ -422,7 +439,7 @@ namespace Tetragrama::Components
//{
// data.SerializedModelPath.replace(data.SerializedModelPath.find(ws), ws.size(), "");
// }

//
// context_ptr->CurrentScenePtr->Push(data.SerializedMeshesPath, data.SerializedModelPath, data.SerializedMaterialsPath);
}

Expand Down Expand Up @@ -513,8 +530,9 @@ namespace Tetragrama::Components
auto ctx = reinterpret_cast<EditorContext*>(context);

// Todo : Ensure no data race on CurrentScenePtr
ZEngine::Helpers::secure_strcpy(ctx->CurrentScenePtr->Name, 50, scene.Name);
ZEngine::Helpers::secure_strcpy(ctx->ConfigurationPtr->ActiveSceneName, ZEngine::Helpers::secure_strlen(scene.Name), scene.Name);

ctx->CurrentScenePtr->Name = ctx->ConfigurationPtr->ActiveSceneName;
ctx->CurrentScenePtr->Data = scene.Data;
ctx->CurrentScenePtr->MeshFiles = scene.MeshFiles;
ctx->CurrentScenePtr->ModelFiles = scene.ModelFiles;
Expand Down Expand Up @@ -552,11 +570,11 @@ namespace Tetragrama::Components
{
if (!filename.empty())
{
auto parent_path = std::filesystem::path(filename).parent_path().string();
auto asset_name = fs::path(filename).filename().replace_extension().string();
auto import_config = m_default_import_configuration;
import_config.AssetFilename = asset_name;
import_config.InputBaseAssetFilePath = parent_path;
auto parent_path = std::filesystem::path(filename).parent_path().string();
auto asset_name = fs::path(filename).filename().replace_extension().string();
auto import_config = m_default_import_configuration;
import_config.AssetFilename.init(&(ParentLayer->LayerArena), asset_name.c_str());
import_config.InputBaseAssetFilePath.init(&(ParentLayer->LayerArena), parent_path.c_str());
co_await m_asset_importer->ImportAsync(filename, import_config);
}
co_return;
Expand Down
22 changes: 11 additions & 11 deletions Tetragrama/Components/DockspaceUIComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ namespace Tetragrama::Components
static float s_editor_scene_serializer_progress;

private:
bool m_open_asset_importer{false};
bool m_open_exit{false};
bool m_pending_shutdown{false};
bool m_open_save_scene{false};
bool m_open_save_scene_as{false};
bool m_request_save_scene_ui_close{false};
ImGuiDockNodeFlags m_dockspace_node_flag;
ImGuiWindowFlags m_window_flags;
Importers::ImportConfiguration m_default_import_configuration;
ZEngine::Helpers::Scope<Importers::IAssetImporter> m_asset_importer;
ZEngine::Helpers::Scope<Serializers::EditorSceneSerializer> m_editor_serializer;
bool m_open_asset_importer{false};
bool m_open_exit{false};
bool m_pending_shutdown{false};
bool m_open_save_scene{false};
bool m_open_save_scene_as{false};
bool m_request_save_scene_ui_close{false};
ImGuiDockNodeFlags m_dockspace_node_flag;
ImGuiWindowFlags m_window_flags;
Importers::ImportConfiguration m_default_import_configuration;
ZRawPtr(Importers::IAssetImporter) m_asset_importer;
ZRawPtr(Serializers::EditorSceneSerializer) m_editor_serializer;
};
} // namespace Tetragrama::Components
1 change: 1 addition & 0 deletions Tetragrama/Controllers/EditorCameraController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Tetragrama::Controllers
m_position = {0.0f, 0.0f, 1.5f};
m_process_event = true;
m_controller_type = Controllers::CameraControllerType::PERSPECTIVE_CONTROLLER;
m_window = window;

m_perspective_camera = ZPushStructCtor(arena, PerspectiveCamera);
m_perspective_camera->Initialize(m_camera_fov, m_aspect_ratio, m_camera_near, m_camera_far, glm::radians(yaw_degree), glm::radians(pitch_degree));
Expand Down
12 changes: 6 additions & 6 deletions Tetragrama/Controllers/ICameraController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace Tetragrama::Controllers
struct ICameraController : public IController
{
ICameraController() {}
virtual ~ICameraController() = default;
virtual ~ICameraController() = default;

virtual glm::vec3 GetPosition() const = 0;
virtual void SetPosition(const glm::vec3& position) = 0;
virtual const ZEngine::Helpers::Ref<ZEngine::Rendering::Cameras::Camera> GetCamera() const = 0;
virtual void UpdateProjectionMatrix() = 0;
virtual glm::vec3 GetPosition() const = 0;
virtual void SetPosition(const glm::vec3& position) = 0;
virtual ZRawPtr(ZEngine::Rendering::Cameras::Camera) GetCamera() const = 0;
virtual void UpdateProjectionMatrix() = 0;

float GetRotationAngle() const
float GetRotationAngle() const
{
return m_rotation_angle;
}
Expand Down
2 changes: 1 addition & 1 deletion Tetragrama/Controllers/IController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Tetragrama::Controllers
{
struct IController : public ZEngine::Core::IUpdatable, public ZEngine::Core::IEventable, public ZEngine::Helpers::RefCounted
struct IController : public ZEngine::Core::IUpdatable, public ZEngine::Core::IEventable
{
IController() = default;
~IController() = default;
Expand Down
2 changes: 1 addition & 1 deletion Tetragrama/Controllers/PerspectiveCameraController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace Tetragrama::Controllers
}
}

const Ref<Rendering::Cameras::Camera> PerspectiveCameraController::GetCamera() const
ZRawPtr(ZEngine::Rendering::Cameras::Camera) PerspectiveCameraController::GetCamera() const
{
return m_perspective_camera;
}
Expand Down
32 changes: 16 additions & 16 deletions Tetragrama/Controllers/PerspectiveCameraController.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ namespace Tetragrama::Controllers
PerspectiveCameraController();
virtual ~PerspectiveCameraController() = default;

void Update(ZEngine::Core::TimeStep) override;
bool OnEvent(ZEngine::Core::CoreEvent&) override;
void Update(ZEngine::Core::TimeStep) override;
bool OnEvent(ZEngine::Core::CoreEvent&) override;

const ZEngine::Helpers::Ref<ZEngine::Rendering::Cameras::Camera> GetCamera() const override;
ZRawPtr(ZEngine::Rendering::Cameras::Camera) GetCamera() const override;

void UpdateProjectionMatrix() override;
void UpdateProjectionMatrix() override;

virtual glm::vec3 GetPosition() const override;
virtual void SetPosition(const glm::vec3& position) override;
virtual glm::vec3 GetPosition() const override;
virtual void SetPosition(const glm::vec3& position) override;

virtual float GetFieldOfView() const;
virtual void SetFieldOfView(float rad_fov);
virtual float GetFieldOfView() const;
virtual void SetFieldOfView(float rad_fov);

virtual float GetNear() const;
virtual void SetNear(float value);
virtual float GetNear() const;
virtual void SetNear(float value);

virtual float GetFar() const;
virtual void SetFar(float value);
virtual float GetFar() const;
virtual void SetFar(float value);

void SetViewport(float width, float height);
void SetTarget(const glm::vec3& target);
void SetViewport(float width, float height);
void SetTarget(const glm::vec3& target);

virtual void ResumeEventProcessing();
virtual void PauseEventProcessing();
virtual void ResumeEventProcessing();
virtual void PauseEventProcessing();

public:
bool OnMouseButtonPressed(ZEngine::Windows::Events::MouseButtonPressedEvent&) override
Expand Down
67 changes: 45 additions & 22 deletions Tetragrama/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <nlohmann/json.hpp>

using namespace ZEngine;
using namespace ZEngine::Core::Container;
using namespace ZEngine::Core::Memory;
using namespace ZEngine::Helpers;
using namespace Tetragrama::Layers;
Expand All @@ -23,34 +24,30 @@ namespace Tetragrama
{
Context = ZPushStruct(arena, EditorContext);

arena->CreateSubArena(ZMega(3), &(Context->Arena));
arena->CreateSubArena(ZMega(10), &(Context->Arena));

Context->ConfigurationPtr = ZPushStructCtor(&(Context->Arena), EditorConfiguration);
Context->CameraControllerPtr = ZPushStructCtor(&(Context->Arena), EditorCameraController);
Context->ConfigurationPtr = ZPushStructCtor(&(Context->Arena), EditorConfiguration);
Context->CameraControllerPtr = ZPushStructCtor(&(Context->Arena), EditorCameraController);
UILayer = ZPushStructCtor(&(Context->Arena), ImguiLayer);
CanvasLayer = ZPushStructCtor(&(Context->Arena), RenderLayer);

Context->CurrentScenePtr = ZPushStructCtor(&(Context->Arena), EditorScene);
Context->CurrentScenePtr->RenderScene = ZPushStructCtor(&(Context->Arena), ZEngine::Rendering::Scenes::GraphicScene);

UILayer = ZPushStructCtor(&(Context->Arena), ImguiLayer);
CanvasLayer = ZPushStructCtor(&(Context->Arena), RenderLayer);
Context->CurrentScenePtr = ZPushStructCtor(&(Context->Arena), EditorScene);

if (Helpers::secure_strlen(file))
{
Context->ConfigurationPtr->ReadConfig(file);

if (Helpers::secure_strlen(Context->ConfigurationPtr->ActiveSceneName))
{
Helpers::secure_strcpy(Context->CurrentScenePtr->Name, 50, Context->ConfigurationPtr->ActiveSceneName);
Context->CurrentScenePtr->Initialize(&(Context->Arena), Context->ConfigurationPtr->ActiveSceneName);
}
}

Context->CurrentScenePtr->RenderScene->IsDrawDataDirty = true;

UILayer->ParentContext = reinterpret_cast<void*>(Context);
CanvasLayer->ParentContext = reinterpret_cast<void*>(Context);
UILayer->ParentContext = reinterpret_cast<void*>(Context);
CanvasLayer->ParentContext = reinterpret_cast<void*>(Context);

std::string title = fmt::format("{0} - Active Scene : {1}", Context->ConfigurationPtr->ProjectName, Context->CurrentScenePtr->Name);
Windows::WindowConfiguration window_conf = {.EnableVsync = true};
std::string title = fmt::format("{0} - Active Scene : {1}", Context->ConfigurationPtr->ProjectName, Context->CurrentScenePtr->Name);
Windows::WindowConfiguration window_conf = {.EnableVsync = true};
window_conf.Title.init(&(Context->Arena), title.c_str());
window_conf.RenderingLayerCollection.init(&(Context->Arena), 1, 0);
window_conf.OverlayLayerCollection.init(&(Context->Arena), 1, 0);
Expand All @@ -70,22 +67,48 @@ namespace Tetragrama
// ZEngine::Engine::Run();
}

void EditorScene::Push(std::string_view mesh, std::string_view model, std::string_view material)
void EditorScene::Initialize(ZEngine::Core::Memory::ArenaAllocator* arena, const char* name)
{
Name = name;

RenderScene = ZPushStructCtor(arena, ZEngine::Rendering::Scenes::GraphicScene);
RenderScene->IsDrawDataDirty = true;

MeshFiles.init(arena, 1, 0);
ModelFiles.init(arena, 1, 0);
MaterialFiles.init(arena, 1, 0);
Hashes.init(arena, 1, 0);
}

void EditorScene::Push(ZEngine::Core::Memory::ArenaAllocator* arena, const char* mesh, const char* model, const char* material)
{
uint16_t mesh_file_id = MeshFiles.size();
uint16_t model_file_id = ModelFiles.size();
uint16_t material_file_id = MaterialFiles.size();

MeshFiles.emplace_back(mesh);
ModelFiles.emplace_back(model);
MaterialFiles.emplace_back(material);
String mesh_file = {};
String model_file = {};
String mat_file = {};

mesh_file.init(arena, mesh);
model_file.init(arena, model);
mat_file.init(arena, material);

MeshFiles.push(mesh_file);
ModelFiles.push(model_file);
MaterialFiles.push(mat_file);

std::stringstream ss;
ss << mesh_file_id << ":" << model_file_id << ":" << material_file_id;
auto hash = ss.str();
Data[hash] = {.MeshFileIndex = mesh_file_id, .ModelPathIndex = model_file_id, .MaterialPathIndex = material_file_id};
auto hash = ss.str();

String hash_str = {};
hash_str.init(arena, hash.c_str());
Hashes.push(hash_str);

Data[hash_str.c_str()] = {.MeshFileIndex = mesh_file_id, .ModelPathIndex = model_file_id, .MaterialPathIndex = material_file_id};

m_has_pending_change = true;
m_has_pending_change = true;
}

bool EditorScene::HasPendingChange() const
Expand Down
Loading