GUIEnumField.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup GUI-Editor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Editor GUI element that displays a list box with elements of an enumeration and an optional label.
  13. /// </summary>
  14. public sealed class GUIEnumField : GUIElement
  15. {
  16. public delegate void OnSelectionChangedDelegate(UInt64 value);
  17. /// <summary>
  18. /// Triggered whenever user selects a new element in the list box. Returns the value of the enumeration entry that
  19. /// was selected.
  20. /// </summary>
  21. public event OnSelectionChangedDelegate OnSelectionChanged;
  22. /// <summary>
  23. /// Value of the enumeration entry currently selected. This is a combination of all selected values in case a
  24. /// multiselect list is used.
  25. /// </summary>
  26. public UInt64 Value
  27. {
  28. get { return Internal_GetValue(mCachedPtr); }
  29. set { Internal_SetValue(mCachedPtr, value); }
  30. }
  31. /// <summary>
  32. /// States of all element in the list box (enabled or disabled).
  33. /// </summary>
  34. public bool[] States
  35. {
  36. get { return Internal_GetElementStates(mCachedPtr); }
  37. set { Internal_SetElementStates(mCachedPtr, value); }
  38. }
  39. /// <summary>
  40. /// Creates a new list box with enumeration entries as its elements and a label.
  41. /// </summary>
  42. /// <param name="enumType">Type of enum of whose entries to display in the list box.</param>
  43. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  44. /// </param>
  45. /// <param name="title">Content to display on the label.</param>
  46. /// <param name="titleWidth">Width of the title label in pixels.</param>
  47. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  48. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  49. /// default element style is used.</param>
  50. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  51. /// override any similar options set by style.</param>
  52. public GUIEnumField(Type enumType, bool multiselect, GUIContent title, int titleWidth = 100, string style = "", params GUIOption[] options)
  53. {
  54. Internal_CreateInstance(this, Enum.GetNames(enumType), Enum.GetValues(enumType), multiselect,
  55. ref title, titleWidth, style, options, true);
  56. }
  57. /// <summary>
  58. /// Creates a new list box with enumeration entries as its elements and a label.
  59. /// </summary>
  60. /// <param name="enumType">Type of enum of whose entries to display in the list box.</param>
  61. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  62. /// </param>
  63. /// <param name="title">Content to display on the label.</param>
  64. /// <param name="titleWidth">Width of the title label in pixels.</param>
  65. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  66. /// override any similar options set by style.</param>
  67. public GUIEnumField(Type enumType, bool multiselect, GUIContent title, int titleWidth, params GUIOption[] options)
  68. {
  69. Internal_CreateInstance(this, Enum.GetNames(enumType), Enum.GetValues(enumType), multiselect,
  70. ref title, titleWidth, "", options, true);
  71. }
  72. /// <summary>
  73. /// Creates a new single selection list box with enumeration entries as its elements and a label.
  74. /// </summary>
  75. /// <param name="enumType">Type of enum of whose entries to display in the list box.</param>
  76. /// <param name="title">Content to display on the label.</param>
  77. /// <param name="titleWidth">Width of the title label in pixels.</param>
  78. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  79. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  80. /// default element style is used.</param>
  81. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  82. /// override any similar options set by style.</param>
  83. public GUIEnumField(Type enumType, GUIContent title, int titleWidth = 100, string style = "", params GUIOption[] options)
  84. {
  85. Internal_CreateInstance(this, Enum.GetNames(enumType), Enum.GetValues(enumType), false,
  86. ref title, titleWidth, style, options, true);
  87. }
  88. /// <summary>
  89. /// Creates a new single selection list box with enumeration entries as its elements and a label.
  90. /// </summary>
  91. /// <param name="enumType">Type of enum of whose entries to display in the list box.</param>
  92. /// <param name="title">Content to display on the label.</param>
  93. /// <param name="titleWidth">Width of the title label in pixels.</param>
  94. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  95. /// override any similar options set by style.</param>
  96. public GUIEnumField(Type enumType, GUIContent title, int titleWidth, params GUIOption[] options)
  97. {
  98. Internal_CreateInstance(this, Enum.GetNames(enumType), Enum.GetValues(enumType), false,
  99. ref title, titleWidth, "", options, true);
  100. }
  101. /// <summary>
  102. /// Creates a new list box with enumeration entries as its elements and no label.
  103. /// </summary>
  104. /// <param name="enumType">Type of enum of whose entries to display in the list box.</param>
  105. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  106. /// </param>
  107. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  108. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  109. /// default element style is used.</param>
  110. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  111. /// override any similar options set by style.</param>
  112. public GUIEnumField(Type enumType, bool multiselect, string style = "", params GUIOption[] options)
  113. {
  114. GUIContent emptyContent = new GUIContent();
  115. Internal_CreateInstance(this, Enum.GetNames(enumType), Enum.GetValues(enumType), multiselect,
  116. ref emptyContent, 0, style, options, false);
  117. }
  118. /// <summary>
  119. /// Creates a new list box with enumeration entries as its elements and no label.
  120. /// </summary>
  121. /// <param name="enumType">Type of enum of whose entries to display in the list box.</param>
  122. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  123. /// </param>
  124. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  125. /// override any similar options set by style.</param>
  126. public GUIEnumField(Type enumType, bool multiselect = false, params GUIOption[] options)
  127. {
  128. GUIContent emptyContent = new GUIContent();
  129. Internal_CreateInstance(this, Enum.GetNames(enumType), Enum.GetValues(enumType), multiselect,
  130. ref emptyContent, 0, "", options, false);
  131. }
  132. /// <summary>
  133. /// Makes the element with the specified index selected.
  134. /// </summary>
  135. /// <param name="idx">Sequential index in the elements array provided on construction.</param>
  136. public void SelectElement(int idx)
  137. {
  138. Internal_SelectElement(mCachedPtr, idx);
  139. }
  140. /// <summary>
  141. /// Deselect element the element with the specified index. Only relevant for multi-select list boxes.
  142. /// </summary>
  143. /// <param name="idx">Sequential index in the elements array provided on construction.</param>
  144. public void DeselectElement(int idx)
  145. {
  146. Internal_DeselectElement(mCachedPtr, idx);
  147. }
  148. /// <summary>
  149. /// Colors the element with a specific tint.
  150. /// </summary>
  151. /// <param name="color">Tint to apply to the element.</param>
  152. public void SetTint(Color color)
  153. {
  154. Internal_SetTint(mCachedPtr, ref color);
  155. }
  156. /// <summary>
  157. /// Triggered by the native interop object when a user selects an object in the list.
  158. /// </summary>
  159. private void DoOnSelectionChanged(UInt64 index)
  160. {
  161. if (OnSelectionChanged != null)
  162. OnSelectionChanged(index);
  163. }
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. private static extern void Internal_CreateInstance(GUIEnumField instance, string[] names, Array values,
  166. bool multiselect, ref GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle);
  167. [MethodImpl(MethodImplOptions.InternalCall)]
  168. private static extern UInt64 Internal_GetValue(IntPtr nativeInstance);
  169. [MethodImpl(MethodImplOptions.InternalCall)]
  170. private static extern void Internal_SetValue(IntPtr nativeInstance, UInt64 value);
  171. [MethodImpl(MethodImplOptions.InternalCall)]
  172. private static extern bool[] Internal_GetElementStates(IntPtr nativeInstance);
  173. [MethodImpl(MethodImplOptions.InternalCall)]
  174. private static extern void Internal_SetElementStates(IntPtr nativeInstance, bool[] states);
  175. [MethodImpl(MethodImplOptions.InternalCall)]
  176. private static extern void Internal_SelectElement(IntPtr nativeInstance, int idx);
  177. [MethodImpl(MethodImplOptions.InternalCall)]
  178. private static extern void Internal_DeselectElement(IntPtr nativeInstance, int idx);
  179. [MethodImpl(MethodImplOptions.InternalCall)]
  180. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  181. }
  182. /** @} */
  183. }