GUIListBoxField.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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, 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, 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, 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, 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. Internal_CreateInstance(this, elements, multiselect, new GUIContent(), 0, style, options, false);
  119. }
  120. /// <summary>
  121. /// Creates a new list box with the specified elements and no label.
  122. /// </summary>
  123. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  124. /// order as in the array.</param>
  125. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  126. /// </param>
  127. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  128. /// override any similar options set by style.</param>
  129. public GUIListBoxField(LocString[] elements, bool multiselect = false, params GUIOption[] options)
  130. {
  131. Internal_CreateInstance(this, elements, multiselect, new GUIContent(), 0, "", options, false);
  132. }
  133. /// <summary>
  134. /// Creates a new list box with the specified elements and a label.
  135. /// </summary>
  136. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  137. /// order as in the array.</param>
  138. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  139. /// </param>
  140. /// <param name="title">Content to display on the label.</param>
  141. /// <param name="titleWidth">Width of the title label in pixels.</param>
  142. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  143. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  144. /// default element style is used.</param>
  145. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  146. /// override any similar options set by style.</param>
  147. public GUIListBoxField(string[] elements, bool multiselect, GUIContent title, int titleWidth, string style = "",
  148. params GUIOption[] options)
  149. {
  150. Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, title, titleWidth, style, options, true);
  151. }
  152. /// <summary>
  153. /// Creates a new list box with the specified elements and a label.
  154. /// </summary>
  155. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  156. /// order as in the array.</param>
  157. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  158. /// </param>
  159. /// <param name="title">Content to display on the label.</param>
  160. /// <param name="titleWidth">Width of the title label in pixels.</param>
  161. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  162. /// override any similar options set by style.</param>
  163. public GUIListBoxField(string[] elements, bool multiselect, GUIContent title, int titleWidth = 100, params GUIOption[] options)
  164. {
  165. Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, title, titleWidth, "", options, true);
  166. }
  167. /// <summary>
  168. /// Creates a new single-selection list box with the specified elements and a label.
  169. /// </summary>
  170. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  171. /// order as in the array.</param>
  172. /// <param name="title">Content to display on the label.</param>
  173. /// <param name="titleWidth">Width of the title label in pixels.</param>
  174. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  175. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  176. /// default element style is used.</param>
  177. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  178. /// override any similar options set by style.</param>
  179. public GUIListBoxField(string[] elements, GUIContent title, int titleWidth, string style = "",
  180. params GUIOption[] options)
  181. {
  182. Internal_CreateInstance(this, ToLocalizedElements(elements), false, title, titleWidth, style, options, true);
  183. }
  184. /// <summary>
  185. /// Creates a new single-selection list box with the specified elements and a label.
  186. /// </summary>
  187. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  188. /// order as in the array.</param>
  189. /// <param name="title">Content to display on the label.</param>
  190. /// <param name="titleWidth">Width of the title label in pixels.</param>
  191. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  192. /// override any similar options set by style.</param>
  193. public GUIListBoxField(string[] elements, GUIContent title, int titleWidth = 100, params GUIOption[] options)
  194. {
  195. Internal_CreateInstance(this, ToLocalizedElements(elements), false, title, titleWidth, "", options, true);
  196. }
  197. /// <summary>
  198. /// Creates a new list box with the specified elements and no label.
  199. /// </summary>
  200. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  201. /// order as in the array.</param>
  202. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  203. /// </param>
  204. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  205. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  206. /// default element style is used.</param>
  207. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  208. /// override any similar options set by style.</param>
  209. public GUIListBoxField(string[] elements, bool multiselect = false, string style = "", params GUIOption[] options)
  210. {
  211. Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, new GUIContent(), 0, style, options, false);
  212. }
  213. /// <summary>
  214. /// Creates a new list box with the specified elements and no label.
  215. /// </summary>
  216. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  217. /// order as in the array.</param>
  218. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  219. /// </param>
  220. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  221. /// override any similar options set by style.</param>
  222. public GUIListBoxField(string[] elements, bool multiselect = false, params GUIOption[] options)
  223. {
  224. Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, new GUIContent(), 0, "", options, false);
  225. }
  226. /// <summary>
  227. /// Updates the list box with a new set of elements.
  228. /// </summary>
  229. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  230. /// order as in the array.</param>
  231. public void SetElements(LocString[] elements)
  232. {
  233. Internal_SetElements(mCachedPtr, elements);
  234. }
  235. /// <summary>
  236. /// Updates the list box with a new set of elements.
  237. /// </summary>
  238. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  239. /// order as in the array.</param>
  240. public void SetElements(string[] elements)
  241. {
  242. Internal_SetElements(mCachedPtr, ToLocalizedElements(elements));
  243. }
  244. /// <summary>
  245. /// Makes the element with the specified index selected.
  246. /// </summary>
  247. /// <param name="idx">Sequential index in the elements array provided on construction.</param>
  248. public void SelectElement(int idx)
  249. {
  250. Internal_SelectElement(mCachedPtr, idx);
  251. }
  252. /// <summary>
  253. /// Deselect element the element with the specified index. Only relevant for multi-select list boxes.
  254. /// </summary>
  255. /// <param name="idx">Sequential index in the elements array provided on construction.</param>
  256. public void DeselectElement(int idx)
  257. {
  258. Internal_DeselectElement(mCachedPtr, idx);
  259. }
  260. /// <summary>
  261. /// Colors the element with a specific tint.
  262. /// </summary>
  263. /// <param name="color">Tint to apply to the element.</param>
  264. public void SetTint(Color color)
  265. {
  266. Internal_SetTint(mCachedPtr, ref color);
  267. }
  268. /// <summary>
  269. /// Converts a set of normal strings to a set of localized strings.
  270. /// </summary>
  271. /// <param name="elements">Strings to convert.</param>
  272. /// <returns>Localized strings created from input strings.</returns>
  273. private LocString[] ToLocalizedElements(string[] elements)
  274. {
  275. if (elements == null)
  276. return null;
  277. LocString[] locElements = new LocString[elements.Length];
  278. for (int i = 0; i < locElements.Length; i++)
  279. locElements[i] = elements[i];
  280. return locElements;
  281. }
  282. /// <summary>
  283. /// Triggered by the native interop object when a user selects an object in the list.
  284. /// </summary>
  285. private void DoOnSelectionChanged(int index)
  286. {
  287. if (OnSelectionChanged != null)
  288. OnSelectionChanged(index);
  289. }
  290. [MethodImpl(MethodImplOptions.InternalCall)]
  291. private static extern void Internal_CreateInstance(GUIListBoxField instance, LocString[] entries, bool multiselect,
  292. GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle);
  293. [MethodImpl(MethodImplOptions.InternalCall)]
  294. private static extern void Internal_SetElements(IntPtr nativeInstance, LocString[] elements);
  295. [MethodImpl(MethodImplOptions.InternalCall)]
  296. private static extern int Internal_GetValue(IntPtr nativeInstance);
  297. [MethodImpl(MethodImplOptions.InternalCall)]
  298. private static extern void Internal_SetValue(IntPtr nativeInstance, int value);
  299. [MethodImpl(MethodImplOptions.InternalCall)]
  300. private static extern bool[] Internal_GetElementStates(IntPtr nativeInstance);
  301. [MethodImpl(MethodImplOptions.InternalCall)]
  302. private static extern void Internal_SetElementStates(IntPtr nativeInstance, bool[] states);
  303. [MethodImpl(MethodImplOptions.InternalCall)]
  304. private static extern void Internal_SelectElement(IntPtr nativeInstance, int idx);
  305. [MethodImpl(MethodImplOptions.InternalCall)]
  306. private static extern void Internal_DeselectElement(IntPtr nativeInstance, int idx);
  307. [MethodImpl(MethodImplOptions.InternalCall)]
  308. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  309. }
  310. /** @} */
  311. }