GUIToggleField.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Editor GUI element that displays a toggle button and an optional label.
  8. /// </summary>
  9. public sealed class GUIToggleField : GUIElement
  10. {
  11. public delegate void OnChangedDelegate(bool newValue);
  12. /// <summary>
  13. /// Triggered when the value in the field changes.
  14. /// </summary>
  15. public event OnChangedDelegate OnChanged;
  16. /// <summary>
  17. /// Current state of the toggle button.
  18. /// </summary>
  19. public bool Value
  20. {
  21. get
  22. {
  23. bool value;
  24. Internal_GetValue(mCachedPtr, out value);
  25. return value;
  26. }
  27. set { Internal_SetValue(mCachedPtr, value); }
  28. }
  29. /// <summary>
  30. /// Creates a new toggle field element with a label.
  31. /// </summary>
  32. /// <param name="title">Content to display on the label.</param>
  33. /// <param name="titleWidth">Width of the title label in pixels.</param>
  34. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  35. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  36. /// default element style is used.</param>
  37. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  38. /// override any similar options set by style.</param>
  39. public GUIToggleField(GUIContent title, int titleWidth = 100, string style = "", params GUIOption[] options)
  40. {
  41. Internal_CreateInstance(this, title, titleWidth, style, options, true);
  42. }
  43. /// <summary>
  44. /// Creates a new toggle field element without a label.
  45. /// </summary>
  46. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  47. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  48. /// default element style is used.</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 GUIToggleField(string style = "", params GUIOption[] options)
  52. {
  53. Internal_CreateInstance(this, null, 0, style, options, false);
  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 runtime when the toggle button is toggled.
  65. /// </summary>
  66. /// <param name="newValue">New value of the toggle button.</param>
  67. private void DoOnChanged(bool newValue)
  68. {
  69. if (OnChanged != null)
  70. OnChanged(newValue);
  71. }
  72. [MethodImpl(MethodImplOptions.InternalCall)]
  73. private static extern void Internal_CreateInstance(GUIToggleField instance, GUIContent title, int titleWidth,
  74. string style, GUIOption[] options, bool withTitle);
  75. [MethodImpl(MethodImplOptions.InternalCall)]
  76. private static extern void Internal_GetValue(IntPtr nativeInstance, out bool value);
  77. [MethodImpl(MethodImplOptions.InternalCall)]
  78. private static extern void Internal_SetValue(IntPtr nativeInstance, bool value);
  79. [MethodImpl(MethodImplOptions.InternalCall)]
  80. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  81. }
  82. }