2
0

ContextMenu.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /// </summary>
  17. public class ContextMenu : ScriptObject
  18. {
  19. private List<Action> callbacks = new List<Action>();
  20. /// <summary>
  21. /// Creates a new empty context menu data.
  22. /// </summary>
  23. public ContextMenu()
  24. {
  25. Internal_CreateInstance(this);
  26. }
  27. /// <summary>
  28. /// Adds a new item to the menu.
  29. /// </summary>
  30. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  31. /// All sub-elements of a path will be added automatically.</param>
  32. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  33. public void AddItem(string path, Action callback)
  34. {
  35. Internal_AddItem(mCachedPtr, path, callbacks.Count, ref ShortcutKey.None);
  36. callbacks.Add(callback);
  37. }
  38. /// <summary>
  39. /// Adds a new item to the menu.
  40. /// </summary>
  41. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  42. /// All sub-elements of a path will be added automatically.</param>
  43. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  44. /// <param name="shortcut">Keyboard shortcut to display next to the item name.</param>
  45. public void AddItem(string path, Action callback, ShortcutKey shortcut)
  46. {
  47. Internal_AddItem(mCachedPtr, path, callbacks.Count, ref shortcut);
  48. callbacks.Add(callback);
  49. }
  50. /// <summary>
  51. /// Adds a new item to the menu.
  52. /// </summary>
  53. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  54. /// All sub-elements of a path will be added automatically.</param>
  55. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  56. /// <param name="name">Localized name for the menu item.</param>
  57. public void AddItem(string path, Action callback, LocString name)
  58. {
  59. Internal_AddItem(mCachedPtr, path, callbacks.Count, ref ShortcutKey.None);
  60. callbacks.Add(callback);
  61. }
  62. /// <summary>
  63. /// Adds a new item to the menu.
  64. /// </summary>
  65. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  66. /// All sub-elements of a path will be added automatically.</param>
  67. /// <param name="callback">Callback to trigger when the path element is selected.</param>
  68. /// <param name="shortcut">Keyboard shortcut to display next to the item name.</param>
  69. /// <param name="name">Localized name for the menu item.</param>
  70. public void AddItem(string path, Action callback, ShortcutKey shortcut, LocString name)
  71. {
  72. Internal_AddItem(mCachedPtr, path, callbacks.Count, ref shortcut);
  73. callbacks.Add(callback);
  74. }
  75. /// <summary>
  76. /// Adds a new separator to the menu.
  77. /// </summary>
  78. /// <param name="path">Path that determines where to add the element. See class information on how to specify paths.
  79. /// All sub-elements of a path will be added automatically.</param>
  80. public void AddSeparator(string path)
  81. {
  82. Internal_AddSeparator(mCachedPtr, path);
  83. }
  84. /// <summary>
  85. /// Sets a localized name of a specific menu element. If no localized name is set element labels will be displayed
  86. /// as their names.
  87. /// </summary>
  88. /// <param name="label">Label of the element.</param>
  89. /// <param name="name">Name to display when the menu is shown.</param>
  90. public void SetLocalizedName(string label, LocString name)
  91. {
  92. IntPtr namePtr = IntPtr.Zero;
  93. if (name != null)
  94. namePtr = name.GetCachedPtr();
  95. Internal_SetLocalizedName(mCachedPtr, label, namePtr);
  96. }
  97. /// <summary>
  98. /// Triggered by the runtime when an element is selected the context menu.
  99. /// </summary>
  100. /// <param name="callbackIdx">Unique index of the element that was selected.</param>
  101. private void InternalDoOnEntryTriggered(int callbackIdx)
  102. {
  103. if (callbackIdx < 0 || callbackIdx >= callbacks.Count)
  104. return;
  105. Action callback = callbacks[callbackIdx];
  106. if (callback != null)
  107. callback();
  108. }
  109. [MethodImpl(MethodImplOptions.InternalCall)]
  110. private static extern void Internal_CreateInstance(ContextMenu instance);
  111. [MethodImpl(MethodImplOptions.InternalCall)]
  112. private static extern void Internal_AddItem(IntPtr instance, string path, int callbackIdx, ref ShortcutKey shortcut);
  113. [MethodImpl(MethodImplOptions.InternalCall)]
  114. private static extern void Internal_AddSeparator(IntPtr instance, string path);
  115. [MethodImpl(MethodImplOptions.InternalCall)]
  116. private static extern void Internal_SetLocalizedName(IntPtr instance, string label, IntPtr name);
  117. }
  118. /** @} */
  119. }