2
0

GUIListBox.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. namespace BansheeEngine
  6. {
  7. /** @addtogroup GUI_Engine
  8. * @{
  9. */
  10. /// <summary>
  11. /// List box GUI element which opens a drop down selection with provided elements when active.
  12. /// </summary>
  13. public sealed class GUIListBox : 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. /// States of all element in the list box (enabled or disabled).
  23. /// </summary>
  24. public bool[] States
  25. {
  26. get { return Internal_GetElementStates(mCachedPtr); }
  27. set { Internal_SetElementStates(mCachedPtr, value); }
  28. }
  29. /// <summary>
  30. /// Creates a new list box with the specified elements.
  31. /// </summary>
  32. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  33. /// order as in the array.</param>
  34. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  35. /// </param>
  36. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  37. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  38. /// default element style is used.</param>
  39. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  40. /// override any similar options set by style.</param>
  41. public GUIListBox(LocString[] elements, bool multiselect = false, string style = "", params GUIOption[] options)
  42. {
  43. Internal_CreateInstance(this, elements, multiselect, style, options);
  44. }
  45. /// <summary>
  46. /// Creates a new list box with the specified elements.
  47. /// </summary>
  48. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  49. /// order as in the array.</param>
  50. /// <param name="multiselect">Determines should the listbox allow multiple elements to be selected or just one.
  51. /// </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 GUIListBox(LocString[] elements, bool multiselect = false, params GUIOption[] options)
  55. {
  56. Internal_CreateInstance(this, elements, multiselect, "", options);
  57. }
  58. /// <summary>
  59. /// Updates the list box with a new set of elements.
  60. /// </summary>
  61. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  62. /// order as in the array.</param>
  63. public void SetElements(LocString[] elements)
  64. {
  65. Internal_SetElements(mCachedPtr, elements);
  66. }
  67. /// <summary>
  68. /// Makes the element with the specified index selected.
  69. /// </summary>
  70. /// <param name="idx">Sequential index in the elements array provided on construction.</param>
  71. public void SelectElement(int idx)
  72. {
  73. Internal_SelectElement(mCachedPtr, idx);
  74. }
  75. /// <summary>
  76. /// Deselect element the element with the specified index. Only relevant for multi-select list boxes.
  77. /// </summary>
  78. /// <param name="idx">Sequential index in the elements array provided on construction.</param>
  79. public void DeselectElement(int idx)
  80. {
  81. Internal_DeselectElement(mCachedPtr, idx);
  82. }
  83. /// <summary>
  84. /// Colors the element with a specific tint.
  85. /// </summary>
  86. /// <param name="color">Tint to apply to the element.</param>
  87. public void SetTint(Color color)
  88. {
  89. Internal_SetTint(mCachedPtr, ref color);
  90. }
  91. /// <summary>
  92. /// Triggered by the native interop object when a user selects an object in the list.
  93. /// </summary>
  94. private void DoOnSelectionChanged(int index)
  95. {
  96. if (OnSelectionChanged != null)
  97. OnSelectionChanged(index);
  98. }
  99. [MethodImpl(MethodImplOptions.InternalCall)]
  100. private static extern void Internal_CreateInstance(GUIListBox instance, LocString[] elements, bool multiselect,
  101. string style, GUIOption[] options);
  102. [MethodImpl(MethodImplOptions.InternalCall)]
  103. private static extern void Internal_SetElements(IntPtr nativeInstance, LocString[] elements);
  104. [MethodImpl(MethodImplOptions.InternalCall)]
  105. private static extern void Internal_SelectElement(IntPtr nativeInstance, int idx);
  106. [MethodImpl(MethodImplOptions.InternalCall)]
  107. private static extern void Internal_DeselectElement(IntPtr nativeInstance, int idx);
  108. [MethodImpl(MethodImplOptions.InternalCall)]
  109. private static extern bool[] Internal_GetElementStates(IntPtr nativeInstance);
  110. [MethodImpl(MethodImplOptions.InternalCall)]
  111. private static extern void Internal_SetElementStates(IntPtr nativeInstance, bool[] states);
  112. [MethodImpl(MethodImplOptions.InternalCall)]
  113. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  114. }
  115. /** @} */
  116. }