Asset drag/drop to move file working

gui_api_redesign
Joey Pollack 4 years ago
parent a80d98d0c0
commit ce1ce8ef49

@ -43,6 +43,9 @@ Editor:
Asset Viewer: Asset Viewer:
✔ Get references to the EditorAsset objects instead of the raw file locations @done(22-06-01 18:48) ✔ 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 ☐ 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: Tools:
Tile Map Editor: Tile Map Editor:

@ -15,6 +15,8 @@
#include <editor/editor.h> #include <editor/editor.h>
#include <fstream> #include <fstream>
#include <utils/logger.h>
// Asset types // Asset types
#include "tile_set.h" #include "tile_set.h"
@ -222,7 +224,7 @@ namespace lunarium { namespace editor
container.clear(); container.clear();
for (auto iter = mAssets.begin(); iter != mAssets.end(); iter++) 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) if (temp == dir)
{ {
container.push_back(iter->second); container.push_back(iter->second);
@ -303,6 +305,18 @@ namespace lunarium { namespace editor
delete iter->second; delete iter->second;
mAssets.erase(iter); 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) bool ContentManager::IsValidAsset(nlohmann::json& node)

@ -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); [[nodiscard]] OpRes ImportFile(std::filesystem::path file, std::filesystem::path to_location, AssetType type, uint64_t& id);
void RemoveAsset(uint64_t asset_id); void RemoveAsset(uint64_t asset_id);
void MoveAsset(EditorAsset* asset, std::filesystem::path to);
private: private:
static ContentManager* mpInstance; static ContentManager* mpInstance;
Project* mpProject; Project* mpProject;

@ -38,6 +38,8 @@ namespace lunarium { namespace editor
uint64_t mID; uint64_t mID;
std::filesystem::path mLocation; std::filesystem::path mLocation;
void SetLocation(std::filesystem::path path) { mLocation = path; }
protected: protected:
std::filesystem::path mAssetDir; std::filesystem::path mAssetDir;

@ -74,17 +74,16 @@ namespace editor
float xPos = ImGui::GetCursorPosX(); float xPos = ImGui::GetCursorPosX();
float left = xPos; 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 ImGui::EndChild();
// 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::BeginChild("Files");
// }
// List directories // List directories
for(auto const& dir_entry: std::filesystem::directory_iterator{mSelectedDir}) for(auto const& dir_entry: std::filesystem::directory_iterator{mSelectedDir})
@ -92,6 +91,7 @@ namespace editor
if (dir_entry.is_directory()) if (dir_entry.is_directory())
{ {
ImGui::Text("DIRECTORY: %s", dir_entry.path().filename().string().c_str()); ImGui::Text("DIRECTORY: %s", dir_entry.path().filename().string().c_str());
HandleAssetDrop(dir_entry.path());
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
{ {
// TODO: Open relevant editor // TODO: Open relevant editor
@ -117,7 +117,9 @@ namespace editor
} }
if (ImGui::BeginDragDropSource()) 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::Text("%s", (*iter)->GetFileLocation().stem().string().c_str());
ImGui::EndDragDropSource(); ImGui::EndDragDropSource();
} }
@ -129,6 +131,8 @@ namespace editor
} }
DoContextMenu(); DoContextMenu();
ImGui::EndChild();
} }
} }
ImGui::EndChild(); ImGui::EndChild();
@ -146,27 +150,24 @@ namespace editor
return; return;
std::string next_node_name = dir.filename().string(); 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 (ImGui::TreeNode(next_node_name.c_str()))
{ {
if (!mSyncTree) if (!mSyncTree)
{ {
mSelectedDir = dir; mSelectedDir = dir;
if (ImGui::BeginDragDropTarget()) HandleAssetDrop(mSelectedDir);
{
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();
}
} }
for(auto const& dir_entry: std::filesystem::directory_iterator{dir}) for(auto const& dir_entry: std::filesystem::directory_iterator{dir})
@ -229,6 +230,26 @@ namespace editor
return pNode; 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();
}
}
} }
} }

@ -51,6 +51,8 @@ namespace editor
Node* ReloadAssets(std::filesystem::path dir); Node* ReloadAssets(std::filesystem::path dir);
Node* FindNodeAt(std::filesystem::path dir); Node* FindNodeAt(std::filesystem::path dir);
void HandleAssetDrop(std::filesystem::path dir);
void DoDirTree(std::filesystem::path dir); void DoDirTree(std::filesystem::path dir);
void DoContextMenu(); void DoContextMenu();
}; };

Loading…
Cancel
Save