GUIToggle.cs 8.4 KB

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