GUISlider.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. namespace BansheeEngine
  6. {
  7. /// <summary>
  8. /// A GUI element that represents a horizontal slider with a draggable handle.
  9. /// </summary>
  10. public sealed class GUISliderH : GUIElement
  11. {
  12. public delegate void OnChangedDelegate(float percent);
  13. /// <summary>
  14. /// Triggered when the slider handle moves. Provided parameter represents
  15. /// the position of the handle, in percent ranging [0, 1].
  16. /// </summary>
  17. public event OnChangedDelegate OnChanged;
  18. /// <summary>
  19. /// Returns the position of the slider handle, in percent ranging [0, 1].
  20. /// </summary>
  21. public float Percent
  22. {
  23. get { return Internal_GetPercent(mCachedPtr); }
  24. set { Internal_SetPercent(mCachedPtr, value); }
  25. }
  26. /// <summary>
  27. /// Returns the position of the slider handle, in range determined by <see cref="SetRange"/>. If range is not defined
  28. /// set to [0, 1] this is equivalent of <see cref="Percent"/>.
  29. /// </summary>
  30. public float Value
  31. {
  32. get { return Internal_GetValue(mCachedPtr); }
  33. set { Internal_SetValue(mCachedPtr, value); }
  34. }
  35. /// <summary>
  36. /// Creates a new horizontal slider.
  37. /// </summary>
  38. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  39. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  40. /// default element style is used.</param>
  41. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  42. /// override any similar options set by style.</param>
  43. public GUISliderH(string style, params GUIOption[] options)
  44. {
  45. Internal_CreateInstance(this, style, options);
  46. }
  47. /// <summary>
  48. /// Creates a new vertical slider.
  49. /// </summary>
  50. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  51. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  52. /// default element style is used.</param>
  53. public GUISliderH(string style = "")
  54. {
  55. Internal_CreateInstance(this, style, new GUIOption[0]);
  56. }
  57. /// <summary>
  58. /// Sets a range that will input field values will be clamped to. Set to large negative/positive values if clamping
  59. /// is not required.
  60. /// </summary>
  61. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  62. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  63. public void SetRange(float min, float max)
  64. {
  65. Internal_SetRange(mCachedPtr, min, max);
  66. }
  67. /// <summary>
  68. /// Sets a step value that determines the minimal increment the slider can be increased or decreased by.
  69. /// </summary>
  70. /// <param name="step">Step value in percent if range is not defined, otherwise in same units as the range.</param>
  71. public void SetStep(float step)
  72. {
  73. Internal_SetStep(mCachedPtr, step);
  74. }
  75. /// <summary>
  76. /// Colors the element with a specific tint.
  77. /// </summary>
  78. /// <param name="color">Tint to apply to the element.</param>
  79. public void SetTint(Color color)
  80. {
  81. Internal_SetTint(mCachedPtr, ref color);
  82. }
  83. /// <summary>
  84. /// Triggered by the native interop object when the slider handle is moved.
  85. /// </summary>
  86. /// <param name="percent">New position of the slider handle, in percent ranging [0, 1].</param>
  87. private void DoOnChanged(float percent)
  88. {
  89. if (OnChanged != null)
  90. OnChanged(percent);
  91. }
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. private static extern void Internal_CreateInstance(GUISliderH instance, string style, GUIOption[] options);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. private static extern float Internal_GetPercent(IntPtr nativeInstance);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. private static extern void Internal_SetPercent(IntPtr nativeInstance, float percent);
  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 percent);
  102. [MethodImpl(MethodImplOptions.InternalCall)]
  103. private static extern void Internal_SetRange(IntPtr nativeInstance, float min, float max);
  104. [MethodImpl(MethodImplOptions.InternalCall)]
  105. private static extern void Internal_SetStep(IntPtr nativeInstance, float step);
  106. [MethodImpl(MethodImplOptions.InternalCall)]
  107. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  108. }
  109. /// <summary>
  110. /// A GUI element that represents a vertical slider with a draggable handle.
  111. /// </summary>
  112. public sealed class GUISliderV : GUIElement
  113. {
  114. public delegate void OnChangedDelegate(float percent);
  115. /// <summary>
  116. /// Triggered when the slider handle moves. Provided parameter represents
  117. /// the position of the handle, in percent ranging [0, 1].
  118. /// </summary>
  119. public event OnChangedDelegate OnChanged;
  120. /// <summary>
  121. /// Returns the position of the slider handle, in percent ranging [0, 1].
  122. /// </summary>
  123. public float Percent
  124. {
  125. get { return Internal_GetPercent(mCachedPtr); }
  126. set { Internal_SetPercent(mCachedPtr, value); }
  127. }
  128. /// <summary>
  129. /// Returns the position of the slider handle, in range determined by <see cref="SetRange"/>. If range is not defined
  130. /// set to [0, 1] this is equivalent of <see cref="Percent"/>.
  131. /// </summary>
  132. public float Value
  133. {
  134. get { return Internal_GetValue(mCachedPtr); }
  135. set { Internal_SetValue(mCachedPtr, value); }
  136. }
  137. /// <summary>
  138. /// Creates a new vertical slider.
  139. /// </summary>
  140. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  141. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  142. /// default element style is used.</param>
  143. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  144. /// override any similar options set by style.</param>
  145. public GUISliderV(string style, params GUIOption[] options)
  146. {
  147. Internal_CreateInstance(this, style, options);
  148. }
  149. /// <summary>
  150. /// Creates a new vertical slider.
  151. /// </summary>
  152. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  153. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  154. /// default element style is used.</param>
  155. public GUISliderV(string style = "")
  156. {
  157. Internal_CreateInstance(this, style, new GUIOption[0]);
  158. }
  159. /// <summary>
  160. /// Sets a range that will input field values will be clamped to. Set to large negative/positive values if clamping
  161. /// is not required.
  162. /// </summary>
  163. /// <param name="min">Minimum boundary of the range to clamp values to.</param>
  164. /// <param name="max">Maximum boundary of the range to clamp values to.</param>
  165. public void SetRange(float min, float max)
  166. {
  167. Internal_SetRange(mCachedPtr, min, max);
  168. }
  169. /// <summary>
  170. /// Sets a step value that determines the minimal increment the slider can be increased or decreased by.
  171. /// </summary>
  172. /// <param name="step">Step value in percent if range is not defined, otherwise in same units as the range.</param>
  173. public void SetStep(float step)
  174. {
  175. Internal_SetStep(mCachedPtr, step);
  176. }
  177. /// <summary>
  178. /// Colors the element with a specific tint.
  179. /// </summary>
  180. /// <param name="color">Tint to apply to the element.</param>
  181. public void SetTint(Color color)
  182. {
  183. Internal_SetTint(mCachedPtr, ref color);
  184. }
  185. /// <summary>
  186. /// Triggered by the native interop object when the slider handle is moved.
  187. /// </summary>
  188. /// <param name="percent">New position of the slider handle, in percent ranging [0, 1].</param>
  189. private void DoOnChanged(float percent)
  190. {
  191. if (OnChanged != null)
  192. OnChanged(percent);
  193. }
  194. [MethodImpl(MethodImplOptions.InternalCall)]
  195. private static extern void Internal_CreateInstance(GUISliderV instance, string style, GUIOption[] options);
  196. [MethodImpl(MethodImplOptions.InternalCall)]
  197. private static extern float Internal_GetPercent(IntPtr nativeInstance);
  198. [MethodImpl(MethodImplOptions.InternalCall)]
  199. private static extern void Internal_SetPercent(IntPtr nativeInstance, float percent);
  200. [MethodImpl(MethodImplOptions.InternalCall)]
  201. private static extern float Internal_GetValue(IntPtr nativeInstance);
  202. [MethodImpl(MethodImplOptions.InternalCall)]
  203. private static extern void Internal_SetValue(IntPtr nativeInstance, float percent);
  204. [MethodImpl(MethodImplOptions.InternalCall)]
  205. private static extern void Internal_SetRange(IntPtr nativeInstance, float min, float max);
  206. [MethodImpl(MethodImplOptions.InternalCall)]
  207. private static extern void Internal_SetStep(IntPtr nativeInstance, float step);
  208. [MethodImpl(MethodImplOptions.InternalCall)]
  209. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  210. }
  211. }