rimworld-animation-studio/Assets/Scripts/GUI/Cards/AnimationControlCard.cs
AbstractConcept a55ba7b95b Code refactor
2022-10-28 00:28:51 -05:00

103 lines
3.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace RimWorldAnimationStudio
{
public class AnimationControlCard : MonoBehaviour
{
public Dropdown stageLoopDropdown;
public InputField animationClipTimeField;
public InputField animationClipLengthField;
public Toggle stretchKeyframesToggle;
public InputField playBackSpeedField;
public Button playToggleButton;
public Slider stageTimelineSlider;
private void Start()
{
EventsManager.onStageTickChanged.AddListener(delegate
{
animationClipTimeField.SetTextWithoutNotify(Workspace.StageTick.ToString());
stageTimelineSlider.SetValueWithoutNotify(Workspace.StageTick);
});
EventsManager.onAnimationToggled.AddListener(delegate
{
playToggleButton.image.color = Workspace.isAnimating ? Constants.ColorGoldYellow : Constants.ColorWhite;
});
animationClipLengthField.text = Workspace.StageWindowSize.ToString();
stageTimelineSlider.maxValue = Workspace.StageWindowSize;
}
public void ToggleAnimation(bool forceOff = false)
{
Workspace.isAnimating = !Workspace.isAnimating;
if (forceOff) Workspace.isAnimating = false;
}
public void OnStageTimelineSliderChange()
{
if (Workspace.animationDef == null) return;
if (Workspace.StageTick != (int)stageTimelineSlider.value)
{
Workspace.StageTick = (int)stageTimelineSlider.value;
animationClipTimeField.text = Workspace.StageTick.ToString();
}
}
public void OnAnimationClipTimeFieldChange()
{
if (Workspace.animationDef == null) return;
int.TryParse(animationClipTimeField.text, out int newStageTick);
Workspace.StageTick = Mathf.Clamp(newStageTick, Constants.minTick, Workspace.StageWindowSize);
stageTimelineSlider.value = Workspace.StageTick;
}
public void OnAnimationClipLengthFieldChange()
{
if (Workspace.animationDef == null) return;
int.TryParse(animationClipLengthField.text, out int newStageWindowSize);
newStageWindowSize = Mathf.Clamp(newStageWindowSize, Constants.minAnimationClipLength, Constants.maxAnimationClipLength);
Debug.Log("Resizing animation clip length to " + newStageWindowSize.ToString() + " ticks.");
if (stretchKeyframesToggle.isOn)
{ Workspace.GetCurrentAnimationStage().StretchStageWindow(newStageWindowSize); }
else
{
foreach (PawnAnimationClip clip in Workspace.GetCurrentAnimationStage().AnimationClips)
{
List<PawnKeyframe> keyframes = clip.Keyframes.Where(x => x.atTick > newStageWindowSize)?.ToList();
if (keyframes.NullOrEmpty())
{ continue; }
foreach (PawnKeyframe keyframe in keyframes)
{
if (clip.Keyframes.Count <= 2)
{ break; }
clip.RemovePawnKeyframe(keyframe.keyframeID);
}
}
}
Workspace.GetCurrentAnimationStage().ResizeStageWindow(newStageWindowSize);
Workspace.RecordEvent("Stage length");
}
public void OnPlayBackSpeedChange()
{
Workspace.PlayBackSpeed = float.Parse(playBackSpeedField.text);
playBackSpeedField.SetTextWithoutNotify(Workspace.PlayBackSpeed.ToString());
}
}
}