Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. function PFTween(from, to, durationMilliseconds) {
  2. const Animation = require('Animation');
  3. const Time = require('Time');
  4. const Reactive = require('Reactive');
  5. const samplers = {
  6. linear: (from, to) => Animation.samplers.linear(from, to),
  7. easeInQuad: (from, to) => Animation.samplers.easeInQuad(from, to),
  8. easeOutQuad: (from, to) => Animation.samplers.easeOutQuad(from, to),
  9. easeInOutQuad: (from, to) => Animation.samplers.easeInOutQuad(from, to),
  10. easeInCubic: (from, to) => Animation.samplers.easeInCubic(from, to),
  11. easeOutCubic: (from, to) => Animation.samplers.easeOutCubic(from, to),
  12. easeInOutCubic: (from, to) => Animation.samplers.easeInOutCubic(from, to),
  13. easeInQuart: (from, to) => Animation.samplers.easeInQuart(from, to),
  14. easeOutQuart: (from, to) => Animation.samplers.easeOutQuart(from, to),
  15. easeInOutQuart: (from, to) => Animation.samplers.easeInOutQuart(from, to),
  16. easeInQuint: (from, to) => Animation.samplers.easeInQuint(from, to),
  17. easeOutQuint: (from, to) => Animation.samplers.easeOutQuint(from, to),
  18. easeInOutQuint: (from, to) => Animation.samplers.easeInOutQuint(from, to),
  19. easeInSine: (from, to) => Animation.samplers.easeInSine(from, to),
  20. easeOutSine: (from, to) => Animation.samplers.easeOutSine(from, to),
  21. easeInOutSine: (from, to) => Animation.samplers.easeInOutSine(from, to),
  22. easeInExpo: (from, to) => Animation.samplers.easeInExpo(from, to),
  23. easeOutExpo: (from, to) => Animation.samplers.easeOutExpo(from, to),
  24. easeInOutExpo: (from, to) => Animation.samplers.easeInOutExpo(from, to),
  25. easeInCirc: (from, to) => Animation.samplers.easeInCirc(from, to),
  26. easeOutCirc: (from, to) => Animation.samplers.easeOutCirc(from, to),
  27. easeInOutCirc: (from, to) => Animation.samplers.easeInOutCirc(from, to),
  28. easeInBack: (from, to) => Animation.samplers.easeInBack(from, to),
  29. easeOutBack: (from, to) => Animation.samplers.easeOutBack(from, to),
  30. easeInOutBack: (from, to) => Animation.samplers.easeInOutBack(from, to),
  31. easeInElastic: (from, to) => Animation.samplers.easeInElastic(from, to),
  32. easeOutElastic: (from, to) => Animation.samplers.easeOutElastic(from, to),
  33. easeInOutElastic: (from, to) => Animation.samplers.easeInOutElastic(from, to),
  34. easeInBounce: (from, to) => Animation.samplers.easeInBounce(from, to),
  35. easeOutBounce: (from, to) => Animation.samplers.easeOutBounce(from, to),
  36. easeInOutBounce: (from, to) => Animation.samplers.easeInOutBounce(from, to)
  37. };
  38.  
  39. let _mirror = false;
  40. let _loopCount = 1;
  41. let _samplers = 'easeInOutSine';
  42. let dirver, sampler, animate, _delayMilliseconds, delayTimer;
  43. let _onCompletedCallback = [];
  44. let _onStartCallback = [];
  45. let _onLoop = [];
  46. let hasRegistered_onCompleted, hasRegistered_onLoop;
  47.  
  48. function Refresh() {
  49. dirver = Animation.timeDriver({
  50. durationMilliseconds: durationMilliseconds,
  51. loopCount: _loopCount,
  52. mirror: _mirror
  53. })
  54.  
  55. if (typeof from.pinLastValue != undefined && from.pinLastValue) {
  56. sampler = samplers[_samplers](from.pinLastValue(), to);
  57. } else {
  58. sampler = samplers[_samplers](from, to);
  59. }
  60.  
  61. animate = Animation.animate(dirver, sampler);
  62.  
  63. if (_onCompletedCallback.length != 0 && !hasRegistered_onCompleted) {
  64. hasRegistered_onCompleted = true;
  65. dirver.onCompleted().subscribe(() => _onCompletedCallback.forEach(e => e()));
  66. }
  67.  
  68. if (_delayMilliseconds != undefined) {
  69.  
  70. if (delayTimer != undefined)
  71. Time.clearTimeout(delayTimer);
  72.  
  73. delayTimer = Time.setTimeout(() => {
  74. dirver.start();
  75. if (_onStartCallback.length != 0) {
  76. _onStartCallback.forEach(e => e());
  77. }
  78. }, _delayMilliseconds);
  79.  
  80. if (_onLoop.length != 0 && !hasRegistered_onLoop) {
  81. hasRegistered_onLoop = true;
  82. let _loop = 0;
  83. Time.setTimeout(() => {
  84. const _timer = Time.setInterval(() => {
  85. _onLoop.forEach(e => e());
  86. _loop++;
  87. if (_loop == _loopCount - 1) {
  88. Time.clearInterval(_timer);
  89. }
  90. }, durationMilliseconds);
  91. }, _delayMilliseconds);
  92. }
  93. }
  94. else {
  95. dirver.start();
  96.  
  97. if (_onLoop.length != 0 && !hasRegistered_onLoop) {
  98. hasRegistered_onLoop = true;
  99. let _loop = 0;
  100. const _timer = Time.setInterval(() => {
  101. _onLoop.forEach(e => e());
  102. _loop++;
  103. if (_loop == _loopCount - 1) {
  104. Time.clearInterval(_timer);
  105. }
  106. }, durationMilliseconds);
  107. }
  108. }
  109. }
  110.  
  111. this.OnCompleted = callback => {
  112. _onCompletedCallback.push(callback);
  113. Refresh();
  114. return this;
  115. }
  116.  
  117. this.OnStart = callback => {
  118. _onStartCallback.push(callback);
  119. return this;
  120. }
  121.  
  122. this.SetMirror = isMrror => {
  123. _mirror = isMrror == undefined ? true : isMrror;
  124. Refresh();
  125. return this;
  126. }
  127.  
  128. this.SetLoop = loopCount => {
  129. _loopCount = loopCount == undefined ? Infinity : loopCount;
  130. Refresh();
  131. return this;
  132. }
  133.  
  134. this.SetEase = ease => {
  135. _samplers = ease;
  136. Refresh();
  137. return this;
  138. }
  139.  
  140. this.OnLoop = callback => {
  141. _onLoop.push(callback);
  142. Refresh();
  143. return this;
  144. }
  145.  
  146. this.SetDelay = delayMilliseconds => {
  147. _delayMilliseconds = delayMilliseconds;
  148. Refresh();
  149. return this;
  150. }
  151.  
  152. this.SetVisibleOnStart = sceneObj => {
  153. _onStartCallback.push(() => sceneObj.hidden = false);
  154. Refresh();
  155. return this;
  156. }
  157.  
  158. this.SetHiddenOnStart = sceneObj => {
  159. _onStartCallback.push(() => sceneObj.hidden = true);
  160. Refresh();
  161. return this;
  162. }
  163.  
  164. this.SetHiddenOnCompleted = sceneObj => {
  165. _onCompletedCallback.push(() => sceneObj.hidden = true);
  166. Refresh();
  167. return this;
  168. }
  169.  
  170. this.SetVisibleOnCompleted = sceneObj => {
  171. _onCompletedCallback.push(() => sceneObj.hidden = false);
  172. Refresh();
  173. return this;
  174. }
  175.  
  176. this.ToSignal = () => {
  177. return animate;
  178. }
  179.  
  180. this.ToScale = () => {
  181. return Reactive.scale(animate, animate, animate);
  182. }
  183.  
  184. Refresh();
  185.  
  186. return this;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement