GUISliderField.cs 5.6 KB

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