From ce1ce8ef491cc4f712a9a2efb424968c1ee94290 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Wed, 22 Jun 2022 14:10:20 -0400 Subject: [PATCH] Asset drag/drop to move file working --- docs/tasks/editor.todo | 3 + .../editor/contents/content_manager.cpp | 16 ++++- .../editor/contents/content_manager.h | 2 + src/run_modes/editor/contents/editor_asset.h | 2 + src/run_modes/editor/panels/asset_browser.cpp | 69 ++++++++++++------- src/run_modes/editor/panels/asset_browser.h | 2 + 6 files changed, 69 insertions(+), 25 deletions(-) diff --git a/docs/tasks/editor.todo b/docs/tasks/editor.todo index df96ff8..6154190 100644 --- a/docs/tasks/editor.todo +++ b/docs/tasks/editor.todo @@ -43,6 +43,9 @@ Editor: Asset Viewer: ✔ Get references to the EditorAsset objects instead of the raw file locations @done(22-06-01 18:48) ☐ Put files into a table with columns for the file Properties + ✔ Double click folders in content view to open @done(22-06-21 15:17) + ✔ Drag and Drop asset files to move them to different folders @done(22-06-21 15:18) + ✔ Add directory back button to content view @done(22-06-21 15:45) Tools: Tile Map Editor: diff --git a/src/run_modes/editor/contents/content_manager.cpp b/src/run_modes/editor/contents/content_manager.cpp index 757aa1c..b311125 100644 --- a/src/run_modes/editor/contents/content_manager.cpp +++ b/src/run_modes/editor/contents/content_manager.cpp @@ -15,6 +15,8 @@ #include #include +#include + // Asset types #include "tile_set.h" @@ -222,7 +224,7 @@ namespace lunarium { namespace editor container.clear(); for (auto iter = mAssets.begin(); iter != mAssets.end(); iter++) { - auto temp = iter->second->GetFileLocation().remove_filename(); + auto temp = iter->second->GetFileLocation().remove_filename().parent_path(); if (temp == dir) { container.push_back(iter->second); @@ -303,6 +305,18 @@ namespace lunarium { namespace editor delete iter->second; mAssets.erase(iter); + Save(); + } + + + void ContentManager::MoveAsset(EditorAsset* asset, std::filesystem::path to) + { + std::filesystem::path from = mpProject->GetAssetDirectory() / asset->GetFileLocation(); + std::filesystem::rename(from, to); + std::filesystem::path r = mpProject->MakeRelativeToAssets(to); + asset->mLocation = r; + + Save(); } bool ContentManager::IsValidAsset(nlohmann::json& node) diff --git a/src/run_modes/editor/contents/content_manager.h b/src/run_modes/editor/contents/content_manager.h index b1c9abf..05aeb65 100644 --- a/src/run_modes/editor/contents/content_manager.h +++ b/src/run_modes/editor/contents/content_manager.h @@ -51,6 +51,8 @@ namespace lunarium { namespace editor [[nodiscard]] OpRes ImportFile(std::filesystem::path file, std::filesystem::path to_location, AssetType type, uint64_t& id); void RemoveAsset(uint64_t asset_id); + void MoveAsset(EditorAsset* asset, std::filesystem::path to); + private: static ContentManager* mpInstance; Project* mpProject; diff --git a/src/run_modes/editor/contents/editor_asset.h b/src/run_modes/editor/contents/editor_asset.h index 65c85bd..010ef59 100644 --- a/src/run_modes/editor/contents/editor_asset.h +++ b/src/run_modes/editor/contents/editor_asset.h @@ -38,6 +38,8 @@ namespace lunarium { namespace editor uint64_t mID; std::filesystem::path mLocation; + void SetLocation(std::filesystem::path path) { mLocation = path; } + protected: std::filesystem::path mAssetDir; diff --git a/src/run_modes/editor/panels/asset_browser.cpp b/src/run_modes/editor/panels/asset_browser.cpp index eb1177b..6942e74 100644 --- a/src/run_modes/editor/panels/asset_browser.cpp +++ b/src/run_modes/editor/panels/asset_browser.cpp @@ -74,17 +74,16 @@ namespace editor float xPos = ImGui::GetCursorPosX(); float left = xPos; + ImGui::BeginChild("ToolBar", ImVec2(0, ImGui::GetFrameHeightWithSpacing())); + if (ImGui::Button("Back")) + { + mSelectedDir = mSelectedDir.parent_path(); + mSyncTree = true; + } - // Display each file in a row - // for(auto const& dir_entry: std::filesystem::directory_iterator{mSelectedDir}) - // { - // if (!std::filesystem::is_directory(dir_entry)) - // { - // // TODO: Turn into a table with file properties as columns - // ImGui::TextWrapped(dir_entry.path().filename().string().c_str()); + ImGui::EndChild(); - // } - // } + ImGui::BeginChild("Files"); // List directories for(auto const& dir_entry: std::filesystem::directory_iterator{mSelectedDir}) @@ -92,6 +91,7 @@ namespace editor if (dir_entry.is_directory()) { ImGui::Text("DIRECTORY: %s", dir_entry.path().filename().string().c_str()); + HandleAssetDrop(dir_entry.path()); if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { // TODO: Open relevant editor @@ -117,7 +117,9 @@ namespace editor } if (ImGui::BeginDragDropSource()) { - ImGui::SetDragDropPayload("EditorAsset", (void*)(*iter), sizeof(EditorAsset)); + // Need to pass a pointer to the payload data. This means we need to pass a + // pointer to the EditorAsset pointer (EditorAsset**) which &(*iter) becomes + ImGui::SetDragDropPayload("EditorAsset", (void*)&(*iter), sizeof(EditorAsset*)); ImGui::Text("%s", (*iter)->GetFileLocation().stem().string().c_str()); ImGui::EndDragDropSource(); } @@ -129,6 +131,8 @@ namespace editor } DoContextMenu(); + + ImGui::EndChild(); } } ImGui::EndChild(); @@ -146,27 +150,24 @@ namespace editor return; std::string next_node_name = dir.filename().string(); - if (mSyncTree && mSelectedDir.string().find(next_node_name) != std::string::npos) + if (mSyncTree) { - ImGui::SetNextItemOpen(true); + if (mSelectedDir.string().find(next_node_name) != std::string::npos) + { + ImGui::SetNextItemOpen(true); + } + else + { + ImGui::SetNextItemOpen(false); + } } + if (ImGui::TreeNode(next_node_name.c_str())) { if (!mSyncTree) { mSelectedDir = dir; - if (ImGui::BeginDragDropTarget()) - { - if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("EditorAsset")) - { - EditorAsset* pAsset = (EditorAsset*) payload->Data; - Logger::Info(Editor::LogCat, "Dropping asset: %s - To: %s", - pAsset->GetFileLocation().stem().string().c_str(), mSelectedDir.string().c_str()); - - // TODO: Handle moving the asset - } - ImGui::EndDragDropTarget(); - } + HandleAssetDrop(mSelectedDir); } for(auto const& dir_entry: std::filesystem::directory_iterator{dir}) @@ -229,6 +230,26 @@ namespace editor return pNode; } + + + void AssetBrowser::HandleAssetDrop(std::filesystem::path dir) + { + if (ImGui::BeginDragDropTarget()) + { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("EditorAsset")) + { + EditorAsset* pAsset = *((EditorAsset**) payload->Data); + // EditorAsset* pAsset = ((EditorAsset*) payload->Data); + Logger::Info(Editor::LogCat, "Dropping asset: %s - To: %s", + pAsset->GetFileLocation().stem().string().c_str(), dir.string().c_str()); + + // TODO: Handle moving the asset + ContentManager::GetInstance().MoveAsset(pAsset, dir / pAsset->GetFileLocation().filename()); + Logger::Debug(Editor::LogCat, "Asset Location: %s", pAsset->GetFileLocation().string().c_str()); + } + ImGui::EndDragDropTarget(); + } + } } } diff --git a/src/run_modes/editor/panels/asset_browser.h b/src/run_modes/editor/panels/asset_browser.h index cf2b84a..c9b25f5 100644 --- a/src/run_modes/editor/panels/asset_browser.h +++ b/src/run_modes/editor/panels/asset_browser.h @@ -51,6 +51,8 @@ namespace editor Node* ReloadAssets(std::filesystem::path dir); Node* FindNodeAt(std::filesystem::path dir); + void HandleAssetDrop(std::filesystem::path dir); + void DoDirTree(std::filesystem::path dir); void DoContextMenu(); };