GUIToggle.cs 8.5 KB

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