Button_UI.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. ------------------- Code Monkey -------------------
  3. Thank you for downloading the Code Monkey Utilities
  4. I hope you find them useful in your projects
  5. If you have any questions use the contact form
  6. Cheers!
  7. unitycodemonkey.com
  8. --------------------------------------------------
  9. */
  10. //#define SOUND_MANAGER // Has Sound_Manager in project
  11. //#define CURSOR_MANAGER // Has Cursor_Manager in project
  12. using System;
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. using UnityEngine.EventSystems;
  16. namespace CodeMonkey.Utils {
  17. /*
  18. * Button in the UI
  19. * */
  20. public class Button_UI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler {
  21. public Action ClickFunc = null;
  22. public Action MouseRightClickFunc = null;
  23. public Action MouseMiddleClickFunc = null;
  24. public Action MouseDownOnceFunc = null;
  25. public Action MouseUpFunc = null;
  26. public Action MouseOverOnceTooltipFunc = null;
  27. public Action MouseOutOnceTooltipFunc = null;
  28. public Action MouseOverOnceFunc = null;
  29. public Action MouseOutOnceFunc = null;
  30. public Action MouseOverFunc = null;
  31. public Action MouseOverPerSecFunc = null; //Triggers every sec if mouseOver
  32. public Action MouseUpdate = null;
  33. public Action<PointerEventData> OnPointerClickFunc;
  34. public enum HoverBehaviour {
  35. Custom,
  36. Change_Color,
  37. Change_Color_Auto,
  38. Change_Image,
  39. Change_SetActive,
  40. }
  41. public HoverBehaviour hoverBehaviourType = HoverBehaviour.Custom;
  42. private Action hoverBehaviourFunc_Enter, hoverBehaviourFunc_Exit;
  43. public Color hoverBehaviour_Color_Enter, hoverBehaviour_Color_Exit;
  44. public Image hoverBehaviour_Image;
  45. public Sprite hoverBehaviour_Sprite_Exit, hoverBehaviour_Sprite_Enter;
  46. public bool hoverBehaviour_Move = false;
  47. public Vector2 hoverBehaviour_Move_Amount = Vector2.zero;
  48. private Vector2 posExit, posEnter;
  49. public bool triggerMouseOutFuncOnClick = false;
  50. private bool mouseOver;
  51. private float mouseOverPerSecFuncTimer;
  52. private Action internalOnPointerEnterFunc = null, internalOnPointerExitFunc = null, internalOnPointerClickFunc = null;
  53. #if SOUND_MANAGER
  54. public Sound_Manager.Sound mouseOverSound, mouseClickSound;
  55. #endif
  56. #if CURSOR_MANAGER
  57. public CursorManager.CursorType cursorMouseOver, cursorMouseOut;
  58. #endif
  59. public virtual void OnPointerEnter(PointerEventData eventData) {
  60. if (internalOnPointerEnterFunc != null) internalOnPointerEnterFunc();
  61. if (hoverBehaviour_Move) transform.GetComponent<RectTransform>().anchoredPosition = posEnter;
  62. if (hoverBehaviourFunc_Enter != null) hoverBehaviourFunc_Enter();
  63. if (MouseOverOnceFunc != null) MouseOverOnceFunc();
  64. if (MouseOverOnceTooltipFunc != null) MouseOverOnceTooltipFunc();
  65. mouseOver = true;
  66. mouseOverPerSecFuncTimer = 0f;
  67. }
  68. public virtual void OnPointerExit(PointerEventData eventData) {
  69. if (internalOnPointerExitFunc != null) internalOnPointerExitFunc();
  70. if (hoverBehaviour_Move) transform.GetComponent<RectTransform>().anchoredPosition = posExit;
  71. if (hoverBehaviourFunc_Exit != null) hoverBehaviourFunc_Exit();
  72. if (MouseOutOnceFunc != null) MouseOutOnceFunc();
  73. if (MouseOutOnceTooltipFunc != null) MouseOutOnceTooltipFunc();
  74. mouseOver = false;
  75. }
  76. public virtual void OnPointerClick(PointerEventData eventData) {
  77. if (internalOnPointerClickFunc != null) internalOnPointerClickFunc();
  78. if (OnPointerClickFunc != null) OnPointerClickFunc(eventData);
  79. if (eventData.button == PointerEventData.InputButton.Left) {
  80. if (triggerMouseOutFuncOnClick) {
  81. OnPointerExit(eventData);
  82. }
  83. if (ClickFunc != null) ClickFunc();
  84. }
  85. if (eventData.button == PointerEventData.InputButton.Right)
  86. if (MouseRightClickFunc != null) MouseRightClickFunc();
  87. if (eventData.button == PointerEventData.InputButton.Middle)
  88. if (MouseMiddleClickFunc != null) MouseMiddleClickFunc();
  89. }
  90. public void Manual_OnPointerExit() {
  91. OnPointerExit(null);
  92. }
  93. public bool IsMouseOver() {
  94. return mouseOver;
  95. }
  96. public void OnPointerDown(PointerEventData eventData) {
  97. if (MouseDownOnceFunc != null) MouseDownOnceFunc();
  98. }
  99. public void OnPointerUp(PointerEventData eventData) {
  100. if (MouseUpFunc != null) MouseUpFunc();
  101. }
  102. private void Update() {
  103. if (mouseOver) {
  104. if (MouseOverFunc != null) MouseOverFunc();
  105. mouseOverPerSecFuncTimer -= Time.unscaledDeltaTime;
  106. if (mouseOverPerSecFuncTimer <= 0) {
  107. mouseOverPerSecFuncTimer += 1f;
  108. if (MouseOverPerSecFunc != null) MouseOverPerSecFunc();
  109. }
  110. }
  111. if (MouseUpdate != null) MouseUpdate();
  112. }
  113. private void Awake() {
  114. posExit = transform.GetComponent<RectTransform>().anchoredPosition;
  115. posEnter = transform.GetComponent<RectTransform>().anchoredPosition + hoverBehaviour_Move_Amount;
  116. SetHoverBehaviourType(hoverBehaviourType);
  117. #if SOUND_MANAGER
  118. // Sound Manager
  119. internalOnPointerEnterFunc += () => { if (mouseOverSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseOverSound); };
  120. internalOnPointerClickFunc += () => { if (mouseClickSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseClickSound); };
  121. #endif
  122. #if CURSOR_MANAGER
  123. // Cursor Manager
  124. internalOnPointerEnterFunc += () => { if (cursorMouseOver != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOver); };
  125. internalOnPointerExitFunc += () => { if (cursorMouseOut != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOut); };
  126. #endif
  127. }
  128. public void SetHoverBehaviourType(HoverBehaviour hoverBehaviourType) {
  129. this.hoverBehaviourType = hoverBehaviourType;
  130. switch (hoverBehaviourType) {
  131. case HoverBehaviour.Change_Color:
  132. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Enter; };
  133. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Exit; };
  134. break;
  135. case HoverBehaviour.Change_Image:
  136. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Enter; };
  137. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Exit; };
  138. break;
  139. case HoverBehaviour.Change_SetActive:
  140. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.gameObject.SetActive(true); };
  141. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.gameObject.SetActive(false); };
  142. break;
  143. case HoverBehaviour.Change_Color_Auto:
  144. Color color = hoverBehaviour_Image.color;
  145. if (color.r >= 1f) color.r = .9f;
  146. if (color.g >= 1f) color.g = .9f;
  147. if (color.b >= 1f) color.b = .9f;
  148. Color colorOver = color * 1.3f; // Over color lighter
  149. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.color = colorOver; };
  150. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.color = color; };
  151. break;
  152. }
  153. }
  154. public void RefreshHoverBehaviourType() {
  155. SetHoverBehaviourType(hoverBehaviourType);
  156. }
  157. /*
  158. * Class for temporarily intercepting a button action
  159. * Useful for Tutorial disabling specific buttons
  160. * */
  161. public class InterceptActionHandler {
  162. private Action removeInterceptFunc;
  163. public InterceptActionHandler(Action removeInterceptFunc) {
  164. this.removeInterceptFunc = removeInterceptFunc;
  165. }
  166. public void RemoveIntercept() {
  167. removeInterceptFunc();
  168. }
  169. }
  170. public InterceptActionHandler InterceptActionClick(Func<bool> testPassthroughFunc) {
  171. return InterceptAction("ClickFunc", testPassthroughFunc);
  172. }
  173. public InterceptActionHandler InterceptAction(string fieldName, Func<bool> testPassthroughFunc) {
  174. return InterceptAction(GetType().GetField(fieldName), testPassthroughFunc);
  175. }
  176. public InterceptActionHandler InterceptAction(System.Reflection.FieldInfo fieldInfo, Func<bool> testPassthroughFunc) {
  177. Action backFunc = fieldInfo.GetValue(this) as Action;
  178. InterceptActionHandler interceptActionHandler = new InterceptActionHandler(() => fieldInfo.SetValue(this, backFunc));
  179. fieldInfo.SetValue(this, (Action)delegate () {
  180. if (testPassthroughFunc()) {
  181. // Passthrough
  182. interceptActionHandler.RemoveIntercept();
  183. backFunc();
  184. }
  185. });
  186. return interceptActionHandler;
  187. }
  188. }
  189. }