AMTrack.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [Serializable]
  5. public class AMTrack : ScriptableObject
  6. {
  7. public void setName(int index)
  8. {
  9. this.name = "Track" + (index + 1);
  10. }
  11. public void setName(string name)
  12. {
  13. this.name = name;
  14. }
  15. public bool hasKeyOnFrame(int _frame)
  16. {
  17. foreach (AMKey amkey in this.keys)
  18. {
  19. if (amkey.frame == _frame)
  20. {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26. public virtual void drawGizmos(float gizmo_size)
  27. {
  28. }
  29. public virtual void previewFrame(float frame, AMTrack extraTrack = null)
  30. {
  31. }
  32. public virtual void updateCache()
  33. {
  34. AMCameraFade.doShouldUpdateStill();
  35. }
  36. public virtual AnimatorTimeline.JSONInit getJSONInit()
  37. {
  38. Debug.LogWarning("Animator: No override for getJSONInit()");
  39. return new AnimatorTimeline.JSONInit();
  40. }
  41. public AMKey getKeyOnFrame(int _frame)
  42. {
  43. foreach (AMKey amkey in this.keys)
  44. {
  45. if (amkey.frame == _frame)
  46. {
  47. return amkey;
  48. }
  49. }
  50. Debug.LogError("Animator: No key found on frame " + _frame);
  51. return new AMKey();
  52. }
  53. public virtual string getTrackType()
  54. {
  55. return "Unknown";
  56. }
  57. public void sortKeys()
  58. {
  59. this.keys.Sort((AMKey c, AMKey d) => c.frame.CompareTo(d.frame));
  60. }
  61. public void deleteKeyOnFrame(int frame)
  62. {
  63. for (int i = 0; i < this.keys.Count; i++)
  64. {
  65. if (this.keys[i].frame == frame)
  66. {
  67. this.keys[i].destroy();
  68. this.keys.RemoveAt(i);
  69. }
  70. }
  71. }
  72. public void deleteDuplicateKeys()
  73. {
  74. this.sortKeys();
  75. int num = -1;
  76. for (int i = 0; i < this.keys.Count; i++)
  77. {
  78. if (this.keys[i].frame == num)
  79. {
  80. this.keys[i].destroy();
  81. this.keys.RemoveAt(i);
  82. i--;
  83. }
  84. else
  85. {
  86. num = this.keys[i].frame;
  87. }
  88. }
  89. }
  90. public void deleteAllKeys()
  91. {
  92. foreach (AMKey amkey in this.keys)
  93. {
  94. amkey.destroy();
  95. }
  96. this.destroyCache();
  97. this.keys = new List<AMKey>();
  98. }
  99. public void deleteKeysAfter(int frame)
  100. {
  101. for (int i = 0; i < this.keys.Count; i++)
  102. {
  103. if (this.keys[i].frame > frame)
  104. {
  105. this.keys[i].destroy();
  106. this.keys.RemoveAt(i);
  107. i--;
  108. }
  109. }
  110. }
  111. public void destroy()
  112. {
  113. foreach (AMKey amkey in this.keys)
  114. {
  115. amkey.destroy();
  116. }
  117. this.destroyCache();
  118. UnityEngine.Object.DestroyImmediate(this);
  119. }
  120. public virtual List<GameObject> getDependencies()
  121. {
  122. return new List<GameObject>();
  123. }
  124. public virtual List<GameObject> updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences)
  125. {
  126. return new List<GameObject>();
  127. }
  128. public void destroyCache()
  129. {
  130. if (this.cache == null)
  131. {
  132. return;
  133. }
  134. foreach (AMAction amaction in this.cache)
  135. {
  136. if (!(amaction == null))
  137. {
  138. amaction.destroy();
  139. }
  140. }
  141. this.cache = null;
  142. }
  143. public void offsetKeysFromBy(int frame, int amount)
  144. {
  145. if (this.keys.Count <= 0)
  146. {
  147. return;
  148. }
  149. for (int i = 0; i < this.keys.Count; i++)
  150. {
  151. if (frame <= 0 || this.keys[i].frame >= frame)
  152. {
  153. this.keys[i].frame += amount;
  154. }
  155. }
  156. this.updateCache();
  157. }
  158. public int shiftOutOfBoundsKeys()
  159. {
  160. if (this.keys.Count <= 0)
  161. {
  162. return 0;
  163. }
  164. this.sortKeys();
  165. if (this.keys[0].frame >= 1)
  166. {
  167. return 0;
  168. }
  169. int num = 0;
  170. num = Mathf.Abs(this.keys[0].frame) + 1;
  171. foreach (AMKey amkey in this.keys)
  172. {
  173. amkey.frame += num;
  174. }
  175. this.updateCache();
  176. return num;
  177. }
  178. public AMAction getActionContainingFrame(int frame)
  179. {
  180. for (int i = this.cache.Count - 1; i >= 0; i--)
  181. {
  182. if (frame >= this.cache[i].startFrame)
  183. {
  184. return this.cache[i];
  185. }
  186. }
  187. if (this.cache.Count > 0)
  188. {
  189. return this.cache[0];
  190. }
  191. Debug.LogError("Animator: No action found for frame " + frame);
  192. return ScriptableObject.CreateInstance<AMAction>();
  193. }
  194. public AMAction getActionForFrame(int startFrame)
  195. {
  196. foreach (AMAction amaction in this.cache)
  197. {
  198. if (amaction.startFrame == startFrame)
  199. {
  200. return amaction;
  201. }
  202. }
  203. Debug.LogError("Animator: No action found for frame " + startFrame);
  204. return ScriptableObject.CreateInstance<AMAction>();
  205. }
  206. public int getActionIndexForFrame(int startFrame)
  207. {
  208. for (int i = 0; i < this.cache.Count; i++)
  209. {
  210. if (this.cache[i].startFrame == startFrame)
  211. {
  212. return i;
  213. }
  214. }
  215. return -1;
  216. }
  217. public bool hasActionOnFrame(int _frame)
  218. {
  219. foreach (AMAction amaction in this.cache)
  220. {
  221. if (amaction.startFrame == _frame)
  222. {
  223. return true;
  224. }
  225. }
  226. return false;
  227. }
  228. public int getKeyFrameAfterFrame(int frame, bool wholeTake = true)
  229. {
  230. foreach (AMKey amkey in this.keys)
  231. {
  232. if (amkey.frame > frame)
  233. {
  234. return amkey.frame;
  235. }
  236. }
  237. if (!wholeTake)
  238. {
  239. return -1;
  240. }
  241. if (this.keys.Count > 0)
  242. {
  243. return this.keys[0].frame;
  244. }
  245. Debug.LogError("Animator: No key found after frame " + frame);
  246. return -1;
  247. }
  248. public int getKeyFrameBeforeFrame(int frame, bool wholeTake = true)
  249. {
  250. for (int i = this.keys.Count - 1; i >= 0; i--)
  251. {
  252. if (this.keys[i].frame < frame)
  253. {
  254. return this.keys[i].frame;
  255. }
  256. }
  257. if (!wholeTake)
  258. {
  259. return -1;
  260. }
  261. if (this.keys.Count > 0)
  262. {
  263. return this.keys[this.keys.Count - 1].frame;
  264. }
  265. Debug.LogError("Animator: No key found before frame " + frame);
  266. return -1;
  267. }
  268. public AMKey[] getKeyFramesInBetween(int startFrame, int endFrame)
  269. {
  270. List<AMKey> list = new List<AMKey>();
  271. if (startFrame <= 0 || endFrame <= 0 || startFrame >= endFrame || !this.hasKeyOnFrame(startFrame) || !this.hasKeyOnFrame(endFrame))
  272. {
  273. return list.ToArray();
  274. }
  275. this.sortKeys();
  276. foreach (AMKey amkey in this.keys)
  277. {
  278. if (amkey.frame >= endFrame)
  279. {
  280. break;
  281. }
  282. if (amkey.frame > startFrame)
  283. {
  284. list.Add(amkey);
  285. }
  286. }
  287. return list.ToArray();
  288. }
  289. public float[] getKeyFrameRatiosInBetween(int startFrame, int endFrame)
  290. {
  291. List<float> list = new List<float>();
  292. if (startFrame <= 0 || endFrame <= 0 || startFrame >= endFrame || !this.hasKeyOnFrame(startFrame) || !this.hasKeyOnFrame(endFrame))
  293. {
  294. return list.ToArray();
  295. }
  296. this.sortKeys();
  297. foreach (AMKey amkey in this.keys)
  298. {
  299. if (amkey.frame >= endFrame)
  300. {
  301. break;
  302. }
  303. if (amkey.frame > startFrame)
  304. {
  305. list.Add((float)(amkey.frame - startFrame) / (float)(endFrame - startFrame));
  306. }
  307. }
  308. return list.ToArray();
  309. }
  310. public int id;
  311. public new string name;
  312. public List<AMKey> keys = new List<AMKey>();
  313. public List<AMAction> cache = new List<AMAction>();
  314. public bool foldout = true;
  315. public AMTake parentTake;
  316. }