Skip to content

Commit 82a1c63

Browse files
[Editor] Added support for renaming files and folders;
1 parent e80d3ec commit 82a1c63

File tree

3 files changed

+109
-7
lines changed

3 files changed

+109
-7
lines changed

Engine/Editor/ImGuiUtils.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ internal static class ImGuiUtils
99
{
1010
public class ContentGridItem
1111
{
12+
public bool renaming = false;
13+
public bool selected = false;
14+
public string renamedName;
1215
public string name;
1316
public Texture texture;
1417
public Func<Texture, Texture> ensureValidTexture;
@@ -24,8 +27,9 @@ public class ContentGridItem
2427
/// <param name="onClick">Callback when an item is clicked</param>
2528
/// <param name="onDoubleClick">Callback when an item is double clicked</param>
2629
/// <param name="onDragDropped">Callback when an item was dropped</param>
27-
public static void ContentGrid(List<ContentGridItem> items, float padding, float thumbnailSize, string dragPayload,
28-
Action<int, ContentGridItem> onClick, Action<int, ContentGridItem> onDoubleClick, Action<int, ContentGridItem> onDragDropped)
30+
public static void ContentGrid(List<ContentGridItem> items, float padding, float thumbnailSize, string dragPayload, bool allowRename,
31+
Action<int, ContentGridItem> onClick, Action<int, ContentGridItem> onDoubleClick, Action<int, ContentGridItem> onDragDropped,
32+
Action<int, ContentGridItem, string> onRename)
2933
{
3034
var cellSize = padding + thumbnailSize;
3135
var width = ImGui.GetContentRegionAvail().X;
@@ -53,6 +57,14 @@ public static void ContentGrid(List<ContentGridItem> items, float padding, float
5357
}
5458
else if(ImGui.IsMouseClicked(ImGuiMouseButton.Left))
5559
{
60+
foreach(var j in items)
61+
{
62+
j.selected = false;
63+
j.renaming = false;
64+
}
65+
66+
item.selected = true;
67+
5668
onClick?.Invoke(i, item);
5769
}
5870
else if(dragPayload != null &&
@@ -70,13 +82,37 @@ public static void ContentGrid(List<ContentGridItem> items, float padding, float
7082

7183
ImGui.EndDragDropSource();
7284
}
73-
else if(ImGui.IsMouseDown(ImGuiMouseButton.Left) == false)
85+
else if (ImGui.IsMouseDown(ImGuiMouseButton.Left) == false)
7486
{
7587
StapleEditor.instance.dragDropPayloads.Clear();
7688
}
7789
}
7890

79-
ImGui.TextWrapped(item.name);
91+
if (item.selected)
92+
{
93+
if (Input.GetKeyDown(KeyCode.Enter))
94+
{
95+
item.renaming = true;
96+
item.renamedName = item.name;
97+
}
98+
}
99+
100+
if (item.renaming && Input.GetKeyDown(KeyCode.Escape) == false)
101+
{
102+
if (ImGui.InputText("##RENAME", ref item.renamedName, 1000, ImGuiInputTextFlags.EnterReturnsTrue |
103+
ImGuiInputTextFlags.AutoSelectAll))
104+
{
105+
item.renaming = false;
106+
107+
onRename?.Invoke(i, item, item.renamedName);
108+
}
109+
}
110+
else
111+
{
112+
item.renaming = false;
113+
114+
ImGui.TextWrapped(item.name);
115+
}
80116

81117
ImGui.NextColumn();
82118

Engine/Editor/ProjectBrowser/ProjectBrowser.cs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ void Recursive(ProjectBrowserNode node)
733733
ImGui.BeginChild("ProjectBrowserContentAssets");
734734

735735
ImGuiUtils.ContentGrid(currentContentBrowserNodes, contentPanelPadding, contentPanelThumbnailSize,
736-
"ASSET",
736+
"ASSET", true,
737737
(index, _) =>
738738
{
739739
ProjectBrowserNode item = null;
@@ -963,6 +963,72 @@ void Recursive(MeshAsset.Node current, Transform parent)
963963
}
964964

965965
dropType = ProjectBrowserDropType.None;
966+
}, (index, localItem, name) =>
967+
{
968+
var invalidChars = Path.GetInvalidPathChars()
969+
.Concat(Path.GetInvalidFileNameChars())
970+
.ToList();
971+
972+
if(invalidChars.Any(x => name.Contains(x)))
973+
{
974+
return;
975+
}
976+
977+
978+
ProjectBrowserNode item = null;
979+
980+
if (currentContentNode == null)
981+
{
982+
currentContentNode = projectBrowserNodes[index];
983+
984+
item = currentContentNode;
985+
}
986+
else
987+
{
988+
item = currentContentNode.type == ProjectBrowserNodeType.Folder ? (index >= 0 && index < currentContentNode.subnodes.Count ? currentContentNode.subnodes[index] : null) :
989+
currentContentNode;
990+
}
991+
992+
if (item == null)
993+
{
994+
return;
995+
}
996+
997+
var normalizedPath = Path.Combine(Path.GetDirectoryName(item.path), $"{name}{Path.GetExtension(item.path) ?? ""}");
998+
999+
switch(item.type)
1000+
{
1001+
case ProjectBrowserNodeType.File:
1002+
1003+
try
1004+
{
1005+
File.Move(item.path, normalizedPath);
1006+
1007+
if(File.Exists($"{item.path}.meta"))
1008+
{
1009+
File.Move($"{item.path}.meta", $"{normalizedPath}.meta");
1010+
}
1011+
}
1012+
catch(Exception)
1013+
{
1014+
}
1015+
1016+
break;
1017+
1018+
case ProjectBrowserNodeType.Folder:
1019+
1020+
try
1021+
{
1022+
Directory.Move(item.path, normalizedPath);
1023+
}
1024+
catch(Exception)
1025+
{
1026+
}
1027+
1028+
break;
1029+
}
1030+
1031+
EditorUtils.RefreshAssets(null);
9661032
});
9671033

9681034
var result = ImGui.IsWindowHovered();

Engine/Editor/Windows/AssetPickerWindow.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public override void OnGUI()
6666
}).ToList();
6767

6868
ImGuiUtils.ContentGrid(gridItems, ProjectBrowser.contentPanelPadding, ProjectBrowser.contentPanelThumbnailSize,
69-
null,
69+
null, false,
7070
(index, item) =>
7171
{
7272
},
@@ -233,6 +233,6 @@ public override void OnGUI()
233233
}
234234

235235
Close();
236-
}, null);
236+
}, null, null);
237237
}
238238
}

0 commit comments

Comments
 (0)