GUIEnumField.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 elements of an enumeration and an optional label.
  8. /// </summary>
  9. public sealed class GUIEnumField : GUIElement
  10. {
  11. public delegate void OnSelectionChangedDelegate(int value);
  12. /// <summary>
  13. /// Triggered whenever user selects a new element in the list box. Returns the value of the enumeration entry that
  14. /// was selected.
  15. /// </summary>
  16. public event OnSelectionChangedDelegate OnSelectionChanged;
  17. /// <summary>
  18. /// Value of the enumeration entry currently selected.
  19. /// </summary>
  20. public int Value
  21. {
  22. get { return Internal_GetValue(mCachedPtr); }
  23. set { Internal_SetValue(mCachedPtr, value); }
  24. }
  25. /// <summary>
  26. /// Creates a new list box with enumeration entries as its elements and a label.
  27. /// </summary>
  28. /// <param name="enumType">Type of enum of whose entries to display in the list box.</param>
  29. /// <param name="title">Content to display on the label.</param>
  30. /// <param name="titleWidth">Width of the title label in pixels.</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 GUIEnumField(Type enumType, GUIContent title, int titleWidth = 100, string style = "", params GUIOption[] options)
  37. {
  38. Internal_CreateInstance(this, Enum.GetNames(enumType), Enum.GetValues(enumType), title, titleWidth, style, options, true);
  39. }
  40. /// <summary>
  41. /// Creates a new list box with enumeration entries as its elements and a label.
  42. /// </summary>
  43. /// <param name="enumType">Type of enum of whose entries to display in the list box.</param>
  44. /// <param name="title">Content to display on the label.</param>
  45. /// <param name="titleWidth">Width of the title label in pixels.</param>
  46. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  47. /// override any similar options set by style.</param>
  48. public GUIEnumField(Type enumType, GUIContent title, int titleWidth = 100, params GUIOption[] options)
  49. {
  50. Internal_CreateInstance(this, Enum.GetNames(enumType), Enum.GetValues(enumType), title, titleWidth, "", options, true);
  51. }
  52. /// <summary>
  53. /// Creates a new list box with enumeration entries as its elements and no label.
  54. /// </summary>
  55. /// <param name="enumType">Type of enum of whose entries to display in the list box.</param>
  56. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  57. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  58. /// default element style is used.</param>
  59. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  60. /// override any similar options set by style.</param>
  61. public GUIEnumField(Type enumType, string style = "", params GUIOption[] options)
  62. {
  63. Internal_CreateInstance(this, Enum.GetNames(enumType), Enum.GetValues(enumType), null, 0, style, options, false);
  64. }
  65. /// <summary>
  66. /// Creates a new list box with enumeration entries as its elements and no label.
  67. /// </summary>
  68. /// <param name="enumType">Type of enum of whose entries to display in the list box.</param>
  69. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  70. /// override any similar options set by style.</param>
  71. public GUIEnumField(Type enumType, params GUIOption[] options)
  72. {
  73. Internal_CreateInstance(this, Enum.GetNames(enumType), Enum.GetValues(enumType), null, 0, "", options, false);
  74. }
  75. /// <summary>
  76. /// Colors the element with a specific tint.
  77. /// </summary>
  78. /// <param name="color">Tint to apply to the element.</param>
  79. public void SetTint(Color color)
  80. {
  81. Internal_SetTint(mCachedPtr, color);
  82. }
  83. /// <summary>
  84. /// Triggered by the native interop object when a user selects an object in the list.
  85. /// </summary>
  86. private void DoOnSelectionChanged(int index)
  87. {
  88. if (OnSelectionChanged != null)
  89. OnSelectionChanged(index);
  90. }
  91. [MethodImpl(MethodImplOptions.InternalCall)]
  92. private static extern void Internal_CreateInstance(GUIEnumField instance, string[] names, Array values,
  93. GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. private static extern int Internal_GetValue(IntPtr nativeInstance);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. private static extern void Internal_SetValue(IntPtr nativeInstance, int value);
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  100. }
  101. }