GUIEnumField.cs 10 KB

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