/****************************************************************************** * File - assetBrowser.cpp * Author - Joey Pollack * Date - 2021/11/10 (y/m/d) * Mod Date - 2021/11/10 (y/m/d) * Description - Browse and manage project assets ******************************************************************************/ #include "assetBrowser.h" #include namespace lunarium { AssetBrowser::AssetBrowser(std::filesystem::path dir) : Panel(PanelType::PT_ASSET_BROWSER, true), mAssetDirectory(dir), mTreeRoot(nullptr), mSelectedNode(nullptr) { mTreeRoot = ReloadAssets(mAssetDirectory); mSelectedNode = mTreeRoot; } void AssetBrowser::SetAssetDirectory(std::filesystem::path dir) { mAssetDirectory = dir; } bool AssetBrowser::DoFrame() { if (!mIsOpen) return false; if (!ImGui::Begin("Asset Browser", &mIsOpen, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar)) { ImGui::End(); return false; } // Directory tree // TODO: Figure out a better way to size stuff other // than using this guess-work magic numbers float row_height = ImGui::GetFrameHeightWithSpacing() + 2.0f; ImVec2 wind_size = ImGui::GetWindowSize(); wind_size.y -= (row_height + 8.0f); wind_size.x -= 15.0f; ImGui::SetCursorPosY(row_height); if (ImGui::BeginChild("Directory Tree", ImVec2(wind_size.x * 0.15f, wind_size.y), true, ImGuiWindowFlags_NoCollapse)) { DoDirTree(mAssetDirectory); } ImGui::EndChild(); ImGui::SetCursorPosY(row_height); ImGui::SetCursorPosX(ImGui::GetCursorPosX() + wind_size.x * 0.15f); // Contents if (ImGui::BeginChild("Contents", ImVec2(wind_size.x * 0.85f, wind_size.y), true, ImGuiWindowFlags_NoCollapse)) { if (std::filesystem::exists(mSelectedDir)) { float yPos = ImGui::GetCursorPosY(); float xPos = ImGui::GetCursorPosX(); float left = xPos; // NOTE: Magic - file icon window size float icon_width = 125.0f; float icon_height = 80.0f; // Display each file in a row for(auto const& dir_entry: std::filesystem::directory_iterator{mSelectedDir}) { if (!std::filesystem::is_directory(dir_entry)) { ImGui::SetNextWindowSize(ImVec2(icon_width, icon_height)); ImGui::SetNextWindowPos(ImVec2(xPos, yPos)); // NOTE: BeginChild here is returning false for some reason // ImGui::BeginChild("file", ImVec2(icon_width, icon_height), false, ImGuiWindowFlags_NoCollapse); // TODO: Add file icon, make selection system ImGui::TextWrapped(dir_entry.path().filename().string().c_str()); // ImGui::EndChild(); // Move to the next space or down a line and back to the left side xPos += icon_width + 10; if (xPos > wind_size.x * 0.85f) { xPos = left; yPos += icon_height + 10; } } } } } ImGui::EndChild(); ImGui::End(); return true; } void AssetBrowser::DoDirTree(std::filesystem::path dir) { if ("" == dir) return; if (!std::filesystem::is_directory(dir)) return; if (ImGui::TreeNode(dir.filename().string().c_str())) { mSelectedDir = dir; for(auto const& dir_entry: std::filesystem::directory_iterator{dir}) { DoDirTree(dir_entry); } ImGui::TreePop(); } } AssetBrowser::Node* AssetBrowser::ReloadAssets(std::filesystem::path dir) { if ("" == dir) return nullptr; Node* pNode = new Node; pNode->Myself = dir; for(auto const& dir_entry: std::filesystem::directory_iterator{dir}) { if (std::filesystem::is_directory(dir_entry)) { pNode->Children.push_back(ReloadAssets(dir_entry)); } else { pNode->Files.push_back(dir_entry); } } return pNode; } } // void treeChildren(ImGuiTreeNodeFlags node_flags, bool isOpen, int index) // { // if (isOpen) // { // for (int p = 0; p < meshList[index].children.size(); p++) // { // if (meshList[index].children[p].children.empty()) // { // node_flags |= ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen; // ImGuiTreeNodeFlags_Bullet // ImGui::TreeNodeEx((void *)(intptr_t)p, node_flags, meshList[index].children[p].name.c_str()); // } // else // { // bool o = ImGui::TreeNodeEx((void *)(intptr_t)p, node_flags, meshList[index].children[p].name.c_str()); // ImGui::TreePop(); // treeChildren(node_flags, o, p); // } // } // ImGui::TreePop(); // } // }