GUIToggleField.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 BansheeEngine;
  6. namespace BansheeEditor
  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, 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. Internal_CreateInstance(this, new GUIContent(), 0, style, options, false);
  59. }
  60. /// <summary>
  61. /// Colors the element with a specific tint.
  62. /// </summary>
  63. /// <param name="color">Tint to apply to the element.</param>
  64. public void SetTint(Color color)
  65. {
  66. Internal_SetTint(mCachedPtr, ref color);
  67. }
  68. /// <summary>
  69. /// Triggered by the runtime when the toggle button is toggled.
  70. /// </summary>
  71. /// <param name="newValue">New value of the toggle button.</param>
  72. private void DoOnChanged(bool newValue)
  73. {
  74. if (OnChanged != null)
  75. OnChanged(newValue);
  76. }
  77. [MethodImpl(MethodImplOptions.InternalCall)]
  78. private static extern void Internal_CreateInstance(GUIToggleField instance, GUIContent title, int titleWidth,
  79. string style, GUIOption[] options, bool withTitle);
  80. [MethodImpl(MethodImplOptions.InternalCall)]
  81. private static extern void Internal_GetValue(IntPtr nativeInstance, out bool value);
  82. [MethodImpl(MethodImplOptions.InternalCall)]
  83. private static extern void Internal_SetValue(IntPtr nativeInstance, bool value);
  84. [MethodImpl(MethodImplOptions.InternalCall)]
  85. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  86. }
  87. /** @} */
  88. }