GUIListBox.cs 5.8 KB

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