using System;
using System.Collections.Generic;
using UnityEngine.Scripting.APIUpdating;
namespace UnityEngine.U2D.IK
{
///
/// Component to manager 2D IK Solvers.
///
[DefaultExecutionOrder(-2)]
[MovedFrom("UnityEngine.Experimental.U2D.IK")]
[ExecuteInEditMode]
public partial class IKManager2D : MonoBehaviour
{
#if UNITY_EDITOR
internal static event System.Action onEnabledEditor;
internal static event System.Action onDisabledEditor;
#endif
[SerializeField]
List m_Solvers = new List();
[SerializeField]
[Range(0f, 1f)]
float m_Weight = 1f;
///
/// Get and Set the weight for solvers.
///
public float weight
{
get => m_Weight;
set => m_Weight = Mathf.Clamp01(value);
}
///
/// Get the Solvers that are managed by this manager.
///
public List solvers => m_Solvers;
void OnValidate()
{
m_Weight = Mathf.Clamp01(m_Weight);
OnEditorDataValidate();
}
void Reset()
{
FindChildSolvers();
OnEditorDataValidate();
}
void FindChildSolvers()
{
m_Solvers.Clear();
var solvers = new List();
transform.GetComponentsInChildren(true, solvers);
foreach (var solver in solvers)
{
if (solver.GetComponentInParent() == this)
AddSolver(solver);
}
}
///
/// Add Solver to the manager.
///
/// Solver to add.
public void AddSolver(Solver2D solver)
{
if (!m_Solvers.Contains(solver))
{
m_Solvers.Add(solver);
AddSolverEditorData();
}
}
///
/// Remove Solver from the manager.
///
/// Solver to remove.
public void RemoveSolver(Solver2D solver)
{
RemoveSolverEditorData(solver);
m_Solvers.Remove(solver);
}
///
/// Updates the Solvers in this manager.
///
public void UpdateManager()
{
foreach (var solver in m_Solvers)
{
if (solver == null || !solver.isActiveAndEnabled)
continue;
if (!solver.isValid)
solver.Initialize();
solver.UpdateIK(weight);
}
}
private void LateUpdate()
{
UpdateManager();
}
#if UNITY_EDITOR
void OnEnable()
{
onEnabledEditor?.Invoke(this);
}
void OnDisable()
{
onDisabledEditor?.Invoke(this);
}
internal static Events.UnityEvent onDrawGizmos = new Events.UnityEvent();
void OnDrawGizmos()
{
onDrawGizmos.Invoke();
}
#endif
}
}