Files

201 lines
6.5 KiB
C#
Raw Permalink Normal View History

2023-07-04 21:32:21 +03:00
using System.Collections;
2023-10-09 12:39:56 +03:00
using UnityEditor;
2023-07-04 21:32:21 +03:00
using UnityEngine;
2022-12-14 18:33:44 +02:00
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[RequireComponent(typeof(Audio))]
[RequireComponent(typeof(Button))]
[RequireComponent(typeof(Animator))]
2023-09-24 11:02:44 +02:00
public class BaseButton : MonoBehaviour, ISelectHandler, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
2022-12-14 18:33:44 +02:00
{
[SerializeField] public UnityEvent _onClickedAnimationEnd = default;
[SerializeField] public UnityEvent _onSelected = default;
2023-10-03 01:56:01 +03:00
[SerializeField] public UnityEvent _onDeselected = default;
2022-12-14 18:33:44 +02:00
[SerializeField] public RectTransform _scrollView = default;
[SerializeField] public float _scrollUpAmount = default;
[SerializeField] public float _scrollDownAmount = default;
2023-09-24 23:49:04 +02:00
[SerializeField] private bool _selectOnHover = default;
2023-09-25 16:24:19 +03:00
[SerializeField] private bool _clickAnimation = true;
2022-12-14 18:33:44 +02:00
[SerializeField] private bool _ignoreFirstSelectSound = default;
[SerializeField] private bool _allowMultiplePresses = default;
2023-09-13 15:09:50 +03:00
[SerializeField] private Selectable selectableParent = default;
2023-10-03 02:33:28 +03:00
[SerializeField] protected bool _resetToDefault = false;
2022-12-14 18:33:44 +02:00
protected Audio _audio;
protected Button _button;
protected Animator _animator;
protected RectTransform _rect;
2022-12-14 18:33:44 +02:00
private bool _isIgnoringFirstSelectSound;
private bool _wasClicked;
2023-07-04 21:32:21 +03:00
private Coroutine _moveVerticalCoroutine;
protected Vector2 _defaultPosition;
2023-07-06 16:01:26 +03:00
private readonly int _moveVerticalAmount = 5;
protected bool _isPressed;
2022-12-14 18:33:44 +02:00
protected virtual void Awake()
2022-12-14 18:33:44 +02:00
{
#if UNITY_EDITOR
2023-10-09 12:39:56 +03:00
HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
2023-11-30 21:52:14 +02:00
#endif
2022-12-14 18:33:44 +02:00
_audio = GetComponent<Audio>();
_button = GetComponent<Button>();
_animator = GetComponent<Animator>();
2023-07-04 21:32:21 +03:00
_rect = GetComponent<RectTransform>();
_defaultPosition = _rect.anchoredPosition;
2022-12-14 18:33:44 +02:00
}
2023-08-28 02:38:39 +03:00
public virtual void OnSelect(BaseEventData eventData)
2022-12-14 18:33:44 +02:00
{
_onSelected?.Invoke();
if (!_isIgnoringFirstSelectSound)
{
}
2023-10-01 17:21:54 +03:00
_animator.SetBool("IsHover", true);
2022-12-14 18:33:44 +02:00
_isIgnoringFirstSelectSound = false;
}
2023-08-28 22:50:54 +03:00
public virtual void OnPointerEnter(PointerEventData eventData)
2022-12-14 18:33:44 +02:00
{
2023-09-24 23:49:04 +02:00
if (_selectOnHover)
_button.Select();
2023-07-06 16:01:26 +03:00
_animator.SetBool("IsHover", true);
2023-07-04 21:32:21 +03:00
_audio.Sound("Selected").Play();
Cursor.SetCursor(MouseSetup.Instance.HoverCursor, Vector2.zero, CursorMode.Auto);
2022-12-14 18:33:44 +02:00
}
2023-08-28 22:50:54 +03:00
public virtual void OnPointerExit(PointerEventData eventData)
2023-07-04 21:32:21 +03:00
{
2023-09-25 00:43:16 +02:00
if (!_selectOnHover)
_animator.SetBool("IsHover", false);
2023-07-04 21:32:21 +03:00
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
2023-09-13 15:09:50 +03:00
public void SelectParent() => selectableParent?.Select();
2022-12-14 18:33:44 +02:00
public virtual void OnPress()
{
if (!_wasClicked)
{
2023-10-01 17:21:54 +03:00
_animator.SetBool("IsPress", true);
2022-12-14 18:33:44 +02:00
EventSystem.current.sendNavigationEvents = false;
if (!_allowMultiplePresses)
_wasClicked = true;
}
}
public void OnClickedEndAnimationEvent()
{
_animator.Rebind();
if (_allowMultiplePresses)
_animator.SetBool("IsSelected", true);
EventSystem.current.sendNavigationEvents = true;
2023-11-03 13:45:29 +02:00
if (_onClickedAnimationEnd != null)
_onClickedAnimationEnd?.Invoke();
2022-12-14 18:33:44 +02:00
}
public void MoveScrollDown(BaseEventData eventData)
{
AxisEventData axisEventData = eventData as AxisEventData;
if (axisEventData.moveDir == MoveDirection.Down)
_scrollView.anchoredPosition += new Vector2(0.0f, _scrollDownAmount);
}
public void MoveScrollUp(BaseEventData eventData)
{
AxisEventData axisEventData = eventData as AxisEventData;
if (axisEventData.moveDir == MoveDirection.Up)
_scrollView.anchoredPosition += new Vector2(0.0f, _scrollUpAmount);
}
2023-08-28 02:38:39 +03:00
public virtual void Activate()
2023-02-09 21:31:53 +02:00
{
_button.enabled = true;
2023-10-09 13:33:19 +03:00
gameObject.SetActive(true);
2023-02-09 21:31:53 +02:00
_animator.SetBool("IsDeactivated", false);
}
2023-08-28 02:38:39 +03:00
public virtual void Deactivate()
2023-02-09 21:31:53 +02:00
{
_button.enabled = false;
2023-10-09 13:33:19 +03:00
gameObject.SetActive(false);
2023-02-09 21:31:53 +02:00
_animator.SetBool("IsDeactivated", true);
}
2022-12-14 18:33:44 +02:00
void OnEnable()
{
_isIgnoringFirstSelectSound = _ignoreFirstSelectSound;
}
void OnDisable()
{
2023-09-13 15:09:50 +03:00
if (_moveVerticalCoroutine != null)
{
StopCoroutine(_moveVerticalCoroutine);
_rect.anchoredPosition = _defaultPosition;
}
_isPressed = false;
2022-12-14 18:33:44 +02:00
_wasClicked = false;
if (_ignoreFirstSelectSound)
_isIgnoringFirstSelectSound = true;
_animator.Rebind();
2023-09-25 16:24:19 +03:00
_animator.SetBool("IsHover", false);
_animator.SetBool("IsPress", false);
2022-12-14 18:33:44 +02:00
}
2023-07-04 21:32:21 +03:00
public virtual void OnPointerDown(PointerEventData eventData)
2023-07-04 21:32:21 +03:00
{
if (_isPressed)
return;
2023-07-06 16:01:26 +03:00
_audio.Sound("Pressed").Play();
_animator.SetBool("IsPress", true);
2023-09-25 16:24:19 +03:00
if (!_clickAnimation)
return;
2023-07-04 21:32:21 +03:00
if (_moveVerticalCoroutine != null)
StopCoroutine(_moveVerticalCoroutine);
_rect.anchoredPosition = _defaultPosition;
_moveVerticalCoroutine = StartCoroutine(MoveVerticalCoroutine(-_moveVerticalAmount));
}
public void OnPointerUp(PointerEventData eventData)
{
if (_isPressed)
return;
if (_resetToDefault)
{
_isPressed = false;
2023-09-25 16:24:19 +03:00
_animator.SetBool("IsPress", false);
}
2023-09-25 16:24:19 +03:00
if (!_clickAnimation)
return;
2023-07-04 21:32:21 +03:00
if (_moveVerticalCoroutine != null)
StopCoroutine(_moveVerticalCoroutine);
_rect.anchoredPosition = _defaultPosition + (-_moveVerticalAmount * Vector2.up);
_moveVerticalCoroutine = StartCoroutine(MoveVerticalCoroutine(_moveVerticalAmount));
}
IEnumerator MoveVerticalCoroutine(int moveY)
{
float elapsedTime = 0f;
2023-07-06 16:01:26 +03:00
float waitTime = 0.05f;
2023-07-04 21:32:21 +03:00
Vector2 currentPosition = _rect.anchoredPosition;
Vector2 endPosition = _rect.anchoredPosition + (moveY * Vector2.up);
while (elapsedTime < waitTime)
{
_rect.anchoredPosition = Vector2.Lerp(currentPosition, endPosition, (elapsedTime / waitTime));
elapsedTime += Time.deltaTime;
yield return null;
}
_rect.anchoredPosition = endPosition;
yield return null;
}
2023-08-28 02:38:39 +03:00
public virtual void OnDeselect(BaseEventData eventData)
{
2023-10-03 01:56:01 +03:00
_onDeselected?.Invoke();
2023-09-24 23:49:04 +02:00
_animator.SetBool("IsHover", false);
2023-08-28 02:38:39 +03:00
}
2022-12-14 18:33:44 +02:00
}