// 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;
using UnityEngine.Events;
namespace Doozy.Runtime.UIManager.Animators
{
///
/// Specialized animator component used to animate an int value
/// by listening to a UIToggle (controller) onToggleValueChangedCallback
///
[AddComponentMenu("Doozy/UI/Components/Animators/UIToggle Int Animator")]
public class UIToggleIntAnimator : BaseUIToggleAnimator
{
/// 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 OnAnimation;
/// Toggle On Animation
public IntAnimation onAnimation => OnAnimation ?? (OnAnimation = new IntAnimation(ValueTarget));
[SerializeField] private IntAnimation OffAnimation;
/// Toggle Off Animation
public IntAnimation offAnimation => OffAnimation ?? (OffAnimation = new IntAnimation(ValueTarget));
protected override bool onAnimationIsActive => onAnimation.isActive;
protected override bool offAnimationIsActive => offAnimation.isActive;
protected override UnityAction playOnAnimation => () => onAnimation.Play();
protected override UnityAction playOffAnimation => () => offAnimation.Play();
protected override UnityAction reverseOnAnimation => () => onAnimation.Reverse();
protected override UnityAction reverseOffAnimation => () => offAnimation.Reverse();
protected override UnityAction instantPlayOnAnimation => () => onAnimation.SetProgressAtOne();
protected override UnityAction instantPlayOffAnimation => () => offAnimation.SetProgressAtOne();
protected override UnityAction stopOnAnimation => () => onAnimation.Stop();
protected override UnityAction stopOffAnimation => () => offAnimation.Stop();
protected override UnityAction addResetToOnStateCallback => () => offAnimation.OnStopCallback.AddListener(ResetToOnState);
protected override UnityAction removeResetToOnStateCallback => () => offAnimation.OnStopCallback.RemoveListener(ResetToOnState);
protected override UnityAction addResetToOffStateCallback => () => onAnimation.OnStopCallback.AddListener(ResetToOffState);
protected override UnityAction removeResetToOffStateCallback => () => onAnimation.OnStopCallback.RemoveListener(ResetToOffState);
#if UNITY_EDITOR
protected override void Reset()
{
ValueTarget ??= new ReflectedInt();
base.Reset();
ResetAnimation(onAnimation);
onAnimation.animation.fromCustomValue = 0;
onAnimation.animation.toCustomValue = 100;
ResetAnimation(offAnimation);
offAnimation.animation.fromCustomValue = 100;
offAnimation.animation.toCustomValue = 0;
}
#endif
protected override void Awake()
{
UpdateSettings();
base.Awake();
}
protected override void OnDestroy()
{
base.OnDestroy();
OnAnimation?.Recycle();
OffAnimation?.Recycle();
}
/// 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)
{
onAnimation.SetTarget(reflectedValue);
offAnimation.SetTarget(reflectedValue);
}
/// Refresh the set target and, if the animation is playing, update the calculated values
public override void UpdateSettings()
{
SetTarget(ValueTarget);
if (onAnimation.isPlaying) onAnimation.UpdateValues();
if (offAnimation.isPlaying) offAnimation.UpdateValues();
}
/// Stop all animations
public override void StopAllReactions()
{
onAnimation.Stop();
offAnimation.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 (onAnimation.isActive) onAnimation.Stop();
if (offAnimation.isActive) offAnimation.Stop();
onAnimation.ResetToStartValues(forced);
offAnimation.ResetToStartValues(forced);
if (ValueTarget == null || !ValueTarget.IsValid())
return;
ValueTarget.SetValue(onAnimation.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());
onAnimation.animation.SetHeartbeat(list[0]);
offAnimation.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;
}
}
}