GUIListBoxField.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Editor GUI element that displays a list box with user-specified elements and an optional label.
  8. /// </summary>
  9. public sealed class GUIListBoxField : GUIElement
  10. {
  11. public delegate void OnSelectionChangedDelegate(int index);
  12. /// <summary>
  13. /// Triggered whenever user selects a new element in the list box. Returned index maps to the element in the
  14. /// elements array that the list box was initialized with.
  15. /// </summary>
  16. public event OnSelectionChangedDelegate OnSelectionChanged;
  17. /// <summary>
  18. /// Index of the list box entry currently selected.
  19. /// </summary>
  20. public int Index
  21. {
  22. get { return Internal_GetValue(mCachedPtr); }
  23. set { Internal_SetValue(mCachedPtr, value); }
  24. }
  25. /// <summary>
  26. /// Creates a new list box with the specified elements and a label.
  27. /// </summary>
  28. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  29. /// order as in the array.</param>
  30. /// <param name="title">Content to display on the label.</param>
  31. /// <param name="titleWidth">Width of the title label in pixels.</param>
  32. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  33. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  34. /// default element style is used.</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 GUIListBoxField(LocString[] elements, GUIContent title, int titleWidth = 100, string style = "",
  38. params GUIOption[] options)
  39. {
  40. Internal_CreateInstance(this, elements, title, titleWidth, style, options, true);
  41. }
  42. /// <summary>
  43. /// Creates a new list box with the specified elements and a label.
  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="title">Content to display on the label.</param>
  48. /// <param name="titleWidth">Width of the title label in pixels.</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 GUIListBoxField(LocString[] elements, GUIContent title, int titleWidth = 100, params GUIOption[] options)
  52. {
  53. Internal_CreateInstance(this, elements, title, titleWidth, "", options, true);
  54. }
  55. /// <summary>
  56. /// Creates a new list box with the specified elements and no label.
  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. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  61. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  62. /// default element style is used.</param>
  63. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  64. /// override any similar options set by style.</param>
  65. public GUIListBoxField(LocString[] elements, string style = "", params GUIOption[] options)
  66. {
  67. Internal_CreateInstance(this, elements, null, 0, style, options, false);
  68. }
  69. /// <summary>
  70. /// Creates a new list box with the specified elements and no label.
  71. /// </summary>
  72. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  73. /// order as in the array.</param>
  74. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  75. /// override any similar options set by style.</param>
  76. public GUIListBoxField(LocString[] elements, params GUIOption[] options)
  77. {
  78. Internal_CreateInstance(this, elements, null, 0, "", options, false);
  79. }
  80. /// <summary>
  81. /// Updates the list box with a new set of elements.
  82. /// </summary>
  83. /// <param name="elements">Array of elements to display in the list box. Elements will be displayed in the same
  84. /// order as in the array.</param>
  85. public void SetElements(LocString[] elements)
  86. {
  87. Internal_SetElements(mCachedPtr, elements);
  88. }
  89. /// <summary>
  90. /// Colors the element with a specific tint.
  91. /// </summary>
  92. /// <param name="color">Tint to apply to the element.</param>
  93. public void SetTint(Color color)
  94. {
  95. Internal_SetTint(mCachedPtr, color);
  96. }
  97. /// <summary>
  98. /// Triggered by the native interop object when a user selects an object in the list.
  99. /// </summary>
  100. private void DoOnSelectionChanged(int index)
  101. {
  102. if (OnSelectionChanged != null)
  103. OnSelectionChanged(index);
  104. }
  105. [MethodImpl(MethodImplOptions.InternalCall)]
  106. private static extern void Internal_CreateInstance(GUIListBoxField instance, LocString[] entries,
  107. GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle);
  108. [MethodImpl(MethodImplOptions.InternalCall)]
  109. private static extern void Internal_SetElements(IntPtr nativeInstance, LocString[] elements);
  110. [MethodImpl(MethodImplOptions.InternalCall)]
  111. private static extern int Internal_GetValue(IntPtr nativeInstance);
  112. [MethodImpl(MethodImplOptions.InternalCall)]
  113. private static extern void Internal_SetValue(IntPtr nativeInstance, int value);
  114. [MethodImpl(MethodImplOptions.InternalCall)]
  115. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  116. }
  117. }