GUIListBoxField.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 user-specified elements and an optional label. List box can be
  8. /// a standard list-box that allows a single element to be selected, or a multi-select list box where any number of
  9. /// elements can be selected at the same time.
  10. /// </summary>
  11. public sealed class GUIListBoxField : GUIElement
  12. {
  13. public delegate void OnSelectionChangedDelegate(int index);
  14. /// <summary>
  15. /// Triggered whenever user selects a new element in the list box. Returned index maps to the element in the
  16. /// elements array that the list box was initialized with.
  17. /// </summary>
  18. public event OnSelectionChangedDelegate OnSelectionChanged;
  19. /// <summary>
  20. /// Index of the list box entry currently selected.
  21. /// </summary>
  22. public int Index
  23. {
  24. get { return Internal_GetValue(mCachedPtr); }
  25. set { Internal_SetValue(mCachedPtr, value); }
  26. }
  27. /// <summary>
  28. /// States of all element in the list box (enabled or disabled).
  29. /// </summary>
  30. public bool[] States
  31. {
  32. get { return Internal_GetElementStates(mCachedPtr); }
  33. set { Internal_SetElementStates(mCachedPtr, value); }
  34. }
  35. /// <summary>
  36. /// Creates a new list box with the specified elements and a label.
  37. /// </summary>
  38. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  39. /// order as in the array.</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 GUIListBoxField(LocString[] elements, bool multiselect, GUIContent title, int titleWidth, string style = "",
  50. params GUIOption[] options)
  51. {
  52. Internal_CreateInstance(this, elements, multiselect, title, titleWidth, style, options, true);
  53. }
  54. /// <summary>
  55. /// Creates a new list box with the specified elements and a label.
  56. /// </summary>
  57. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  58. /// order as in the array.</param>
  59. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  60. /// </param>
  61. /// <param name="title">Content to display on the label.</param>
  62. /// <param name="titleWidth">Width of the title label in pixels.</param>
  63. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  64. /// override any similar options set by style.</param>
  65. public GUIListBoxField(LocString[] elements, bool multiselect, GUIContent title, int titleWidth = 100, params GUIOption[] options)
  66. {
  67. Internal_CreateInstance(this, elements, multiselect, title, titleWidth, "", options, true);
  68. }
  69. /// <summary>
  70. /// Creates a new single-selection list box with the specified elements and a label.
  71. /// </summary>
  72. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  73. /// order as in the array.</param>
  74. /// <param name="title">Content to display on the label.</param>
  75. /// <param name="titleWidth">Width of the title label in pixels.</param>
  76. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  77. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  78. /// default element style is used.</param>
  79. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  80. /// override any similar options set by style.</param>
  81. public GUIListBoxField(LocString[] elements, GUIContent title, int titleWidth, string style = "",
  82. params GUIOption[] options)
  83. {
  84. Internal_CreateInstance(this, elements, false, title, titleWidth, style, options, true);
  85. }
  86. /// <summary>
  87. /// Creates a new single-selection list box with the specified elements and a label.
  88. /// </summary>
  89. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  90. /// order as in the array.</param>
  91. /// <param name="title">Content to display on the label.</param>
  92. /// <param name="titleWidth">Width of the title label in pixels.</param>
  93. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  94. /// override any similar options set by style.</param>
  95. public GUIListBoxField(LocString[] elements, GUIContent title, int titleWidth = 100, params GUIOption[] options)
  96. {
  97. Internal_CreateInstance(this, elements, false, title, titleWidth, "", options, true);
  98. }
  99. /// <summary>
  100. /// Creates a new list box with the specified elements and no label.
  101. /// </summary>
  102. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  103. /// order as in the array.</param>
  104. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  105. /// </param>
  106. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  107. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  108. /// default element style is used.</param>
  109. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  110. /// override any similar options set by style.</param>
  111. public GUIListBoxField(LocString[] elements, bool multiselect = false, string style = "", params GUIOption[] options)
  112. {
  113. Internal_CreateInstance(this, elements, multiselect, null, 0, style, options, false);
  114. }
  115. /// <summary>
  116. /// Creates a new list box with the specified elements and no label.
  117. /// </summary>
  118. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  119. /// order as in the array.</param>
  120. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  121. /// </param>
  122. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  123. /// override any similar options set by style.</param>
  124. public GUIListBoxField(LocString[] elements, bool multiselect = false, params GUIOption[] options)
  125. {
  126. Internal_CreateInstance(this, elements, multiselect, null, 0, "", options, false);
  127. }
  128. /// <summary>
  129. /// Creates a new list box with the specified elements and a label.
  130. /// </summary>
  131. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  132. /// order as in the array.</param>
  133. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  134. /// </param>
  135. /// <param name="title">Content to display on the label.</param>
  136. /// <param name="titleWidth">Width of the title label in pixels.</param>
  137. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  138. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  139. /// default element style is used.</param>
  140. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  141. /// override any similar options set by style.</param>
  142. public GUIListBoxField(string[] elements, bool multiselect, GUIContent title, int titleWidth, string style = "",
  143. params GUIOption[] options)
  144. {
  145. Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, title, titleWidth, style, options, true);
  146. }
  147. /// <summary>
  148. /// Creates a new list box with the specified elements and a label.
  149. /// </summary>
  150. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  151. /// order as in the array.</param>
  152. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  153. /// </param>
  154. /// <param name="title">Content to display on the label.</param>
  155. /// <param name="titleWidth">Width of the title label in pixels.</param>
  156. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  157. /// override any similar options set by style.</param>
  158. public GUIListBoxField(string[] elements, bool multiselect, GUIContent title, int titleWidth = 100, params GUIOption[] options)
  159. {
  160. Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, title, titleWidth, "", options, true);
  161. }
  162. /// <summary>
  163. /// Creates a new single-selection list box with the specified elements and a label.
  164. /// </summary>
  165. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  166. /// order as in the array.</param>
  167. /// <param name="title">Content to display on the label.</param>
  168. /// <param name="titleWidth">Width of the title label in pixels.</param>
  169. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  170. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  171. /// default element style is used.</param>
  172. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  173. /// override any similar options set by style.</param>
  174. public GUIListBoxField(string[] elements, GUIContent title, int titleWidth, string style = "",
  175. params GUIOption[] options)
  176. {
  177. Internal_CreateInstance(this, ToLocalizedElements(elements), false, title, titleWidth, style, options, true);
  178. }
  179. /// <summary>
  180. /// Creates a new single-selection list box with the specified elements and a label.
  181. /// </summary>
  182. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  183. /// order as in the array.</param>
  184. /// <param name="title">Content to display on the label.</param>
  185. /// <param name="titleWidth">Width of the title label in pixels.</param>
  186. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  187. /// override any similar options set by style.</param>
  188. public GUIListBoxField(string[] elements, GUIContent title, int titleWidth = 100, params GUIOption[] options)
  189. {
  190. Internal_CreateInstance(this, ToLocalizedElements(elements), false, title, titleWidth, "", options, true);
  191. }
  192. /// <summary>
  193. /// Creates a new list box with the specified elements and no label.
  194. /// </summary>
  195. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  196. /// order as in the array.</param>
  197. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  198. /// </param>
  199. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  200. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  201. /// default element style is used.</param>
  202. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  203. /// override any similar options set by style.</param>
  204. public GUIListBoxField(string[] elements, bool multiselect = false, string style = "", params GUIOption[] options)
  205. {
  206. Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, null, 0, style, options, false);
  207. }
  208. /// <summary>
  209. /// Creates a new list box with the specified elements and no label.
  210. /// </summary>
  211. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  212. /// order as in the array.</param>
  213. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  214. /// </param>
  215. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  216. /// override any similar options set by style.</param>
  217. public GUIListBoxField(string[] elements, bool multiselect = false, params GUIOption[] options)
  218. {
  219. Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, null, 0, "", options, false);
  220. }
  221. /// <summary>
  222. /// Updates the list box with a new set of elements.
  223. /// </summary>
  224. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  225. /// order as in the array.</param>
  226. public void SetElements(LocString[] elements)
  227. {
  228. Internal_SetElements(mCachedPtr, elements);
  229. }
  230. /// <summary>
  231. /// Updates the list box with a new set of elements.
  232. /// </summary>
  233. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  234. /// order as in the array.</param>
  235. public void SetElements(string[] elements)
  236. {
  237. Internal_SetElements(mCachedPtr, ToLocalizedElements(elements));
  238. }
  239. /// <summary>
  240. /// Makes the element with the specified index selected.
  241. /// </summary>
  242. /// <param name="idx">Sequential index in the elements array provided on construction.</param>
  243. public void SelectElement(int idx)
  244. {
  245. Internal_SelectElement(mCachedPtr, idx);
  246. }
  247. /// <summary>
  248. /// Deselect element the element with the specified index. Only relevant for multi-select list boxes.
  249. /// </summary>
  250. /// <param name="idx">Sequential index in the elements array provided on construction.</param>
  251. public void DeselectElement(int idx)
  252. {
  253. Internal_DeselectElement(mCachedPtr, idx);
  254. }
  255. /// <summary>
  256. /// Colors the element with a specific tint.
  257. /// </summary>
  258. /// <param name="color">Tint to apply to the element.</param>
  259. public void SetTint(Color color)
  260. {
  261. Internal_SetTint(mCachedPtr, color);
  262. }
  263. /// <summary>
  264. /// Converts a set of normal strings to a set of localized strings.
  265. /// </summary>
  266. /// <param name="elements">Strings to convert.</param>
  267. /// <returns>Localized strings created from input strings.</returns>
  268. private LocString[] ToLocalizedElements(string[] elements)
  269. {
  270. if (elements == null)
  271. return null;
  272. LocString[] locElements = new LocString[elements.Length];
  273. for (int i = 0; i < locElements.Length; i++)
  274. locElements[i] = elements[i];
  275. return locElements;
  276. }
  277. /// <summary>
  278. /// Triggered by the native interop object when a user selects an object in the list.
  279. /// </summary>
  280. private void DoOnSelectionChanged(int index)
  281. {
  282. if (OnSelectionChanged != null)
  283. OnSelectionChanged(index);
  284. }
  285. [MethodImpl(MethodImplOptions.InternalCall)]
  286. private static extern void Internal_CreateInstance(GUIListBoxField instance, LocString[] entries, bool multiselect,
  287. GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle);
  288. [MethodImpl(MethodImplOptions.InternalCall)]
  289. private static extern void Internal_SetElements(IntPtr nativeInstance, LocString[] elements);
  290. [MethodImpl(MethodImplOptions.InternalCall)]
  291. private static extern int Internal_GetValue(IntPtr nativeInstance);
  292. [MethodImpl(MethodImplOptions.InternalCall)]
  293. private static extern void Internal_SetValue(IntPtr nativeInstance, int value);
  294. [MethodImpl(MethodImplOptions.InternalCall)]
  295. private static extern bool[] Internal_GetElementStates(IntPtr nativeInstance);
  296. [MethodImpl(MethodImplOptions.InternalCall)]
  297. private static extern void Internal_SetElementStates(IntPtr nativeInstance, bool[] states);
  298. [MethodImpl(MethodImplOptions.InternalCall)]
  299. private static extern void Internal_SelectElement(IntPtr nativeInstance, int idx);
  300. [MethodImpl(MethodImplOptions.InternalCall)]
  301. private static extern void Internal_DeselectElement(IntPtr nativeInstance, int idx);
  302. [MethodImpl(MethodImplOptions.InternalCall)]
  303. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  304. }
  305. }