2
0

Stage.AnimationUtils.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. //
  2. // The graphics engine GLXEngine
  3. //
  4. unit Stage.AnimationUtils;
  5. (* Main purpose is to give an easy way to create a linear interpolation *)
  6. interface
  7. {$I Stage.Defines.inc}
  8. uses
  9. System.SysUtils,
  10. System.Classes,
  11. System.Math,
  12. Stage.VectorTypes,
  13. Stage.VectorGeometry;
  14. type
  15. TEaseType=
  16. (
  17. etLinear,
  18. etQuadIn,
  19. etQuadOut,
  20. etQuadInOut,
  21. etQuadOutIn,
  22. etCubicIn,
  23. etCubicOut,
  24. etCubicInOut,
  25. etCubicOutIn,
  26. etQuintIn,
  27. etQuintOut,
  28. etQuintInOut,
  29. etQuintOutIn,
  30. etSineIn,
  31. etSineOut,
  32. etSineInOut,
  33. etSineOutIn,
  34. etCircIn,
  35. etCircOut,
  36. etCircInOut,
  37. etCircOutIn,
  38. etExpoIn,
  39. etExpoOut,
  40. etExpoInOut,
  41. etExpoOutIn,
  42. etElasticIn,
  43. etElasticOut,
  44. etElasticInOut,
  45. etElasticOutIn,
  46. etBackIn,
  47. etBackOut,
  48. etBackInOut,
  49. etBackOutIn,
  50. etBounceIn,
  51. etBounceOut,
  52. etBounceInOut,
  53. etBounceOutIn
  54. );
  55. function Tweener(Current, Target: TAffineVector; Time, Duration: Single;
  56. EaseType: TEaseType): TAffineVector; overload;
  57. function Tweener(Current, Target: TGLVector; Time, Duration: Single;
  58. EaseType: TEaseType): TGLVector; overload;
  59. function Tweener(Current, Target: TVector2f; Time, Duration: Single;
  60. EaseType: TEaseType): TVector2f; overload;
  61. function Tweener(Current, Target: Single; Time, Duration: Single;
  62. EaseType: TEaseType): Single; overload;
  63. implementation //-------------------------------------------------------------
  64. type
  65. TEaseFunction = function(t:Single; b:Single; c:Single; d:Single):Single;
  66. (*
  67. Easing equation function for a simple linear tweening, with no easing.
  68. t Current time (in frames or seconds).
  69. b Starting value.
  70. c Change needed in value.
  71. d Expected easing duration (in frames or seconds).
  72. Result The correct value.
  73. *)
  74. function easeNone (t:Single; b:Single; c:Single; d:Single):Single;
  75. begin
  76. Result := c*t/d + b;
  77. end;
  78. (*
  79. Easing equation function for a quadratic (t^2) easing in: accelerating from zero velocity.
  80. t Current time (in frames or seconds).
  81. b Starting value.
  82. c Change needed in value.
  83. d Expected easing duration (in frames or seconds).
  84. Result The correct value.
  85. *)
  86. function easeInQuad (t:Single; b:Single; c:Single; d:Single):Single;
  87. begin
  88. t := t/d;
  89. Result := c*t*t + b;
  90. end;
  91. (*
  92. Easing equation function for a quadratic (t^2) easing out: decelerating to zero velocity.
  93. t Current time (in frames or seconds).
  94. b Starting value.
  95. c Change needed in value.
  96. d Expected easing duration (in frames or seconds).
  97. Result The correct value.
  98. *)
  99. function easeOutQuad (t:Single; b:Single; c:Single; d:Single):Single;
  100. begin
  101. t := t/d;
  102. Result := -c *t*(t-2) + b;
  103. end;
  104. (*
  105. Easing equation function for a quadratic (t^2) easing in/out: acceleration until halfway, then deceleration.
  106. t Current time (in frames or seconds).
  107. b Starting value.
  108. c Change needed in value.
  109. d Expected easing duration (in frames or seconds).
  110. Result The correct value.
  111. *)
  112. function easeInOutQuad (t:Single; b:Single; c:Single; d:Single):Single;
  113. begin
  114. t := t/(d/2);
  115. if (t < 1) then
  116. Result := c/2*t*t + b
  117. else
  118. begin
  119. t := t-1;
  120. Result := -c/2 * (t*(t-2) - 1) + b;
  121. end;
  122. end;
  123. (*
  124. Easing equation function for a quadratic (t^2) easing out/in: deceleration until halfway, then acceleration.
  125. t Current time (in frames or seconds).
  126. b Starting value.
  127. c Change needed in value.
  128. d Expected easing duration (in frames or seconds).
  129. Result The correct value.
  130. *)
  131. function easeOutInQuad (t:Single; b:Single; c:Single; d:Single):Single;
  132. begin
  133. if (t < d/2) then
  134. Result := easeOutQuad (t*2, b, c/2, d)
  135. else
  136. Result := easeInQuad((t*2)-d, b+c/2, c/2, d);
  137. end;
  138. (*
  139. Easing equation function for a cubic (t^3) easing in: accelerating from zero velocity.
  140. t Current time (in frames or seconds).
  141. b Starting value.
  142. c Change needed in value.
  143. d Expected easing duration (in frames or seconds).
  144. Result The correct value.
  145. *)
  146. function easeInCubic (t:Single; b:Single; c:Single; d:Single):Single;
  147. begin
  148. t := t/d;
  149. Result := c*t*t*t + b;
  150. end;
  151. (*
  152. Easing equation function for a cubic (t^3) easing out: decelerating from zero velocity.
  153. t Current time (in frames or seconds).
  154. b Starting value.
  155. c Change needed in value.
  156. d Expected easing duration (in frames or seconds).
  157. Result The correct value.
  158. *)
  159. function easeOutCubic (t:Single; b:Single; c:Single; d:Single):Single;
  160. begin
  161. t := t/d-1;
  162. Result := c*(t*t*t + 1) + b;
  163. end;
  164. (*
  165. Easing equation function for a cubic (t^3) easing in/out: acceleration until halfway, then deceleration.
  166. t Current time (in frames or seconds).
  167. b Starting value.
  168. c Change needed in value.
  169. d Expected easing duration (in frames or seconds).
  170. Result The correct value.
  171. *)
  172. function easeInOutCubic (t:Single; b:Single; c:Single; d:Single):Single;
  173. begin
  174. t := t/(d/2);
  175. if (t < 1) then
  176. Result := c/2*t*t*t + b
  177. else
  178. begin
  179. t := t-2;
  180. Result := c/2*(t*t*t + 2) + b;
  181. end;
  182. end;
  183. (*
  184. Easing equation function for a cubic (t^3) easing out/in: deceleration until halfway, then acceleration.
  185. t Current time (in frames or seconds).
  186. b Starting value.
  187. c Change needed in value.
  188. d Expected easing duration (in frames or seconds).
  189. Result The correct value.
  190. *)
  191. function easeOutInCubic (t:Single; b:Single; c:Single; d:Single):Single;
  192. begin
  193. if (t < d/2) then
  194. Result := easeOutCubic (t*2, b, c/2, d)
  195. else
  196. Result := easeInCubic((t*2)-d, b+c/2, c/2, d);
  197. end;
  198. (*
  199. Easing equation function for a quartic (t^4) easing in: accelerating from zero velocity.
  200. t Current time (in frames or seconds).
  201. b Starting value.
  202. c Change needed in value.
  203. d Expected easing duration (in frames or seconds).
  204. Result The correct value.
  205. *)
  206. function easeInQuart (t:Single; b:Single; c:Single; d:Single):Single;
  207. begin
  208. t := t/d;
  209. Result := c*t*t*t*t + b;
  210. end;
  211. (*
  212. Easing equation function for a quartic (t^4) easing out: decelerating from zero velocity.
  213. t Current time (in frames or seconds).
  214. b Starting value.
  215. c Change needed in value.
  216. d Expected easing duration (in frames or seconds).
  217. Result The correct value.
  218. *)
  219. function easeOutQuart (t:Single; b:Single; c:Single; d:Single):Single;
  220. begin
  221. t := t/d-1;
  222. Result := -c * (t*t*t*t - 1) + b;
  223. end;
  224. (*
  225. Easing equation function for a quartic (t^4) easing in/out: acceleration until halfway, then deceleration.
  226. t Current time (in frames or seconds).
  227. b Starting value.
  228. c Change needed in value.
  229. d Expected easing duration (in frames or seconds).
  230. Result The correct value.
  231. *)
  232. function easeInOutQuart (t:Single; b:Single; c:Single; d:Single):Single;
  233. begin
  234. t := t/(d/2);
  235. if (t < 1) then
  236. Result := c/2*t*t*t*t + b
  237. else
  238. begin
  239. t := t-2;
  240. Result := -c/2 * (t*t*t*t - 2) + b;
  241. end;
  242. end;
  243. (*
  244. Easing equation function for a quartic (t^4) easing out/in: deceleration until halfway, then acceleration.
  245. t Current time (in frames or seconds).
  246. b Starting value.
  247. c Change needed in value.
  248. d Expected easing duration (in frames or seconds).
  249. Result The correct value.
  250. *)
  251. function easeOutInQuart (t:Single; b:Single; c:Single; d:Single):Single;
  252. begin
  253. if (t < d/2) then
  254. Result := easeOutQuart (t*2, b, c/2, d)
  255. else
  256. Result := easeInQuart((t*2)-d, b+c/2, c/2, d);
  257. end;
  258. (*
  259. Easing equation function for a quintic (t^5) easing in: accelerating from zero velocity.
  260. t Current time (in frames or seconds).
  261. b Starting value.
  262. c Change needed in value.
  263. d Expected easing duration (in frames or seconds).
  264. Result The correct value.
  265. *)
  266. function easeInQuint (t:Single; b:Single; c:Single; d:Single):Single;
  267. begin
  268. t := t/d;
  269. Result := c*t*t*t*t*t + b;
  270. end;
  271. (*
  272. Easing equation function for a quintic (t^5) easing out: decelerating from zero velocity.
  273. t Current time (in frames or seconds).
  274. b Starting value.
  275. c Change needed in value.
  276. d Expected easing duration (in frames or seconds).
  277. Result The correct value.
  278. *)
  279. function easeOutQuint (t:Single; b:Single; c:Single; d:Single):Single;
  280. begin
  281. t := t/d-1;
  282. Result := c*(t*t*t*t*t + 1) + b;
  283. end;
  284. (*
  285. Easing equation function for a quintic (t^5) easing in/out: acceleration until halfway, then deceleration.
  286. t Current time (in frames or seconds).
  287. b Starting value.
  288. c Change needed in value.
  289. d Expected easing duration (in frames or seconds).
  290. Result The correct value.
  291. *)
  292. function easeInOutQuint (t:Single; b:Single; c:Single; d:Single):Single;
  293. begin
  294. t := t/(d/2);
  295. if (t < 1) then
  296. Result := c/2*t*t*t*t*t + b
  297. else
  298. begin
  299. t := t-2;
  300. Result := c/2*(t*t*t*t*t + 2) + b;
  301. end;
  302. end;
  303. (*
  304. Easing equation function for a quintic (t^5) easing out/in: deceleration until halfway, then acceleration.
  305. t Current time (in frames or seconds).
  306. b Starting value.
  307. c Change needed in value.
  308. d Expected easing duration (in frames or seconds).
  309. Result The correct value.
  310. *)
  311. function easeOutInQuint (t:Single; b:Single; c:Single; d:Single):Single;
  312. begin
  313. if (t < d/2) then
  314. Result := easeOutQuint (t*2, b, c/2, d)
  315. else
  316. Result := easeInQuint((t*2)-d, b+c/2, c/2, d);
  317. end;
  318. (*
  319. Easing equation function for a sinusoidal (sin(t)) easing in: accelerating from zero velocity.
  320. t Current time (in frames or seconds).
  321. b Starting value.
  322. c Change needed in value.
  323. d Expected easing duration (in frames or seconds).
  324. Result The correct value.
  325. *)
  326. function easeInSine (t:Single; b:Single; c:Single; d:Single):Single;
  327. begin
  328. Result := -c * cos(t/d * (PI/2)) + c + b;
  329. end;
  330. (*
  331. Easing equation function for a sinusoidal (sin(t)) easing out: decelerating from zero velocity.
  332. t Current time (in frames or seconds).
  333. b Starting value.
  334. c Change needed in value.
  335. d Expected easing duration (in frames or seconds).
  336. Result The correct value.
  337. *)
  338. function easeOutSine (t:Single; b:Single; c:Single; d:Single):Single;
  339. begin
  340. Result := c * sin(t/d * (PI/2)) + b;
  341. end;
  342. (*
  343. Easing equation function for a sinusoidal (sin(t)) easing in/out: acceleration until halfway, then deceleration.
  344. t Current time (in frames or seconds).
  345. b Starting value.
  346. c Change needed in value.
  347. d Expected easing duration (in frames or seconds).
  348. Result The correct value.
  349. *)
  350. function easeInOutSine (t:Single; b:Single; c:Single; d:Single):Single;
  351. begin
  352. Result := -c/2 * (cos(PI*t/d) - 1) + b;
  353. end;
  354. (*
  355. Easing equation function for a sinusoidal (sin(t)) easing out/in: deceleration until halfway, then acceleration.
  356. t Current time (in frames or seconds).
  357. b Starting value.
  358. c Change needed in value.
  359. d Expected easing duration (in frames or seconds).
  360. Result The correct value.
  361. *)
  362. function easeOutInSine (t:Single; b:Single; c:Single; d:Single):Single;
  363. begin
  364. if (t < d/2) then
  365. Result := easeOutSine (t*2, b, c/2, d)
  366. else
  367. Result := easeInSine((t*2)-d, b+c/2, c/2, d);
  368. end;
  369. (*
  370. Easing equation function for an exponential (2^t) easing in: accelerating from zero velocity.
  371. t Current time (in frames or seconds).
  372. b Starting value.
  373. c Change needed in value.
  374. d Expected easing duration (in frames or seconds).
  375. Result The correct value.
  376. *)
  377. function easeInExpo (t:Single; b:Single; c:Single; d:Single):Single;
  378. begin
  379. if t=0 then
  380. Result := b
  381. else
  382. Result := c * Power(2, 10 * (t/d - 1)) + b - c * 0.001;
  383. end;
  384. (*
  385. Easing equation function for an exponential (2^t) easing out: decelerating from zero velocity.
  386. t Current time (in frames or seconds).
  387. b Starting value.
  388. c Change needed in value.
  389. d Expected easing duration (in frames or seconds).
  390. Result The correct value.
  391. *)
  392. function easeOutExpo (t:Single; b:Single; c:Single; d:Single):Single;
  393. begin
  394. if t=d then
  395. Result := b+c
  396. else
  397. Result := c * 1.001 * (-Power(2, -10 * t/d) + 1) + b;
  398. end;
  399. (*
  400. Easing equation function for an exponential (2^t) easing in/out: acceleration until halfway, then deceleration.
  401. t Current time (in frames or seconds).
  402. b Starting value.
  403. c Change needed in value.
  404. d Expected easing duration (in frames or seconds).
  405. Result The correct value.
  406. *)
  407. function easeInOutExpo (t:Single; b:Single; c:Single; d:Single):Single;
  408. begin
  409. if (t=0) then
  410. Result := b
  411. else
  412. if (t=d) then
  413. Result := b+c
  414. else
  415. begin
  416. t := t/(d/2);
  417. if (t < 1) then
  418. Result := c/2 * Power(2, 10 * (t - 1)) + b - c * 0.0005
  419. else
  420. begin
  421. t := t-1;
  422. Result := c/2 * 1.0005 * (-Power(2, -10 * t) + 2) + b;
  423. end;
  424. end;
  425. end;
  426. (*
  427. Easing equation function for an exponential (2^t) easing out/in: deceleration until halfway, then acceleration.
  428. t Current time (in frames or seconds).
  429. b Starting value.
  430. c Change needed in value.
  431. d Expected easing duration (in frames or seconds).
  432. Result The correct value.
  433. *)
  434. function easeOutInExpo (t:Single; b:Single; c:Single; d:Single):Single; begin
  435. if (t < d/2) then
  436. Result := easeOutExpo (t*2, b, c/2, d)
  437. else
  438. Result := easeInExpo((t*2)-d, b+c/2, c/2, d);
  439. end;
  440. (*
  441. Easing equation function for a circular (sqrt(1-t^2)) easing in: accelerating from zero velocity.
  442. t Current time (in frames or seconds).
  443. b Starting value.
  444. c Change needed in value.
  445. d Expected easing duration (in frames or seconds).
  446. Result The correct value.
  447. *)
  448. function easeInCirc (t:Single; b:Single; c:Single; d:Single):Single;
  449. begin
  450. t := t/d;
  451. Result := -c * (Sqrt(1 - t*t) - 1) + b;
  452. end;
  453. (*
  454. Easing equation function for a circular (sqrt(1-t^2)) easing out: decelerating from zero velocity.
  455. t Current time (in frames or seconds).
  456. b Starting value.
  457. c Change needed in value.
  458. d Expected easing duration (in frames or seconds).
  459. Result The correct value.
  460. *)
  461. function easeOutCirc (t:Single; b:Single; c:Single; d:Single):Single;
  462. begin
  463. t := t/d-1;
  464. Result := c * Sqrt(1 - t*t) + b;
  465. end;
  466. (*
  467. Easing equation function for a circular (sqrt(1-t^2)) easing in/out: acceleration until halfway, then deceleration.
  468. t Current time (in frames or seconds).
  469. b Starting value.
  470. c Change needed in value.
  471. d Expected easing duration (in frames or seconds).
  472. Result The correct value.
  473. *)
  474. function easeInOutCirc (t:Single; b:Single; c:Single; d:Single):Single;
  475. begin
  476. t := t/(d/2);
  477. if (t < 1) then
  478. Result := -c/2 * (Sqrt(1 - t*t) - 1) + b
  479. else
  480. begin
  481. t := t-2;
  482. Result := c/2 * (Sqrt(1 - t*t) + 1) + b;
  483. end;
  484. end;
  485. (*
  486. Easing equation function for a circular (sqrt(1-t^2)) easing out/in: deceleration until halfway, then acceleration.
  487. t Current time (in frames or seconds).
  488. b Starting value.
  489. c Change needed in value.
  490. d Expected easing duration (in frames or seconds).
  491. Result The correct value.
  492. *)
  493. function easeOutInCirc (t:Single; b:Single; c:Single; d:Single):Single;
  494. begin
  495. if (t < d/2) then
  496. Result := easeOutCirc (t*2, b, c/2, d)
  497. else
  498. Result := easeInCirc((t*2)-d, b+c/2, c/2, d);
  499. end;
  500. (*
  501. Easing equation function for an elastic (exponentially decaying sine wave) easing in: accelerating from zero velocity.
  502. t Current time (in frames or seconds).
  503. b Starting value.
  504. c Change needed in value.
  505. d Expected easing duration (in frames or seconds).
  506. a Amplitude.
  507. p Period.
  508. Result The correct value.
  509. *)
  510. function easeInElastic (t:Single; b:Single; c:Single; d:Single):Single;
  511. var
  512. p, a, s: Single;
  513. begin
  514. if t=0 then
  515. Result := b
  516. else
  517. begin
  518. t := t/d;
  519. if t=1 then
  520. Result := b+c
  521. else
  522. begin
  523. p := d*0.3;
  524. a := c;
  525. s := p/4;
  526. t := t-1;
  527. Result := -((a * Power(2, 10*t)) * sin((t*d-s)*(2*PI)/p )) + b;
  528. end;
  529. end;
  530. end;
  531. (*
  532. Easing equation function for an elastic (exponentially decaying sine wave) easing out: decelerating from zero velocity.
  533. t Current time (in frames or seconds).
  534. b Starting value.
  535. c Change needed in value.
  536. d Expected easing duration (in frames or seconds).
  537. a Amplitude.
  538. p Period.
  539. Result The correct value.
  540. *)
  541. function easeOutElastic (t:Single; b:Single; c:Single; d:Single):Single;
  542. var
  543. p, a, s: Single;
  544. begin
  545. if t=0 then
  546. Result := b
  547. else
  548. begin
  549. t := t/d;
  550. if t=1 then
  551. Result := b+c
  552. else
  553. begin
  554. p := d*0.3;
  555. a := c;
  556. s := p/4;
  557. Result := (a* Power(2,-10*t) * sin( (t*d-s)*(2*PI)/p ) + c + b);
  558. end;
  559. end;
  560. end;
  561. (*
  562. Easing equation function for an elastic (exponentially decaying sine wave) easing in/out: acceleration until halfway, then deceleration.
  563. t Current time (in frames or seconds).
  564. b Starting value.
  565. c Change needed in value.
  566. d Expected easing duration (in frames or seconds).
  567. a Amplitude.
  568. p Period.
  569. Result The correct value.
  570. *)
  571. function easeInOutElastic (t:Single; b:Single; c:Single; d:Single):Single;
  572. var
  573. p, a, s: Single;
  574. begin
  575. if t=0 then
  576. Result := b
  577. else
  578. begin
  579. t := t/(d/2);
  580. if t=2 then
  581. Result := b+c
  582. else
  583. begin
  584. p := d*0.3*1.5;
  585. a := c;
  586. s := p/4;
  587. t := t-1;
  588. if t<1 then
  589. Result := -0.5* ((a* Power(2,10*t)) * sin( (t*d-s)*(2*PI)/p)) + b
  590. else
  591. Result := (a* Power(2,-10*t)) * sin( (t*d-s)*(2*PI)/p )*0.5 + c + b;
  592. end;
  593. end;
  594. end;
  595. (*
  596. Easing equation function for an elastic (exponentially decaying sine wave) easing out/in: deceleration until halfway, then acceleration.
  597. t Current time (in frames or seconds).
  598. b Starting value.
  599. c Change needed in value.
  600. d Expected easing duration (in frames or seconds).
  601. a Amplitude.
  602. p Period.
  603. Result The correct value.
  604. *)
  605. function easeOutInElastic (t:Single; b:Single; c:Single; d:Single):Single;
  606. begin
  607. if (t < d/2) then
  608. Result := easeOutElastic (t*2, b, c/2, d)
  609. else
  610. Result := easeInElastic((t*2)-d, b+c/2, c/2, d)
  611. end;
  612. (*
  613. Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in: accelerating from zero velocity.
  614. t Current time (in frames or seconds).
  615. b Starting value.
  616. c Change needed in value.
  617. d Expected easing duration (in frames or seconds).
  618. s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
  619. Result The correct value.
  620. *)
  621. function easeInBack (t:Single; b:Single; c:Single; d:Single):Single;
  622. var
  623. s: Single;
  624. begin
  625. s := 1.70158;
  626. t := t/d;
  627. Result := c*t*t*((s+1)*t - s) + b;
  628. end;
  629. (*
  630. Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out: decelerating from zero velocity.
  631. t Current time (in frames or seconds).
  632. b Starting value.
  633. c Change needed in value.
  634. d Expected easing duration (in frames or seconds).
  635. s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
  636. Result The correct value.
  637. *)
  638. function easeOutBack (t:Single; b:Single; c:Single; d:Single):Single;
  639. var
  640. s: Single;
  641. begin
  642. s := 1.70158;
  643. t := t/d-1;
  644. Result := c*(t*t*((s+1)*t + s) + 1) + b;
  645. end;
  646. (*
  647. Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out: acceleration until halfway, then deceleration.
  648. t Current time (in frames or seconds).
  649. b Starting value.
  650. c Change needed in value.
  651. d Expected easing duration (in frames or seconds).
  652. s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
  653. Result The correct value.
  654. *)
  655. function easeInOutBack (t:Single; b:Single; c:Single; d:Single):Single;
  656. var
  657. s: Single;
  658. begin
  659. s := 1.70158;
  660. t := t/(d/2);
  661. s := s*1.525;
  662. if (t < 1) then
  663. Result := c/2*(t*t*((s+1)*t - s)) + b
  664. else
  665. begin
  666. t := t-2;
  667. Result := c/2*(t*t*((s+1)*t + s) + 2) + b;
  668. end;
  669. end;
  670. (*
  671. Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in: deceleration until halfway, then acceleration.
  672. t Current time (in frames or seconds).
  673. b Starting value.
  674. c Change needed in value.
  675. d Expected easing duration (in frames or seconds).
  676. s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
  677. Result The correct value.
  678. *)
  679. function easeOutInBack (t:Single; b:Single; c:Single; d:Single):Single;
  680. begin
  681. if (t < d/2) then
  682. Result := easeOutBack (t*2, b, c/2, d)
  683. else
  684. Result := easeInBack((t*2)-d, b+c/2, c/2, d);
  685. end;
  686. (*
  687. Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out: decelerating from zero velocity.
  688. t Current time (in frames or seconds).
  689. b Starting value.
  690. c Change needed in value.
  691. d Expected easing duration (in frames or seconds).
  692. Result The correct value.
  693. *)
  694. function easeOutBounce (t:Single; b:Single; c:Single; d:Single):Single;
  695. begin
  696. t := t/d;
  697. if t < (1/2.75) then
  698. Result := c*(7.5625*t*t) + b
  699. else
  700. if t < (2/2.75) then
  701. begin
  702. t := t-(1.5/2.75);
  703. Result := c*(7.5625*t*t + 0.75) + b;
  704. end
  705. else
  706. if (t < (2.5/2.75)) then
  707. begin
  708. t := t-(2.25/2.75);
  709. Result := c*(7.5625*t*t + 0.9375) + b;
  710. end
  711. else
  712. begin
  713. t := t-(2.625/2.75);
  714. Result := c*(7.5625*t*t + 0.984375) + b;
  715. end;
  716. end;
  717. (*
  718. Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in: accelerating from zero velocity.
  719. t Current time (in frames or seconds).
  720. b Starting value.
  721. c Change needed in value.
  722. d Expected easing duration (in frames or seconds).
  723. * Result The correct value.
  724. *)
  725. function easeInBounce (t:Single; b:Single; c:Single; d:Single):Single;
  726. begin
  727. Result := c - easeOutBounce (d-t, 0, c, d) + b;
  728. end;
  729. (*
  730. Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in/out: acceleration until halfway, then deceleration.
  731. t Current time (in frames or seconds).
  732. b Starting value.
  733. c Change needed in value.
  734. d Expected easing duration (in frames or seconds).
  735. * Result The correct value.
  736. *)
  737. function easeInOutBounce (t:Single; b:Single; c:Single; d:Single):Single;
  738. begin
  739. if (t < d/2) then
  740. Result := easeInBounce(t*2, 0, c, d) * 0.5 + b
  741. else
  742. Result := easeOutBounce(t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
  743. end;
  744. (*
  745. Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out/in: deceleration until halfway, then acceleration.
  746. t Current time (in frames or seconds).
  747. b Starting value.
  748. c Change needed in value.
  749. d Expected easing duration (in frames or seconds).
  750. Result The correct value.
  751. *)
  752. function easeOutInBounce (t:Single; b:Single; c:Single; d:Single):Single;
  753. begin
  754. if (t < d/2) then
  755. Result := easeOutBounce(t*2, b, c/2, d)
  756. else
  757. Result := easeInBounce((t*2)-d, b+c/2, c/2, d);
  758. end;
  759. function GetEaseFunctionFromType(const EaseType: TEaseType): TEaseFunction;
  760. begin
  761. case EaseType of
  762. etLinear : Result := easeNone;
  763. etQuadIn : Result := easeInQuad;
  764. etQuadOut : Result := easeOutQuad;
  765. etQuadInOut : Result := easeInOutQuad;
  766. etQuadOutIn : Result := easeOutInQuad;
  767. etCubicIn : Result := easeInCubic;
  768. etCubicOut : Result := easeOutCubic;
  769. etCubicInOut : Result := easeInOutCubic;
  770. etCubicOutIn : Result := easeOutInCubic;
  771. etQuintIn : Result := easeInQuint;
  772. etQuintOut : Result := easeOutQuint;
  773. etQuintInOut : Result := easeInOutQuint;
  774. etQuintOutIn : Result := easeOutInQuint;
  775. etSineIn : Result := easeInSine;
  776. etSineOut : Result := easeOutSine;
  777. etSineInOut : Result := easeInOutSine;
  778. etSineOutIn : Result := easeOutInSine;
  779. etCircIn : Result := easeInCirc;
  780. etCircOut : Result := easeOutCirc;
  781. etCircInOut : Result := easeInOutCirc;
  782. etCircOutIn : Result := easeOutInCirc;
  783. etExpoIn : Result := easeInExpo;
  784. etExpoOut : Result := easeOutExpo;
  785. etExpoInOut : Result := easeInOutExpo;
  786. etExpoOutIn : Result := easeOutInExpo;
  787. etElasticIn : Result := easeInElastic;
  788. etElasticOut : Result := easeOutElastic;
  789. etElasticInOut : Result := easeInOutElastic;
  790. etElasticOutIn : Result := easeOutInElastic;
  791. etBackIn : Result := easeInBack;
  792. etBackOut : Result := easeOutBack;
  793. etBackInOut : Result := easeInOutBack;
  794. etBackOutIn : Result := easeOutBack;
  795. etBounceIn : Result := easeInBounce;
  796. etBounceOut : Result := easeOutBounce;
  797. etBounceInOut : Result := easeInOutBounce;
  798. etBounceOutIn : Result := easeOutInBounce
  799. else
  800. Result := nil;
  801. end;
  802. end;
  803. function Tweener(Current, Target: TAffineVector; Time, Duration: Single; EaseType: TEaseType): TAffineVector;
  804. var
  805. i: integer;
  806. EaseFunction : TEaseFunction;
  807. begin
  808. if Time > Duration then
  809. Time := Duration;
  810. EaseFunction := GetEaseFunctionFromType(EaseType);
  811. for i := 0 to 2 do
  812. begin
  813. Target.V[i] := Target.V[i]-Current.V[i];
  814. Result.V[i] := EaseFunction(Time, Current.V[i], Target.V[i], Duration);
  815. end;
  816. end;
  817. function Tweener(Current, Target: TGLVector; Time, Duration: Single; EaseType: TEaseType): TGLVector;
  818. var
  819. i: integer;
  820. EaseFunction : TEaseFunction;
  821. begin
  822. if Time > Duration then
  823. Time := Duration;
  824. EaseFunction := GetEaseFunctionFromType(EaseType);
  825. for i := 0 to 3 do
  826. begin
  827. Target.V[i] := Target.V[i]-Current.V[i];
  828. Result.V[i] := EaseFunction(Time, Current.V[i], Target.V[i], Duration);
  829. end;
  830. end;
  831. function Tweener(Current, Target: TVector2f; Time, Duration: Single; EaseType: TEaseType): TVector2f;
  832. var
  833. i: integer;
  834. EaseFunction : TEaseFunction;
  835. begin
  836. if Time > Duration then
  837. Time := Duration;
  838. EaseFunction := GetEaseFunctionFromType(EaseType);
  839. for i := 0 to 1 do
  840. begin
  841. Target.V[i] := Target.V[i]-Current.V[i];
  842. Result.V[i] := EaseFunction(Time, Current.V[i], Target.V[i], Duration);
  843. end;
  844. end;
  845. function Tweener(Current, Target: Single; Time, Duration: Single; EaseType: TEaseType): Single;
  846. var
  847. EaseFunction : TEaseFunction;
  848. begin
  849. if Time > Duration then
  850. Time := Duration;
  851. EaseFunction := GetEaseFunctionFromType(EaseType);
  852. Result := EaseFunction(Time, Current, Target-Current, Duration);
  853. end;
  854. //----------------------------------------------------------------------------
  855. end.