GUISliderField.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Editor GUI element that displays a slider with floating point input field and an optional label.
  8. /// </summary>
  9. public sealed class GUISliderField : GUIElement
  10. {
  11. public delegate void OnChangedDelegate(float newValue);
  12. /// <summary>
  13. /// Triggered when the value in the field changes.
  14. /// </summary>
  15. public event OnChangedDelegate OnChanged;
  16. /// <summary>
  17. /// Value displayed by the field input box.
  18. /// </summary>
  19. public float Value
  20. {
  21. get { return Internal_GetValue(mCachedPtr); }
  22. set { Internal_SetValue(mCachedPtr, value); }
  23. }
  24. /// <summary>
  25. /// Creates a new slider field element with a label.
  26. /// </summary>
  27. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  28. /// <param name="max">Maximum boundary of the range to clamp values to.</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 GUISliderField(float min, float max, GUIContent title, int titleWidth = 100,
  37. string style = "", params GUIOption[] options)
  38. {
  39. Internal_CreateInstance(this, min, max, title, titleWidth, style, options, true);
  40. }
  41. /// <summary>
  42. /// Creates a new slider field element without a label.
  43. /// </summary>
  44. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  45. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  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 GUISliderField(float min, float max, string style = "", params GUIOption[] options)
  52. {
  53. Internal_CreateInstance(this, min, max, 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. /// Sets a range that will input field values will be clamped to. Set to large negative/positive values if clamping
  65. /// is not required.
  66. /// </summary>
  67. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  68. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  69. public void SetRange(float min, float max)
  70. {
  71. Internal_SetRange(mCachedPtr, min, max);
  72. }
  73. /// <summary>
  74. /// Sets a step value that determines the minimal increment the slider can be increased or decreased by.
  75. /// </summary>
  76. /// <param name="step">Step value in percent if range is not defined, otherwise in same units as the range.</param>
  77. public void SetStep(float step)
  78. {
  79. Internal_SetStep(mCachedPtr, step);
  80. }
  81. /// <summary>
  82. /// Triggered by the runtime when the value of the float field changes.
  83. /// </summary>
  84. /// <param name="newValue">New value of the float field.</param>
  85. private void DoOnChanged(float newValue)
  86. {
  87. if (OnChanged != null)
  88. OnChanged(newValue);
  89. }
  90. [MethodImpl(MethodImplOptions.InternalCall)]
  91. private static extern void Internal_CreateInstance(GUISliderField instance, float min, float max,
  92. GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle);
  93. [MethodImpl(MethodImplOptions.InternalCall)]
  94. private static extern float Internal_GetValue(IntPtr nativeInstance);
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. private static extern void Internal_SetValue(IntPtr nativeInstance, float value);
  97. [MethodImpl(MethodImplOptions.InternalCall)]
  98. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  99. [MethodImpl(MethodImplOptions.InternalCall)]
  100. private static extern void Internal_SetRange(IntPtr nativeInstance, float min, float max);
  101. [MethodImpl(MethodImplOptions.InternalCall)]
  102. private static extern void Internal_SetStep(IntPtr nativeInstance, float step);
  103. }
  104. }