GUIListBox.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 elements
  13. /// array that the list box was initialized with.
  14. /// </summary>
  15. public event OnSelectionChangedDelegate OnSelectionChanged;
  16. /// <summary>
  17. /// Creates a new list box with the specified elements.
  18. /// </summary>
  19. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same order
  20. /// as in the array.
  21. /// </param>
  22. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  23. /// default layout options. Style will be retrieved from the active GUISkin. If not specified default element style
  24. /// is used.
  25. /// </param>
  26. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  27. /// override any similar options set by style.
  28. /// </param>
  29. public GUIListBox(LocString[] elements, string style, params GUIOption[] options)
  30. {
  31. Internal_CreateInstance(this, elements, style, options);
  32. }
  33. /// <summary>
  34. /// Creates a new list box with the specified elements.
  35. /// </summary>
  36. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same order
  37. /// as in the array.
  38. /// </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.
  41. /// </param>
  42. public GUIListBox(LocString[] elements, params GUIOption[] options)
  43. {
  44. Internal_CreateInstance(this, elements, "", options);
  45. }
  46. /// <summary>
  47. /// Updates the list box with a new set of elements.
  48. /// </summary>
  49. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same order
  50. /// as in the array.</param>
  51. public void SetElements(LocString[] elements)
  52. {
  53. Internal_SetElements(mCachedPtr, elements);
  54. }
  55. /// <summary>
  56. /// Colors the element with a specific tint.
  57. /// </summary>
  58. /// <param name="color">Tint to apply to the element.</param>
  59. public void SetTint(Color color)
  60. {
  61. Internal_SetTint(mCachedPtr, color);
  62. }
  63. /// <summary>
  64. /// Triggered by the native interop object when a user selects an object in the list.
  65. /// </summary>
  66. private void DoOnSelectionChanged(int index)
  67. {
  68. if (OnSelectionChanged != null)
  69. OnSelectionChanged(index);
  70. }
  71. [MethodImpl(MethodImplOptions.InternalCall)]
  72. private static extern void Internal_CreateInstance(GUIListBox instance, LocString[] elements, string style, params GUIOption[] options);
  73. [MethodImpl(MethodImplOptions.InternalCall)]
  74. private static extern void Internal_SetElements(IntPtr nativeInstance, LocString[] elements);
  75. [MethodImpl(MethodImplOptions.InternalCall)]
  76. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  77. }
  78. }