103 lines
2.8 KiB
C#
103 lines
2.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace SepCore.UI
|
|
{
|
|
public class CommonButton : Button
|
|
{
|
|
[SerializeField] private Image _background = null;
|
|
|
|
[SerializeField] private UnityEvent _onPointerEnterAction = new();
|
|
|
|
[SerializeField] private UnityEvent _onPointerExitAction = new();
|
|
|
|
public override void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
base.OnPointerEnter(eventData);
|
|
if (eventData.button != PointerEventData.InputButton.Left || !IsActive() || !IsInteractable())
|
|
{
|
|
return;
|
|
}
|
|
|
|
EnterHoverState();
|
|
}
|
|
|
|
public override void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
base.OnPointerExit(eventData);
|
|
if (eventData.button != PointerEventData.InputButton.Left)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ExitHoverState();
|
|
}
|
|
|
|
public override void OnSelect(BaseEventData eventData)
|
|
{
|
|
base.OnSelect(eventData);
|
|
if (!IsActive() || !IsInteractable())
|
|
{
|
|
return;
|
|
}
|
|
|
|
EnterHoverState();
|
|
}
|
|
|
|
public override void OnDeselect(BaseEventData eventData)
|
|
{
|
|
base.OnDeselect(eventData);
|
|
ExitHoverState();
|
|
}
|
|
|
|
protected override void DoStateTransition(SelectionState state, bool instant)
|
|
{
|
|
base.DoStateTransition(state, instant);
|
|
if (transition == Transition.SpriteSwap)
|
|
{
|
|
_background.canvasRenderer.SetColor(
|
|
state == SelectionState.Highlighted || state == SelectionState.Selected
|
|
? new Color(1f, 0.96f, 0.8f, 1f)
|
|
: Color.white);
|
|
}
|
|
}
|
|
|
|
private void EnterHoverState()
|
|
{
|
|
if (EventSystem.current.currentSelectedGameObject != gameObject)
|
|
{
|
|
EventSystem.current.SetSelectedGameObject(this.gameObject);
|
|
}
|
|
|
|
_onPointerEnterAction.Invoke();
|
|
}
|
|
|
|
private void ExitHoverState()
|
|
{
|
|
_onPointerExitAction.Invoke();
|
|
}
|
|
|
|
public void RegisterPointerEnterAction(UnityAction action)
|
|
{
|
|
_onPointerEnterAction.AddListener(action);
|
|
}
|
|
|
|
public void RegisterPointerExitAction(UnityAction action)
|
|
{
|
|
_onPointerExitAction.AddListener(action);
|
|
}
|
|
|
|
public void UnregisterPointerEnterAction(UnityAction action)
|
|
{
|
|
_onPointerEnterAction.RemoveListener(action);
|
|
}
|
|
|
|
public void UnregisterPointerExitAction(UnityAction action)
|
|
{
|
|
_onPointerExitAction.RemoveListener(action);
|
|
}
|
|
}
|
|
}
|