// 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.Targets; using Doozy.Runtime.Reactor.Ticker; using UnityEngine; namespace Doozy.Runtime.UIManager.Animators { /// /// Specialized animator component used to animate a set of Sprites for a Reactor Sprite Target /// by listening to a UIContainer (controller) show/hide commands. /// [AddComponentMenu("Doozy/UI/Containers/Animators/UIContainer Sprite Animator")] public class UIContainerSpriteAnimator : BaseUIContainerAnimator { [SerializeField] private ReactorSpriteTarget SpriteTarget; /// Reference to a sprite target component public ReactorSpriteTarget spriteTarget => SpriteTarget; /// Check if a sprite target is referenced or not public bool hasSpriteTarget => SpriteTarget != null; [SerializeField] private SpriteAnimation ShowAnimation; /// Container Show Animation public SpriteAnimation showAnimation => ShowAnimation ?? (ShowAnimation = new SpriteAnimation(spriteTarget)); [SerializeField] private SpriteAnimation HideAnimation; /// Container Hide Animation public SpriteAnimation hideAnimation => HideAnimation ?? (HideAnimation = new SpriteAnimation(spriteTarget)); #if UNITY_EDITOR protected override void Reset() { FindTarget(); ResetAnimation(showAnimation); ResetAnimation(hideAnimation); base.Reset(); } #endif public void FindTarget() { if (SpriteTarget != null) return; SpriteTarget = ReactorSpriteTarget.FindTarget(gameObject); UpdateSettings(); } protected override void Awake() { FindTarget(); 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(); /// Update the animations settings (if a spriteTarget is referenced) public override void UpdateSettings() { if(spriteTarget == null) return; showAnimation.SetTarget(spriteTarget); hideAnimation.SetTarget(spriteTarget); } /// 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 (spriteTarget == null) return; spriteTarget.sprite = showAnimation.sprites[showAnimation.startFrame]; #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(SpriteAnimation target) { target.animation.Reset(); target.animation.enabled = false; target.animation.settings.duration = 1f; } } }