rimworld-animation-studio/Assets/Scripts/GUI/KeyframeSlider.cs

148 lines
4.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace RimWorldAnimationStudio
{
public class KeyframeSlider : Slider, IPointerClickHandler, IBeginDragHandler, IEndDragHandler
{
public AnimationTimeline timeline;
public Transform ghostSliders;
public Slider ghostSliderPrefab;
public int maxGhosts = 4;
public int actorID;
public int keyframeID;
private PawnAnimationClip clip;
private PawnKeyframe keyframe;
public void Initialize(AnimationTimeline timeline, int actorID, int keyframeID)
{
this.timeline = timeline;
this.clip = Workspace.Instance.GetPawnAnimationClip(actorID);
this.keyframe = Workspace.Instance.GetPawnKeyframe(actorID, keyframeID);
this.actorID = actorID;
this.keyframeID = keyframeID;
PawnKeyframe keyframe = Workspace.Instance.GetPawnKeyframe(actorID, keyframeID);
2022-09-18 05:01:12 +00:00
maxValue = Workspace.StageWindowSize;
value = keyframe.atTick.Value;
OnValueChanged();
onValueChanged.AddListener(delegate (float value) { OnValueChanged(); });
}
public void OnValueChanged()
{
keyframe.atTick = (int)value;
clip.BuildSimpleCurves();
AnimationController.Instance.stageTick = keyframe.atTick.Value;
2022-09-19 00:07:44 +00:00
timeline.InitiateUpdateOfGhostFrames();
}
// Ghost sliders are non-interactable slider handle
public void UpdateGhostFrames()
{
if (maxGhosts == 0)
{ return; }
2022-09-19 00:07:44 +00:00
int requiredGhosts = GetGhostFramesRequired();
int currentGhostCount = ghostSliders.childCount;
2022-09-19 00:07:44 +00:00
for (int i = 0; i < Mathf.Max(requiredGhosts, currentGhostCount); i++)
{
2022-09-19 00:07:44 +00:00
int targetTick = (int)(i * clip.duration + keyframe.atTick);
2022-09-19 00:07:44 +00:00
if (ghostSliders.childCount <= i)
{ Instantiate(ghostSliderPrefab, ghostSliders); }
2022-09-19 00:07:44 +00:00
GameObject ghostSliderObject = ghostSliders.GetChild(i).gameObject;
ghostSliderObject.SetActive(i < requiredGhosts);
2022-09-19 00:07:44 +00:00
Slider ghostSlider = ghostSliderObject.GetComponent<Slider>();
ghostSlider.maxValue = Workspace.StageWindowSize;
ghostSlider.value = targetTick;
if (targetTick > ghostSlider.maxValue)
{ ghostSlider.gameObject.SetActive(false); }
}
}
public int GetGhostFramesRequired()
{
if (Workspace.animationDef.animationStages[Workspace.stageID].isLooping == false)
{ return 0; }
if (clip.duration <= 1)
{ return 0; }
2022-09-18 05:01:12 +00:00
return Math.Min(Mathf.CeilToInt((float)Workspace.StageWindowSize / clip.duration), maxGhosts);
}
public void OnPointerClick(PointerEventData eventData)
{
2022-09-18 06:52:54 +00:00
Workspace.actorID = actorID;
Workspace.keyframeID = keyframeID;
if (eventData.clickCount >= 2)
2022-09-19 00:07:44 +00:00
{ AnimationController.Instance.stageTick = keyframe.atTick.Value; }
Workspace.Instance.MakeDirty();
}
public void OnBeginDrag(PointerEventData eventData)
{
2022-09-18 06:52:54 +00:00
AnimationController.Instance.stageTick = keyframe.atTick.Value;
Workspace.actorID = actorID;
Workspace.keyframeID = keyframeID;
if (keyframe.atTick == 1)
{ return; }
interactable = true;
}
public override void OnDrag(PointerEventData eventData)
{
base.OnDrag(eventData);
2022-09-18 06:52:54 +00:00
AnimationController.Instance.stageTick = keyframe.atTick.Value;
Workspace.actorID = actorID;
}
public void OnEndDrag(PointerEventData eventData)
{
interactable = false;
2022-09-19 00:07:44 +00:00
Workspace.Instance.MakeDirty();
}
2022-09-18 06:52:54 +00:00
protected override void Update()
{
2022-09-18 06:52:54 +00:00
base.Update();
2022-09-18 06:52:54 +00:00
if (Workspace.keyframeID == keyframeID && AnimationController.Instance.stageTick == keyframe.atTick.Value)
{ transform.FindDeepChild("Handle").GetComponent<Image>().color = Constants.ColorPurple; }
2022-09-18 06:52:54 +00:00
else if (Workspace.keyframeID == keyframeID)
{ transform.FindDeepChild("Handle").GetComponent<Image>().color = Constants.ColorCyan; }
2022-09-18 06:52:54 +00:00
else if (AnimationController.Instance.stageTick == keyframe.atTick.Value)
{ transform.FindDeepChild("Handle").GetComponent<Image>().color = Constants.ColorPink; }
else
{ transform.FindDeepChild("Handle").GetComponent<Image>().color = Constants.ColorWhite; }
}
}
}