2020-11-04 20:01:28 +00:00
|
|
|
|
using OWML.Common;
|
|
|
|
|
using QSB.Utility;
|
|
|
|
|
using System.Collections.Generic;
|
2020-02-18 21:39:18 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
2020-02-18 11:08:08 +01:00
|
|
|
|
|
2021-04-26 14:30:21 +01:00
|
|
|
|
namespace QSB.Animation.Player
|
2020-02-18 11:08:08 +01:00
|
|
|
|
{
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public class AnimatorMirror : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private const float SmoothTime = 0.05f;
|
2020-02-18 21:39:18 +01:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
private Animator _from;
|
|
|
|
|
private Animator _to;
|
2020-02-18 11:08:08 +01:00
|
|
|
|
|
2021-11-20 19:49:50 +00:00
|
|
|
|
private readonly Dictionary<string, AnimFloatParam> _floatParams = new();
|
2020-02-18 21:39:18 +01:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public void Init(Animator from, Animator to)
|
|
|
|
|
{
|
2021-03-13 18:37:42 +00:00
|
|
|
|
if (from == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - Trying to init AnimatorMirror with null \"from\".", MessageType.Error);
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-03-13 18:37:42 +00:00
|
|
|
|
if (to == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - Trying to init AnimatorMirror with null \"to\".", MessageType.Error);
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-03-13 18:37:42 +00:00
|
|
|
|
if (to == null || from == null)
|
|
|
|
|
{
|
2021-05-08 00:10:30 +01:00
|
|
|
|
// Doing the return this way so you can see if one or both are null
|
2021-03-13 18:37:42 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
_from = from;
|
|
|
|
|
_to = to;
|
|
|
|
|
if (_from.runtimeAnimatorController == null)
|
|
|
|
|
{
|
|
|
|
|
_from.runtimeAnimatorController = _to.runtimeAnimatorController;
|
|
|
|
|
}
|
|
|
|
|
else if (_to.runtimeAnimatorController == null)
|
|
|
|
|
{
|
|
|
|
|
_to.runtimeAnimatorController = _from.runtimeAnimatorController;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
foreach (var param in _from.parameters.Where(p => p.type == AnimatorControllerParameterType.Float))
|
|
|
|
|
{
|
|
|
|
|
_floatParams.Add(param.name, new AnimFloatParam());
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-18 11:08:08 +01:00
|
|
|
|
|
2020-12-14 21:41:56 +01:00
|
|
|
|
public void Update()
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
|
|
|
|
if (_to == null || _from == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
if (_to.runtimeAnimatorController != _from.runtimeAnimatorController)
|
|
|
|
|
{
|
|
|
|
|
_to.runtimeAnimatorController = _from.runtimeAnimatorController;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
SyncParams();
|
|
|
|
|
SmoothFloats();
|
|
|
|
|
}
|
2020-02-18 11:08:08 +01:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
private void SyncParams()
|
|
|
|
|
{
|
|
|
|
|
foreach (var fromParam in _from.parameters)
|
|
|
|
|
{
|
|
|
|
|
switch (fromParam.type)
|
|
|
|
|
{
|
|
|
|
|
case AnimatorControllerParameterType.Float:
|
|
|
|
|
if (!_floatParams.ContainsKey(fromParam.name))
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Warning - Tried to sync anim float that doesn't exist in dict : {fromParam.name}", MessageType.Warning);
|
|
|
|
|
RebuildFloatParams();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
_floatParams[fromParam.name].Target = _from.GetFloat(fromParam.name);
|
|
|
|
|
break;
|
2020-12-03 08:28:05 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
case AnimatorControllerParameterType.Bool:
|
|
|
|
|
_to.SetBool(fromParam.name, _from.GetBool(fromParam.name));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-18 11:08:08 +01:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
private void SmoothFloats()
|
|
|
|
|
{
|
|
|
|
|
foreach (var floatParam in _floatParams)
|
|
|
|
|
{
|
|
|
|
|
var current = floatParam.Value.Smooth(SmoothTime);
|
|
|
|
|
_to.SetFloat(floatParam.Key, current);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-07 21:25:22 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public void RebuildFloatParams()
|
|
|
|
|
{
|
|
|
|
|
_floatParams.Clear();
|
|
|
|
|
foreach (var param in _from.parameters.Where(p => p.type == AnimatorControllerParameterType.Float))
|
|
|
|
|
{
|
|
|
|
|
_floatParams.Add(param.name, new AnimFloatParam());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-08 16:13:10 +00:00
|
|
|
|
}
|