ContextMenu.cs 6.1 KB

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