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.

210 lines
8.4 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 System.Collections.Generic;
  15. using UnityEngine.EventSystems;
  16. namespace CodeMonkey.Utils {
  17. /*
  18. * Button Actions on a World BoxCollider
  19. * */
  20. public class Button_Sprite : MonoBehaviour {
  21. private static Func<Camera> GetWorldCamera;
  22. public static void SetGetWorldCamera(Func<Camera> GetWorldCamera) {
  23. Button_Sprite.GetWorldCamera = GetWorldCamera;
  24. }
  25. public Action ClickFunc = null;
  26. public Action MouseRightDownOnceFunc = null;
  27. public Action MouseRightDownFunc = null;
  28. public Action MouseRightUpFunc = null;
  29. public Action MouseDownOnceFunc = null;
  30. public Action MouseUpOnceFunc = null;
  31. public Action MouseOverOnceFunc = null;
  32. public Action MouseOutOnceFunc = null;
  33. public Action MouseOverOnceTooltipFunc = null;
  34. public Action MouseOutOnceTooltipFunc = null;
  35. private bool draggingMouseRight;
  36. private Vector3 mouseRightDragStart;
  37. public Action<Vector3, Vector3> MouseRightDragFunc = null;
  38. public Action<Vector3, Vector3> MouseRightDragUpdateFunc = null;
  39. public bool triggerMouseRightDragOnEnter = false;
  40. public enum HoverBehaviour {
  41. Custom,
  42. Change_Color,
  43. Change_Image,
  44. Change_SetActive,
  45. }
  46. public HoverBehaviour hoverBehaviourType = HoverBehaviour.Custom;
  47. private Action hoverBehaviourFunc_Enter, hoverBehaviourFunc_Exit;
  48. public Color hoverBehaviour_Color_Enter = new Color(1, 1, 1, 1), hoverBehaviour_Color_Exit = new Color(1, 1, 1, 1);
  49. public SpriteRenderer hoverBehaviour_Image;
  50. public Sprite hoverBehaviour_Sprite_Exit, hoverBehaviour_Sprite_Enter;
  51. public bool hoverBehaviour_Move = false;
  52. public Vector2 hoverBehaviour_Move_Amount = Vector2.zero;
  53. private Vector3 posExit, posEnter;
  54. public bool triggerMouseOutFuncOnClick = false;
  55. public bool clickThroughUI = false;
  56. private Action internalOnMouseDownFunc, internalOnMouseEnterFunc, internalOnMouseExitFunc;
  57. #if SOUND_MANAGER
  58. public Sound_Manager.Sound mouseOverSound, mouseClickSound;
  59. #endif
  60. #if CURSOR_MANAGER
  61. public CursorManager.CursorType cursorMouseOver, cursorMouseOut;
  62. #endif
  63. public void SetHoverBehaviourChangeColor(Color colorOver, Color colorOut) {
  64. hoverBehaviourType = HoverBehaviour.Change_Color;
  65. hoverBehaviour_Color_Enter = colorOver;
  66. hoverBehaviour_Color_Exit = colorOut;
  67. if (hoverBehaviour_Image == null) hoverBehaviour_Image = transform.GetComponent<SpriteRenderer>();
  68. hoverBehaviour_Image.color = hoverBehaviour_Color_Exit;
  69. SetupHoverBehaviour();
  70. }
  71. void OnMouseDown() {
  72. if (!clickThroughUI && IsPointerOverUI()) return; // Over UI!
  73. if (internalOnMouseDownFunc != null) internalOnMouseDownFunc();
  74. if (ClickFunc != null) ClickFunc();
  75. if (triggerMouseOutFuncOnClick) OnMouseExit();
  76. }
  77. public void Manual_OnMouseExit() {
  78. OnMouseExit();
  79. }
  80. void OnMouseUp() {
  81. if (MouseUpOnceFunc != null) MouseUpOnceFunc();
  82. }
  83. void OnMouseEnter() {
  84. if (!clickThroughUI && IsPointerOverUI()) return; // Over UI!
  85. if (internalOnMouseEnterFunc != null) internalOnMouseEnterFunc();
  86. if (hoverBehaviour_Move) transform.localPosition = posEnter;
  87. if (hoverBehaviourFunc_Enter != null) hoverBehaviourFunc_Enter();
  88. if (MouseOverOnceFunc != null) MouseOverOnceFunc();
  89. if (MouseOverOnceTooltipFunc != null) MouseOverOnceTooltipFunc();
  90. }
  91. void OnMouseExit() {
  92. if (internalOnMouseExitFunc != null) internalOnMouseExitFunc();
  93. if (hoverBehaviour_Move) transform.localPosition = posExit;
  94. if (hoverBehaviourFunc_Exit != null) hoverBehaviourFunc_Exit();
  95. if (MouseOutOnceFunc != null) MouseOutOnceFunc();
  96. if (MouseOutOnceTooltipFunc != null) MouseOutOnceTooltipFunc();
  97. }
  98. void OnMouseOver() {
  99. if (!clickThroughUI && IsPointerOverUI()) return; // Over UI!
  100. if (Input.GetMouseButton(1)) {
  101. if (MouseRightDownFunc != null) MouseRightDownFunc();
  102. if (!draggingMouseRight && triggerMouseRightDragOnEnter) {
  103. draggingMouseRight = true;
  104. mouseRightDragStart = GetWorldPositionFromUI();
  105. }
  106. }
  107. if (Input.GetMouseButtonDown(1)) {
  108. draggingMouseRight = true;
  109. mouseRightDragStart = GetWorldPositionFromUI();
  110. if (MouseRightDownOnceFunc != null) MouseRightDownOnceFunc();
  111. }
  112. }
  113. void Update() {
  114. if (draggingMouseRight) {
  115. if (MouseRightDragUpdateFunc != null) MouseRightDragUpdateFunc(mouseRightDragStart, GetWorldPositionFromUI());
  116. }
  117. if (Input.GetMouseButtonUp(1)) {
  118. if (draggingMouseRight) {
  119. draggingMouseRight = false;
  120. if (MouseRightDragFunc != null) MouseRightDragFunc(mouseRightDragStart, GetWorldPositionFromUI());
  121. }
  122. if (MouseRightUpFunc != null) MouseRightUpFunc();
  123. }
  124. }
  125. void Awake() {
  126. if (GetWorldCamera == null) SetGetWorldCamera(() => Camera.main); // Set default World Camera
  127. posExit = transform.localPosition;
  128. posEnter = transform.localPosition + (Vector3)hoverBehaviour_Move_Amount;
  129. SetupHoverBehaviour();
  130. #if SOUND_MANAGER
  131. // Sound Manager
  132. internalOnMouseDownFunc += () => { if (mouseClickSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseClickSound); };
  133. internalOnMouseEnterFunc += () => { if (mouseOverSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseOverSound); };
  134. #endif
  135. #if CURSOR_MANAGER
  136. // Cursor Manager
  137. internalOnMouseExitFunc += () => { if (cursorMouseOut != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOut); };
  138. internalOnMouseEnterFunc += () => { if (cursorMouseOver != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOver); };
  139. #endif
  140. }
  141. private void SetupHoverBehaviour() {
  142. switch (hoverBehaviourType) {
  143. case HoverBehaviour.Change_Color:
  144. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Enter; };
  145. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Exit; };
  146. break;
  147. case HoverBehaviour.Change_Image:
  148. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Enter; };
  149. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Exit; };
  150. break;
  151. case HoverBehaviour.Change_SetActive:
  152. hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.gameObject.SetActive(true); };
  153. hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.gameObject.SetActive(false); };
  154. break;
  155. }
  156. }
  157. private static Vector3 GetWorldPositionFromUI() {
  158. Vector3 worldPosition = GetWorldCamera().ScreenToWorldPoint(Input.mousePosition);
  159. return worldPosition;
  160. }
  161. private static bool IsPointerOverUI() {
  162. if (EventSystem.current.IsPointerOverGameObject()) {
  163. return true;
  164. } else {
  165. PointerEventData pe = new PointerEventData(EventSystem.current);
  166. pe.position = Input.mousePosition;
  167. List<RaycastResult> hits = new List<RaycastResult>();
  168. EventSystem.current.RaycastAll(pe, hits);
  169. return hits.Count > 0;
  170. }
  171. }
  172. }
  173. }