using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace BansheeEngine
{
///
/// Contains data used for initializing a context menu used on GUI elements. When specifying menu items you must provide
/// a path. Path must be formated in a certain way. All path elements must be separated by /, e.g. "View/Toolbars/Find".
/// "View" would be the top level path element, "Toolbars" a child in its menu that opens up its own submenu, and "Find"
/// a child in the "Toolbars" sub-menu with an optional callback.
///
public class ContextMenu : ScriptObject
{
private List callbacks = new List();
///
/// Creates a new empty context menu data.
///
public ContextMenu()
{
Internal_CreateInstance(this);
}
///
/// Adds a new item to the menu.
///
/// Path that determines where to add the element. See class information on how to specify paths.
/// All sub-elements of a path will be added automatically.
/// Callback to trigger when the path element is selected.
public void AddItem(string path, Action callback)
{
Internal_AddItem(mCachedPtr, path, callbacks.Count, ShortcutKey.None);
callbacks.Add(callback);
}
///
/// Adds a new item to the menu.
///
/// Path that determines where to add the element. See class information on how to specify paths.
/// All sub-elements of a path will be added automatically.
/// Callback to trigger when the path element is selected.
/// Keyboard shortcut to display next to the item name.
public void AddItem(string path, Action callback, ShortcutKey shortcut)
{
Internal_AddItem(mCachedPtr, path, callbacks.Count, shortcut);
callbacks.Add(callback);
}
///
/// Adds a new item to the menu.
///
/// Path that determines where to add the element. See class information on how to specify paths.
/// All sub-elements of a path will be added automatically.
/// Callback to trigger when the path element is selected.
/// Localized name for the menu item.
public void AddItem(string path, Action callback, LocString name)
{
Internal_AddItem(mCachedPtr, path, callbacks.Count, ShortcutKey.None);
callbacks.Add(callback);
}
///
/// Adds a new item to the menu.
///
/// Path that determines where to add the element. See class information on how to specify paths.
/// All sub-elements of a path will be added automatically.
/// Callback to trigger when the path element is selected.
/// Keyboard shortcut to display next to the item name.
/// Localized name for the menu item.
public void AddItem(string path, Action callback, ShortcutKey shortcut, LocString name)
{
Internal_AddItem(mCachedPtr, path, callbacks.Count, shortcut);
callbacks.Add(callback);
}
///
/// Adds a new separator to the menu.
///
/// Path that determines where to add the element. See class information on how to specify paths.
/// All sub-elements of a path will be added automatically.
public void AddSeparator(string path)
{
Internal_AddSeparator(mCachedPtr, path);
}
///
/// Sets a localized name of a specific menu element. If no localized name is set element labels will be displayed
/// as their names.
///
/// Label of the element.
/// Name to display when the menu is shown.
public void SetLocalizedName(string label, LocString name)
{
IntPtr namePtr = IntPtr.Zero;
if (name != null)
namePtr = name.GetCachedPtr();
Internal_SetLocalizedName(mCachedPtr, label, namePtr);
}
///
/// Triggered by the runtime when an element is selected the context menu.
///
/// Unique index of the element that was selected.
private void InternalDoOnEntryTriggered(int callbackIdx)
{
if (callbackIdx < 0 || callbackIdx >= callbacks.Count)
return;
Action callback = callbacks[callbackIdx];
if (callback != null)
callback();
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_CreateInstance(ContextMenu instance);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_AddItem(IntPtr instance, string path, int callbackIdx, ShortcutKey shortcut);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_AddSeparator(IntPtr instance, string path);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetLocalizedName(IntPtr instance, string label, IntPtr name);
}
}