GUIListBoxField.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 bs;
  6. namespace bs.Editor
  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. Internal_CreateInstance(this, ToLocalizedElements(elements), false, ref title, titleWidth, "", options, true);
  198. }
  199. /// <summary>
  200. /// Creates a new list box with the specified elements and no label.
  201. /// </summary>
  202. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  203. /// order as in the array.</param>
  204. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  205. /// </param>
  206. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  207. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  208. /// default element style is used.</param>
  209. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  210. /// override any similar options set by style.</param>
  211. public GUIListBoxField(string[] elements, bool multiselect = false, string style = "", params GUIOption[] options)
  212. {
  213. GUIContent emptyContent = new GUIContent();
  214. Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, ref emptyContent, 0, style, options, false);
  215. }
  216. /// <summary>
  217. /// Creates a new list box with the specified elements and no label.
  218. /// </summary>
  219. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  220. /// order as in the array.</param>
  221. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  222. /// </param>
  223. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  224. /// override any similar options set by style.</param>
  225. public GUIListBoxField(string[] elements, bool multiselect = false, params GUIOption[] options)
  226. {
  227. GUIContent emptyContent = new GUIContent();
  228. Internal_CreateInstance(this, ToLocalizedElements(elements), multiselect, ref emptyContent, 0, "", options, false);
  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(LocString[] elements)
  236. {
  237. Internal_SetElements(mCachedPtr, elements);
  238. }
  239. /// <summary>
  240. /// Updates the list box with a new set of elements.
  241. /// </summary>
  242. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  243. /// order as in the array.</param>
  244. public void SetElements(string[] elements)
  245. {
  246. Internal_SetElements(mCachedPtr, ToLocalizedElements(elements));
  247. }
  248. /// <summary>
  249. /// Makes the element with the specified index selected.
  250. /// </summary>
  251. /// <param name="idx">Sequential index in the elements array provided on construction.</param>
  252. public void SelectElement(int idx)
  253. {
  254. Internal_SelectElement(mCachedPtr, idx);
  255. }
  256. /// <summary>
  257. /// Deselect element the element with the specified index. Only relevant for multi-select list boxes.
  258. /// </summary>
  259. /// <param name="idx">Sequential index in the elements array provided on construction.</param>
  260. public void DeselectElement(int idx)
  261. {
  262. Internal_DeselectElement(mCachedPtr, idx);
  263. }
  264. /// <summary>
  265. /// Colors the element with a specific tint.
  266. /// </summary>
  267. /// <param name="color">Tint to apply to the element.</param>
  268. public void SetTint(Color color)
  269. {
  270. Internal_SetTint(mCachedPtr, ref color);
  271. }
  272. /// <summary>
  273. /// Converts a set of normal strings to a set of localized strings.
  274. /// </summary>
  275. /// <param name="elements">Strings to convert.</param>
  276. /// <returns>Localized strings created from input strings.</returns>
  277. private LocString[] ToLocalizedElements(string[] elements)
  278. {
  279. if (elements == null)
  280. return null;
  281. LocString[] locElements = new LocString[elements.Length];
  282. for (int i = 0; i < locElements.Length; i++)
  283. locElements[i] = elements[i];
  284. return locElements;
  285. }
  286. /// <summary>
  287. /// Triggered by the native interop object when a user selects an object in the list.
  288. /// </summary>
  289. private void DoOnSelectionChanged(int index)
  290. {
  291. if (OnSelectionChanged != null)
  292. OnSelectionChanged(index);
  293. }
  294. [MethodImpl(MethodImplOptions.InternalCall)]
  295. private static extern void Internal_CreateInstance(GUIListBoxField instance, LocString[] entries, bool multiselect,
  296. ref GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle);
  297. [MethodImpl(MethodImplOptions.InternalCall)]
  298. private static extern void Internal_SetElements(IntPtr nativeInstance, LocString[] elements);
  299. [MethodImpl(MethodImplOptions.InternalCall)]
  300. private static extern int Internal_GetValue(IntPtr nativeInstance);
  301. [MethodImpl(MethodImplOptions.InternalCall)]
  302. private static extern void Internal_SetValue(IntPtr nativeInstance, int value);
  303. [MethodImpl(MethodImplOptions.InternalCall)]
  304. private static extern bool[] Internal_GetElementStates(IntPtr nativeInstance);
  305. [MethodImpl(MethodImplOptions.InternalCall)]
  306. private static extern void Internal_SetElementStates(IntPtr nativeInstance, bool[] states);
  307. [MethodImpl(MethodImplOptions.InternalCall)]
  308. private static extern void Internal_SelectElement(IntPtr nativeInstance, int idx);
  309. [MethodImpl(MethodImplOptions.InternalCall)]
  310. private static extern void Internal_DeselectElement(IntPtr nativeInstance, int idx);
  311. [MethodImpl(MethodImplOptions.InternalCall)]
  312. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  313. }
  314. /** @} */
  315. }