Skip to content

Commit 4392679

Browse files
Supporting open scene file by drag and drop operation (#451)
* refactored arena splitting * support drag n drop scene opening
1 parent 8442ac3 commit 4392679

15 files changed

+124
-61
lines changed

Tetragrama/Components/DockspaceUIComponent.cpp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace Tetragrama::Components
3333
{
3434
UIComponent::Initialize(parent, name, visibility, closed);
3535

36-
parent->LocalArena.CreateSubArena(ZKilo(100), &LocalArena);
36+
parent->LocalArena.CreateSubArena(ZMega(1), &LocalArena);
3737

3838
m_asset_importer = ZPushStructCtor(parent->Arena, Importers::AssimpImporter);
3939
m_editor_serializer = ZPushStructCtor(parent->Arena, Serializers::EditorSceneSerializer);
@@ -588,31 +588,44 @@ namespace Tetragrama::Components
588588
co_return;
589589
}
590590

591+
std::future<void> DockspaceUIComponent::OnOpenSceneRequestAsync(const char* filename)
592+
{
593+
if (!ZEngine::Helpers::secure_strlen(filename))
594+
{
595+
co_return;
596+
}
597+
598+
s_is_scene_loading = true;
599+
m_editor_serializer->Deserialize(filename);
600+
co_return;
601+
}
602+
591603
std::future<void> DockspaceUIComponent::OnImportAssetAsync(const char* filename)
592604
{
593-
if (ZEngine::Helpers::secure_strlen(filename) == 0)
605+
if ((ZEngine::Helpers::secure_strlen(filename) == 0) || m_asset_importer->IsImporting())
594606
{
595607
co_return;
596608
}
597609

598610
LocalArena.Clear();
599611

600-
auto context = reinterpret_cast<EditorContext*>(ParentLayer->ParentContext);
601-
auto arena = &LocalArena;
602-
const auto& editor_config = *context->ConfigurationPtr;
603-
auto parent_path = std::filesystem::path(filename).parent_path().string();
604-
auto asset_name = fs::path(filename).filename().replace_extension().string();
605-
auto output_asset_file = fmt::format("{}.zemesh", asset_name.c_str());
606-
607-
Importers::ImportConfiguration config = {};
608-
config.OutputWorkingSpacePath.init(arena, editor_config.WorkingSpacePath.c_str());
609-
config.OutputTextureFilesPath.init(arena, editor_config.DefaultImportTexturePath.c_str());
610-
config.OutputAssetsPath.init(arena, editor_config.SceneDataPath.c_str());
611-
config.AssetName.init(arena, asset_name.c_str());
612-
config.OutputAssetFile.init(arena, output_asset_file.c_str());
613-
config.InputBaseAssetFilePath.init(arena, parent_path.c_str());
614-
615-
co_await m_asset_importer->ImportAsync(filename, config);
612+
auto context = reinterpret_cast<EditorContext*>(ParentLayer->ParentContext);
613+
auto arena = &LocalArena;
614+
const auto& editor_config = *context->ConfigurationPtr;
615+
auto parent_path = std::filesystem::path(filename).parent_path().string();
616+
auto asset_name = fs::path(filename).filename().replace_extension().string();
617+
auto output_asset_file = fmt::format("{}.zemesh", asset_name.c_str());
618+
619+
auto config = ZPushStruct(arena, Importers::ImportConfiguration);
620+
config->OutputWorkingSpacePath.init(arena, editor_config.WorkingSpacePath.c_str());
621+
config->OutputTextureFilesPath.init(arena, editor_config.DefaultImportTexturePath.c_str());
622+
config->OutputAssetsPath.init(arena, editor_config.SceneDataPath.c_str());
623+
config->AssetName.init(arena, asset_name.c_str());
624+
config->OutputAssetFile.init(arena, output_asset_file.c_str());
625+
config->InputBaseAssetFilePath.init(arena, parent_path.c_str());
626+
627+
m_asset_importer->ImportAsync(filename, *config);
628+
co_return;
616629
}
617630

618631
std::future<void> DockspaceUIComponent::OnExitAsync()

Tetragrama/Components/DockspaceUIComponent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace Tetragrama::Components
4848

4949
std::future<void> OnNewSceneAsync();
5050
std::future<void> OnOpenSceneAsync();
51+
std::future<void> OnOpenSceneRequestAsync(const char* filename);
5152
std::future<void> OnImportAssetAsync(const char* filename);
5253
std::future<void> OnExitAsync();
5354

Tetragrama/Components/LogUIComponent.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Tetragrama::Components
2222
{
2323
UIComponent::Initialize(parent, name, visibility, closed);
2424

25-
parent->LocalArena.CreateSubArena(ZMega(1), &m_local_arena);
25+
parent->Arena->CreateSubArena(ZMega(1), &m_local_arena);
2626
m_log_queue.init(&(m_local_arena), m_maxCount, m_maxCount);
2727
m_handler_cookie = Logger::AddEventHandler(std::bind(&LogUIComponent::OnLog, this, std::placeholders::_1));
2828
}
@@ -147,7 +147,12 @@ namespace Tetragrama::Components
147147
m_log_queue.clear();
148148
}
149149

150-
m_log_queue[m_currentCount] = std::move(message);
150+
auto& m = m_log_queue[m_currentCount];
151+
m.Message = message.Message;
152+
m.Color[0] = message.Color[0];
153+
m.Color[1] = message.Color[1];
154+
m.Color[2] = message.Color[2];
155+
m.Color[3] = message.Color[3];
151156
m_currentCount.store(++count, std::memory_order_release);
152157
}
153158
}

Tetragrama/Components/ProjectViewUIComponent.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,18 @@ namespace Tetragrama::Components
134134
ImGui::PopStyleColor();
135135
ImGui::SetCursorPos(ImVec2(cursorPos.x, cursorPos.y + m_thumbnail_size + margin));
136136

137-
if (ImGui::BeginDragDropSource())
137+
/*
138+
* Drag and Drop scene file
139+
* We don't support drag-and-drop for folder for now
140+
*/
141+
if (!entry.is_directory())
138142
{
139-
std::string itemPath = entry.path().string();
140-
ImGui::SetDragDropPayload("CONTENT_BROWSER_ITEM", itemPath.c_str(), (itemPath.length() + 1) * sizeof(char));
141-
ImGui::EndDragDropSource();
143+
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID))
144+
{
145+
std::string itemPath = entry.path().string();
146+
ImGui::SetDragDropPayload("CONTENT_BROWSER_FILE_DRAG_OP", itemPath.c_str(), (itemPath.length() + 1) * sizeof(char));
147+
ImGui::EndDragDropSource();
148+
}
142149
}
143150

144151
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))

Tetragrama/Components/SceneViewportUIComponent.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ namespace Tetragrama::Components
114114

115115
ImGuizmo::SetDrawlist();
116116

117+
if (ImGui::BeginDragDropTarget())
118+
{
119+
char buf[DEFAULT_STR_BUFFER] = {0};
120+
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CONTENT_BROWSER_FILE_DRAG_OP"))
121+
{
122+
ZEngine::Helpers::secure_memcpy(buf, DEFAULT_STR_BUFFER, payload->Data, payload->DataSize);
123+
if (ZEngine::Helpers::secure_strlen(buf) > 0)
124+
{
125+
auto file_ext = std::filesystem::path(buf).extension().string();
126+
if (file_ext == ".zescene")
127+
{
128+
Messengers::IMessenger::SendAsync<Windows::Layers::Layer, Messengers::GenericMessage<std::string>>(EDITOR_COMPONENT_DOCKSPACE_REQUEST_OPENSCENE, Messengers::GenericMessage<std::string>(buf));
129+
}
130+
}
131+
}
132+
ImGui::EndDragDropTarget();
133+
}
134+
117135
ImGui::End();
118136

119137
ImGui::PopStyleVar();

Tetragrama/EntryPoint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int applicationEntryPoint(int argc, char* argv[])
2222
CLI11_PARSE(app, argc, argv);
2323

2424
MemoryManager manager = {};
25-
MemoryConfiguration config = {.DefaultSize = ZGiga(1u)};
25+
MemoryConfiguration config = {.DefaultSize = ZGiga(2u)};
2626
manager.Initialize(config);
2727
auto arena = &(manager.ArenaAllocator);
2828

Tetragrama/Importers/AssimpImporter.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@ namespace Tetragrama::Importers
2424

2525
AssimpImporter::~AssimpImporter() {}
2626

27-
std::future<void> AssimpImporter::ImportAsync(const char* filename, ImportConfiguration& config)
27+
std::future<void> AssimpImporter::ImportAsync(const char* filename, const ImportConfiguration& cfg)
2828
{
29-
ThreadPoolHelper::Submit([this, path = filename, config] {
29+
ThreadPoolHelper::Submit([this, path = filename, cfg] {
3030
std::unique_lock l(m_mutex);
3131

32+
Arena.Clear();
33+
auto thread_local_arena = &Arena;
34+
ImportConfiguration config = {};
35+
36+
config.OutputWorkingSpacePath.init(thread_local_arena, cfg.OutputWorkingSpacePath.c_str());
37+
config.OutputTextureFilesPath.init(thread_local_arena, cfg.OutputTextureFilesPath.c_str());
38+
config.OutputAssetsPath.init(thread_local_arena, cfg.OutputAssetsPath.c_str());
39+
config.AssetName.init(thread_local_arena, cfg.AssetName.c_str());
40+
config.OutputAssetFile.init(thread_local_arena, cfg.OutputAssetFile.c_str());
41+
config.InputBaseAssetFilePath.init(thread_local_arena, cfg.InputBaseAssetFilePath.c_str());
42+
3243
m_is_importing.store(true, std::memory_order_release);
3344

3445
Assimp::Importer importer{};
@@ -54,27 +65,27 @@ namespace Tetragrama::Importers
5465
Array<AssetMaterial> materials = {};
5566
Array<AssetTexture> textures = {};
5667

57-
ExtractMeshes(&Arena, scene, gen, mesh);
58-
ExtractMaterials(&Arena, scene, gen, materials, hierarchies);
59-
ExtractTextures(&Arena, scene, gen, materials, textures);
68+
ExtractMeshes(thread_local_arena, scene, gen, mesh);
69+
ExtractMaterials(thread_local_arena, scene, gen, materials, hierarchies);
70+
ExtractTextures(thread_local_arena, scene, gen, materials, textures);
6071

61-
CreateHierachy(&Arena, scene, gen, hierarchies, mesh, materials);
62-
CopyTextureFiles(&Arena, textures, config);
72+
CreateHierachy(thread_local_arena, scene, gen, hierarchies, mesh, materials);
73+
CopyTextureFiles(thread_local_arena, textures, config);
6374

6475
Array<AssetImporterOutput> outputs = {};
65-
outputs.init(&Arena, 100);
76+
outputs.init(thread_local_arena, 100);
6677

67-
auto out_m = SerializeMeshAssetFile(&Arena, mesh, hierarchies, config);
78+
auto out_m = SerializeMeshAssetFile(thread_local_arena, mesh, hierarchies, config);
6879
outputs.push(out_m);
6980

7081
for (unsigned i = 0; i < materials.size(); ++i)
7182
{
7283
auto& material = materials[i];
73-
auto out_mat = SerializeMaterialAssetFile(&Arena, material, config);
84+
auto out_mat = SerializeMaterialAssetFile(thread_local_arena, material, config);
7485
outputs.push(out_mat);
7586
}
7687

77-
auto out_tex = SerializeTextureAssetFiles(&Arena, ArrayView{textures}, config);
88+
auto out_tex = SerializeTextureAssetFiles(thread_local_arena, ArrayView{textures}, config);
7889
outputs.push(out_tex);
7990

8091
auto result = fmt::format("{0}{1}{2}", config.OutputAssetsPath.c_str(), PLATFORM_OS_BACKSLASH, config.OutputAssetFile.c_str());
@@ -88,8 +99,6 @@ namespace Tetragrama::Importers
8899
importer.FreeScene();
89100

90101
m_is_importing.store(false, std::memory_order_release);
91-
92-
Arena.Clear();
93102
});
94103

95104
co_return;
@@ -114,7 +123,7 @@ namespace Tetragrama::Importers
114123
}
115124

116125
mesh.MeshUUID = generator();
117-
mesh.SubMeshes.init(arena, t_meshes);
126+
mesh.SubMeshes.init(arena, t_meshes, t_meshes);
118127
mesh.Vertices.init(arena, t_vertices);
119128
mesh.Indices.init(arena, t_indices);
120129

@@ -164,7 +173,7 @@ namespace Tetragrama::Importers
164173
}
165174
}
166175

167-
auto& subMesh = mesh.SubMeshes.push_use({});
176+
auto& subMesh = mesh.SubMeshes[m];
168177
subMesh.VertexCount = vertex_count;
169178
subMesh.VertexOffset = VertexOffset;
170179
subMesh.VertexUnitStreamSize = sizeof(float) * (3 + 3 + 2) /*pos-cmp + normal-cmp + tex-cmp*/;
@@ -364,13 +373,13 @@ namespace Tetragrama::Importers
364373
AssetNode.NodeHierarchyUUID = generator();
365374
AssetNode.MeshUUID = asset_mesh.MeshUUID;
366375

367-
AssetNode.Hierarchies.init(arena, 10);
368-
AssetNode.LocalTransforms.init(arena, 10);
369-
AssetNode.GlobalTransforms.init(arena, 10);
370-
AssetNode.Names.init(arena, 10);
371-
AssetNode.NodeNames.init(arena, 10);
372-
AssetNode.NodeMeshes.init(arena, 10);
373-
AssetNode.NodeMaterials.init(arena, 10);
376+
AssetNode.Hierarchies.init(arena, 3000);
377+
AssetNode.LocalTransforms.init(arena, 3000);
378+
AssetNode.GlobalTransforms.init(arena, 3000);
379+
AssetNode.Names.init(arena, 3000);
380+
AssetNode.NodeNames.init(arena, 3000);
381+
AssetNode.NodeMeshes.init(arena, 3000);
382+
AssetNode.NodeMaterials.init(arena, 3000);
374383

375384
TraverseNode(arena, scene, scene->mRootNode, AssetNode, asset_mesh, materials, -1, 0);
376385
}

Tetragrama/Importers/AssimpImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Tetragrama::Importers
2626
AssimpImporter();
2727
virtual ~AssimpImporter();
2828

29-
virtual std::future<void> ImportAsync(const char* filename, ImportConfiguration& config) override;
29+
virtual std::future<void> ImportAsync(const char* filename, const ImportConfiguration& config) override;
3030
void CopyTextureFiles(ZEngine::Core::Memory::ArenaAllocator*, ZEngine::Core::Containers::Array<AssetTexture>&, const ImportConfiguration&);
3131

3232
private:

Tetragrama/Importers/IAssetImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace Tetragrama::Importers
5757

5858
void IAssetImporter::Initialize(ZEngine::Core::Memory::ArenaAllocator* arena)
5959
{
60-
arena->CreateSubArena(ZMega(100), &Arena);
60+
arena->CreateSubArena(ZMega(200), &Arena);
6161
}
6262

6363
void IAssetImporter::SetOnCompleteCallback(on_import_complete_fn callback)

Tetragrama/Importers/IAssetImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace Tetragrama::Importers
6767
virtual void SetOnLogCallback(on_import_log_fn callback);
6868
virtual bool IsImporting();
6969

70-
virtual std::future<void> ImportAsync(const char* filename, ImportConfiguration& config) = 0;
70+
virtual std::future<void> ImportAsync(const char* filename, const ImportConfiguration& config) = 0;
7171

7272
static AssetImporterOutput SerializeMeshAssetFile(ZEngine::Core::Memory::ArenaAllocator* arena, AssetMesh& data, AssetNodeHierarchy& hierarchies, const ImportConfiguration&);
7373
static AssetImporterOutput SerializeMaterialAssetFile(ZEngine::Core::Memory::ArenaAllocator* arena, AssetMaterial& data, const ImportConfiguration&);

0 commit comments

Comments
 (0)