GUIListBox.cs 6.1 KB

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