107 lines
2.9 KiB
C#

using Cysharp.Threading.Tasks;
using DG.Tweening;
using System.Threading;
using UnityEngine;
public abstract class InfoPanel : ClosablePanel
{
[Header("Панель")]
[SerializeField] protected RectTransform root;
[SerializeField] protected float yOffset = 700;
[SerializeField] protected float ratioHW = 4.5f;
[Header("Заголовок")]
[SerializeField] protected RectTransform titleSign;
[SerializeField] protected float titleOpenY, titleCloseY, titleDuration;
[Header("Кнопка")]
[SerializeField] protected RectTransform button;
[SerializeField] protected float buttonOpenX, buttonCloseX, buttonDuration;
[SerializeField] protected RectTransform info;
[SerializeField] protected float infoOpenX, infoCloseX, infoDuration;
[Header("Общие настройки")]
[SerializeField] protected float timeOffset = 0.1f;
protected CancellationTokenSource cts;
protected Sequence openSequence;
protected Sequence closeSequence;
public override void Enable(int direction, float delay)
{
CancelAndDispose();
Vector2 startPos = GetStartPosition(direction);
root.anchoredPosition = startPos;
InitializeBeforeOpen();
openSequence?.Kill();
openSequence = GetOpenSequence(delay);
PlaySequence(openSequence);
}
public override void Disable(int direction)
{
CancelAndDispose();
Vector2 endPos = GetEndPosition(direction);
closeSequence?.Kill();
closeSequence = GetCloseSequence(endPos);
PlaySequence(closeSequence);
CleanupAfterClose();
}
public override void Interact()
{
//throw new System.NotImplementedException();
}
protected virtual void InitializeBeforeOpen() { }
protected virtual void CleanupAfterClose() { }
protected abstract Sequence GetOpenSequence(float delay);
protected abstract Sequence GetCloseSequence(Vector2 position);
protected virtual Vector2 GetStartPosition(int direction)
{
float y = direction > 0 ? -yOffset : yOffset;
float x = y / ratioHW;
return new Vector2(x, y);
}
protected virtual Vector2 GetEndPosition(int direction)
{
float y = direction < 0 ? -yOffset : yOffset;
float x = y / ratioHW;
return new Vector2(x, y);
}
protected void CancelAndDispose()
{
cts?.Cancel();
cts?.Dispose();
cts = new CancellationTokenSource();
}
protected void PlaySequence(Sequence sequence)
{
if (sequence == null) return;
sequence.PlayForward();
sequence.ToUniTask(cancellationToken: cts.Token).Forget();
}
protected void KillSequences()
{
openSequence?.Kill();
closeSequence?.Kill();
}
private void OnApplicationQuit()
{
cts?.Cancel();
cts?.Dispose();
KillSequences();
}
}