using System; using System.Collections.Generic; using UnityEngine; [Serializable] public class AMTrack : ScriptableObject { public void setName(int index) { this.name = "Track" + (index + 1); } public void setName(string name) { this.name = name; } public bool hasKeyOnFrame(int _frame) { foreach (AMKey amkey in this.keys) { if (amkey.frame == _frame) { return true; } } return false; } public virtual void drawGizmos(float gizmo_size) { } public virtual void previewFrame(float frame, AMTrack extraTrack = null) { } public virtual void updateCache() { AMCameraFade.doShouldUpdateStill(); } public virtual AnimatorTimeline.JSONInit getJSONInit() { Debug.LogWarning("Animator: No override for getJSONInit()"); return new AnimatorTimeline.JSONInit(); } public AMKey getKeyOnFrame(int _frame) { foreach (AMKey amkey in this.keys) { if (amkey.frame == _frame) { return amkey; } } Debug.LogError("Animator: No key found on frame " + _frame); return new AMKey(); } public virtual string getTrackType() { return "Unknown"; } public void sortKeys() { this.keys.Sort((AMKey c, AMKey d) => c.frame.CompareTo(d.frame)); } public void deleteKeyOnFrame(int frame) { for (int i = 0; i < this.keys.Count; i++) { if (this.keys[i].frame == frame) { this.keys[i].destroy(); this.keys.RemoveAt(i); } } } public void deleteDuplicateKeys() { this.sortKeys(); int num = -1; for (int i = 0; i < this.keys.Count; i++) { if (this.keys[i].frame == num) { this.keys[i].destroy(); this.keys.RemoveAt(i); i--; } else { num = this.keys[i].frame; } } } public void deleteAllKeys() { foreach (AMKey amkey in this.keys) { amkey.destroy(); } this.destroyCache(); this.keys = new List(); } public void deleteKeysAfter(int frame) { for (int i = 0; i < this.keys.Count; i++) { if (this.keys[i].frame > frame) { this.keys[i].destroy(); this.keys.RemoveAt(i); i--; } } } public void destroy() { foreach (AMKey amkey in this.keys) { amkey.destroy(); } this.destroyCache(); UnityEngine.Object.DestroyImmediate(this); } public virtual List getDependencies() { return new List(); } public virtual List updateDependencies(List newReferences, List oldReferences) { return new List(); } public void destroyCache() { if (this.cache == null) { return; } foreach (AMAction amaction in this.cache) { if (!(amaction == null)) { amaction.destroy(); } } this.cache = null; } public void offsetKeysFromBy(int frame, int amount) { if (this.keys.Count <= 0) { return; } for (int i = 0; i < this.keys.Count; i++) { if (frame <= 0 || this.keys[i].frame >= frame) { this.keys[i].frame += amount; } } this.updateCache(); } public int shiftOutOfBoundsKeys() { if (this.keys.Count <= 0) { return 0; } this.sortKeys(); if (this.keys[0].frame >= 1) { return 0; } int num = 0; num = Mathf.Abs(this.keys[0].frame) + 1; foreach (AMKey amkey in this.keys) { amkey.frame += num; } this.updateCache(); return num; } public AMAction getActionContainingFrame(int frame) { for (int i = this.cache.Count - 1; i >= 0; i--) { if (frame >= this.cache[i].startFrame) { return this.cache[i]; } } if (this.cache.Count > 0) { return this.cache[0]; } Debug.LogError("Animator: No action found for frame " + frame); return ScriptableObject.CreateInstance(); } public AMAction getActionForFrame(int startFrame) { foreach (AMAction amaction in this.cache) { if (amaction.startFrame == startFrame) { return amaction; } } Debug.LogError("Animator: No action found for frame " + startFrame); return ScriptableObject.CreateInstance(); } public int getActionIndexForFrame(int startFrame) { for (int i = 0; i < this.cache.Count; i++) { if (this.cache[i].startFrame == startFrame) { return i; } } return -1; } public bool hasActionOnFrame(int _frame) { foreach (AMAction amaction in this.cache) { if (amaction.startFrame == _frame) { return true; } } return false; } public int getKeyFrameAfterFrame(int frame, bool wholeTake = true) { foreach (AMKey amkey in this.keys) { if (amkey.frame > frame) { return amkey.frame; } } if (!wholeTake) { return -1; } if (this.keys.Count > 0) { return this.keys[0].frame; } Debug.LogError("Animator: No key found after frame " + frame); return -1; } public int getKeyFrameBeforeFrame(int frame, bool wholeTake = true) { for (int i = this.keys.Count - 1; i >= 0; i--) { if (this.keys[i].frame < frame) { return this.keys[i].frame; } } if (!wholeTake) { return -1; } if (this.keys.Count > 0) { return this.keys[this.keys.Count - 1].frame; } Debug.LogError("Animator: No key found before frame " + frame); return -1; } public AMKey[] getKeyFramesInBetween(int startFrame, int endFrame) { List list = new List(); if (startFrame <= 0 || endFrame <= 0 || startFrame >= endFrame || !this.hasKeyOnFrame(startFrame) || !this.hasKeyOnFrame(endFrame)) { return list.ToArray(); } this.sortKeys(); foreach (AMKey amkey in this.keys) { if (amkey.frame >= endFrame) { break; } if (amkey.frame > startFrame) { list.Add(amkey); } } return list.ToArray(); } public float[] getKeyFrameRatiosInBetween(int startFrame, int endFrame) { List list = new List(); if (startFrame <= 0 || endFrame <= 0 || startFrame >= endFrame || !this.hasKeyOnFrame(startFrame) || !this.hasKeyOnFrame(endFrame)) { return list.ToArray(); } this.sortKeys(); foreach (AMKey amkey in this.keys) { if (amkey.frame >= endFrame) { break; } if (amkey.frame > startFrame) { list.Add((float)(amkey.frame - startFrame) / (float)(endFrame - startFrame)); } } return list.ToArray(); } public int id; public new string name; public List keys = new List(); public List cache = new List(); public bool foldout = true; public AMTake parentTake; }