using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; namespace RimWorldAnimationStudio { public class AnimationTimeline : MonoBehaviour, IPointerClickHandler { public int actorID; public KeyframeSlider keyframeSliderPrefab; private Transform anchorTransform; public void Initialize(int actorID) { anchorTransform = transform.parent; this.actorID = actorID; Reset(); PawnAnimationClip clip = Workspace.Instance.GetPawnAnimationClip(actorID); if (clip == null || clip.keyframes.NullOrEmpty()) { //Debug.Log("Clip was empty"); clip = new PawnAnimationClip(); clip.keyframes.Add(new PawnKeyframe()); clip.BuildSimpleCurves(); } foreach (PawnKeyframe keyframe in clip.keyframes) { KeyframeSlider keyframeSlider = Instantiate(keyframeSliderPrefab, transform); keyframeSlider.Initialize(this, actorID, keyframe.keyframeID); } } public void Reset() { foreach(KeyframeSlider keyframeSlider in GetComponentsInChildren()) { Destroy(keyframeSlider.gameObject); } } public void AddPawnKeyFrame(int keyframeID) { KeyframeSlider keyframeSlider = Instantiate(keyframeSliderPrefab, transform); keyframeSlider.Initialize(this, actorID, keyframeID); } public void RemovePawnKeyFrame(int keyframeID) { KeyframeSlider keyframeSlider = GetComponentsInChildren().FirstOrDefault(x => x.keyframeID == keyframeID); Destroy(keyframeSlider?.gameObject); } public void Update() { if (Workspace.actorID == actorID) { GetComponent().color = Constants.ColorGoldYellow; } else { GetComponent().color = Constants.ColorMidGrey; } } public void InitiateUpdateOfGhostFrames() { BroadcastMessage("UpdateGhostFrames"); } public void OnMoveTimeline(int delta) { int? siblingIndex = anchorTransform.parent.GetComponentsInChildren()?.ToList()?.IndexOf(this); int? siblingCount = anchorTransform.parent.GetComponentsInChildren()?.ToList()?.Count(); if (siblingIndex != null && siblingCount != null && MoveAnimationTimeline(siblingIndex.Value, delta)) { //siblingIndex = Mathf.Clamp(siblingCount.Value + delta, 0, siblingCount.Value - 1); //transform.SetSiblingIndex(transform.GetSiblingIndex() + delta); AnimationController.Instance.ResetAnimationTimeline(); AnimationController.Instance.InitializeAnimationTimeline(); } } public bool MoveAnimationTimeline(int startIndex, int delta) { if (startIndex + delta < 0 || startIndex + delta >= Workspace.animationDef.animationStages[Workspace.stageID].animationClips.Count) { Debug.Log("Cannot move animation timeline - movement would exceed bounds"); return false; } Actor actor = Workspace.animationDef.actors[startIndex]; Workspace.animationDef.actors[startIndex] = Workspace.animationDef.actors[startIndex + delta]; Workspace.animationDef.actors[startIndex + delta] = actor; PawnAnimationClip clip = Workspace.Instance.GetPawnAnimationClip(startIndex); Workspace.animationDef.animationStages[Workspace.stageID].animationClips[startIndex] = Workspace.animationDef.animationStages[Workspace.stageID].animationClips[startIndex + delta]; Workspace.animationDef.animationStages[Workspace.stageID].animationClips[startIndex + delta] = clip; Workspace.actorID = startIndex + delta; Workspace.Instance.RecordEvent("Timeline move"); return true; } public void OnPointerClick(PointerEventData eventData) { Workspace.actorID = actorID; Workspace.keyframeID.Clear(); } } }