126 lines
3.8 KiB
C#
126 lines
3.8 KiB
C#
using System;
|
|
using DG.Tweening;
|
|
using SLSUtilities.General;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace SLSUtilities.UI
|
|
{
|
|
public partial class AttributeBarBase : UIElementBase
|
|
{
|
|
public Image barContainer;
|
|
public Image fillImage;
|
|
public LerpFloat fillLerp = new LerpFloat(1f, 0.2f);
|
|
public bool useLerpColor;
|
|
public LerpColor fillColor;
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
fillLerp = new LerpFloat(1f, 0.2f);
|
|
}
|
|
|
|
public void UpdateFillImage(float currentValue, float maxValue, bool isInstant = false)
|
|
{
|
|
if (maxValue < 0)
|
|
{
|
|
Debug.LogWarning("Max value is less than zero. Cannot update fill image.");
|
|
return;
|
|
}
|
|
|
|
if (currentValue <= 0)
|
|
{
|
|
fillImage.fillAmount = 0;
|
|
return;
|
|
}
|
|
|
|
if (!isInstant)
|
|
{
|
|
float targetFill = currentValue / maxValue;
|
|
fillLerp.targetValue = targetFill;
|
|
}
|
|
else
|
|
{
|
|
fillLerp.targetValue = currentValue / maxValue;
|
|
fillLerp.currentValue = currentValue / maxValue;
|
|
}
|
|
}
|
|
|
|
public void UpdateFillColor(Color color, bool isInstant = false)
|
|
{
|
|
if (useLerpColor)
|
|
{
|
|
if (!isInstant)
|
|
{
|
|
fillColor.targetValue = color;
|
|
}
|
|
else
|
|
{
|
|
fillColor.targetValue = color;
|
|
fillColor.currentValue = color;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
fillImage.color = color;
|
|
}
|
|
}
|
|
|
|
public virtual void Update()
|
|
{
|
|
fillLerp.Update(1);
|
|
fillImage.fillAmount = fillLerp.currentValue;
|
|
|
|
if (useLerpColor)
|
|
{
|
|
fillColor.Update(1);
|
|
//fillImage.color = fillColor.currentValue;
|
|
}
|
|
}
|
|
|
|
private Tweener blinkTweener;
|
|
|
|
public virtual void Blink(Color blinkColor, float duration = 0.2f)
|
|
{
|
|
/*blinkTweener?.Kill();
|
|
Color originalColor = useLerpColor ? fillColor.targetValue : fillImage.color;
|
|
blinkTweener = fillImage.DOColor(blinkColor, duration / 2).SetLoops(2, LoopType.Yoyo).OnComplete(() =>
|
|
{
|
|
fillImage.color = originalColor;
|
|
});
|
|
blinkTweener.Play();*/
|
|
}
|
|
}
|
|
|
|
public partial class AttributeBarBase
|
|
{
|
|
private Sequence fadeInSequence;
|
|
private Sequence fadeOutSequence;
|
|
|
|
public void FadeIn(float duration = 0.2f)
|
|
{
|
|
gameObject.SetActive(true);
|
|
Tweener alphaFade = canvasGroup.DOFade(1f, duration).From(0f);
|
|
Tweener containerPositionFade = barContainer.rectTransform.DOAnchorPosY(0f, duration).From(new Vector2(0, 50f));
|
|
fadeInSequence?.Kill();
|
|
fadeInSequence = DOTween.Sequence();
|
|
fadeInSequence.Append(alphaFade);
|
|
fadeInSequence.Join(containerPositionFade);
|
|
fadeInSequence.Play();
|
|
}
|
|
|
|
public void FadeOut(float duration = 0.2f)
|
|
{
|
|
Tweener alphaFade = canvasGroup.DOFade(0f, duration).From(1f);
|
|
Tweener containerPositionFade = barContainer.rectTransform.DOAnchorPosY(50f, duration).From(new Vector2(0, 0f));
|
|
fadeOutSequence?.Kill();
|
|
fadeOutSequence = DOTween.Sequence();
|
|
fadeOutSequence.Append(alphaFade);
|
|
fadeOutSequence.Join(containerPositionFade);
|
|
fadeOutSequence.OnComplete(() =>
|
|
{
|
|
gameObject.SetActive(false);
|
|
});
|
|
fadeOutSequence.Play();
|
|
}
|
|
}
|
|
} |