rimworld-animation-studio/Assets/Scripts/GUI/ActorKeyframeCard.cs
2022-09-21 00:40:58 -05:00

81 lines
No EOL
3.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace RimWorldAnimationStudio
{
public class ActorKeyframeCard : MonoBehaviour
{
public InputField positionXField;
public InputField positionZField;
public InputField rotationField;
public InputField headBobField;
public InputField headRotationField;
public InputField appendageRotationField;
private int lastTick = -1;
private bool isDirty = false;
// Move to anim controller
public void Update()
{
if ((Workspace.animationDef == null || AnimationController.Instance.stageTick == lastTick) && isDirty == false)
{ return; }
if (Workspace.actorID >= AnimationController.Instance.transform.childCount)
{ return; }
ActorBody actorBody = AnimationController.Instance.actorBodies.GetComponentsInChildren<ActorBody>()[Workspace.actorID];
string bodyType = actorBody.bodyType;
PawnAnimationClip clip = Workspace.animationDef.animationStages[Workspace.stageID].animationClips[Workspace.actorID];
float clipPercent = (float)(AnimationController.Instance.stageTick % clip.duration) / clip.duration;
Vector3 deltaPos = new Vector3(clip.BodyOffsetX.Evaluate(clipPercent), 0, clip.BodyOffsetZ.Evaluate(clipPercent));
deltaPos += Workspace.animationDef.actors[Workspace.actorID].bodyTypeOffset.GetOffset(bodyType);
float bodyAngle = clip.BodyAngle.Evaluate(clipPercent);
float headAngle = clip.HeadAngle.Evaluate(clipPercent);
if (bodyAngle < 0) bodyAngle = 360 - ((-1f * bodyAngle) % 360);
if (bodyAngle > 360) bodyAngle %= 360;
if (headAngle < 0) headAngle = 360 - ((-1f * headAngle) % 360);
if (headAngle > 360) headAngle %= 360;
float headBob = clip.HeadBob.Evaluate(clipPercent);
Vector3 bodyPos = new Vector3(deltaPos.x, deltaPos.z, 0);
float appendageRotation = clip.GenitalAngle.Evaluate(clipPercent);
positionXField.text = bodyPos.x.ToString("0.000");
positionZField.text = bodyPos.y.ToString("0.000");
rotationField.text = bodyAngle.ToString("0.000");
headBobField.text = headBob.ToString("0.000");
headRotationField.text = headAngle.ToString("0.000");
appendageRotationField.text = appendageRotation.ToString("0.000");
lastTick = AnimationController.Instance.stageTick;
isDirty = false;
}
public void OnValueChanged()
{
PawnKeyframe keyframe = Workspace.Instance.GetCurrentPawnKeyframe(true);
keyframe.bodyOffsetX = float.Parse(positionXField.text);
keyframe.bodyOffsetZ = float.Parse(positionZField.text);
keyframe.bodyAngle = float.Parse(rotationField.text);
keyframe.headBob = float.Parse(headBobField.text);
keyframe.headAngle = float.Parse(headRotationField.text);
keyframe.genitalAngle = float.Parse(appendageRotationField.text);
Workspace.Instance.GetPawnAnimationClip(Workspace.actorID).BuildSimpleCurves();
isDirty = true;
Workspace.Instance.RecordEvent("Actor position / orientation");
}
}
}