using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
namespace RR.Common.UI
{
///
/// Меняет цвет TMP-текста внутри кнопки при фокусе и наведении.
///
public class ButtonTextFocusColor : MonoBehaviour, ISelectHandler, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler
{
[SerializeField] private TextMeshProUGUI targetText;
[SerializeField] private Color normalColor = new(0f, 0.29411766f, 0.1882353f, 1f);
[SerializeField] private Color focusedColor = Color.white;
private bool _isPointerInside;
private bool _isSelected;
private void Awake()
{
ApplyCurrentState();
}
public void OnSelect(BaseEventData eventData)
{
_isSelected = true;
ApplyCurrentState();
}
public void OnDeselect(BaseEventData eventData)
{
_isSelected = false;
ApplyCurrentState();
}
public void OnPointerEnter(PointerEventData eventData)
{
_isPointerInside = true;
ApplyCurrentState();
}
public void OnPointerExit(PointerEventData eventData)
{
_isPointerInside = false;
ApplyCurrentState();
}
private void ApplyCurrentState()
{
if (targetText == null)
return;
targetText.color = (_isSelected || _isPointerInside) ? focusedColor : normalColor;
}
#if UNITY_EDITOR
private void Reset()
{
targetText = GetComponentInChildren(true);
}
#endif
}
}