// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System.Collections.Generic; using Doozy.Runtime.Reactor; using Doozy.Runtime.Reactor.Animations; using Doozy.Runtime.Reactor.Reflection; using Doozy.Runtime.Reactor.Ticker; using UnityEngine; namespace Doozy.Runtime.UIManager.Animators { /// /// Specialized animator component used to animate an int value /// by listening to a UIContainer (controller) show/hide commands. /// [AddComponentMenu("Doozy/UI/Containers/Animators/UIContainer Int Animator")] public class UIContainerIntAnimator : BaseUIContainerAnimator { /// Int value target accessed via reflection public ReflectedInt ValueTarget = new ReflectedInt(); /// Check if the value target is set up correctly public bool isValid => ValueTarget.IsValid(); [SerializeField] private IntAnimation ShowAnimation; /// Container Show Animation public IntAnimation showAnimation => ShowAnimation ?? (ShowAnimation = new IntAnimation(ValueTarget)); [SerializeField] private IntAnimation HideAnimation; /// Container Hide Animation public IntAnimation hideAnimation => HideAnimation ?? (HideAnimation = new IntAnimation(ValueTarget)); #if UNITY_EDITOR protected override void Reset() { ValueTarget ??= new ReflectedInt(); base.Reset(); ResetAnimation(showAnimation); ResetAnimation(hideAnimation); } #endif protected override void Awake() { UpdateSettings(); base.Awake(); } protected override void OnDestroy() { base.OnDestroy(); ShowAnimation.Recycle(); HideAnimation.Recycle(); } /// Connect to Controller protected override void ConnectToController() { base.ConnectToController(); if (!controller) return; controller.showReactions.Add(showAnimation.animation); controller.hideReactions.Add(hideAnimation.animation); } /// Disconnect from Controller protected override void DisconnectFromController() { base.DisconnectFromController(); if (!controller) return; controller.showReactions.Remove(showAnimation.animation); controller.hideReactions.Remove(hideAnimation.animation); } /// Play the show animation public override void Show() { if (reversingShow) { showAnimation.OnFinishCallback.RemoveListener(OnReverseShowComplete); reversingShow = false; } showAnimation.Play(PlayDirection.Forward); } /// Reverse the show animation (if playing) public override void ReverseShow() { if (showAnimation.isPlaying) { showAnimation.OnFinishCallback.AddListener(OnReverseShowComplete); showAnimation.Reverse(); reversingShow = true; return; } Hide(); } private void OnReverseShowComplete() { InstantHide(); showAnimation.OnFinishCallback.RemoveListener(OnReverseShowComplete); reversingShow = false; } /// Play the hide animation public override void Hide() { if (reversingHide) { hideAnimation.OnFinishCallback.RemoveListener(OnReverseHideComplete); reversingHide = false; } hideAnimation.Play(PlayDirection.Forward); } /// Reverse the hide animation (if playing) public override void ReverseHide() { if (hideAnimation.isPlaying) { hideAnimation.OnFinishCallback.AddListener(OnReverseHideComplete); hideAnimation.Reverse(); reversingHide = true; return; } Show(); } private void OnReverseHideComplete() { InstantShow(); hideAnimation.OnFinishCallback.RemoveListener(OnReverseHideComplete); reversingHide = false; } /// Set show animation's progress at one public override void InstantShow() => showAnimation.SetProgressAtOne(); /// Set hide animation's progress at one public override void InstantHide() => hideAnimation.SetProgressAtOne(); /// Set the value target /// Reflected value target private void SetTarget(object reflectedValue) => SetTarget(reflectedValue as ReflectedInt); /// Set the value target /// Reflected value target private void SetTarget(ReflectedInt reflectedValue) { showAnimation.SetTarget(reflectedValue); hideAnimation.SetTarget(reflectedValue); } /// Refresh the set target and, if the animation is playing, update the calculated values public override void UpdateSettings() { SetTarget(ValueTarget); if (showAnimation.isPlaying) showAnimation.UpdateValues(); if (hideAnimation.isPlaying) hideAnimation.UpdateValues(); } /// Stop all animations public override void StopAllReactions() { showAnimation.Stop(); hideAnimation.Stop(); } /// Reset all the reactions to their initial values (if the animation is enabled) /// If true, forced will ignore if the animation is enabled or not public override void ResetToStartValues(bool forced = false) { if (showAnimation.isActive) showAnimation.Stop(); if (hideAnimation.isActive) hideAnimation.Stop(); showAnimation.ResetToStartValues(forced); hideAnimation.ResetToStartValues(forced); if (ValueTarget == null || !ValueTarget.IsValid()) return; ValueTarget.SetValue(showAnimation.startValue); #if UNITY_EDITOR UnityEditor.EditorUtility.SetDirty(rectTransform); UnityEditor.SceneView.RepaintAll(); #endif } /// Set animation heartbeat public override List SetHeartbeat() { var list = new List(); for (int i = 0; i < 2; i++) list.Add(new T()); showAnimation.animation.SetHeartbeat(list[0]); hideAnimation.animation.SetHeartbeat(list[1]); return list; } private static void ResetAnimation(IntAnimation animation) { var reaction = animation.animation; reaction.Reset(); reaction.enabled = true; reaction.fromReferenceValue = ReferenceValue.CustomValue; reaction.fromCustomValue = 0; reaction.toReferenceValue = ReferenceValue.CustomValue; reaction.toCustomValue = 100; reaction.settings.duration = 0.5f; } } }