AMTranslationTrack.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. [Serializable]
  6. public class AMTranslationTrack : AMTrack
  7. {
  8. public Transform obj
  9. {
  10. get
  11. {
  12. return this._obj;
  13. }
  14. set
  15. {
  16. if (value != null && this.cache.Count <= 0)
  17. {
  18. this.cachedInitialPosition = value.position;
  19. }
  20. this._obj = value;
  21. if (this._obj != null)
  22. {
  23. this.objName = this._obj.name;
  24. }
  25. else
  26. {
  27. this.objName = string.Empty;
  28. }
  29. }
  30. }
  31. public override string getTrackType()
  32. {
  33. return "Translation";
  34. }
  35. public void addKey(int _frame, Vector3 _position, int _interp, int _easeType)
  36. {
  37. foreach (AMKey amkey in this.keys)
  38. {
  39. AMTranslationKey amtranslationKey = (AMTranslationKey)amkey;
  40. if (amtranslationKey.frame == _frame)
  41. {
  42. amtranslationKey.position = _position;
  43. amtranslationKey.interp = _interp;
  44. amtranslationKey.easeType = _easeType;
  45. this.updateCache();
  46. return;
  47. }
  48. }
  49. AMTranslationKey amtranslationKey2 = ScriptableObject.CreateInstance<AMTranslationKey>();
  50. amtranslationKey2.frame = _frame;
  51. amtranslationKey2.position = _position;
  52. amtranslationKey2.interp = _interp;
  53. amtranslationKey2.easeType = _easeType;
  54. this.keys.Add(amtranslationKey2);
  55. this.updateCache();
  56. }
  57. public void addKey(int _frame, Vector3 _position)
  58. {
  59. foreach (AMKey amkey in this.keys)
  60. {
  61. AMTranslationKey amtranslationKey = (AMTranslationKey)amkey;
  62. if (amtranslationKey.frame == _frame)
  63. {
  64. amtranslationKey.position = _position;
  65. this.updateCache();
  66. return;
  67. }
  68. }
  69. AMTranslationKey amtranslationKey2 = ScriptableObject.CreateInstance<AMTranslationKey>();
  70. amtranslationKey2.frame = _frame;
  71. amtranslationKey2.position = _position;
  72. this.keys.Add(amtranslationKey2);
  73. this.updateCache();
  74. }
  75. public bool changeObject(Transform _obj)
  76. {
  77. this.obj = _obj;
  78. this.updateCache();
  79. return true;
  80. }
  81. public override void previewFrame(float frame, AMTrack extraTrack = null)
  82. {
  83. if (!this.obj)
  84. {
  85. return;
  86. }
  87. if (string.IsNullOrEmpty(this.objName))
  88. {
  89. this.objName = this.obj.name;
  90. }
  91. if (this.cache.Count <= 0)
  92. {
  93. return;
  94. }
  95. if (frame <= (float)this.cache[0].startFrame)
  96. {
  97. this.obj.position = (this.cache[0] as AMTranslationAction).path[0];
  98. return;
  99. }
  100. if (frame >= (float)(this.cache[this.cache.Count - 1] as AMTranslationAction).endFrame)
  101. {
  102. this.obj.position = (this.cache[this.cache.Count - 1] as AMTranslationAction).path[(this.cache[this.cache.Count - 1] as AMTranslationAction).path.Length - 1];
  103. return;
  104. }
  105. foreach (AMAction amaction in this.cache)
  106. {
  107. AMTranslationAction amtranslationAction = (AMTranslationAction)amaction;
  108. if (frame >= (float)amtranslationAction.startFrame && frame <= (float)amtranslationAction.endFrame)
  109. {
  110. if (amtranslationAction.path.Length == 1)
  111. {
  112. this.obj.position = amtranslationAction.path[0];
  113. break;
  114. }
  115. float num = frame - (float)amtranslationAction.startFrame;
  116. if (num < 0f)
  117. {
  118. num = 0f;
  119. }
  120. AnimationCurve curve = null;
  121. AMTween.EasingFunction easingFunction;
  122. if (amtranslationAction.hasCustomEase())
  123. {
  124. if (AMTranslationTrack.<>f__mg$cache0 == null)
  125. {
  126. AMTranslationTrack.<>f__mg$cache0 = new AMTween.EasingFunction(AMTween.customEase);
  127. }
  128. easingFunction = AMTranslationTrack.<>f__mg$cache0;
  129. curve = amtranslationAction.easeCurve;
  130. }
  131. else
  132. {
  133. easingFunction = AMTween.GetEasingFunction((AMTween.EaseType)amtranslationAction.easeType);
  134. }
  135. float value = easingFunction(0f, 1f, num / (float)amtranslationAction.getNumberOfFrames(), curve);
  136. AMTween.PutOnPath(this.obj, amtranslationAction.path, Mathf.Clamp(value, 0f, 1f));
  137. break;
  138. }
  139. }
  140. }
  141. public bool autoKey(Transform _obj, int frame)
  142. {
  143. if (!this.obj)
  144. {
  145. return false;
  146. }
  147. if (_obj != this.obj)
  148. {
  149. return false;
  150. }
  151. if (this.cache.Count <= 0)
  152. {
  153. if (_obj.position != this.cachedInitialPosition)
  154. {
  155. this.addKey(frame, _obj.position);
  156. return true;
  157. }
  158. return false;
  159. }
  160. else
  161. {
  162. Vector3 positionAtFrame = this.getPositionAtFrame((float)frame);
  163. if (_obj.position != positionAtFrame)
  164. {
  165. this.addKey(frame, _obj.position);
  166. return true;
  167. }
  168. return false;
  169. }
  170. }
  171. public Vector3 getPositionAtFrame(float frame)
  172. {
  173. if (this.cache.Count <= 0)
  174. {
  175. return this.obj.position;
  176. }
  177. if (frame <= (float)this.cache[0].startFrame)
  178. {
  179. return (this.cache[0] as AMTranslationAction).path[0];
  180. }
  181. if (frame >= (float)(this.cache[this.cache.Count - 1] as AMTranslationAction).endFrame)
  182. {
  183. return (this.cache[this.cache.Count - 1] as AMTranslationAction).path[(this.cache[this.cache.Count - 1] as AMTranslationAction).path.Length - 1];
  184. }
  185. foreach (AMAction amaction in this.cache)
  186. {
  187. AMTranslationAction amtranslationAction = (AMTranslationAction)amaction;
  188. if ((int)frame >= amtranslationAction.startFrame && (int)frame <= amtranslationAction.endFrame)
  189. {
  190. if (amtranslationAction.path.Length == 1)
  191. {
  192. return amtranslationAction.path[0];
  193. }
  194. AnimationCurve curve = null;
  195. AMTween.EasingFunction easingFunction;
  196. if (amtranslationAction.hasCustomEase())
  197. {
  198. if (AMTranslationTrack.<>f__mg$cache1 == null)
  199. {
  200. AMTranslationTrack.<>f__mg$cache1 = new AMTween.EasingFunction(AMTween.customEase);
  201. }
  202. easingFunction = AMTranslationTrack.<>f__mg$cache1;
  203. curve = amtranslationAction.easeCurve;
  204. }
  205. else
  206. {
  207. easingFunction = AMTween.GetEasingFunction((AMTween.EaseType)amtranslationAction.easeType);
  208. }
  209. float num = frame - (float)amtranslationAction.startFrame;
  210. if (num < 0f)
  211. {
  212. num = 0f;
  213. }
  214. return AMTween.PointOnPath(amtranslationAction.path, Mathf.Clamp(easingFunction(0f, 1f, num / (float)amtranslationAction.getNumberOfFrames(), curve), 0f, 1f));
  215. }
  216. }
  217. Debug.LogError(string.Concat(new object[]
  218. {
  219. "Animator: Could not get ",
  220. this.obj.name,
  221. " position at frame '",
  222. frame,
  223. "'"
  224. }));
  225. return new Vector3(0f, 0f, 0f);
  226. }
  227. public override void drawGizmos(float gizmo_size)
  228. {
  229. foreach (AMAction amaction in this.cache)
  230. {
  231. AMTranslationAction amtranslationAction = (AMTranslationAction)amaction;
  232. if (amtranslationAction.path.Length > 1)
  233. {
  234. AMTween.DrawPath(amtranslationAction.path, new Color(255f, 255f, 255f, 0.5f));
  235. Gizmos.color = Color.green;
  236. Gizmos.DrawSphere(amtranslationAction.path[0], gizmo_size);
  237. Gizmos.DrawSphere(amtranslationAction.path[amtranslationAction.path.Length - 1], gizmo_size);
  238. }
  239. }
  240. }
  241. private AMPath getPathFromIndex(int startIndex)
  242. {
  243. List<Vector3> list = new List<Vector3>();
  244. int endIndex = startIndex;
  245. int frame = this.keys[startIndex].frame;
  246. int frame2 = this.keys[startIndex].frame;
  247. list.Add((this.keys[startIndex] as AMTranslationKey).position);
  248. for (int i = startIndex + 1; i < this.keys.Count; i++)
  249. {
  250. list.Add((this.keys[i] as AMTranslationKey).position);
  251. frame2 = this.keys[i].frame;
  252. endIndex = i;
  253. if ((this.keys[i] as AMTranslationKey).interp == 1)
  254. {
  255. break;
  256. }
  257. }
  258. return new AMPath(list.ToArray(), (this.keys[startIndex] as AMTranslationKey).interp, frame, frame2, startIndex, endIndex);
  259. }
  260. public override void updateCache()
  261. {
  262. if (this._obj != null)
  263. {
  264. this.objName = this._obj.name;
  265. }
  266. else
  267. {
  268. this.objName = string.Empty;
  269. }
  270. base.destroyCache();
  271. this.cache = new List<AMAction>();
  272. base.sortKeys();
  273. for (int i = 0; i < this.keys.Count; i++)
  274. {
  275. AMPath pathFromIndex = this.getPathFromIndex(i);
  276. AMTranslationAction amtranslationAction = ScriptableObject.CreateInstance<AMTranslationAction>();
  277. amtranslationAction.startFrame = pathFromIndex.startFrame;
  278. amtranslationAction.endFrame = pathFromIndex.endFrame;
  279. amtranslationAction.obj = this.obj;
  280. amtranslationAction.path = pathFromIndex.path;
  281. amtranslationAction.easeType = (this.keys[i] as AMTranslationKey).easeType;
  282. amtranslationAction.customEase = new List<float>(this.keys[i].customEase);
  283. this.cache.Add(amtranslationAction);
  284. if (i < this.keys.Count - 1)
  285. {
  286. i = pathFromIndex.endIndex - 1;
  287. }
  288. }
  289. foreach (AMTrack amtrack in this.parentTake.trackValues)
  290. {
  291. if (amtrack is AMOrientationTrack && ((amtrack as AMOrientationTrack).obj == this.obj || (amtrack as AMOrientationTrack).hasTarget(this.obj)))
  292. {
  293. amtrack.updateCache();
  294. }
  295. }
  296. base.updateCache();
  297. }
  298. public AMTranslationKey getActionStartKeyFor(int frame)
  299. {
  300. foreach (AMAction amaction in this.cache)
  301. {
  302. AMTranslationAction amtranslationAction = (AMTranslationAction)amaction;
  303. if (frame >= amtranslationAction.startFrame && frame < amtranslationAction.endFrame)
  304. {
  305. return (AMTranslationKey)base.getKeyOnFrame(amtranslationAction.startFrame);
  306. }
  307. }
  308. Debug.LogError("Animator: Action for frame " + frame + " does not exist in cache.");
  309. return new AMTranslationKey();
  310. }
  311. public Vector3 getInitialPosition()
  312. {
  313. return (this.keys[0] as AMTranslationKey).position;
  314. }
  315. public override AnimatorTimeline.JSONInit getJSONInit()
  316. {
  317. if (!this.obj || this.keys.Count <= 0)
  318. {
  319. return null;
  320. }
  321. AnimatorTimeline.JSONInit jsoninit = new AnimatorTimeline.JSONInit();
  322. jsoninit.type = "position";
  323. jsoninit.go = this.obj.gameObject.name;
  324. AnimatorTimeline.JSONVector3 jsonvector = new AnimatorTimeline.JSONVector3();
  325. jsonvector.setValue(this.getInitialPosition());
  326. jsoninit.position = jsonvector;
  327. return jsoninit;
  328. }
  329. public override List<GameObject> getDependencies()
  330. {
  331. List<GameObject> list = new List<GameObject>();
  332. if (this.obj)
  333. {
  334. list.Add(this.obj.gameObject);
  335. }
  336. return list;
  337. }
  338. public override List<GameObject> updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences)
  339. {
  340. if (!this.obj)
  341. {
  342. return new List<GameObject>();
  343. }
  344. for (int i = 0; i < oldReferences.Count; i++)
  345. {
  346. if (oldReferences[i] == this.obj.gameObject)
  347. {
  348. this.obj = newReferences[i].transform;
  349. break;
  350. }
  351. }
  352. return new List<GameObject>();
  353. }
  354. [SerializeField]
  355. private Transform _obj;
  356. public string objName;
  357. public Vector3 cachedInitialPosition;
  358. [CompilerGenerated]
  359. private static AMTween.EasingFunction <>f__mg$cache0;
  360. [CompilerGenerated]
  361. private static AMTween.EasingFunction <>f__mg$cache1;
  362. }