SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
8.5 KiB

4 years ago
  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_Image,
  38. Change_SetActive,
  39. }
  40. public HoverBehaviour hoverBehaviourType = HoverBehaviour.Custom;
  41. private Action hoverBehaviourFunc_Enter, hoverBehaviourFunc_Exit;
  42. public Color hoverBehaviour_Color_Enter, hoverBehaviour_Color_Exit;
  43. public Image hoverBehaviour_Image;
  44. public Sprite hoverBehaviour_Sprite_Exit, hoverBehaviour_Sprite_Enter;
  45. public bool hoverBehaviour_Move = false;
  46. public Vector2 hoverBehaviour_Move_Amount = Vector2.zero;
  47. private Vector2 posExit, posEnter;
  48. public bool triggerMouseOutFuncOnClick = false;
  49. private bool mouseOver;
  50. private float mouseOverPerSecFuncTimer;
  51. private Action internalOnPointerEnterFunc, internalOnPointerExitFunc, internalOnPointerClickFunc;
  52. #if SOUND_MANAGER
  53. public Sound_Manager.Sound mouseOverSound, mouseClickSound;
  54. #endif
  55. #if CURSOR_MANAGER
  56. public CursorManager.CursorType cursorMouseOver, cursorMouseOut;
  57. #endif
  58. public virtual void OnPointerEnter(PointerEventData eventData) {
  59. if (internalOnPointerEnterFunc != null) internalOnPointerEnterFunc();
  60. if (hoverBehaviour_Move) transform.localPosition = posEnter;
  61. if (hoverBehaviourFunc_Enter != null) hoverBehaviourFunc_Enter();
  62. if (MouseOverOnceFunc != null) MouseOverOnceFunc();
  63. if (MouseOverOnceTooltipFunc != null) MouseOverOnceTooltipFunc();
  64. mouseOver = true;
  65. mouseOverPerSecFuncTimer = 0f;
  66. }
  67. public virtual void OnPointerExit(PointerEventData eventData) {
  68. if (internalOnPointerExitFunc != null) internalOnPointerExitFunc();
  69. if (hoverBehaviour_Move) transform.localPosition = posExit;
  70. if (hoverBehaviourFunc_Exit != null) hoverBehaviourFunc_Exit();
  71. if (MouseOutOnceFunc != null) MouseOutOnceFunc();
  72. if (MouseOutOnceTooltipFunc != null) MouseOutOnceTooltipFunc();
  73. mouseOver = false;
  74. }
  75. public virtual void OnPointerClick(PointerEventData eventData) {
  76. if (internalOnPointerClickFunc != null) internalOnPointerClickFunc();
  77. if (OnPointerClickFunc != null) OnPointerClickFunc(eventData);
  78. if (eventData.button == PointerEventData.InputButton.Left) {
  79. if (triggerMouseOutFuncOnClick) {
  80. OnPointerExit(eventData);
  81. }
  82. if (ClickFunc != null) ClickFunc();
  83. }
  84. if (eventData.button == PointerEventData.InputButton.Right)
  85. if (MouseRightClickFunc != null) MouseRightClickFunc();
  86. if (eventData.button == PointerEventData.InputButton.Middle)
  87. if (MouseMiddleClickFunc != null) MouseMiddleClickFunc();
  88. }
  89. public void Manual_OnPointerExit() {
  90. OnPointerExit(null);
  91. }
  92. public bool IsMouseOver() {
  93. return mouseOver;
  94. }
  95. public void OnPointerDown(PointerEventData eventData) {
  96. if (MouseDownOnceFunc != null) MouseDownOnceFunc();
  97. }
  98. public void OnPointerUp(PointerEventData eventData) {
  99. if (MouseUpFunc != null) MouseUpFunc();
  100. }
  101. void Update() {
  102. if (mouseOver) {
  103. if (MouseOverFunc != null) MouseOverFunc();
  104. mouseOverPerSecFuncTimer -= Time.unscaledDeltaTime;
  105. if (mouseOverPerSecFuncTimer <= 0) {
  106. mouseOverPerSecFuncTimer += 1f;
  107. if (MouseOverPerSecFunc != null) MouseOverPerSecFunc();
  108. }
  109. }
  110. if (MouseUpdate != null) MouseUpdate();
  111. }
  112. void Awake() {
  113. posExit = transform.localPosition;
  114. posEnter = (Vector2)transform.localPosition + hoverBehaviour_Move_Amount;
  115. SetHoverBehaviourType(hoverBehaviourType);
  116. #if SOUND_MANAGER
  117. // Sound Manager
  118. internalOnPointerEnterFunc += () => { if (mouseOverSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseOverSound); };
  119. internalOnPointerClickFunc += () => { if (mouseClickSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseClickSound); };
  120. #endif
  121. #if CURSOR_MANAGER
  122. // Cursor Manager
  123. internalOnPointerEnterFunc += () => { if (cursorMouseOver != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOver); };
  124. internalOnPointerExitFunc += () => { if (cursorMouseOut != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOut); };
  125. #endif
  126. }
  127. public void SetHoverBehaviourType(HoverBehaviour hoverBehaviourType) {
  128. this.hoverBehaviourType = hoverBehaviourType;
  129. switch (hoverBehaviourType) {
  130. case HoverBehaviour.Change_Color:
  131. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Enter; };
  132. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Exit; };
  133. break;
  134. case HoverBehaviour.Change_Image:
  135. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Enter; };
  136. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Exit; };
  137. break;
  138. case HoverBehaviour.Change_SetActive:
  139. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.gameObject.SetActive(true); };
  140. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.gameObject.SetActive(false); };
  141. break;
  142. }
  143. }
  144. /*
  145. * Class for temporarily intercepting a button action
  146. * Useful for Tutorial disabling specific buttons
  147. * */
  148. public class InterceptActionHandler {
  149. private Action removeInterceptFunc;
  150. public InterceptActionHandler(Action removeInterceptFunc) {
  151. this.removeInterceptFunc = removeInterceptFunc;
  152. }
  153. public void RemoveIntercept() {
  154. removeInterceptFunc();
  155. }
  156. }
  157. public InterceptActionHandler InterceptActionClick(Func<bool> testPassthroughFunc) {
  158. return InterceptAction("ClickFunc", testPassthroughFunc);
  159. }
  160. public InterceptActionHandler InterceptAction(string fieldName, Func<bool> testPassthroughFunc) {
  161. return InterceptAction(GetType().GetField(fieldName), testPassthroughFunc);
  162. }
  163. public InterceptActionHandler InterceptAction(System.Reflection.FieldInfo fieldInfo, Func<bool> testPassthroughFunc) {
  164. Action backFunc = fieldInfo.GetValue(this) as Action;
  165. InterceptActionHandler interceptActionHandler = new InterceptActionHandler(() => fieldInfo.SetValue(this, backFunc));
  166. fieldInfo.SetValue(this, (Action)delegate () {
  167. if (testPassthroughFunc()) {
  168. // Passthrough
  169. interceptActionHandler.RemoveIntercept();
  170. backFunc();
  171. }
  172. });
  173. return interceptActionHandler;
  174. }
  175. }
  176. }