From 628225af45990f16085caa609539e60747cb9e07 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Fri, 11 Nov 2022 13:10:05 +0100 Subject: [PATCH] Asset explorer showing files After loading a project the asset explorer now show all the project files. --- Editor/src/AssetManagement/Asset.h | 15 +- Editor/src/AssetManagement/AssetManager.cpp | 149 ++++++++++++++++++++ Editor/src/AssetManagement/AssetManager.h | 120 ++-------------- Editor/src/UI/widgets.cpp | 48 ++++--- Editor/src/app.cpp | 5 +- 5 files changed, 200 insertions(+), 137 deletions(-) create mode 100644 Editor/src/AssetManagement/AssetManager.cpp diff --git a/Editor/src/AssetManagement/Asset.h b/Editor/src/AssetManagement/Asset.h index 0cb2fe9..b76bf5c 100644 --- a/Editor/src/AssetManagement/Asset.h +++ b/Editor/src/AssetManagement/Asset.h @@ -1,9 +1,7 @@ #pragma once +#include #include -#include "../../YoggieEngine/src/Graphics/Primitives/Mesh.h" -#include "../../YoggieEngine/src/Graphics/Primitives/Texture.h" -#include "../../../YoggieEngine/src/Scene/UUID.h" -typedef uint64_t ASSET_UUID ; +#include "../../YoggieEngine/src/YoggieEngine.h" enum class ASSET_TYPE { Unknown = -1, @@ -14,11 +12,12 @@ enum class ASSET_TYPE { class Asset { public: - Asset(const char* name): ID(UUID::Generate()), name(std::string(name)) {} - virtual ASSET_TYPE GetType() = 0; + Asset(const char* name): name(name) {} + virtual ASSET_TYPE GetType() { return ASSET_TYPE::Unknown; } + const char* GetName() const { return name.c_str(); } + bool isFolder = false ; protected: - std::string& name; - ASSET_UUID ID; + std::string name; }; diff --git a/Editor/src/AssetManagement/AssetManager.cpp b/Editor/src/AssetManagement/AssetManager.cpp new file mode 100644 index 0000000..502c494 --- /dev/null +++ b/Editor/src/AssetManagement/AssetManager.cpp @@ -0,0 +1,149 @@ +#include "AssetManager.h" +#include "../../YoggieEngine/src/AssetManager/ModelImporter.h" +#include +#include +#include + +std::vector AssetManager::assets; +std::filesystem::path AssetManager::currentPath; + + +void AssetManager::Init() +{ + assets = std::vector(); + currentPath = std::filesystem::path("."); +} + +void AssetManager::BuildAssetView() +{ + if (currentPath.empty()) { + return; + } + + for (auto& dir_entry : std::filesystem::directory_iterator(currentPath)) + { + auto asset = Asset(dir_entry.path().filename().string().c_str()); + + if (dir_entry.is_directory()) { + asset.isFolder = true; + } + + assets.push_back(asset); + } + +} + +void AssetManager::setAssetPath(std::filesystem::path path) +{ + currentPath = path; +} + + +YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path assetPath) +{ + YoggieEngine::Mesh imported; + + std::ifstream AssetFile; + AssetFile.open(assetPath, std::ios::binary); + if (AssetFile.is_open()) { + + char* Header = (char*)malloc(8); + unsigned long long Vsize = 0; + uint32_t Vnum = 0; + uint32_t Enum = 0; + + // Read header + AssetFile.read(Header, 8); + AssetFile.read((char*)&Vsize, sizeof(unsigned long long)); + AssetFile.read((char*)&Vnum, sizeof(uint32_t)); + AssetFile.read((char*)&Enum, sizeof(uint32_t)); + + // print Header info + std::cout << "File has header: " << Header << std::endl; + std::cout << "Vertex size: " << Vsize << std::endl; + std::cout << "Number of Vertices: " << Vnum << std::endl; + std::cout << "Number of Elements: " << Enum << std::endl; + free(Header); + + // Load Vertices (Vertex + UV ) + imported.vertices = std::vector < YoggieEngine::Vertex>(); + for (int i = 0; i < Vnum; i++) + { + YoggieEngine::Vertex data = YoggieEngine::Vertex(); + AssetFile.read((char*)&data, Vsize); + + imported.vertices.push_back(data); + + } + + // skip x bytes + AssetFile.ignore(sizeof(char) * 3); + + // Load Elements + imported.elements = std::vector(); + for (int i = 0; i < Enum; i++) { + unsigned int data = 0; + AssetFile.read((char*)&data, sizeof(unsigned int)); + imported.elements.push_back(data); + } + + } + else { + std::cout << "Failed ot open mesh " << std::endl; + } + + + return nullptr; + + +} + +YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder) +{ + + auto model = (YoggieEngine::ModelImporter()).Import(srcPath.string()); + YoggieEngine::Mesh* exportMesh = model->renderable->mesh; + std::filesystem::path MeshFileName = assetFolder / srcPath.filename().replace_extension(".mesh"); + std::cout << "Save path: " << MeshFileName << std::endl; + + std::ofstream meshAsset; + meshAsset.open(MeshFileName.c_str(), std::ios::binary); + if (meshAsset.is_open()) { + + // write a header + static const char* HEADER = "MESH"; + meshAsset.write(HEADER, sizeof(HEADER)); + auto Vsize = sizeof(YoggieEngine::Vertex); + std::cout << "size of vertex: " << Vsize << std::endl; + std::cout << "Addr of vSize: " << &Vsize << std::endl; + auto Vnum = exportMesh->vertices.size(); + auto Enum = exportMesh->elements.size(); + + meshAsset.write((char*)&Vsize, sizeof(unsigned long long)); + meshAsset.write((char*)&Vnum, sizeof(uint32_t)); + meshAsset.write((char*)&Enum, sizeof(uint32_t)); + // write all vertices + for (auto vertice : exportMesh->vertices) + { + meshAsset.write((char*)&vertice, sizeof(vertice)); + } + + // write 3 x 0 byte + meshAsset.write((const char*)"\0\0\0", sizeof(char) * 3); + + // write all indices + for (auto index : exportMesh->elements) { + meshAsset.write((char*)&index, sizeof(index)); + } + + + meshAsset.close(); + } + else { + + spdlog::error("Failed to create/open mesh file."); + } + + return model->renderable; + +} \ No newline at end of file diff --git a/Editor/src/AssetManagement/AssetManager.h b/Editor/src/AssetManagement/AssetManager.h index ba9ab03..08f93d1 100644 --- a/Editor/src/AssetManagement/AssetManager.h +++ b/Editor/src/AssetManagement/AssetManager.h @@ -1,118 +1,20 @@ #pragma once -#include -#include - -#include "../../../YoggieEngine/src/Graphics/Renderable.h" -#include "../../../YoggieEngine/src/AssetManager/ModelImporter.h" -#include -#include +#include +#include "Asset.h" class AssetManager { public: - static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath ) - { - YoggieEngine::Mesh imported; + static void Init(); + static void BuildAssetView(); + static void setAssetPath(std::filesystem::path path); - std::ifstream AssetFile; - AssetFile.open(assetPath, std::ios::binary); - if (AssetFile.is_open()) { + static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath); + static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder); + + static std::vector assets ; - char* Header = (char*) malloc(8); - unsigned long long Vsize = 0; - uint32_t Vnum = 0; - uint32_t Enum = 0; +private: + static std::filesystem::path currentPath; - // Read header - AssetFile.read(Header, 8); - AssetFile.read((char*)&Vsize, sizeof(unsigned long long )); - AssetFile.read((char*)&Vnum, sizeof(uint32_t)); - AssetFile.read((char*)&Enum, sizeof(uint32_t)); - - // print Header info - std::cout << "File has header: " << Header << std::endl; - std::cout << "Vertex size: " << Vsize << std::endl; - std::cout << "Number of Vertices: " << Vnum << std::endl; - std::cout << "Number of Elements: " << Enum << std::endl; - free(Header); - - // Load Vertices (Vertex + UV ) - imported.vertices = std::vector < YoggieEngine::Vertex>(); - for (int i = 0; i < Vnum; i++) - { - YoggieEngine::Vertex data = YoggieEngine::Vertex(); - AssetFile.read((char*) & data, Vsize); - - imported.vertices.push_back(data); - - } - - // skip x bytes - AssetFile.ignore(sizeof(char) * 3); - - // Load Elements - imported.elements = std::vector(); - for (int i = 0; i < Enum; i++) { - unsigned int data = 0; - AssetFile.read((char*)&data, sizeof(unsigned int)); - imported.elements.push_back(data); - } - - } - else { - std::cout << "Failed ot open mesh " << std::endl; - } - - - return nullptr; - - - } - - static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath , const std::filesystem::path assetFolder ) { - auto model = (YoggieEngine::ModelImporter()).Import(srcPath.string()); - YoggieEngine::Mesh* exportMesh = model->renderable->mesh; - std::filesystem::path MeshFileName = assetFolder / srcPath.filename().replace_extension( ".mesh"); - std::cout << "Save path: " << MeshFileName << std::endl; - - std::ofstream meshAsset; - meshAsset.open(MeshFileName, std::ios::binary ); - if (meshAsset.is_open()) { - - // write a header - static const char* HEADER = "MESH"; - meshAsset.write(HEADER, sizeof(HEADER)); - auto Vsize = sizeof(YoggieEngine::Vertex); - std::cout << "size of vertex: " << Vsize << std::endl; - std::cout << "Addr of vSize: " << &Vsize << std::endl; - auto Vnum = exportMesh->vertices.size(); - auto Enum = exportMesh->elements.size(); - - meshAsset.write((char*) & Vsize, sizeof(unsigned long long ) ); - meshAsset.write((char*)&Vnum, sizeof(uint32_t)); - meshAsset.write((char*)&Enum, sizeof(uint32_t)); - // write all vertices - for (auto vertice : exportMesh->vertices) - { - meshAsset.write((char*)&vertice, sizeof(vertice)); - } - - // write 3 x 0 byte - meshAsset.write((const char*)"\0\0\0", sizeof(char) * 3); - - // write all indices - for (auto index : exportMesh->elements) { - meshAsset.write((char*)&index, sizeof(index)); - } - - - meshAsset.close(); - } - else { - spdlog::error("Failed to create/open mesh file."); - } - - return model->renderable; - - } }; \ No newline at end of file diff --git a/Editor/src/UI/widgets.cpp b/Editor/src/UI/widgets.cpp index e89a28b..9cd3eb3 100644 --- a/Editor/src/UI/widgets.cpp +++ b/Editor/src/UI/widgets.cpp @@ -3,7 +3,7 @@ #include #include "../../YoggieEngine/src/Scene/Components.h" #include "../../YoggieEngine/src/Scene/Entity.h" - +#include "../AssetManagement/AssetManager.h" class Editor; void ComponentView(const std::string& componentName, voidFunction func) @@ -184,28 +184,38 @@ void AssetsFinder() { ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.f, 0.f, 0.f, 0.f)); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f)); - for (int row = 0; row < 4; row++) { - ImGui::TableNextRow(); - for (int column = 0; column < 3; column++) { - ImGui::TableSetColumnIndex(column); - - if (column % 2) { - ImGui::ImageButton( - (ImTextureID)folderIcon.GetID(), - ImVec2{ (float)iconSize,(float)iconSize }); - ImGui::Text("Folder %d", row); - } - else { - ImGui::ImageButton( - (ImTextureID)AssetIcon.GetID(), - ImVec2{ (float)iconSize, (float)iconSize }); - ImGui::Text("Asset %d", row); - } + int row = 0; + int column = 0 ; + for (auto& asset : AssetManager::assets) { + if (column % 3 == 0) { + ImGui::TableNextRow(); + column = 0; + row++; + } + + ImGui::TableSetColumnIndex(column); + + if (asset.isFolder) { + ImGui::ImageButton( + (ImTextureID)folderIcon.GetID(), + ImVec2{ (float)iconSize,(float)iconSize }); + ImGui::Text(asset.GetName(), row); } - } + else { + ImGui::ImageButton( + (ImTextureID)AssetIcon.GetID(), + ImVec2{ (float)iconSize, (float)iconSize }); + ImGui::Text(asset.GetName(), row); + } + + + column++; + } + + ImGui::PopStyleColor(3); ImGui::EndTable(); diff --git a/Editor/src/app.cpp b/Editor/src/app.cpp index b83b467..34d48b4 100644 --- a/Editor/src/app.cpp +++ b/Editor/src/app.cpp @@ -36,6 +36,7 @@ public: double previous = glfwGetTime(); double lag = 0.0; + AssetManager::Init(); renderer->Prepare(activeRuntime.MainScene); while (!mainWindow.WindowShouldClose()) @@ -139,6 +140,8 @@ public: switch (result) { case(NFD_OKAY): Project::LoadProject(path, activeRuntime.CurrentProject); + AssetManager::setAssetPath(activeRuntime.CurrentProject.get()->GetProjectDirectory()); + AssetManager::BuildAssetView(); break; case(NFD_CANCEL): break; @@ -225,7 +228,7 @@ public: switch (result) { case(NFD_OKAY): // Import Model - AssetManager::LoadFromSource(modelImportPath, "."); + AssetManager::LoadFromSource(modelImportPath, activeRuntime.CurrentProject.get()->GetProjectDirectory() / "Assets"); break; case(NFD_CANCEL): break;