Browse Source

Invoke rename on creation of library files except for shader and script files

Robert Campbell 6 years ago
parent
commit
a495455f6a

+ 35 - 6
Source/EditorManaged/Windows/Library/LibraryMenu.cs

@@ -64,6 +64,23 @@ namespace bs.Editor
             return EditorWindow.GetWindow<LibraryWindow>() != null;
             return EditorWindow.GetWindow<LibraryWindow>() != null;
         }
         }
 
 
+        /// <summary>
+        /// Scrolls to and selects the specified resource path then start a rename operation.
+        /// </summary>
+        /// <param name="path">Path to the resource to rename</param>
+        /// <param name="window">Reference to the library window</param>
+        private static void StartRename(string path, LibraryWindow window)
+        {
+            if (window == null)
+                return;
+
+            window.Refresh();
+            window.HasFocus = true;
+            window.GoToEntry(path);
+            window.Select(path);
+            window.RenameSelection();
+        }
+
         /// <summary>
         /// <summary>
         /// Creates a new material with the default shader in the currently selected project library folder.
         /// Creates a new material with the default shader in the currently selected project library folder.
         /// </summary>
         /// </summary>
@@ -74,7 +91,9 @@ namespace bs.Editor
             if (win == null)
             if (win == null)
                 return;
                 return;
 
 
-            LibraryUtility.CreateFolder(win.SelectedFolder);
+            string path = LibraryUtility.CreateFolder(win.SelectedFolder);
+
+            StartRename(path, win);
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -88,7 +107,9 @@ namespace bs.Editor
             if(win == null)
             if(win == null)
                 return;
                 return;
 
 
-            LibraryUtility.CreateEmptyMaterial(win.SelectedFolder);
+            string path = LibraryUtility.CreateEmptyMaterial(win.SelectedFolder);
+
+            StartRename(path, win);
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -130,7 +151,9 @@ namespace bs.Editor
             if (win == null)
             if (win == null)
                 return;
                 return;
 
 
-            LibraryUtility.CreateEmptySpriteTexture(win.SelectedFolder);
+            string path = LibraryUtility.CreateEmptySpriteTexture(win.SelectedFolder);
+
+            StartRename(path, win);
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -143,7 +166,9 @@ namespace bs.Editor
             if (win == null)
             if (win == null)
                 return;
                 return;
 
 
-            LibraryUtility.CreateEmptyGUISkin(win.SelectedFolder);
+            string path = LibraryUtility.CreateEmptyGUISkin(win.SelectedFolder);
+
+            StartRename(path, win);
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -156,7 +181,9 @@ namespace bs.Editor
             if (win == null)
             if (win == null)
                 return;
                 return;
 
 
-            LibraryUtility.CreateEmptyStringTable(win.SelectedFolder);
+            string path = LibraryUtility.CreateEmptyStringTable(win.SelectedFolder);
+
+            StartRename(path, win);
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -169,7 +196,9 @@ namespace bs.Editor
             if (win == null)
             if (win == null)
                 return;
                 return;
 
 
-            LibraryUtility.CreateEmptyPhysicsMaterial(win.SelectedFolder);
+            string path = LibraryUtility.CreateEmptyPhysicsMaterial(win.SelectedFolder);
+
+            StartRename(path, win);
         }
         }
 
 
         /// <summary>
         /// <summary>

+ 39 - 15
Source/EditorManaged/Windows/Library/LibraryUtility.cs

@@ -19,105 +19,129 @@ namespace bs.Editor
         /// Creates a new folder with in the specified folder.
         /// Creates a new folder with in the specified folder.
         /// </summary>
         /// </summary>
         /// <param name="folder">Folder relative to project library to create the new folder in.</param>
         /// <param name="folder">Folder relative to project library to create the new folder in.</param>
-        public static void CreateFolder(string folder)
+        /// <returns>The path of the created resource.</returns>
+        public static string CreateFolder(string folder)
         {
         {
             string path = Path.Combine(folder, "New Folder");
             string path = Path.Combine(folder, "New Folder");
             path = GetUniquePath(path);
             path = GetUniquePath(path);
 
 
             ProjectLibrary.CreateFolder(path);
             ProjectLibrary.CreateFolder(path);
+
+            return path;
         }
         }
 
 
         /// <summary>
         /// <summary>
         /// Creates a new material with the default shader in the specified folder.
         /// Creates a new material with the default shader in the specified folder.
         /// </summary>
         /// </summary>
         /// <param name="folder">Folder relative to project library to create the material in.</param>
         /// <param name="folder">Folder relative to project library to create the material in.</param>
-        public static void CreateEmptyMaterial(string folder)
+        /// <returns>The path of the created resource.</returns>
+        public static string CreateEmptyMaterial(string folder)
         {
         {
             string path = Path.Combine(folder, "New Material.asset");
             string path = Path.Combine(folder, "New Material.asset");
             path = GetUniquePath(path);
             path = GetUniquePath(path);
 
 
             Material material = new Material(Builtin.GetShader(BuiltinShader.Standard));
             Material material = new Material(Builtin.GetShader(BuiltinShader.Standard));
             ProjectLibrary.Create(material, path);
             ProjectLibrary.Create(material, path);
+
+            return path;
         }
         }
 
 
         /// <summary>
         /// <summary>
         /// Creates a new empty sprite texture in the specified folder.
         /// Creates a new empty sprite texture in the specified folder.
         /// </summary>
         /// </summary>
         /// <param name="folder">Folder relative to project library to create the sprite texture in.</param>
         /// <param name="folder">Folder relative to project library to create the sprite texture in.</param>
-        public static void CreateEmptySpriteTexture(string folder)
+        /// <returns>The path of the created resource.</returns>
+        public static string CreateEmptySpriteTexture(string folder)
         {
         {
             string path = Path.Combine(folder, "New Sprite Texture.asset");
             string path = Path.Combine(folder, "New Sprite Texture.asset");
             path = GetUniquePath(path);
             path = GetUniquePath(path);
 
 
             SpriteTexture spriteTexture = new SpriteTexture(null);
             SpriteTexture spriteTexture = new SpriteTexture(null);
             ProjectLibrary.Create(spriteTexture, path);
             ProjectLibrary.Create(spriteTexture, path);
+
+            return path;
         }
         }
 
 
         /// <summary>
         /// <summary>
         /// Creates a new empty string table in the specified folder.
         /// Creates a new empty string table in the specified folder.
         /// </summary>
         /// </summary>
         /// <param name="folder">Folder relative to project library to create the string table in.</param>
         /// <param name="folder">Folder relative to project library to create the string table in.</param>
-        public static void CreateEmptyStringTable(string folder)
+        /// <returns>The path of the created resource.</returns>
+        public static string CreateEmptyStringTable(string folder)
         {
         {
             string path = Path.Combine(folder, "New String Table.asset");
             string path = Path.Combine(folder, "New String Table.asset");
             path = GetUniquePath(path);
             path = GetUniquePath(path);
 
 
             StringTable stringTable = new StringTable();
             StringTable stringTable = new StringTable();
             ProjectLibrary.Create(stringTable, path);
             ProjectLibrary.Create(stringTable, path);
+
+            return path;
         }
         }
 
 
         /// <summary>
         /// <summary>
         /// Creates a new empty GUI skin in the specified folder.
         /// Creates a new empty GUI skin in the specified folder.
         /// </summary>
         /// </summary>
         /// <param name="folder">Folder relative to project library to create the GUI skin in.</param>
         /// <param name="folder">Folder relative to project library to create the GUI skin in.</param>
-        public static void CreateEmptyGUISkin(string folder)
+        /// <returns>The path of the created resource.</returns>
+        public static string CreateEmptyGUISkin(string folder)
         {
         {
             string path = Path.Combine(folder, "New GUI Skin.asset");
             string path = Path.Combine(folder, "New GUI Skin.asset");
             path = GetUniquePath(path);
             path = GetUniquePath(path);
 
 
             GUISkin guiSkin = new GUISkin();
             GUISkin guiSkin = new GUISkin();
             ProjectLibrary.Create(guiSkin, path);
             ProjectLibrary.Create(guiSkin, path);
+
+            return path;
         }
         }
 
 
         /// <summary>
         /// <summary>
         /// Creates a new shader containing a rough code outline in the specified folder.
         /// Creates a new shader containing a rough code outline in the specified folder.
         /// </summary>
         /// </summary>
         /// <param name="folder">Folder relative to project library to create the shader in.</param>
         /// <param name="folder">Folder relative to project library to create the shader in.</param>
-        public static void CreateEmptyShader(string folder)
+        /// <returns>The path of the created resource.</returns>
+        public static string CreateEmptyShader(string folder)
         {
         {
             string path = Path.Combine(folder, "New Shader.bsl");
             string path = Path.Combine(folder, "New Shader.bsl");
-            path = Path.Combine(ProjectLibrary.ResourceFolder, path);
             path = GetUniquePath(path);
             path = GetUniquePath(path);
+            string filePath = Path.Combine(ProjectLibrary.ResourceFolder, path);
 
 
-            File.WriteAllText(path, EditorBuiltin.EmptyShaderCode);
-            ProjectLibrary.Refresh(path);
+            File.WriteAllText(filePath, EditorBuiltin.EmptyShaderCode);
+            ProjectLibrary.Refresh(filePath);
+
+            return path;
         }
         }
 
 
         /// <summary>
         /// <summary>
         /// Creates a new C# script containing a rough code outline in the specified folder.
         /// Creates a new C# script containing a rough code outline in the specified folder.
         /// </summary>
         /// </summary>
         /// <param name="folder">Folder relative to project library to create the C# script in.</param>
         /// <param name="folder">Folder relative to project library to create the C# script in.</param>
-        public static void CreateEmptyCSScript(string folder)
+        /// <returns>The path of the created resource.</returns>
+        public static string CreateEmptyCSScript(string folder)
         {
         {
             string path = Path.Combine(folder, "NewScript.cs");
             string path = Path.Combine(folder, "NewScript.cs");
-            path = Path.Combine(ProjectLibrary.ResourceFolder, path);
             path = GetUniquePath(path);
             path = GetUniquePath(path);
+            string filePath = Path.Combine(ProjectLibrary.ResourceFolder, path);
+
+            File.WriteAllText(filePath, EditorBuiltin.EmptyCSScriptCode);
+            ProjectLibrary.Refresh(filePath);
 
 
-            File.WriteAllText(path, EditorBuiltin.EmptyCSScriptCode);
-            ProjectLibrary.Refresh(path);
+            return path;
         }
         }
 
 
         /// <summary>
         /// <summary>
         /// Creates a new physics material with the default properties in the specified folder.
         /// Creates a new physics material with the default properties in the specified folder.
         /// </summary>
         /// </summary>
         /// <param name="folder">Folder relative to project library to create the material in.</param>
         /// <param name="folder">Folder relative to project library to create the material in.</param>
-        public static void CreateEmptyPhysicsMaterial(string folder)
+        /// <returns>The path of the created resource.</returns>
+        public static string CreateEmptyPhysicsMaterial(string folder)
         {
         {
             string path = Path.Combine(folder, "New Physics Material.asset");
             string path = Path.Combine(folder, "New Physics Material.asset");
             path = GetUniquePath(path);
             path = GetUniquePath(path);
 
 
             PhysicsMaterial material = new PhysicsMaterial();
             PhysicsMaterial material = new PhysicsMaterial();
             ProjectLibrary.Create(material, path);
             ProjectLibrary.Create(material, path);
+
+            return path;
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -145,7 +169,7 @@ namespace bs.Editor
                     idx++;
                     idx++;
                 }
                 }
             }
             }
-           
+
             string destination = path;
             string destination = path;
             while (ProjectLibrary.Exists(destination))
             while (ProjectLibrary.Exists(destination))
             {
             {

+ 20 - 2
Source/EditorManaged/Windows/Library/LibraryWindow.cs

@@ -753,9 +753,27 @@ namespace bs.Editor
 
 
         /// <summary>
         /// <summary>
         /// Scrolls the contents GUI area so that the element at the specified path becomes visible.
         /// Scrolls the contents GUI area so that the element at the specified path becomes visible.
+        /// If the entry is in a subdirectory then the subdirectory is entered first.
         /// </summary>
         /// </summary>
         /// <param name="path">Project library path to the element.</param>
         /// <param name="path">Project library path to the element.</param>
-        private void ScrollToEntry(string path)
+        internal void GoToEntry(string path)
+        {
+            if (!string.IsNullOrEmpty(path))
+            {
+                string parentDir = PathEx.GetParent(path);
+
+                if (!PathEx.Compare(parentDir, CurrentFolder))
+                    EnterDirectory(parentDir);
+            }
+
+            ScrollToEntry(path);
+        }
+
+        /// <summary>
+        /// Scrolls the contents GUI area so that the element at the specified path becomes visible.
+        /// </summary>
+        /// <param name="path">Project library path to the element.</param>
+        internal void ScrollToEntry(string path)
         {
         {
             LibraryGUIEntry entryGUI;
             LibraryGUIEntry entryGUI;
             if (!content.TryGetEntry(path, out entryGUI))
             if (!content.TryGetEntry(path, out entryGUI))
@@ -785,7 +803,7 @@ namespace bs.Editor
         /// <summary>
         /// <summary>
         /// Rebuilds the library window GUI. Should be called any time the active folder or contents change.
         /// Rebuilds the library window GUI. Should be called any time the active folder or contents change.
         /// </summary>
         /// </summary>
-        private void Refresh()
+        internal void Refresh()
         {
         {
             requiresRefresh = false;
             requiresRefresh = false;
 
 

+ 2 - 2
Source/EditorManaged/Windows/Library/ProjectLibrary.cs

@@ -116,7 +116,7 @@ namespace bs.Editor
         /// <returns>Instance of the loaded resource, or null if not found.</returns>
         /// <returns>Instance of the loaded resource, or null if not found.</returns>
         public static T Load<T>(string path) where T : Resource
         public static T Load<T>(string path) where T : Resource
         {
         {
-            return (T) Internal_Load(path);
+            return (T)Internal_Load(path);
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -323,7 +323,7 @@ namespace bs.Editor
                 int numRemaining = totalFilesToImport - inProgressImports;
                 int numRemaining = totalFilesToImport - inProgressImports;
 
 
                 float pct = 1.0f;
                 float pct = 1.0f;
-                if(totalFilesToImport > 0)
+                if (totalFilesToImport > 0)
                     pct = numRemaining / (float)totalFilesToImport;
                     pct = numRemaining / (float)totalFilesToImport;
 
 
                 ImportProgressPercent = pct;
                 ImportProgressPercent = pct;