Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. using Amrita.Meta.ScriptableObjects;
  8. using Amrita.Meta.Audio;
  9. using System;
  10.  
  11. namespace Amrita.Meta.Utility
  12. {
  13. public class ButtonOnClickController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler
  14. {
  15. private const float CLICK_COOLDOWN = 100f;
  16. private const float HOVER_DELAY = 0.3f;
  17. public bool Interactable
  18. {
  19. get { return m_interactable; }
  20. set
  21. {
  22. m_interactable = value;
  23. IsInteractable();
  24. }
  25. }
  26. public Material disabledMaterial;
  27. public Image targetImage;
  28. public Transform trToTween;
  29. public bool isScrollable;
  30. public bool shouldBeDisabled;
  31. public Button.ButtonClickedEvent onClick;
  32. public Button.ButtonClickedEvent onPointerDown;
  33. public AnimationData AnimationData;
  34. [SerializeField] private bool m_interactable = true;
  35. [SerializeField] private bool m_canPlaySound = false;
  36. private Vector2 m_startPos;
  37. private Vector2 m_endPos;
  38.  
  39. private RectTransform m_rectTransform;
  40. private bool m_isClicked;
  41. private Camera m_camera;
  42. private DateTime m_clickDateTime;
  43.  
  44. private void OnEnable()
  45. {
  46. trToTween.localScale = Vector3.one;
  47. }
  48.  
  49. private void Awake()
  50. {
  51. if (shouldBeDisabled)
  52. {
  53. targetImage = GetComponent<Image>();
  54. disabledMaterial = disabledMaterial == null ? Resources.Load<Material>("DesaturatedButtonsUI") : disabledMaterial;
  55. }
  56.  
  57. m_clickDateTime = DateTime.Now;
  58. m_camera = Camera.main;
  59. m_rectTransform = GetComponent<RectTransform>();
  60. trToTween = trToTween == null ? transform : trToTween;
  61. AnimationData = AnimationData == null ? Resources.Load<AnimationData>("GamePlaySO/AnimationsData/OnClickAnimationData") : AnimationData;
  62. }
  63.  
  64. public void IsInteractable()
  65. {
  66. if (shouldBeDisabled)
  67. {
  68. targetImage.material = m_interactable ? null : disabledMaterial;
  69. }
  70. }
  71. public void OnHover()
  72. {
  73. if (gameObject.activeInHierarchy)
  74. {
  75. StartCoroutine(RunPointerAnimation(AnimationData.OnHoverTime, AnimationData.OnHoverCurve));
  76. }
  77.  
  78. }
  79.  
  80. public void OnPointerClick()
  81. {
  82. OnPointerClick(null);
  83. }
  84.  
  85. public void OnPointerClick(PointerEventData eventData)
  86. {
  87. if (m_interactable && isScrollable)
  88. {
  89. OnHover();
  90. InvokeOnClick();
  91. if (m_canPlaySound)
  92. {
  93. MetaAudioManager.Instance.PlayUIAudio(UIAudioType.OkButton);
  94. }
  95. m_isClicked = true;
  96. }
  97. }
  98.  
  99.  
  100. public void OnPointerUp(PointerEventData eventData)
  101. {
  102. if (m_interactable)
  103. {
  104. if (isScrollable)
  105. {
  106. m_endPos = eventData.position;
  107. var diff = Mathf.Abs(Vector2.Distance(m_startPos, m_endPos));
  108.  
  109. if (diff <= 5f)
  110. {
  111.  
  112. onClick.Invoke();
  113. if (m_canPlaySound)
  114. {
  115. MetaAudioManager.Instance.PlayUIAudio(UIAudioType.OkButton);
  116. }
  117. m_isClicked = true;
  118. }
  119. else
  120. {
  121. OnHover();
  122. }
  123.  
  124. }
  125. else
  126. {
  127. OnHover();
  128. InvokeOnClick();
  129. if (m_canPlaySound)
  130. {
  131. MetaAudioManager.Instance.PlayUIAudio(UIAudioType.OkButton);
  132. }
  133. m_isClicked = true;
  134. }
  135.  
  136. }
  137. }
  138.  
  139. private void InvokeOnClick()
  140. {
  141. var clickTime = DateTime.Now;
  142. var span = clickTime - m_clickDateTime;
  143. if (span.TotalMilliseconds > CLICK_COOLDOWN)
  144. {
  145. onClick.Invoke();
  146. m_clickDateTime = clickTime;
  147. }
  148. }
  149.  
  150. private IEnumerator WaitUntilUp()
  151. {
  152. yield return new WaitForSeconds(HOVER_DELAY);
  153. var pos = Input.mousePosition;
  154.  
  155. if (RectTransformUtility.RectangleContainsScreenPoint(m_rectTransform,pos,m_camera))
  156. {
  157. if(!m_isClicked)
  158. {
  159. onClick.Invoke();
  160. }
  161. }
  162. else
  163. {
  164. m_isClicked = false;
  165. }
  166.  
  167. }
  168.  
  169. public void OnPointerDown(PointerEventData eventData)
  170. {
  171. if(m_interactable)
  172. {
  173. m_startPos = eventData.position;
  174. onPointerDown.Invoke();
  175. StartCoroutine(RunPointerAnimation(AnimationData.OnClickTime,AnimationData.OnClickCurve));
  176. }
  177. }
  178.  
  179. private IEnumerator RunPointerAnimation(float animationLength, AnimationCurve animationCurve)
  180. {
  181. var timeElapsed = 0f;
  182. while(timeElapsed < animationLength)
  183. {
  184. timeElapsed += Time.deltaTime;
  185. yield return new WaitForEndOfFrame();
  186. var currentValue = animationCurve.Evaluate(timeElapsed);
  187. trToTween.localScale = new Vector3(currentValue,currentValue,currentValue);
  188. }
  189. }
  190.  
  191. private void OnDisable()
  192. {
  193. StopAllCoroutines();
  194. }
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement