Untitled
unknown
plain_text
a year ago
14 kB
7
Indexable
Never
using System; using DarkTonic.MasterAudio; using DG.Tweening; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class UITabFramelessWithIcon : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler { [SerializeField] private Image myIconImage; [SerializeField] private Image myFrameImage; [SerializeField] private Image myBgImage; [SerializeField] private Image extendedFrame; [SerializeField] private TextMeshProUGUI tmpButtonName; [SerializeField] public RectTransform myRectTrasnform; [SerializeField] private Material hoverMaterial; private string hoverSound1 = "TabHover"; private string clickSound1 = "TabClick"; private string clickCanceledSound = "CancelledClick"; private string clickDenied1 = "UI_click_denied"; private string hoverSound2 = "UI_hover_alternate"; private string clickSound2 = "UI_click_alternate"; private float timeOfClick; private float graceTimer = 0.55f; private Sequence currSeq; private Tweener deniedTweener; private static readonly float hoverEndValue = 1.2f; private static readonly float spacingHoverEndValue = 4f; private static readonly float spacingClickValue = -3f; private static readonly float hoverAnimationDuration = 0.15f; private static readonly float spacingLetterHoverAnimationDuration = 0.18f; private static readonly float frameDesaturatedSaturationValue = 0.82f; // 0.25f private static readonly float frameDesaturatedBrightnessValue = 0.86f; // 0.86f private static readonly float bgDesaturatedSaturationValue = 0.55f; private static readonly float bgDesaturatedBrightnessValue = 0.9f; private static readonly float iconDesaturatedSaturationValue = 0.55f; private static readonly float iconDesaturatedBrightnessValue = 1f; private static readonly float clickSaturaitonValue = 1.75f; private static readonly float clickHueShiftValue = 90; private static readonly int HsvShift = Shader.PropertyToID("_HsvShift"); private static readonly int HsvSaturation = Shader.PropertyToID("_HsvSaturation"); private static readonly int HsvBright = Shader.PropertyToID("_HsvBright"); private bool wasInitialized; private bool isMouseOverThis; private bool isSelectedAsPrime; public delegate void MyDelegateForButtonPointerDown(UITabFramelessWithIcon disIcon); public event MyDelegateForButtonPointerDown PointerDown; public delegate void MyDelegateForButtonPointerUp(UITabFramelessWithIcon disIcon); public event MyDelegateForButtonPointerUp PointerUp; public delegate void MyDelegateForButtonPointerEnter(UITabFramelessWithIcon disIcon); public event MyDelegateForButtonPointerEnter ButtonPointerEnter; public delegate void MyDelegateForButtonPointerExit(UITabFramelessWithIcon disIcon); public event MyDelegateForButtonPointerExit ButtonPointerExit; private void CustomInitialization() { if (wasInitialized) { return; } wasInitialized = true; graceTimer = GameSettingManager.settingProfile.controlsSettings.graceClickTimer; hoverMaterial.SetFloat(HsvShift,0); hoverMaterial.SetFloat(HsvSaturation,1); hoverMaterial.SetFloat(HsvBright,1); myBgImage.material = new Material(hoverMaterial); myFrameImage.material = new Material(hoverMaterial); extendedFrame.material = myFrameImage.material; myIconImage.material = new Material(hoverMaterial); } public void SetAsDeselected(bool withAnimation) { CustomInitialization(); isSelectedAsPrime = false; extendedFrame.enabled = false; if (withAnimation) { ProcessPointerExit(); } else { currSeq.Kill(); myBgImage.materialForRendering.SetFloat(HsvSaturation, bgDesaturatedSaturationValue); myBgImage.materialForRendering.SetFloat(HsvBright, bgDesaturatedBrightnessValue); myBgImage.materialForRendering.SetFloat(HsvShift, 0); myFrameImage.materialForRendering.SetFloat(HsvSaturation, frameDesaturatedSaturationValue); myFrameImage.materialForRendering.SetFloat(HsvBright, frameDesaturatedBrightnessValue); extendedFrame.materialForRendering.SetFloat(HsvSaturation, frameDesaturatedSaturationValue); extendedFrame.materialForRendering.SetFloat(HsvBright, frameDesaturatedBrightnessValue); myIconImage.materialForRendering.SetFloat(HsvSaturation, 1); myIconImage.materialForRendering.SetFloat(HsvBright, 1); } } public void SetAsSelected() { CustomInitialization(); isSelectedAsPrime = true; extendedFrame.enabled = true; myBgImage.materialForRendering.SetFloat(HsvShift, 0); if (isMouseOverThis) { return; } currSeq.Kill(); myBgImage.materialForRendering.SetFloat(HsvSaturation, 1); myBgImage.materialForRendering.SetFloat(HsvBright, 1); myFrameImage.materialForRendering.SetFloat(HsvSaturation, 1); myFrameImage.materialForRendering.SetFloat(HsvBright, 1); extendedFrame.materialForRendering.SetFloat(HsvSaturation, 1); extendedFrame.materialForRendering.SetFloat(HsvBright, 1); myIconImage.materialForRendering.SetFloat(HsvSaturation, hoverEndValue); myIconImage.materialForRendering.SetFloat(HsvBright, hoverEndValue); } public void ProcessHover() { isMouseOverThis = true; DoHoverAnimation(true); PlayHoverSound(); } private void DoHoverAnimation(bool extendetSpacing) { currSeq.Kill(); var spriteAnimationTime = hoverAnimationDuration; myBgImage.materialForRendering.SetFloat(HsvShift, 0); var iconTweenOne = myIconImage.materialForRendering.DOFloat(hoverEndValue, HsvBright, spriteAnimationTime) .SetEase(Ease.OutQuad); var iconTweenTwo = myIconImage.materialForRendering.DOFloat(hoverEndValue, HsvSaturation, spriteAnimationTime) .SetEase(Ease.OutQuad); var tweenBgOne = myBgImage.materialForRendering.DOFloat(hoverEndValue, HsvBright, spriteAnimationTime) .SetEase(Ease.OutQuad); var tweenBgTwo = myBgImage.materialForRendering.DOFloat(hoverEndValue, HsvSaturation, spriteAnimationTime) .SetEase(Ease.OutQuad); var tweenFrameOne = myFrameImage.materialForRendering.DOFloat(1, HsvBright, spriteAnimationTime) .SetEase(Ease.OutQuad); var tweenFrameTwo = myFrameImage.materialForRendering.DOFloat(1, HsvSaturation, spriteAnimationTime) .SetEase(Ease.OutQuad); float spacingValue = 1; if (extendetSpacing) { spacingValue = spacingHoverEndValue; } var tweenLetterSpacing = DOTween.To(() => tmpButtonName.characterSpacing, x => tmpButtonName.characterSpacing = x, spacingValue, spacingLetterHoverAnimationDuration); var seq = DOTween.Sequence(); seq.Append(tweenLetterSpacing); seq.Insert(0, iconTweenOne); seq.Insert(0, iconTweenTwo); seq.Insert(0, tweenBgOne); seq.Insert(0, tweenBgTwo); seq.Insert(0, tweenFrameOne); seq.Insert(0, tweenFrameTwo); currSeq = seq; } public void ProcessPointerExit() { isMouseOverThis = false; currSeq.Kill(); var seq = DOTween.Sequence(); var spriteAnimationTime = hoverAnimationDuration / 2; myBgImage.materialForRendering.SetFloat(HsvShift, 0); if (isSelectedAsPrime) { var iconTweenOne = myIconImage.materialForRendering.DOFloat(hoverEndValue, HsvBright, spriteAnimationTime) .SetEase(Ease.OutQuad); var iconTweenTwo = myIconImage.materialForRendering .DOFloat(hoverEndValue, HsvSaturation, spriteAnimationTime).SetEase(Ease.OutQuad); var tweenBgOne = myBgImage.materialForRendering.DOFloat(1, HsvBright, spriteAnimationTime) .SetEase(Ease.OutQuad); var tweenBgTwo = myBgImage.materialForRendering.DOFloat(1, HsvSaturation, spriteAnimationTime) .SetEase(Ease.OutQuad); var tweenFrameOne = myFrameImage.materialForRendering.DOFloat(1, HsvBright, spriteAnimationTime) .SetEase(Ease.OutQuad); var tweenFrameTwo = myFrameImage.materialForRendering.DOFloat(1, HsvSaturation, spriteAnimationTime) .SetEase(Ease.OutQuad); seq.Insert(0, iconTweenOne); seq.Insert(0, iconTweenTwo); seq.Insert(0, tweenBgOne); seq.Insert(0, tweenBgTwo); seq.Insert(0, tweenFrameOne); seq.Insert(0, tweenFrameTwo); } else { var iconTweenOne = myIconImage.materialForRendering.DOFloat(1, HsvBright, spriteAnimationTime) .SetEase(Ease.OutQuad); var iconTweenTwo = myIconImage.materialForRendering.DOFloat(1, HsvSaturation, spriteAnimationTime) .SetEase(Ease.OutQuad); var tweenBgOne = myBgImage.materialForRendering .DOFloat(bgDesaturatedBrightnessValue, HsvBright, spriteAnimationTime).SetEase(Ease.OutQuad); var tweenBgTwo = myBgImage.materialForRendering .DOFloat(bgDesaturatedSaturationValue, HsvSaturation, spriteAnimationTime).SetEase(Ease.OutQuad); var tweenFrameOne = myFrameImage.materialForRendering .DOFloat(frameDesaturatedBrightnessValue, HsvBright, spriteAnimationTime).SetEase(Ease.OutQuad); var tweenFrameTwo = myFrameImage.materialForRendering .DOFloat(frameDesaturatedSaturationValue, HsvSaturation, spriteAnimationTime).SetEase(Ease.OutQuad); seq.Insert(0, iconTweenOne); seq.Insert(0, iconTweenTwo); seq.Insert(0, tweenBgOne); seq.Insert(0, tweenBgTwo); seq.Insert(0, tweenFrameOne); seq.Insert(0, tweenFrameTwo); } var tweenLetterSpacing = DOTween.To(() => tmpButtonName.characterSpacing, x => tmpButtonName.characterSpacing = x, 0, spriteAnimationTime); seq.Insert(0, tweenLetterSpacing); currSeq = seq; } public void ProcessPointerDown() { currSeq.Kill(); var seq = DOTween.Sequence(); var spriteAnimationTime = hoverAnimationDuration *0.22f; myBgImage.materialForRendering.SetFloat(HsvShift, clickHueShiftValue); var iconTweenOne = myIconImage.materialForRendering.DOFloat(clickSaturaitonValue, HsvBright, spriteAnimationTime) .SetEase(Ease.OutQuad); var iconTweenTwo = myIconImage.materialForRendering.DOFloat(clickSaturaitonValue, HsvSaturation, spriteAnimationTime) .SetEase(Ease.OutQuad); var tweenBgOne = myBgImage.materialForRendering .DOFloat(clickSaturaitonValue, HsvBright, spriteAnimationTime).SetEase(Ease.OutQuad); var tweenBgTwo = myBgImage.materialForRendering .DOFloat(clickSaturaitonValue, HsvSaturation, spriteAnimationTime).SetEase(Ease.OutQuad); var tweenFrameOne = myFrameImage.materialForRendering .DOFloat(1, HsvBright, spriteAnimationTime).SetEase(Ease.OutQuad); var tweenFrameTwo = myFrameImage.materialForRendering .DOFloat(1, HsvSaturation, spriteAnimationTime).SetEase(Ease.OutQuad); seq.Insert(0, iconTweenOne); seq.Insert(0, iconTweenTwo); seq.Insert(0, tweenBgOne); seq.Insert(0, tweenBgTwo); seq.Insert(0, tweenFrameOne); seq.Insert(0, tweenFrameTwo); var tweenLetterSpacing = DOTween.To(() => tmpButtonName.characterSpacing, x => tmpButtonName.characterSpacing = x, spacingClickValue, spriteAnimationTime); seq.Insert(0,tweenLetterSpacing); currSeq = seq; PlayClickSound(); } public void ProcessPointerUp() { myBgImage.materialForRendering.SetFloat(HsvShift, clickHueShiftValue); if (isMouseOverThis) { DoHoverAnimation(true); } else { } } public void PlayHoverSound() { MasterAudio.PlaySound(hoverSound1); } public void PlayClickSound() { MasterAudio.PlaySound(clickSound1); } public void PlayClickCanceledSound() { MasterAudio.PlaySound(clickCanceledSound); } public void OnPointerEnter(PointerEventData eventData) { ButtonPointerEnter?.Invoke(this); } public void OnPointerExit(PointerEventData eventData) { ButtonPointerExit?.Invoke(this); } public void OnPointerDown(PointerEventData eventData) { if (eventData.button == PointerEventData.InputButton.Left) { timeOfClick = Time.unscaledTime; PointerDown?.Invoke(this); } } public void OnPointerUp(PointerEventData eventData) { if (eventData.button == PointerEventData.InputButton.Left) { if (isMouseOverThis || timeOfClick + graceTimer > Time.unscaledTime) { PointerUp?.Invoke(this); } else { PlayClickCanceledSound(); } } } }