Fixed AssetHierarchy not updating + Drag'n'drop Files in Asset Browser

This commit is contained in:
Simon Lübeß
2023-08-02 13:48:06 +02:00
parent 92334d8150
commit f654a4f379
2 changed files with 98 additions and 12 deletions
+56 -2
View File
@@ -188,6 +188,60 @@ class AssetHierarchy
// We don't need to rename the .ass file, because the filesystem watcher will handle this for us! // We don't need to rename the .ass file, because the filesystem watcher will handle this for us!
} }
public Result<void> MoveFileToNode(StringView assetIdentifierToMove, TreeNode<AssetNode> newParentEntry)
{
TreeNode<AssetNode> fileToMove = GetNodeFromIdentifier(assetIdentifierToMove).GetValueOrDefault();
if (fileToMove == null)
{
Log.EngineLogger.Error($"Couldn't find asset node for asset {assetIdentifierToMove}");
return .Err;
}
String newPathName = scope .();
Path.Combine(newPathName, newParentEntry->Path, fileToMove->Name);
if (File.Exists(newPathName))
{
Log.EngineLogger.Error("Can't move file. Target already exists.");
return .Err;
}
if (fileToMove->IsDirectory)
{
if (Directory.Move(fileToMove->Path, newPathName) case .Err(let error))
{
Log.EngineLogger.Error($"Couldn't move directory from {fileToMove->Path} to {newPathName} ({error}).");
return .Err;
}
}
else
{
if (File.Move(fileToMove->Path, newPathName) case .Err(let error))
{
Log.EngineLogger.Error($"Couldn't move file from {fileToMove->Path} to {newPathName} ({error}).");
return .Err;
}
}
if (fileToMove->AssetFile != null)
{
if (File.Exists(fileToMove->AssetFile.AssetConfigPath))
{
String newConfigPathName = scope .(newPathName);
newConfigPathName.Append(AssetFile.ConfigFileExtension);
if (File.Move(fileToMove->AssetFile.AssetConfigPath, newConfigPathName) case .Err(let error))
{
Log.EngineLogger.Error($"Couldn't move asset description file from {fileToMove->AssetFile.AssetConfigPath} to {newConfigPathName} ({error}). Sorry :(");
return .Err;
}
}
}
return .Ok;
}
/// Sets path to the directory that contains the engine assets. /// Sets path to the directory that contains the engine assets.
public void SetResourcesDirectory(StringView fileName) public void SetResourcesDirectory(StringView fileName)
{ {
@@ -269,10 +323,10 @@ class AssetHierarchy
/// Invokes the the file tree update on the mainthread. /// Invokes the the file tree update on the mainthread.
private void DeferFileTreeUpdate() private void DeferFileTreeUpdate()
{ {
_fileTreeUpdateRequested = true;
if (!_fileTreeUpdateRequested) if (!_fileTreeUpdateRequested)
{ {
_fileTreeUpdateRequested = true;
Application.Instance.InvokeOnMainThread(new () => Application.Instance.InvokeOnMainThread(new () =>
{ {
UpdateFiles(); UpdateFiles();
@@ -328,7 +328,7 @@ namespace GlitchyEditor.EditWindows
files = currentDirectoryNode->Children; files = currentDirectoryNode->Children;
// show back button (".."-File) // show back button (".."-File)
if (currentDirectoryNode->Parent != null) if (currentDirectoryNode->Parent != _manager.AssetHierarchy.RootNode)
{ {
ImGui.PushID("Back"); ImGui.PushID("Back");
@@ -403,11 +403,14 @@ namespace GlitchyEditor.EditWindows
ImGui.PopStyleColor(); ImGui.PopStyleColor();
FileDropTarget(entry);
if (ImGui.IsItemHovered() && ImGui.IsMouseClicked(.Left)) if (ImGui.IsItemHovered() && ImGui.IsMouseClicked(.Left))
{ {
if (_selectedFile != entry->Path) if (_selectedFile != entry->Path)
{ {
_selectedFile.Set(entry->Path); _selectedFile.Set(entry->Path);
_assetToRename.Clear();
} }
} }
@@ -421,6 +424,38 @@ namespace GlitchyEditor.EditWindows
ImGui.EndChild(); ImGui.EndChild();
} }
/// Makes a drop target for the given asset node that files and directories can be dropped on, so that Files can be moved
private void FileDropTarget(TreeNode<AssetNode> dropTarget)
{
if (dropTarget->IsDirectory && ImGui.BeginDragDropTarget())
{
bool allowDrop = false;
ImGui.Payload* peekPayload = ImGui.AcceptDragDropPayload(.ContentBrowserItem, .AcceptPeekOnly);
if (peekPayload != null)
{
StringView assetIdentifier = .((char8*)peekPayload.Data, (int)peekPayload.DataSize);
allowDrop = assetIdentifier != dropTarget->Identifier;
}
if (allowDrop)
{
ImGui.Payload* payload = ImGui.AcceptDragDropPayload(.ContentBrowserItem);
if (payload != null)
{
StringView movedChild = .((char8*)payload.Data, (int)payload.DataSize);
_manager.AssetHierarchy.MoveFileToNode(movedChild, dropTarget);
}
}
ImGui.EndDragDropTarget();
}
}
/// Renders the button for the given directory item. /// Renders the button for the given directory item.
private void DrawDirectoryItem(TreeNode<AssetNode> entry) private void DrawDirectoryItem(TreeNode<AssetNode> entry)
{ {
@@ -458,11 +493,14 @@ namespace GlitchyEditor.EditWindows
ImGui.EndDragDropSource(); ImGui.EndDragDropSource();
} }
FileDropTarget(entry);
if (ImGui.IsItemHovered() && ImGui.IsMouseClicked(.Left)) if (ImGui.IsItemHovered() && ImGui.IsMouseClicked(.Left))
{ {
if (_selectedFile != entry->Path) if (_selectedFile != entry->Path)
{ {
_selectedFile.Set(entry->Path); _selectedFile.Set(entry->Path);
_assetToRename.Clear();
} }
} }
@@ -475,12 +513,12 @@ namespace GlitchyEditor.EditWindows
{ {
ImGui.PushItemWidth((.)IconSize.X); ImGui.PushItemWidth((.)IconSize.X);
if (ImGui.InputText("##renameBox", &_renameFileNameBuffer, _renameFileNameBuffer.Count - 1, .AutoSelectAll | .EnterReturnsTrue)) ImGui.InputText("##renameBox", &_renameFileNameBuffer, _renameFileNameBuffer.Count - 1, .AutoSelectAll);
{
}
ImGui.PopItemWidth(); ImGui.PopItemWidth();
ImGui.SetKeyboardFocusHere(-1);
if (ImGui.IsKeyDown(.Escape)) if (ImGui.IsKeyDown(.Escape))
{ {
_assetToRename.Clear(); _assetToRename.Clear();
@@ -498,12 +536,6 @@ namespace GlitchyEditor.EditWindows
{ {
_assetToRename.Clear(); _assetToRename.Clear();
} }
// TODO: Textbox
// TODO: Save when focus lost
// TODO: Abort on escape
//ImGui.TextUnformatted(entry->Name);
} }
else else
{ {