ContextMenu.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. namespace BansheeEngine
  5. {
  6. /// <summary>
  7. /// Contains data used for initializing a context menu used on GUI elements. When specifying menu items you must provide
  8. /// a path. Path must be formated in a certain way. All path elements must be separated by /, e.g. "View/Toolbars/Find".
  9. /// "View" would be the top level path element, "Toolbars" a child in its menu that opens up its own submenu, and "Find"
  10. /// a child in the "Toolbars" sub-menu with an optional callback.
  11. /// </summary>
  12. public class ContextMenu : ScriptObject
  13. {
  14. private List<Action> callbacks = new List<Action>();
  15. /// <summary>
  16. /// Creates a new empty context menu data.
  17. /// </summary>
  18. public ContextMenu()
  19. {
  20. Internal_CreateInstance(this);
  21. }
  22. /// <summary>
  23. /// Adds a new item to the menu.
  24. /// </summary>
  25. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  26. /// All sub-elements of a path will be added automatically.</param>
  27. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  28. public void AddItem(string path, Action callback)
  29. {
  30. Internal_AddItem(mCachedPtr, path, callbacks.Count, ShortcutKey.None);
  31. callbacks.Add(callback);
  32. }
  33. /// <summary>
  34. /// Adds a new item to the menu.
  35. /// </summary>
  36. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  37. /// All sub-elements of a path will be added automatically.</param>
  38. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  39. /// <param name="shortcut">Keyboard shortcut to display next to the item name.</param>
  40. public void AddItem(string path, Action callback, ShortcutKey shortcut)
  41. {
  42. Internal_AddItem(mCachedPtr, path, callbacks.Count, shortcut);
  43. callbacks.Add(callback);
  44. }
  45. /// <summary>
  46. /// Adds a new item to the menu.
  47. /// </summary>
  48. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  49. /// All sub-elements of a path will be added automatically.</param>
  50. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  51. /// <param name="name">Localized name for the menu item.</param>
  52. public void AddItem(string path, Action callback, LocString name)
  53. {
  54. Internal_AddItem(mCachedPtr, path, callbacks.Count, ShortcutKey.None);
  55. callbacks.Add(callback);
  56. }
  57. /// <summary>
  58. /// Adds a new item to the menu.
  59. /// </summary>
  60. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  61. /// All sub-elements of a path will be added automatically.</param>
  62. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  63. /// <param name="shortcut">Keyboard shortcut to display next to the item name.</param>
  64. /// <param name="name">Localized name for the menu item.</param>
  65. public void AddItem(string path, Action callback, ShortcutKey shortcut, LocString name)
  66. {
  67. Internal_AddItem(mCachedPtr, path, callbacks.Count, shortcut);
  68. callbacks.Add(callback);
  69. }
  70. /// <summary>
  71. /// Adds a new separator to the menu.
  72. /// </summary>
  73. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  74. /// All sub-elements of a path will be added automatically.</param>
  75. public void AddSeparator(string path)
  76. {
  77. Internal_AddSeparator(mCachedPtr, path);
  78. }
  79. /// <summary>
  80. /// Sets a localized name of a specific menu element. If no localized name is set element labels will be displayed
  81. /// as their names.
  82. /// </summary>
  83. /// <param name="label">Label of the element.</param>
  84. /// <param name="name">Name to display when the menu is shown.</param>
  85. public void SetLocalizedName(string label, LocString name)
  86. {
  87. IntPtr namePtr = IntPtr.Zero;
  88. if (name != null)
  89. namePtr = name.GetCachedPtr();
  90. Internal_SetLocalizedName(mCachedPtr, label, namePtr);
  91. }
  92. /// <summary>
  93. /// Triggered by the runtime when an element is selected the context menu.
  94. /// </summary>
  95. /// <param name="callbackIdx">Unique index of the element that was selected.</param>
  96. private void InternalDoOnEntryTriggered(int callbackIdx)
  97. {
  98. if (callbackIdx < 0 || callbackIdx >= callbacks.Count)
  99. return;
  100. Action callback = callbacks[callbackIdx];
  101. if (callback != null)
  102. callback();
  103. }
  104. [MethodImpl(MethodImplOptions.InternalCall)]
  105. private static extern void Internal_CreateInstance(ContextMenu instance);
  106. [MethodImpl(MethodImplOptions.InternalCall)]
  107. private static extern void Internal_AddItem(IntPtr instance, string path, int callbackIdx, ShortcutKey shortcut);
  108. [MethodImpl(MethodImplOptions.InternalCall)]
  109. private static extern void Internal_AddSeparator(IntPtr instance, string path);
  110. [MethodImpl(MethodImplOptions.InternalCall)]
  111. private static extern void Internal_SetLocalizedName(IntPtr instance, string label, IntPtr name);
  112. }
  113. }