//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System.IO;
using bs;
namespace bs.Editor
{
/** @addtogroup Library
* @{
*/
///
/// Handles main menu and context menu items and callbacks for project library window.
///
internal static class LibraryMenu
{
///
/// Creates the context menu used by project library window. New context menu must be created when a new instance
/// of the project library window is created.
///
/// Instance of the project library window.
/// Context menu bound to the specified instance of the project library window.
internal static ContextMenu CreateContextMenu(LibraryWindow win)
{
ContextMenu entryContextMenu = new ContextMenu();
entryContextMenu.AddItem("Create", null);
entryContextMenu.AddItem("Create/Folder", CreateFolder);
entryContextMenu.AddItem("Create/Material", CreateEmptyMaterial);
entryContextMenu.AddItem("Create/Physics material", CreateEmptyPhysicsMaterial);
entryContextMenu.AddItem("Create/Shader", CreateEmptyShader);
entryContextMenu.AddItem("Create/C# script", CreateEmptyCSScript);
entryContextMenu.AddItem("Create/Sprite texture", CreateEmptySpriteTexture);
entryContextMenu.AddItem("Create/GUI skin", CreateEmptyGUISkin);
entryContextMenu.AddItem("Create/String table", CreateEmptyStringTable);
entryContextMenu.AddSeparator("");
entryContextMenu.AddItem("Rename", win.RenameSelection, new ShortcutKey(ButtonModifier.None, ButtonCode.F2));
entryContextMenu.AddSeparator("");
entryContextMenu.AddItem("Cut", win.CutSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.X));
entryContextMenu.AddItem("Copy", win.CopySelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.C));
entryContextMenu.AddItem("Duplicate", win.DuplicateSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.D));
entryContextMenu.AddItem("Paste", win.PasteToSelection, new ShortcutKey(ButtonModifier.Ctrl, ButtonCode.V));
entryContextMenu.AddSeparator("");
entryContextMenu.AddItem("Delete", win.DeleteSelection, new ShortcutKey(ButtonModifier.None, ButtonCode.Delete));
entryContextMenu.AddSeparator("");
entryContextMenu.AddItem("Open externally", OpenExternally);
entryContextMenu.AddItem("Explore location", ExploreLocation);
entryContextMenu.SetLocalizedName("Rename", new LocEdString("Rename"));
entryContextMenu.SetLocalizedName("Cut", new LocEdString("Cut"));
entryContextMenu.SetLocalizedName("Copy", new LocEdString("Copy"));
entryContextMenu.SetLocalizedName("Duplicate", new LocEdString("Duplicate"));
entryContextMenu.SetLocalizedName("Paste", new LocEdString("Paste"));
entryContextMenu.SetLocalizedName("Delete", new LocEdString("Delete"));
return entryContextMenu;
}
///
/// Queries if a library window is displayed.
///
/// True if a library window is active, false if not.
internal static bool IsLibraryWindowActive()
{
return EditorWindow.GetWindow() != null;
}
///
/// Scrolls to and selects the specified resource path then start a rename operation.
///
/// Path to the resource to rename
/// Reference to the library window
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();
}
///
/// Creates a new material with the default shader in the currently selected project library folder.
///
[MenuItem("Resources/Create/Folder", 9051, false, "IsLibraryWindowActive")]
internal static void CreateFolder()
{
LibraryWindow win = EditorWindow.GetWindow();
if (win == null)
return;
string path = LibraryUtility.CreateFolder(win.SelectedFolder);
StartRename(path, win);
}
///
/// Creates a new material with the default shader in the currently selected project library folder.
///
[MenuItem("Resources/Create/Material", 9050, false, "IsLibraryWindowActive")]
[ToolbarItem("Material", ToolbarIcon.NewMat, "New material", 1498)]
internal static void CreateEmptyMaterial()
{
LibraryWindow win = EditorWindow.GetWindow();
if(win == null)
return;
string path = LibraryUtility.CreateEmptyMaterial(win.SelectedFolder);
StartRename(path, win);
}
///
/// Creates a new shader containing a rough code outline in the currently selected project library folder.
///
[MenuItem("Resources/Create/Shader", 9049, false, "IsLibraryWindowActive")]
[ToolbarItem("Shader", ToolbarIcon.NewShader, "New shader", 1499)]
internal static void CreateEmptyShader()
{
LibraryWindow win = EditorWindow.GetWindow();
if (win == null)
return;
LibraryUtility.CreateEmptyShader(win.SelectedFolder);
}
///
/// Creates a new C# script containing a rough code outline in the currently selected project library folder.
///
[MenuItem("Resources/Create/C# script", 9048, false, "IsLibraryWindowActive")]
[ToolbarItem("C# script", ToolbarIcon.NewCSScript, "New C# script", 1500, true)]
internal static void CreateEmptyCSScript()
{
LibraryWindow win = EditorWindow.GetWindow();
if (win == null)
return;
LibraryUtility.CreateEmptyCSScript(win.SelectedFolder);
}
///
/// Creates a new empty sprite texture in the currently selected project library folder.
///
[MenuItem("Resources/Create/Sprite texture", 9047, false, "IsLibraryWindowActive")]
[ToolbarItem("Sprite texture", ToolbarIcon.NewSpriteTex, "New sprite texture", 1497)]
internal static void CreateEmptySpriteTexture()
{
LibraryWindow win = EditorWindow.GetWindow();
if (win == null)
return;
string path = LibraryUtility.CreateEmptySpriteTexture(win.SelectedFolder);
StartRename(path, win);
}
///
/// Creates a new empty GUI skin in the currently selected project library folder.
///
[MenuItem("Resources/Create/GUI skin", 9046, false, "IsLibraryWindowActive")]
internal static void CreateEmptyGUISkin()
{
LibraryWindow win = EditorWindow.GetWindow();
if (win == null)
return;
string path = LibraryUtility.CreateEmptyGUISkin(win.SelectedFolder);
StartRename(path, win);
}
///
/// Creates a new empty string table in the currently selected project library folder.
///
[MenuItem("Resources/Create/String table", 9045, false, "IsLibraryWindowActive")]
internal static void CreateEmptyStringTable()
{
LibraryWindow win = EditorWindow.GetWindow();
if (win == null)
return;
string path = LibraryUtility.CreateEmptyStringTable(win.SelectedFolder);
StartRename(path, win);
}
///
/// Creates a new physics material with the default properties in the currently selected project library folder.
///
[MenuItem("Resources/Create/Physics material", 9044, false, "IsLibraryWindowActive")]
internal static void CreateEmptyPhysicsMaterial()
{
LibraryWindow win = EditorWindow.GetWindow();
if (win == null)
return;
string path = LibraryUtility.CreateEmptyPhysicsMaterial(win.SelectedFolder);
StartRename(path, win);
}
///
/// Opens the currently selected project library file or folder in the default external application.
///
[MenuItem("Resources/Open externally", 9040, true, "IsLibraryWindowActive")]
internal static void OpenExternally()
{
LibraryWindow win = EditorWindow.GetWindow();
if (win == null)
return;
EditorApplication.OpenFolder(Path.Combine(ProjectLibrary.ResourceFolder, win.SelectedEntry));
}
///
/// Explores the current project library folder in the external file system explorer.
///
[MenuItem("Resources/Explore location", 9039, false, "IsLibraryWindowActive")]
internal static void ExploreLocation()
{
LibraryWindow win = EditorWindow.GetWindow();
if (win == null)
return;
EditorApplication.OpenFolder(Path.Combine(ProjectLibrary.ResourceFolder, win.CurrentFolder));
}
}
/** @} */
}