GUIListBoxField.cs 20 KB

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