forked from Shardstone/trail-into-darkness
52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace Jovian.PopupSystem {
|
|
public sealed class FadePopupAnimator : IPopupAnimator {
|
|
private CanvasGroup target;
|
|
private float duration;
|
|
private float elapsed;
|
|
private float startAlpha;
|
|
private float endAlpha;
|
|
private Action onComplete;
|
|
|
|
public bool IsAnimating => target != null;
|
|
|
|
public void Show(CanvasGroup canvasGroup, float duration, Action onComplete) {
|
|
target = canvasGroup;
|
|
this.duration = Mathf.Max(duration, 0.001f);
|
|
elapsed = 0f;
|
|
startAlpha = 0f;
|
|
endAlpha = 1f;
|
|
this.onComplete = onComplete;
|
|
canvasGroup.alpha = 0f;
|
|
}
|
|
|
|
public void Hide(CanvasGroup canvasGroup, float duration, Action onComplete) {
|
|
target = canvasGroup;
|
|
this.duration = Mathf.Max(duration, 0.001f);
|
|
elapsed = 0f;
|
|
startAlpha = canvasGroup.alpha;
|
|
endAlpha = 0f;
|
|
this.onComplete = onComplete;
|
|
}
|
|
|
|
public void Tick(float deltaTime) {
|
|
if(target == null) {
|
|
return;
|
|
}
|
|
|
|
elapsed += deltaTime;
|
|
var t = Mathf.Clamp01(elapsed / duration);
|
|
target.alpha = Mathf.Lerp(startAlpha, endAlpha, t);
|
|
|
|
if(t >= 1f) {
|
|
var callback = onComplete;
|
|
target = null;
|
|
onComplete = null;
|
|
callback?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
}
|