GUIEnumField.cs 11 KB

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