GUIToggleField.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup GUI-Editor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Editor GUI element that displays a toggle button and an optional label.
  13. /// </summary>
  14. public sealed class GUIToggleField : GUIElement
  15. {
  16. public delegate void OnChangedDelegate(bool newValue);
  17. /// <summary>
  18. /// Triggered when the value in the field changes.
  19. /// </summary>
  20. public event OnChangedDelegate OnChanged;
  21. /// <summary>
  22. /// Current state of the toggle button.
  23. /// </summary>
  24. public bool Value
  25. {
  26. get
  27. {
  28. bool value;
  29. Internal_GetValue(mCachedPtr, out value);
  30. return value;
  31. }
  32. set { Internal_SetValue(mCachedPtr, value); }
  33. }
  34. /// <summary>
  35. /// Creates a new toggle field element with a label.
  36. /// </summary>
  37. /// <param name="title">Content to display on the label.</param>
  38. /// <param name="titleWidth">Width of the title label in pixels.</param>
  39. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  40. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  41. /// default element style is used.</param>
  42. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  43. /// override any similar options set by style.</param>
  44. public GUIToggleField(GUIContent title, int titleWidth = 100, string style = "", params GUIOption[] options)
  45. {
  46. Internal_CreateInstance(this, ref title, titleWidth, style, options, true);
  47. }
  48. /// <summary>
  49. /// Creates a new toggle field element without a label.
  50. /// </summary>
  51. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  52. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  53. /// default element style is used.</param>
  54. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  55. /// override any similar options set by style.</param>
  56. public GUIToggleField(string style = "", params GUIOption[] options)
  57. {
  58. GUIContent emptyContent = new GUIContent();
  59. Internal_CreateInstance(this, ref emptyContent, 0, style, options, false);
  60. }
  61. /// <summary>
  62. /// Colors the element with a specific tint.
  63. /// </summary>
  64. /// <param name="color">Tint to apply to the element.</param>
  65. public void SetTint(Color color)
  66. {
  67. Internal_SetTint(mCachedPtr, ref color);
  68. }
  69. /// <summary>
  70. /// Triggered by the runtime when the toggle button is toggled.
  71. /// </summary>
  72. /// <param name="newValue">New value of the toggle button.</param>
  73. private void DoOnChanged(bool newValue)
  74. {
  75. if (OnChanged != null)
  76. OnChanged(newValue);
  77. }
  78. [MethodImpl(MethodImplOptions.InternalCall)]
  79. private static extern void Internal_CreateInstance(GUIToggleField instance, ref GUIContent title, int titleWidth,
  80. string style, GUIOption[] options, bool withTitle);
  81. [MethodImpl(MethodImplOptions.InternalCall)]
  82. private static extern void Internal_GetValue(IntPtr nativeInstance, out bool value);
  83. [MethodImpl(MethodImplOptions.InternalCall)]
  84. private static extern void Internal_SetValue(IntPtr nativeInstance, bool value);
  85. [MethodImpl(MethodImplOptions.InternalCall)]
  86. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  87. }
  88. /** @} */
  89. }