LibraryMenu.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.IO;
  4. using bs;
  5. namespace bs.Editor
  6. {
  7. /** @addtogroup Library
  8. * @{
  9. */
  10. /// <summary>
  11. /// Handles main menu and context menu items and callbacks for project library window.
  12. /// </summary>
  13. internal static class LibraryMenu
  14. {
  15. /// <summary>
  16. /// Creates the context menu used by project library window. New context menu must be created when a new instance
  17. /// of the project library window is created.
  18. /// </summary>
  19. /// <param name="win">Instance of the project library window.</param>
  20. /// <returns>Context menu bound to the specified instance of the project library window.</returns>
  21. internal static ContextMenu CreateContextMenu(LibraryWindow win)
  22. {
  23. ContextMenu entryContextMenu = new ContextMenu();
  24. entryContextMenu.AddItem("Create", null);
  25. entryContextMenu.AddItem("Create/Folder", CreateFolder);
  26. entryContextMenu.AddItem("Create/Material", CreateEmptyMaterial);
  27. entryContextMenu.AddItem("Create/Physics material", CreateEmptyPhysicsMaterial);
  28. entryContextMenu.AddItem("Create/Shader", CreateEmptyShader);
  29. entryContextMenu.AddItem("Create/C# script", CreateEmptyCSScript);
  30. entryContextMenu.AddItem("Create/Sprite texture", CreateEmptySpriteTexture);
  31. entryContextMenu.AddItem("Create/GUI skin", CreateEmptyGUISkin);
  32. entryContextMenu.AddItem("Create/String table", CreateEmptyStringTable);
  33. entryContextMenu.AddSeparator("");
  34. entryContextMenu.AddItem("Rename", win.RenameSelection, new ShortcutKey(ButtonModifier.None, ButtonCode.F2));
  35. entryContextMenu.AddSeparator("");
  36. entryContextMenu.AddItem("Cut", win.CutSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.X));
  37. entryContextMenu.AddItem("Copy", win.CopySelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.C));
  38. entryContextMenu.AddItem("Duplicate", win.DuplicateSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.D));
  39. entryContextMenu.AddItem("Paste", win.PasteToSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.V));
  40. entryContextMenu.AddSeparator("");
  41. entryContextMenu.AddItem("Delete", win.DeleteSelection, new ShortcutKey(ButtonModifier.None, ButtonCode.Delete));
  42. entryContextMenu.AddSeparator("");
  43. entryContextMenu.AddItem("Open externally", OpenExternally);
  44. entryContextMenu.AddItem("Explore location", ExploreLocation);
  45. entryContextMenu.SetLocalizedName("Rename", new LocEdString("Rename"));
  46. entryContextMenu.SetLocalizedName("Cut", new LocEdString("Cut"));
  47. entryContextMenu.SetLocalizedName("Copy", new LocEdString("Copy"));
  48. entryContextMenu.SetLocalizedName("Duplicate", new LocEdString("Duplicate"));
  49. entryContextMenu.SetLocalizedName("Paste", new LocEdString("Paste"));
  50. entryContextMenu.SetLocalizedName("Delete", new LocEdString("Delete"));
  51. return entryContextMenu;
  52. }
  53. /// <summary>
  54. /// Queries if a library window is displayed.
  55. /// </summary>
  56. /// <returns>True if a library window is active, false if not.</returns>
  57. internal static bool IsLibraryWindowActive()
  58. {
  59. return EditorWindow.GetWindow<LibraryWindow>() != null;
  60. }
  61. /// <summary>
  62. /// Scrolls to and selects the specified resource path then start a rename operation.
  63. /// </summary>
  64. /// <param name="path">Path to the resource to rename</param>
  65. /// <param name="window">Reference to the library window</param>
  66. private static void StartRename(string path, LibraryWindow window)
  67. {
  68. if (window == null)
  69. return;
  70. window.Refresh();
  71. window.HasFocus = true;
  72. window.GoToEntry(path);
  73. window.Select(path);
  74. window.RenameSelection();
  75. }
  76. /// <summary>
  77. /// Creates a new material with the default shader in the currently selected project library folder.
  78. /// </summary>
  79. [MenuItem("Resources/Create/Folder", 9051, false, "IsLibraryWindowActive")]
  80. internal static void CreateFolder()
  81. {
  82. LibraryWindow win = EditorWindow.GetWindow<LibraryWindow>();
  83. if (win == null)
  84. return;
  85. string path = LibraryUtility.CreateFolder(win.SelectedFolder);
  86. StartRename(path, win);
  87. }
  88. /// <summary>
  89. /// Creates a new material with the default shader in the currently selected project library folder.
  90. /// </summary>
  91. [MenuItem("Resources/Create/Material", 9050, false, "IsLibraryWindowActive")]
  92. [ToolbarItem("Material", ToolbarIcon.NewMat, "New material", 1498)]
  93. internal static void CreateEmptyMaterial()
  94. {
  95. LibraryWindow win = EditorWindow.GetWindow<LibraryWindow>();
  96. if(win == null)
  97. return;
  98. string path = LibraryUtility.CreateEmptyMaterial(win.SelectedFolder);
  99. StartRename(path, win);
  100. }
  101. /// <summary>
  102. /// Creates a new shader containing a rough code outline in the currently selected project library folder.
  103. /// </summary>
  104. [MenuItem("Resources/Create/Shader", 9049, false, "IsLibraryWindowActive")]
  105. [ToolbarItem("Shader", ToolbarIcon.NewShader, "New shader", 1499)]
  106. internal static void CreateEmptyShader()
  107. {
  108. LibraryWindow win = EditorWindow.GetWindow<LibraryWindow>();
  109. if (win == null)
  110. return;
  111. LibraryUtility.CreateEmptyShader(win.SelectedFolder);
  112. }
  113. /// <summary>
  114. /// Creates a new C# script containing a rough code outline in the currently selected project library folder.
  115. /// </summary>
  116. [MenuItem("Resources/Create/C# script", 9048, false, "IsLibraryWindowActive")]
  117. [ToolbarItem("C# script", ToolbarIcon.NewCSScript, "New C# script", 1500, true)]
  118. internal static void CreateEmptyCSScript()
  119. {
  120. LibraryWindow win = EditorWindow.GetWindow<LibraryWindow>();
  121. if (win == null)
  122. return;
  123. LibraryUtility.CreateEmptyCSScript(win.SelectedFolder);
  124. }
  125. /// <summary>
  126. /// Creates a new empty sprite texture in the currently selected project library folder.
  127. /// </summary>
  128. [MenuItem("Resources/Create/Sprite texture", 9047, false, "IsLibraryWindowActive")]
  129. [ToolbarItem("Sprite texture", ToolbarIcon.NewSpriteTex, "New sprite texture", 1497)]
  130. internal static void CreateEmptySpriteTexture()
  131. {
  132. LibraryWindow win = EditorWindow.GetWindow<LibraryWindow>();
  133. if (win == null)
  134. return;
  135. string path = LibraryUtility.CreateEmptySpriteTexture(win.SelectedFolder);
  136. StartRename(path, win);
  137. }
  138. /// <summary>
  139. /// Creates a new empty GUI skin in the currently selected project library folder.
  140. /// </summary>
  141. [MenuItem("Resources/Create/GUI skin", 9046, false, "IsLibraryWindowActive")]
  142. internal static void CreateEmptyGUISkin()
  143. {
  144. LibraryWindow win = EditorWindow.GetWindow<LibraryWindow>();
  145. if (win == null)
  146. return;
  147. string path = LibraryUtility.CreateEmptyGUISkin(win.SelectedFolder);
  148. StartRename(path, win);
  149. }
  150. /// <summary>
  151. /// Creates a new empty string table in the currently selected project library folder.
  152. /// </summary>
  153. [MenuItem("Resources/Create/String table", 9045, false, "IsLibraryWindowActive")]
  154. internal static void CreateEmptyStringTable()
  155. {
  156. LibraryWindow win = EditorWindow.GetWindow<LibraryWindow>();
  157. if (win == null)
  158. return;
  159. string path = LibraryUtility.CreateEmptyStringTable(win.SelectedFolder);
  160. StartRename(path, win);
  161. }
  162. /// <summary>
  163. /// Creates a new physics material with the default properties in the currently selected project library folder.
  164. /// </summary>
  165. [MenuItem("Resources/Create/Physics material", 9044, false, "IsLibraryWindowActive")]
  166. internal static void CreateEmptyPhysicsMaterial()
  167. {
  168. LibraryWindow win = EditorWindow.GetWindow<LibraryWindow>();
  169. if (win == null)
  170. return;
  171. string path = LibraryUtility.CreateEmptyPhysicsMaterial(win.SelectedFolder);
  172. StartRename(path, win);
  173. }
  174. /// <summary>
  175. /// Opens the currently selected project library file or folder in the default external application.
  176. /// </summary>
  177. [MenuItem("Resources/Open externally", 9040, true, "IsLibraryWindowActive")]
  178. internal static void OpenExternally()
  179. {
  180. LibraryWindow win = EditorWindow.GetWindow<LibraryWindow>();
  181. if (win == null)
  182. return;
  183. EditorApplication.OpenFolder(Path.Combine(ProjectLibrary.ResourceFolder, win.SelectedEntry));
  184. }
  185. /// <summary>
  186. /// Explores the current project library folder in the external file system explorer.
  187. /// </summary>
  188. [MenuItem("Resources/Explore location", 9039, false, "IsLibraryWindowActive")]
  189. internal static void ExploreLocation()
  190. {
  191. LibraryWindow win = EditorWindow.GetWindow<LibraryWindow>();
  192. if (win == null)
  193. return;
  194. EditorApplication.OpenFolder(Path.Combine(ProjectLibrary.ResourceFolder, win.CurrentFolder));
  195. }
  196. }
  197. /** @} */
  198. }