GUIListBox.cs 3.8 KB

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