GUIToggle.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /// GUI element representing a toggle (on/off) button.
  12. /// </summary>
  13. public sealed class GUIToggle : GUIElement
  14. {
  15. public delegate void OnClickDelegate();
  16. public delegate void OnHoverDelegate();
  17. public delegate void OnOutDelegate();
  18. public delegate void OnToggleDelegate(bool toggled);
  19. public delegate void OnDoubleClickDelegate();
  20. public event OnClickDelegate OnClick;
  21. public event OnHoverDelegate OnHover;
  22. public event OnOutDelegate OnOut;
  23. public event OnToggleDelegate OnToggled;
  24. public event OnDoubleClickDelegate OnDoubleClick;
  25. /// <summary>
  26. /// On/off state of the button.
  27. /// </summary>
  28. public bool Value
  29. {
  30. get { return Internal_GetValue(mCachedPtr); }
  31. set { Internal_SetValue(mCachedPtr, value); }
  32. }
  33. /// <summary>
  34. /// Creates a new toggle button with the specified label.
  35. /// </summary>
  36. /// <param name="content">Content to display on the button.</param>
  37. /// <param name="toggleGroup">Optional toggle group that is used for grouping multiple toggle buttons
  38. /// together. </param>
  39. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  40. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  41. /// default element style is used.</param>
  42. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  43. /// override any similar options set by style.</param>
  44. public GUIToggle(GUIContent content, GUIToggleGroup toggleGroup, string style, params GUIOption[] options)
  45. {
  46. Internal_CreateInstance(this, ref content, toggleGroup, style, options);
  47. }
  48. /// <summary>
  49. /// Creates a new toggle button with the specified label.
  50. /// </summary>
  51. /// <param name="content">Content to display on the button.</param>
  52. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  53. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  54. /// default element style is used.</param>
  55. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  56. /// override any similar options set by style.</param>
  57. public GUIToggle(GUIContent content, string style, params GUIOption[] options)
  58. {
  59. Internal_CreateInstance(this, ref content, null, style, options);
  60. }
  61. /// <summary>
  62. /// Creates a new toggle button with the specified label.
  63. /// </summary>
  64. /// <param name="content">Content to display on the button.</param>
  65. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  66. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  67. /// default element style is used.</param>
  68. public GUIToggle(GUIContent content, string style)
  69. {
  70. Internal_CreateInstance(this, ref content, null, style, new GUIOption[0]);
  71. }
  72. /// <summary>
  73. /// Creates a new toggle button with the specified label.
  74. /// </summary>
  75. /// <param name="content">Content to display on the button.</param>
  76. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  77. /// override any similar options set by style.</param>
  78. public GUIToggle(GUIContent content, params GUIOption[] options)
  79. {
  80. Internal_CreateInstance(this, ref content, null, "", options);
  81. }
  82. /// <summary>
  83. /// Creates a new toggle button with the specified label.
  84. /// </summary>
  85. /// <param name="content">Content to display on the button.</param>
  86. /// <param name="toggleGroup">Optional toggle group that is used for grouping multiple toggle buttons
  87. /// together.</param>
  88. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  89. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  90. /// default element style is used.</param>
  91. public GUIToggle(GUIContent content, GUIToggleGroup toggleGroup, string style)
  92. {
  93. Internal_CreateInstance(this, ref content, toggleGroup, style, new GUIOption[0]);
  94. }
  95. /// <summary>
  96. /// Creates a new toggle button with the specified label.
  97. /// </summary>
  98. /// <param name="content">Content to display on the button.</param>
  99. /// <param name="toggleGroup">Optional toggle group that is used for grouping multiple toggle buttons
  100. /// together.</param>
  101. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  102. /// override any similar options set by style.</param>
  103. public GUIToggle(GUIContent content, GUIToggleGroup toggleGroup, params GUIOption[] options)
  104. {
  105. Internal_CreateInstance(this, ref content, toggleGroup, "", options);
  106. }
  107. /// <summary>
  108. /// Updates the contents displayed on the button.
  109. /// </summary>
  110. /// <param name="content">Content to display on the button.</param>
  111. public void SetContent(GUIContent content)
  112. {
  113. Internal_SetContent(mCachedPtr, ref content);
  114. }
  115. /// <summary>
  116. /// Colors the element with a specific tint.
  117. /// </summary>
  118. /// <param name="color">Tint to apply to the element.</param>
  119. public void SetTint(Color color)
  120. {
  121. Internal_SetTint(mCachedPtr, ref color);
  122. }
  123. /// <summary>
  124. /// Triggered by the native interop object when a click occurs.
  125. /// </summary>
  126. private void DoOnClick()
  127. {
  128. if (OnClick != null)
  129. OnClick();
  130. }
  131. /// <summary>
  132. /// Triggered by the native interop object when the pointer is hovered over the element.
  133. /// </summary>
  134. private void DoOnHover()
  135. {
  136. if (OnHover != null)
  137. OnHover();
  138. }
  139. /// <summary>
  140. /// Triggered by the native interop object when the pointer leaves the element.
  141. /// </summary>
  142. private void DoOnOut()
  143. {
  144. if (OnOut != null)
  145. OnOut();
  146. }
  147. /// <summary>
  148. /// Triggered by the native interop object when the button is toggle on or off.
  149. /// </summary>
  150. /// <param name="toggled">New state of the element.</param>
  151. private void DoOnToggled(bool toggled)
  152. {
  153. if (OnToggled != null)
  154. OnToggled(toggled);
  155. }
  156. /// <summary>
  157. /// Triggered by the native interop object when a double click occurs.
  158. /// </summary>
  159. private void DoOnDoubleClick()
  160. {
  161. if (OnDoubleClick != null)
  162. OnDoubleClick();
  163. }
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. private static extern void Internal_CreateInstance(GUIToggle instance, ref GUIContent content,
  166. GUIToggleGroup toggleGroup, string style, GUIOption[] options);
  167. [MethodImpl(MethodImplOptions.InternalCall)]
  168. private static extern void Internal_SetContent(IntPtr nativeInstance, ref GUIContent content);
  169. [MethodImpl(MethodImplOptions.InternalCall)]
  170. private static extern bool Internal_GetValue(IntPtr nativeInstance);
  171. [MethodImpl(MethodImplOptions.InternalCall)]
  172. private static extern void Internal_SetValue(IntPtr nativeInstance, bool value);
  173. [MethodImpl(MethodImplOptions.InternalCall)]
  174. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  175. }
  176. /** @} */
  177. }