GUIButton.cs 5.4 KB

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