GUISliderField.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 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. /// A step value that determines the minimal increment the slider can be increased or decreased by.
  31. /// </summary>
  32. /// <param name="step">Step value in percent if range is not defined, otherwise in same units as the range.</param>
  33. public float Step
  34. {
  35. get { return Internal_GetStep(mCachedPtr); }
  36. set { Internal_SetStep(mCachedPtr, value); }
  37. }
  38. /// <summary>
  39. /// Creates a new slider field element with a label.
  40. /// </summary>
  41. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  42. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  43. /// <param name="title">Content to display on the label.</param>
  44. /// <param name="titleWidth">Width of the title label in pixels.</param>
  45. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  46. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  47. /// default element style is used.</param>
  48. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  49. /// override any similar options set by style.</param>
  50. public GUISliderField(float min, float max, GUIContent title, int titleWidth = 100,
  51. string style = "", params GUIOption[] options)
  52. {
  53. Internal_CreateInstance(this, min, max, ref title, titleWidth, style, options, true);
  54. }
  55. /// <summary>
  56. /// Creates a new slider field element without a label.
  57. /// </summary>
  58. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  59. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  60. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  61. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  62. /// default element style is used.</param>
  63. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  64. /// override any similar options set by style.</param>
  65. public GUISliderField(float min, float max, string style = "", params GUIOption[] options)
  66. {
  67. GUIContent emptyContent = new GUIContent();
  68. Internal_CreateInstance(this, min, max, ref emptyContent, 0, style, options, false);
  69. }
  70. /// <summary>
  71. /// Checks does the element currently has input focus. Input focus means the element has an input caret displayed
  72. /// and will accept input from the keyboard.
  73. /// </summary>
  74. public bool HasInputFocus
  75. {
  76. get
  77. {
  78. bool value;
  79. Internal_HasInputFocus(mCachedPtr, out value);
  80. return value;
  81. }
  82. }
  83. /// <summary>
  84. /// Colors the element with a specific tint.
  85. /// </summary>
  86. /// <param name="color">Tint to apply to the element.</param>
  87. public void SetTint(Color color)
  88. {
  89. Internal_SetTint(mCachedPtr, ref color);
  90. }
  91. /// <summary>
  92. /// Sets a range that will input field values will be clamped to. Set to large negative/positive values if clamping
  93. /// is not required.
  94. /// </summary>
  95. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  96. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  97. public void SetRange(float min, float max)
  98. {
  99. Internal_SetRange(mCachedPtr, min, max);
  100. }
  101. /// <summary>
  102. /// Triggered by the runtime when the value of the float field changes.
  103. /// </summary>
  104. /// <param name="newValue">New value of the float field.</param>
  105. private void DoOnChanged(float newValue)
  106. {
  107. if (OnChanged != null)
  108. OnChanged(newValue);
  109. }
  110. [MethodImpl(MethodImplOptions.InternalCall)]
  111. private static extern void Internal_CreateInstance(GUISliderField instance, float min, float max,
  112. ref GUIContent title, int titleWidth, string style, GUIOption[] options, bool withTitle);
  113. [MethodImpl(MethodImplOptions.InternalCall)]
  114. private static extern float Internal_GetValue(IntPtr nativeInstance);
  115. [MethodImpl(MethodImplOptions.InternalCall)]
  116. private static extern float Internal_GetStep(IntPtr nativeInstance);
  117. [MethodImpl(MethodImplOptions.InternalCall)]
  118. private static extern float Internal_SetValue(IntPtr nativeInstance, float value);
  119. [MethodImpl(MethodImplOptions.InternalCall)]
  120. private static extern void Internal_HasInputFocus(IntPtr nativeInstance, out bool focus);
  121. [MethodImpl(MethodImplOptions.InternalCall)]
  122. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  123. [MethodImpl(MethodImplOptions.InternalCall)]
  124. private static extern void Internal_SetRange(IntPtr nativeInstance, float min, float max);
  125. [MethodImpl(MethodImplOptions.InternalCall)]
  126. private static extern void Internal_SetStep(IntPtr nativeInstance, float step);
  127. }
  128. /** @} */
  129. }