// 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.Common.Extensions;
using Doozy.Runtime.Common.Utils;
using Doozy.Runtime.Mody;
using Doozy.Runtime.Mody.Actions;
using Doozy.Runtime.Reactor;
using Doozy.Runtime.Reactor.Animators.Internal;
using UnityEngine;
// ReSharper disable MemberCanBePrivate.Global
namespace Doozy.Runtime.UIManager.Modules
{
/// Mody module used to control a list of Reactor animators
[AddComponentMenu("Doozy/Mody/Animator Module")]
public class AnimatorModule : ModyModule
{
#if UNITY_EDITOR
[UnityEditor.MenuItem("GameObject/Doozy/Mody/Animator Module", false, 8)]
private static void CreateComponent(UnityEditor.MenuCommand menuCommand)
{
GameObjectUtils.AddToScene("Animator Module", false, true);
}
#endif
/// Default module name
public const string k_DefaultModuleName = "Animator";
/// Construct an Animator Module with the default name
public AnimatorModule() : this(k_DefaultModuleName) {}
/// Construct an Animator Module with the given name
/// Module name
public AnimatorModule(string moduleName) : base(moduleName.IsNullOrEmpty() ? k_DefaultModuleName : moduleName) {}
/// All the animators controlled by this module
public List Animators = new List();
/// Simple action to Play forward for all animations
public SimpleModyAction PlayForward;
/// Simple action to Play in reverse for all animations
public SimpleModyAction PlayReverse;
/// Simple action to Stop playing for all animations (does not call Finish)
public SimpleModyAction Stop;
/// Simple action to Finish playing for all animations(also calls Stop)
public SimpleModyAction Finish;
/// Simple action to Reverse all playing animations
public SimpleModyAction Reverse;
/// Simple action to Rewind all animations
public SimpleModyAction Rewind;
/// Simple action to Pause all playing animations
public SimpleModyAction Pause;
/// Simple action to Resume all paused animations
public SimpleModyAction Resume;
/// Float action to set the progress of all animations to a given value
public FloatModyAction SetProgressAt;
/// Simple action to set the progress of all animations to zero
public SimpleModyAction SetProgressAtZero;
/// Simple action to set the progress of all animations to one
public SimpleModyAction SetProgressAtOne;
/// Float action to Play all the animations to a given progress value (from the current value)
public FloatModyAction PlayToProgress;
/// Float action to Play all the animations from a given progress value (to the current value)
public FloatModyAction PlayFromProgress;
/// Simple action to Update the values for all the animations
public SimpleModyAction UpdateValues;
/// Initialize the actions
protected override void SetupActions()
{
this.AddAction(PlayForward ??= new SimpleModyAction(this, nameof(PlayForward), ExecutePlayForward));
this.AddAction(PlayReverse ??= new SimpleModyAction(this, nameof(PlayReverse), ExecutePlayReverse));
this.AddAction(Stop ??= new SimpleModyAction(this, nameof(Stop), ExecuteStop));
this.AddAction(Finish ??= new SimpleModyAction(this, nameof(Finish), ExecuteFinish));
this.AddAction(Reverse ??= new SimpleModyAction(this, nameof(Reverse), ExecuteReverse));
this.AddAction(Rewind ??= new SimpleModyAction(this, nameof(Rewind), ExecuteRewind));
this.AddAction(Pause ??= new SimpleModyAction(this, nameof(Pause), ExecutePause));
this.AddAction(Resume ??= new SimpleModyAction(this, nameof(Resume), ExecuteResume));
this.AddAction(SetProgressAt ??= new FloatModyAction(this, nameof(SetProgressAt), ExecuteSetProgressAt));
this.AddAction(SetProgressAtZero ??= new SimpleModyAction(this, nameof(SetProgressAtZero), ExecuteSetProgressAtZero));
this.AddAction(SetProgressAtOne ??= new SimpleModyAction(this, nameof(SetProgressAtOne), ExecuteSetProgressAtOne));
this.AddAction(PlayToProgress ??= new FloatModyAction(this, nameof(PlayToProgress), ExecutePlayToProgress));
this.AddAction(PlayFromProgress ??= new FloatModyAction(this, nameof(PlayFromProgress), ExecutePlayFromProgress));
this.AddAction(UpdateValues ??= new SimpleModyAction(this, nameof(UpdateValues), ExecuteUpdateValues));
}
/// Remove any null animator references
public void CleanAnimatorsList()
{
for (int i = Animators.Count - 1; i >= 0; i--)
if (Animators[i] == null)
Animators.RemoveAt(i);
}
/// Execute Play forward for all animations
public void ExecutePlayForward()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.Play(PlayDirection.Forward);
}
/// Execute Play in reverse for all animations
public void ExecutePlayReverse()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.Play(PlayDirection.Reverse);
}
/// Execute Stop for all animations
public void ExecuteStop()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.Stop();
}
/// Execute Finish for all animations
public void ExecuteFinish()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.Finish();
}
/// Execute Reverse for all animations
public void ExecuteReverse()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.Reverse();
}
/// Execute Rewind for all animations
public void ExecuteRewind()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.Rewind();
}
/// Execute Pause for all animations
public void ExecutePause()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.Pause();
}
/// Execute Resume for all animations
public void ExecuteResume()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.Resume();
}
/// Execute SetProgressAt the given value for all animations
public void ExecuteSetProgressAt(float value)
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.SetProgressAt(value);
}
/// Execute SetProgressAtZero for all animations
public void ExecuteSetProgressAtZero()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.SetProgressAtZero();
}
/// Execute SetProgressAtOne for all animations
public void ExecuteSetProgressAtOne()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.SetProgressAtOne();
}
/// Execute PlayToProgress to the given value for all animations
public void ExecutePlayToProgress(float value)
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.PlayToProgress(value);
}
/// Execute PlayFromProgress from the given value for all animations
public void ExecutePlayFromProgress(float value)
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.PlayFromProgress(value);
}
/// Execute UpdateValues for all animations
public void ExecuteUpdateValues()
{
CleanAnimatorsList();
foreach (ReactorAnimator animator in Animators)
animator.UpdateValues();
}
}
}