GUIButton.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.</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 GUIButton(GUIContent content, string style, params GUIOption[] options)
  40. {
  41. Internal_CreateInstance(this, content, style, options);
  42. }
  43. /// <summary>
  44. /// Creates a new 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. public GUIButton(GUIContent content, string style)
  51. {
  52. Internal_CreateInstance(this, content, style, new GUIOption[0]);
  53. }
  54. /// <summary>
  55. /// Creates a new button with the specified label.
  56. /// </summary>
  57. /// <param name="content">Content to display on the button.</param>
  58. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  59. /// override any similar options set by style.</param>
  60. public GUIButton(GUIContent content, params GUIOption[] options)
  61. {
  62. Internal_CreateInstance(this, content, "", options);
  63. }
  64. /// <summary>
  65. /// Updates the contents displayed on the button.
  66. /// </summary>
  67. /// <param name="content">Content to display on the button.</param>
  68. public void SetContent(GUIContent content)
  69. {
  70. Internal_SetContent(mCachedPtr, content);
  71. }
  72. /// <summary>
  73. /// Colors the element with a specific tint.
  74. /// </summary>
  75. /// <param name="color">Tint to apply to the element.</param>
  76. public void SetTint(Color color)
  77. {
  78. Internal_SetTint(mCachedPtr, color);
  79. }
  80. /// <summary>
  81. /// Triggered by the native interop object when a click occurs.
  82. /// </summary>
  83. private void DoOnClick()
  84. {
  85. if (OnClick != null)
  86. OnClick();
  87. }
  88. /// <summary>
  89. /// Triggered by the native interop object when a double click occurs.
  90. /// </summary>
  91. private void DoOnDoubleClick()
  92. {
  93. if (OnDoubleClick != null)
  94. OnDoubleClick();
  95. }
  96. /// <summary>
  97. /// Triggered by the native interop object when the pointer is hovered over the element.
  98. /// </summary>
  99. private void DoOnHover()
  100. {
  101. if (OnHover != null)
  102. OnHover();
  103. }
  104. /// <summary>
  105. /// Triggered by the native interop object when the pointer leaves the element.
  106. /// </summary>
  107. private void DoOnOut()
  108. {
  109. if (OnOut != null)
  110. OnOut();
  111. }
  112. [MethodImpl(MethodImplOptions.InternalCall)]
  113. private static extern void Internal_CreateInstance(GUIButton instance, GUIContent content, string style,
  114. GUIOption[] options);
  115. [MethodImpl(MethodImplOptions.InternalCall)]
  116. private static extern void Internal_SetContent(IntPtr nativeInstance, GUIContent content);
  117. [MethodImpl(MethodImplOptions.InternalCall)]
  118. private static extern void Internal_SetTint(IntPtr nativeInstance, Color color);
  119. }
  120. }