Tween.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /************************************************************************
  2. * Tween.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. /**
  22. * The {@code TweenData} class represents the data that a Tween GameObject needs to
  23. * interpolate a given property.
  24. *
  25. * ! All GameObjects need to be inside the tree to do anything (can be added as a child
  26. * ! of another GameObject on the tree or as a root).
  27. *
  28. * @author Pedro Schneider
  29. *
  30. * @class
  31. */
  32. class TweenData
  33. {
  34. /**
  35. * Creates a TweenData Object with the specified parameters.
  36. *
  37. * @param {Object} target Object that has the property to be interpolated.
  38. * @param {String} property name of the property of target to be interpolated.
  39. * target[property] should be number, Vector2 or Color.
  40. * @param {PROPERTY_TYPE} propertyType type of the property to be interpolated.
  41. * @param {number, Vector2, Color} initVal initial value for the interpolation.
  42. * Should be the same type as target[property].
  43. * @param {number, Vector2, Color} finalVal final value for the interpolation.
  44. * Should be the same type as target[property].
  45. * @param {number} duration duration in seconds of the interpolation.
  46. * @param {TRANS_TYPE} transType transition type of the interpolation.
  47. * @param {EASE_TYPE} easeType easing type of the interpolation.
  48. * @param {number} delay delay in seconds for the interpolation to start.
  49. *
  50. * @constructor
  51. */
  52. constructor(target, property, propertyType, initVal, finalVal, duration, transType, easeType, delay)
  53. {
  54. this.target = target; // Object that has the property to be interpolated.
  55. this.property = property; // Name of the property to be interpolated.
  56. this.propertyType = propertyType; // Type of the property to be interpolated.
  57. // Initializes new objects for final value and initial value depending on the type.
  58. switch (this.propertyType)
  59. {
  60. case PROPERTY_TYPE.COLOR:
  61. this.initVal = new Color(initVal.r, initVal.g, initVal.b, initVal.a);
  62. this.finalVal = new Color(finalVal.r, finalVal.g, finalVal.b, finalVal.a);
  63. break;
  64. case PROPERTY_TYPE.VECTOR2:
  65. this.initVal = new Vector2(initVal.x, initVal.y);
  66. this.finalVal = new Vector2(finalVal.x, finalVal.y);
  67. break;
  68. case PROPERTY_TYPE.NUMBER:
  69. this.initVal = initVal;
  70. this.finalVal = finalVal;
  71. break;
  72. }
  73. this.duration = duration; // Duration in seconds of the interpolation
  74. this.transType = transType; // Type of the transition.
  75. this.easeType = easeType; // Type of the easing.
  76. this.t = -delay; // Current time of the interpolation.
  77. this.playing = false; // Is the interpolation playing?
  78. this.done = false; // Is the interpolation done?
  79. this.p = []; // List of sub-properties to be interpolated depending on the type.
  80. switch (this.propertyType)
  81. {
  82. case PROPERTY_TYPE.COLOR:
  83. this.p.push("r");
  84. this.p.push("g");
  85. this.p.push("b");
  86. break;
  87. case PROPERTY_TYPE.VECTOR2:
  88. this.p.push("x");
  89. this.p.push("y");
  90. break;
  91. case PROPERTY_TYPE.NUMBER:
  92. break;
  93. }
  94. this.trans = ""; // String for the transition type.
  95. switch (this.transType)
  96. {
  97. case TRANS_TYPE.LINEAR:
  98. this.trans = "Linear";
  99. break;
  100. case TRANS_TYPE.QUAD:
  101. this.trans = "Quad";
  102. break;
  103. case TRANS_TYPE.CUBIC:
  104. this.trans = "Cubic";
  105. break;
  106. case TRANS_TYPE.QUART:
  107. this.trans = "Quart";
  108. break;
  109. case TRANS_TYPE.QUINT:
  110. this.trans = "Quint";
  111. break;
  112. case TRANS_TYPE.SINE:
  113. this.trans = "Sine";
  114. break;
  115. case TRANS_TYPE.EXPONENTIAL:
  116. this.trans = "Expo";
  117. break;
  118. case TRANS_TYPE.CIRCULAR:
  119. this.trans = "Circ";
  120. break;
  121. case TRANS_TYPE.ELASTIC:
  122. this.trans = "Elastic";
  123. break;
  124. case TRANS_TYPE.BACK:
  125. this.trans = "Back";
  126. break;
  127. case TRANS_TYPE.BOUNCE:
  128. this.trans = "Bounce";
  129. break;
  130. }
  131. this.ease = ""; // String for the easing type.
  132. if (this.transType == TRANS_TYPE.LINEAR) this.ease = "ease";
  133. else
  134. {
  135. switch (this.easeType)
  136. {
  137. case EASE_TYPE.IN:
  138. this.ease = "easeIn";
  139. break;
  140. case EASE_TYPE.OUT:
  141. this.ease = "easeOut";
  142. break;
  143. case EASE_TYPE.IN_OUT:
  144. this.ease = "easeInOut";
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. /**
  151. * The {@code Tween} class represents a Tween GameObject that has functionality to
  152. * interpolate any property of another GameObject if the properties are of the type
  153. * number, Vector2 or Color.
  154. *
  155. * @author Pedro Schneider
  156. *
  157. * @class
  158. */
  159. class Tween extends GameObject
  160. {
  161. /**
  162. * Creates an empty Tween GameObject.
  163. *
  164. * @param {String} name name of the Tween GameObject.
  165. *
  166. * @constructor
  167. */
  168. constructor(name)
  169. {
  170. super(name);
  171. this.tweenData = [];
  172. this.doneTweens = 0;
  173. this.done = false;
  174. }
  175. /**
  176. * Add a new TweenData Object to this Tween with the necessary information to interpolate
  177. * the target's property.
  178. *
  179. * @param {Object} target Object that has the property to be interpolated.
  180. * @param {String} property name of the property of target to be interpolated.
  181. * target[property] should be number, Vector2 or Color.
  182. * @param {PROPERTY_TYPE} propertyType type of the property to be interpolated.
  183. * @param {number, Vector2, Color} initVal initial value for the interpolation.
  184. * Should be the same type as target[property].
  185. * @param {number, Vector2, Color} finalVal final value for the interpolation.
  186. * Should be the same type as target[property].
  187. * @param {number} duration duration in seconds of the interpolation.
  188. * @param {TRANS_TYPE} transType transition type of the interpolation.
  189. * Default is TRANS_TYPE.LINEAR.
  190. * @param {EASE_TYPE} easeType easing type of the interpolation.
  191. * Default is EASY_TYPE.IN_OUT.
  192. * @param {number} delay delay in seconds for the interpolation to start.
  193. * Default is 0.
  194. */
  195. interpolateProperty(target, property, propertyType, initVal, finalVal, duration, transType = 1, easeType = 3, delay = 0)
  196. {
  197. this.done = false; // Are all TweenData on this Tween done?
  198. // Adding a new TweenData.
  199. this.tweenData.push(new TweenData(target, property, propertyType, initVal, finalVal, duration, transType, easeType, delay));
  200. }
  201. /**
  202. * Given a TweenData, sets its interpolation's target's property to the appropriate value for
  203. * the current time of the interpolation.
  204. *
  205. * @param {TweenData} td reference to the TweenData Object.
  206. */
  207. interpolate(td)
  208. {
  209. if (td.propertyType == PROPERTY_TYPE.NUMBER)
  210. td.target[td.property] = Easings[td.trans][td.ease](td.t, td.initVal, td.finalVal - td.initVal, td.duration);
  211. else
  212. {
  213. for (let i = 0; i < td.p.length; i++)
  214. td.target[td.property][td.p[i]] = Easings[td.trans][td.ease](td.t, td.initVal[td.p[i]], td.finalVal[td.p[i]] - td.initVal[td.p[i]], td.duration);
  215. }
  216. }
  217. /**
  218. * Starts interpolating all TweenData Objectcs currently added to this Tween.
  219. */
  220. startAll()
  221. {
  222. for (let i = 0; i < this.tweenData.length; i++)
  223. this.tweenData[i].playing = true;
  224. }
  225. /**
  226. * Starts interpolating a specific TweenData Object based on its index.
  227. *
  228. * ! Since TwennData are not GameObjects, this is the only way to query
  229. * ! for them. The index refera to the order you added the TweenData to
  230. * ! this Tween, starting at 0.
  231. *
  232. * @param {number} idx index of the desired TweenData to start.
  233. */
  234. startByIndex(idx)
  235. {
  236. if (idx < 0 && idx >= this.tweenData.length) return;
  237. this.tweenData[idx].playing = true;
  238. }
  239. /**
  240. * Stops interpolating all TweenData Objects currently added to this Tween.
  241. */
  242. stopAll()
  243. {
  244. for (let i = 0; i < this.tweenData.length; i++)
  245. this.tweenData[i].playing = false;
  246. }
  247. /**
  248. * Stops interpolating a specific TweenData Object based on its index.
  249. *
  250. * ! Since TwennData are not GameObjects, this is the only way to query
  251. * ! for them. The index refera to the order you added the TweenData to
  252. * ! this Tween, starting at 0.
  253. *
  254. * @param {number} idx index of the desired TweenData to stop.
  255. */
  256. stopByIndex(idx)
  257. {
  258. if (idx < 0 && idx >= this.tweenData.length) return;
  259. this.tweenData[idx].playing = false;
  260. }
  261. /**
  262. * Resumes interpolating all TweenData currently added to this Tween.
  263. */
  264. resumeAll()
  265. {
  266. for (let i = 0; i < this.tweenData.length; i++)
  267. this.tweenData[i].playing = true;
  268. }
  269. /**
  270. * Resumes interpolating a specific TweenData Object based on its index.
  271. *
  272. * ! Since TwennData are not GameObjects, this is the only way to query
  273. * ! for them. The index refera to the order you added the TweenData to
  274. * ! this Tween, starting at 0.
  275. *
  276. * @param {number} idx index of the desired TweenData to resume.
  277. */
  278. resumeByIndex(idx)
  279. {
  280. if (idx < 0 && idx >= this.tweenData.length) return;
  281. this.tweenData[idx].playing = true;
  282. }
  283. /**
  284. * Resets all TweenData currently added to this Tween.
  285. */
  286. resetAll()
  287. {
  288. this.doneTweens = 0;
  289. this.done = false;
  290. for (let i = 0; i < this.tweenData.length; i++)
  291. {
  292. this.tweenData[i].t = 0;
  293. this.tweenData[i].done = false;
  294. }
  295. }
  296. /**
  297. * Resets a specific TweenData Object based on its index.
  298. *
  299. * ! Since TwennData are not GameObjects, this is the only way to query
  300. * ! for them. The index refera to the order you added the TweenData to
  301. * ! this Tween, starting at 0.
  302. *
  303. * @param {number} idx index of the desired TweenData to reset.
  304. */
  305. resetByIndex(idx)
  306. {
  307. if (idx < 0 && idx >= this.tweenData.length) return;
  308. this.doneTweens--;
  309. this.done = false;
  310. this.tweenData[idx].t = 0;
  311. this.tweenData[idx].done = false;
  312. }
  313. /**
  314. * Removes all TweenData currently added to this Tween.
  315. */
  316. removeAll()
  317. {
  318. while (this.tweenData.length > 0)
  319. this.tweenData.pop();
  320. }
  321. /**
  322. * Removes a specific TweenData Object based on its index.
  323. *
  324. * ! Since TwennData are not GameObjects, this is the only way to query
  325. * ! for them. The index refera to the order you added the TweenData to
  326. * ! this Tween, starting at 0.
  327. *
  328. * @param {number} idx index of the desired TweenData to remove.
  329. */
  330. removeByIndex(idx)
  331. {
  332. if (idx < 0 && idx >= this.tweenData.length) return;
  333. this.tweenData.splice(idx, 1);
  334. }
  335. /**
  336. * Sets the current time of all TweenData currently added to this Tween
  337. * to the specified time.
  338. *
  339. * @param {number} time time in seconds to seek all TweenData on this Tween.
  340. */
  341. seekAll(time)
  342. {
  343. if (time < 0) return;
  344. for (let i = 0; i < this.tweenData.length; i++)
  345. this.tweenData[i].t = min(time, this.tweenData[i].duration);
  346. }
  347. /**
  348. * Sets the current time of a specific TweenData Object, based on its index,
  349. * to the specified time.
  350. *
  351. * ! Since TwennData are not GameObjects, this is the only way to query
  352. * ! for them. The index refera to the order you added the TweenData to
  353. * ! this Tween, starting at 0.
  354. *
  355. * @param {number} idx index of the TweenData to seek to the time.
  356. * @param {number} time time in seconds to seek the specified TweenData
  357. */
  358. seekByIndex(idx, time)
  359. {
  360. if (idx < 0 && idx >= this.tweenData.length) return;
  361. this.tweenData[idx].t = min(time, this.tweenData[idx].duration);
  362. }
  363. /**
  364. * Called once every time all TweenData on this Tween are completed.
  365. * Emits the tweenDataAllCompleted signal.
  366. */
  367. allDone()
  368. {
  369. this.emitSignal("tweenAllCompleted");
  370. this.done = true;
  371. }
  372. /**
  373. * Adds default signals for the Tween GameObject and serves as a caller
  374. * to the _initSignals() callback.
  375. *
  376. * @signal tweenAllCompleted Emited once when all TweenData on this Tween
  377. * are done.
  378. * @signal tweenCompleted Emited once when one TweenData on this Tween
  379. * is done. Passes the completed TweenData as a
  380. * parameter.
  381. * @signal tweenStarted Emited once when one TweenData on this Tween
  382. * starts. Passes the started TweenData as a
  383. * parameter.
  384. *
  385. * @override
  386. */
  387. initSignals()
  388. {
  389. this.addSignal("tweenAllCompleted");
  390. this.addSignal("tweenCompleted");
  391. this.addSignal("tweenStarted");
  392. this._initSignals();
  393. }
  394. /**
  395. * Updates all TweenData added to this Tween and recursively calls the _update(delta)
  396. * callback for this GameObject and all of it's children.
  397. *
  398. * @param {number} delta number of ellapsed seconds since the last frame.
  399. *
  400. * @override
  401. */
  402. update(delta)
  403. {
  404. // Checks if all TweenData are done.
  405. if (!this.done && this.doneTweens == this.tweenData.length) this.allDone();
  406. for (let i = 0; i < this.tweenData.length; i++)
  407. {
  408. // Ignores TweenData that aren't playing.
  409. if (!this.tweenData[i].playing) continue;
  410. // Interpolates TweenData that are out of the delay.
  411. if (this.tweenData[i].t >= 0)
  412. this.interpolate(this.tweenData[i]);
  413. // Checks if the TweenData just went out of the delay (just started).
  414. if (this.tweenData[i].t <= 0 && this.tweenData[i].t + delta >= 0)
  415. this.emitSignal("tweenStarted", this.tweenData[i]);
  416. // Updates TweenData's current time.
  417. this.tweenData[i].t = min(this.tweenData[i].t + delta, this.tweenData[i].duration);
  418. // Checks if the TweenData is done.
  419. if (!this.tweenData[i].done && this.tweenData[i].t == this.tweenData[i].duration)
  420. {
  421. this.emitSignal("tweenDone", this.tweenData[i]);
  422. this.tweenData[i].done = true;
  423. this.doneTweens += 1;
  424. }
  425. }
  426. this.updateChildren(delta);
  427. }
  428. }