83 lines
2.2 KiB
C#
83 lines
2.2 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
|
|
public class LetterSlot : MonoBehaviour
|
|
{
|
|
[SerializeField] private TextMeshProUGUI _label;
|
|
|
|
[Header("Анимация выбора")]
|
|
[SerializeField] private float _normalScale = 1f;
|
|
[SerializeField] private float _selectedScale = 1.1f;
|
|
[SerializeField] private Color _normalColor;
|
|
[SerializeField] private Color _selectedColor;
|
|
[SerializeField] private float _animationDuration = 0.2f;
|
|
|
|
private int _symbolIndex;
|
|
private string _alphabet;
|
|
private Sequence _animation;
|
|
|
|
public char Symbol => _alphabet[_symbolIndex];
|
|
|
|
private void OnDestroy() => StopAnimation();
|
|
|
|
public void Init(char symbol, string alphabet)
|
|
{
|
|
_alphabet = alphabet;
|
|
_symbolIndex = _alphabet.IndexOf(symbol);
|
|
if( _symbolIndex < 0 ) _symbolIndex = 0;
|
|
|
|
SetSymbol(_symbolIndex);
|
|
SetSelected(false, instant: true);
|
|
}
|
|
|
|
public void SetSelected(bool selected, bool instant = false)
|
|
{
|
|
StopAnimation();
|
|
float targetScale = selected ? _selectedScale : _normalScale;
|
|
Color targetColor = selected ? _selectedColor : _normalColor;
|
|
|
|
if (instant)
|
|
{
|
|
transform.localScale = Vector3.one * targetScale;
|
|
_label.color = targetColor;
|
|
return;
|
|
}
|
|
|
|
_animation = DOTween.Sequence();
|
|
_animation.Join(transform.DOScale(targetScale, _animationDuration));
|
|
_animation.Join(_label.DOColor(targetColor, _animationDuration));
|
|
|
|
_animation.PlayForward();
|
|
}
|
|
|
|
public void ChangeSymbol(int direction)
|
|
{
|
|
_symbolIndex = (_symbolIndex + direction + _alphabet.Length) % _alphabet.Length;
|
|
SetSymbol(_symbolIndex);
|
|
}
|
|
|
|
public void SetSymbol(int index)
|
|
{
|
|
_symbolIndex = index;
|
|
_label.text = Symbol.ToString();
|
|
}
|
|
|
|
public void SetSymbol(char symbol)
|
|
{
|
|
int index = _alphabet.IndexOf(symbol);
|
|
if (index < 0)
|
|
SetSymbol(0);
|
|
else
|
|
SetSymbol(index);
|
|
}
|
|
|
|
private void StopAnimation()
|
|
{
|
|
if (_animation != null)
|
|
{
|
|
_animation.Kill();
|
|
_animation = null;
|
|
}
|
|
}
|
|
} |