GUIListBoxField.cs 20 KB

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