ContextMenu.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup GUI_Engine
  9. * @{
  10. */
  11. /// <summary>
  12. /// Contains data used for initializing a context menu used on GUI elements. When specifying menu items you must provide
  13. /// a path. Path must be formated in a certain way. All path elements must be separated by /, for example
  14. /// "View/Toolbars/Find". "View" would be the top level path element, "Toolbars" a child in its menu that opens up its
  15. /// own submenu, and "Find" a child in the "Toolbars" sub-menu with an optional callback.
  16. ///
  17. /// A context menu can either by provided to GUIElements, or opened manually by calling <see cref="Open"/>.
  18. /// </summary>
  19. public class ContextMenu : ScriptObject
  20. {
  21. private List<Action> callbacks = new List<Action>();
  22. /// <summary>
  23. /// Creates a new empty context menu data.
  24. /// </summary>
  25. public ContextMenu()
  26. {
  27. Internal_CreateInstance(this);
  28. }
  29. /// <summary>
  30. /// Opens the context menu at the specified position.
  31. /// </summary>
  32. /// <param name="position">Position relative to the <paramref name="parent"/>.</param>
  33. /// <param name="parent">GUI layout over which to open the context menu. Context menu can be opened outside of the
  34. /// area of the layout, as long as the area belongs to the same window.</param>
  35. public void Open(Vector2I position, GUILayout parent)
  36. {
  37. if (parent == null)
  38. return;
  39. IntPtr parentPtr = parent.GetCachedPtr();
  40. Internal_Open(mCachedPtr, ref position, parentPtr);
  41. }
  42. /// <summary>
  43. /// Adds a new item to the menu.
  44. /// </summary>
  45. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  46. /// All sub-elements of a path will be added automatically.</param>
  47. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  48. public void AddItem(string path, Action callback)
  49. {
  50. Internal_AddItem(mCachedPtr, path, callbacks.Count, ref ShortcutKey.None);
  51. callbacks.Add(callback);
  52. }
  53. /// <summary>
  54. /// Adds a new item to the menu.
  55. /// </summary>
  56. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  57. /// All sub-elements of a path will be added automatically.</param>
  58. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  59. /// <param name="shortcut">Keyboard shortcut to display next to the item name.</param>
  60. public void AddItem(string path, Action callback, ShortcutKey shortcut)
  61. {
  62. Internal_AddItem(mCachedPtr, path, callbacks.Count, ref shortcut);
  63. callbacks.Add(callback);
  64. }
  65. /// <summary>
  66. /// Adds a new item to the menu.
  67. /// </summary>
  68. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  69. /// All sub-elements of a path will be added automatically.</param>
  70. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  71. /// <param name="name">Localized name for the menu item.</param>
  72. public void AddItem(string path, Action callback, LocString name)
  73. {
  74. Internal_AddItem(mCachedPtr, path, callbacks.Count, ref ShortcutKey.None);
  75. callbacks.Add(callback);
  76. }
  77. /// <summary>
  78. /// Adds a new item to the menu.
  79. /// </summary>
  80. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  81. /// All sub-elements of a path will be added automatically.</param>
  82. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  83. /// <param name="shortcut">Keyboard shortcut to display next to the item name.</param>
  84. /// <param name="name">Localized name for the menu item.</param>
  85. public void AddItem(string path, Action callback, ShortcutKey shortcut, LocString name)
  86. {
  87. Internal_AddItem(mCachedPtr, path, callbacks.Count, ref shortcut);
  88. callbacks.Add(callback);
  89. }
  90. /// <summary>
  91. /// Adds a new separator to the menu.
  92. /// </summary>
  93. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  94. /// All sub-elements of a path will be added automatically.</param>
  95. public void AddSeparator(string path)
  96. {
  97. Internal_AddSeparator(mCachedPtr, path);
  98. }
  99. /// <summary>
  100. /// Sets a localized name of a specific menu element. If no localized name is set element labels will be displayed
  101. /// as their names.
  102. /// </summary>
  103. /// <param name="label">Label of the element.</param>
  104. /// <param name="name">Name to display when the menu is shown.</param>
  105. public void SetLocalizedName(string label, LocString name)
  106. {
  107. IntPtr namePtr = IntPtr.Zero;
  108. if (name != null)
  109. namePtr = name.GetCachedPtr();
  110. Internal_SetLocalizedName(mCachedPtr, label, namePtr);
  111. }
  112. /// <summary>
  113. /// Triggered by the runtime when an element is selected the context menu.
  114. /// </summary>
  115. /// <param name="callbackIdx">Unique index of the element that was selected.</param>
  116. private void InternalDoOnEntryTriggered(int callbackIdx)
  117. {
  118. if (callbackIdx < 0 || callbackIdx >= callbacks.Count)
  119. return;
  120. Action callback = callbacks[callbackIdx];
  121. if (callback != null)
  122. callback();
  123. }
  124. [MethodImpl(MethodImplOptions.InternalCall)]
  125. private static extern void Internal_CreateInstance(ContextMenu instance);
  126. [MethodImpl(MethodImplOptions.InternalCall)]
  127. private static extern void Internal_Open(IntPtr instance, ref Vector2I position, IntPtr layoutPtr);
  128. [MethodImpl(MethodImplOptions.InternalCall)]
  129. private static extern void Internal_AddItem(IntPtr instance, string path, int callbackIdx, ref ShortcutKey shortcut);
  130. [MethodImpl(MethodImplOptions.InternalCall)]
  131. private static extern void Internal_AddSeparator(IntPtr instance, string path);
  132. [MethodImpl(MethodImplOptions.InternalCall)]
  133. private static extern void Internal_SetLocalizedName(IntPtr instance, string label, IntPtr name);
  134. }
  135. /** @} */
  136. }