GUIButton.cs 5.4 KB

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