GUISliderField.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 slider with floating point input field and an optional label.
  13. /// </summary>
  14. public sealed class GUISliderField : GUIElement
  15. {
  16. public delegate void OnChangedDelegate(float newValue);
  17. /// <summary>
  18. /// Triggered when the value in the field changes.
  19. /// </summary>
  20. public event OnChangedDelegate OnChanged;
  21. /// <summary>
  22. /// Value displayed by the field input box.
  23. /// </summary>
  24. public float Value
  25. {
  26. get { return Internal_GetValue(mCachedPtr); }
  27. set { Internal_SetValue(mCachedPtr, value); }
  28. }
  29. /// <summary>
  30. /// Creates a new slider field element with a label.
  31. /// </summary>
  32. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  33. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  34. /// <param name="title">Content to display on the label.</param>
  35. /// <param name="titleWidth">Width of the title label in pixels.</param>
  36. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  37. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  38. /// default element style is used.</param>
  39. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  40. /// override any similar options set by style.</param>
  41. public GUISliderField(float min, float max, GUIContent title, int titleWidth = 100,
  42. string style = "", params GUIOption[] options)
  43. {
  44. Internal_CreateInstance(this, min, max, title, titleWidth, style, options, true);
  45. }
  46. /// <summary>
  47. /// Creates a new slider field element without a label.
  48. /// </summary>
  49. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  50. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  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 GUISliderField(float min, float max, string style = "", params GUIOption[] options)
  57. {
  58. Internal_CreateInstance(this, min, max, null, 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. /// Sets a range that will input field values will be clamped to. Set to large negative/positive values if clamping
  70. /// is not required.
  71. /// </summary>
  72. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  73. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  74. public void SetRange(float min, float max)
  75. {
  76. Internal_SetRange(mCachedPtr, min, max);
  77. }
  78. /// <summary>
  79. /// Sets a step value that determines the minimal increment the slider can be increased or decreased by.
  80. /// </summary>
  81. /// <param name="step">Step value in percent if range is not defined, otherwise in same units as the range.</param>
  82. public void SetStep(float step)
  83. {
  84. Internal_SetStep(mCachedPtr, step);
  85. }
  86. /// <summary>
  87. /// Triggered by the runtime when the value of the float field changes.
  88. /// </summary>
  89. /// <param name="newValue">New value of the float field.</param>
  90. private void DoOnChanged(float newValue)
  91. {
  92. if (OnChanged != null)
  93. OnChanged(newValue);
  94. }
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. private static extern void Internal_CreateInstance(GUISliderField instance, float min, float max,
  97. GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle);
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. private static extern float Internal_GetValue(IntPtr nativeInstance);
  100. [MethodImpl(MethodImplOptions.InternalCall)]
  101. private static extern void Internal_SetValue(IntPtr nativeInstance, float value);
  102. [MethodImpl(MethodImplOptions.InternalCall)]
  103. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  104. [MethodImpl(MethodImplOptions.InternalCall)]
  105. private static extern void Internal_SetRange(IntPtr nativeInstance, float min, float max);
  106. [MethodImpl(MethodImplOptions.InternalCall)]
  107. private static extern void Internal_SetStep(IntPtr nativeInstance, float step);
  108. }
  109. /** @} */
  110. }