GUIToggle.cs 8.6 KB

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