GUIButton.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// GUI button that can be clicked. Has normal, hover and active states with an optional label.
  7. /// </summary>
  8. public sealed class GUIButton : GUIElement
  9. {
  10. public delegate void OnClickDelegate();
  11. public delegate void OnDoubleClickDelegate();
  12. public delegate void OnHoverDelegate();
  13. public delegate void OnOutDelegate();
  14. /// <summary>
  15. /// Triggered when button is clicked.
  16. /// </summary>
  17. public event OnClickDelegate OnClick;
  18. /// <summary>
  19. /// Triggered when button is clicked twice in rapid succession.
  20. /// </summary>
  21. public event OnDoubleClickDelegate OnDoubleClick;
  22. /// <summary>
  23. /// Triggered when pointer hovers over the button.
  24. /// </summary>
  25. public event OnHoverDelegate OnHover;
  26. /// <summary>
  27. /// Triggered when pointer that was previously hovering leaves the button.
  28. /// </summary>
  29. public event OnOutDelegate OnOut;
  30. /// <summary>
  31. /// Creates a new button with the specified label.
  32. /// </summary>
  33. /// <param name="content">Content to display on the button.
  34. /// </param>
  35. /// <param name="style">Optional style to use for the element. Style controls the look
  36. /// of the element, as well as default layout options. Style will be retrieved
  37. /// from the active GUISkin. If not specified default button style is used.
  38. /// </param>
  39. /// <param name="options">Options that allow you to control how is the element
  40. /// positioned and sized. This will override any similar options set by style.
  41. /// </param>
  42. public GUIButton(GUIContent content, string style, params GUIOption[] options)
  43. {
  44. Internal_CreateInstance(this, content, style, options);
  45. }
  46. /// <summary>
  47. /// Creates a new 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
  52. /// of the element, as well as default layout options. Style will be retrieved
  53. /// from the active GUISkin. If not specified default button style is used.
  54. /// </param>
  55. public GUIButton(GUIContent content, string style)
  56. {
  57. Internal_CreateInstance(this, content, style, new GUIOption[0]);
  58. }
  59. /// <summary>
  60. /// Creates a new button with the specified label.
  61. /// </summary>
  62. /// <param name="content">Content to display on the button.
  63. /// </param>
  64. /// <param name="options">Options that allow you to control how is the element
  65. /// positioned and sized. This will override any similar options set by style.
  66. /// </param>
  67. public GUIButton(GUIContent content, params GUIOption[] options)
  68. {
  69. Internal_CreateInstance(this, content, "", options);
  70. }
  71. /// <summary>
  72. /// Creates a new button with the specified label.
  73. /// </summary>
  74. /// <param name="content">Content to display on the button.</param>
  75. public void SetContent(GUIContent content)
  76. {
  77. Internal_SetContent(mCachedPtr, content);
  78. }
  79. /// <summary>
  80. /// Colors the element with a specific tint.
  81. /// </summary>
  82. /// <param name="color">Tint to apply to the element.</param>
  83. public void SetTint(Color color)
  84. {
  85. Internal_SetTint(mCachedPtr, color);
  86. }
  87. /// <summary>
  88. /// Triggered by the native interop object when a click occurs.
  89. /// </summary>
  90. private void DoOnClick()
  91. {
  92. if (OnClick != null)
  93. OnClick();
  94. }
  95. /// <summary>
  96. /// Triggered by the native interop object when a double click occurs.
  97. /// </summary>
  98. private void DoOnDoubleClick()
  99. {
  100. if (OnDoubleClick != null)
  101. OnDoubleClick();
  102. }
  103. /// <summary>
  104. /// Triggered by the native interop object when the pointer is hovered over the element.
  105. /// </summary>
  106. private void DoOnHover()
  107. {
  108. if (OnHover != null)
  109. OnHover();
  110. }
  111. /// <summary>
  112. /// Triggered by the native interop object when the pointer leaves the element.
  113. /// </summary>
  114. private void DoOnOut()
  115. {
  116. if (OnOut != null)
  117. OnOut();
  118. }
  119. [MethodImpl(MethodImplOptions.InternalCall)]
  120. private static extern void Internal_CreateInstance(GUIButton instance, GUIContent content, string style, GUIOption[] options);
  121. [MethodImpl(MethodImplOptions.InternalCall)]
  122. private static extern void Internal_SetContent(IntPtr nativeInstance, GUIContent content);
  123. [MethodImpl(MethodImplOptions.InternalCall)]
  124. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  125. }
  126. }