GUISlider.cs 12 KB

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