2
0

tweens.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. class "TweenManager"
  2. function TweenManager:TweenManager()
  3. self.tweens = {}
  4. end
  5. function TweenManager:addTween(tween)
  6. self.tweens[count(self.tweens)+1] = tween
  7. end
  8. function TweenManager:Update(elapsed)
  9. for i=#self.tweens,1,-1 do
  10. self.tweens[i]:Update(elapsed)
  11. if self.tweens[i].complete == true then
  12. table.remove(self.tweens, i)
  13. end
  14. end
  15. end
  16. class "Tween" (EventDispatcher)
  17. Tween.EASE_NONE = 0
  18. Tween.EASE_IN_QUAD = 1
  19. Tween.EASE_OUT_QUAD = 2
  20. Tween.EASE_INOUT_QUAD = 3
  21. Tween.EASE_IN_CUBIC= 4
  22. Tween.EASE_OUT_CUBIC= 5
  23. Tween.EASE_INOUT_CUBIC= 6
  24. Tween.EASE_IN_QUART= 7
  25. Tween.EASE_OUT_QUART= 8
  26. Tween.EASE_INOUT_QUART= 9
  27. Tween.EASE_IN_QUINT= 10
  28. Tween.EASE_OUT_QUINT= 11
  29. Tween.EASE_INOUT_QUINT= 12
  30. Tween.EASE_IN_SINE= 13
  31. Tween.EASE_OUT_SINE= 14
  32. Tween.EASE_INOUT_SINE= 15
  33. Tween.EASE_IN_EXPO= 16
  34. Tween.EASE_OUT_EXPO= 17
  35. Tween.EASE_INOUT_EXPO= 18
  36. Tween.EASE_IN_CIRC= 19
  37. Tween.EASE_OUT_CIRC= 20
  38. Tween.EASE_INOUT_CIRC= 21
  39. Tween.EASE_IN_BOUNCE= 22
  40. Tween.EASE_OUT_BOUNCE = 23
  41. Tween.EASE_INOUT_BOUNCE = 24
  42. function Tween.fEASE_IN_QUAD(t, startVal, cVal, endTime)
  43. t = t / endTime
  44. return cVal*t*t + startVal
  45. end
  46. function Tween.fEASE_OUT_QUAD(t, startVal, cVal, endTime)
  47. t = t / endTime
  48. return -cVal * t*(t-2.0) + startVal
  49. end
  50. function Tween.fEASE_INOUT_QUAD(t, startVal, cVal, endTime)
  51. t = t / (endTime/2.0)
  52. if t < 1.0 then return cVal/2.0*t*t + startVal end
  53. t = t - 1
  54. return -cVal/2.0 * (t*(t-2.0) - 1.0) + startVal
  55. end
  56. function Tween.fEASE_IN_CUBIC(t, startVal, cVal, endTime)
  57. t = t / endTime
  58. return cVal*t*t*t + startVal
  59. end
  60. function Tween.fEASE_OUT_CUBIC(t, startVal, cVal, endTime)
  61. t = t / endTime
  62. t = t - 1
  63. return cVal*(t*t*t + 1.0) + startVal
  64. end
  65. function Tween.fEASE_INOUT_CUBIC(t, startVal, cVal, endTime)
  66. t = t / (endTime/2.0)
  67. if t < 1.0 then return cVal/2.0*t*t*t + startVal end
  68. t = t - 2.0
  69. return cVal/2.0*(t*t*t + 2.0) + startVal
  70. end
  71. function Tween.fEASE_IN_QUART(t, startVal, cVal, endTime)
  72. t = t / endTime
  73. return cVal*t*t*t*t + startVal
  74. end
  75. function Tween.fEASE_OUT_QUART(t, startVal, cVal, endTime)
  76. t = t / endTime
  77. t = t - 1
  78. return -cVal * (t*t*t*t - 1.0) + startVal
  79. end
  80. function Tween.fEASE_INOUT_QUART(t, startVal, cVal, endTime)
  81. t = t / (endTime/2.0)
  82. if t < 1.0 then return (cVal/2.0*t*t*t*t) + startVal end
  83. t = t - 2.0
  84. return -cVal/2.0 * (t*t*t*t - 2.0) + startVal
  85. end
  86. function Tween.fEASE_IN_QUINT(t, startVal, cVal, endTime)
  87. t = t / endTime
  88. return cVal*t*t*t*t*t + startVal
  89. end
  90. function Tween.fEASE_OUT_QUINT(t, startVal, cVal, endTime)
  91. t = t / endTime
  92. t = t - 1
  93. return cVal*(t*t*t*t*t + 1.0) + startVal
  94. end
  95. function Tween.fEASE_INOUT_QUINT(t, startVal, cVal, endTime)
  96. t = t / (endTime / 2.0)
  97. if t < 1.0 then
  98. return (cVal/2.0*t*t*t*t*t) + startVal
  99. else
  100. t = t - 2.0
  101. return ((cVal/2.0)*((t*t*t*t*t) + 2.0)) + startVal
  102. end
  103. end
  104. function Tween.fEASE_IN_SINE(t, startVal, cVal, endTime)
  105. return -cVal * cos(t/endTime * (pi/2.0)) + cVal + startVal
  106. end
  107. function Tween.fEASE_OUT_SINE(t, startVal, cVal, endTime)
  108. return cVal * sin(t/endTime * (pi/2.0)) + startVal
  109. end
  110. function Tween.fEASE_INOUT_SINE(t, startVal, cVal, endTime)
  111. return -cVal/2.0 * (cos(pi*t/endTime) - 1.0) + startVal
  112. end
  113. function Tween.fEASE_IN_EXPO(t, startVal, cVal, endTime)
  114. return (cVal * pow( 2.0, 10.0 * (t/endTime - 1.0) ) + startVal)
  115. end
  116. function Tween.fEASE_OUT_EXPO(t, startVal, cVal, endTime)
  117. return cVal * ( -pow( 2.0, -10.0 * t/endTime ) + 1.0 ) + startVal
  118. end
  119. function Tween.fEASE_INOUT_EXPO(t, startVal, cVal, endTime)
  120. t = t / (endTime/2.0)
  121. if t < 1.0 then return cVal/2.0 * pow( 2.0, 10.0 * (t - 1.0) ) + startVal end
  122. t = t - 1
  123. return cVal/2.0 * ( -pow( 2.0, -10.0 * t) + 2.0 ) + startVal
  124. end
  125. function Tween.fEASE_IN_CIRC(t, startVal, cVal, endTime)
  126. t = t / endTime
  127. return -cVal * (sqrt(1.0 - t*t) - 1.0) + startVal
  128. end
  129. function Tween.fEASE_OUT_CIRC(t, startVal, cVal, endTime)
  130. t = t/endTime
  131. t = t - 1
  132. return cVal * sqrt(1.0 - t*t) + startVal
  133. end
  134. function Tween.fEASE_INOUT_CIRC(t, startVal, cVal, endTime)
  135. t = t/(endTime/2.0)
  136. if t < 1.0 then return -cVal/2.0 * (sqrt(1.0 - t*t) - 1.0) + startVal end
  137. t = t - 2.0
  138. return cVal/2.0 * (sqrt(1.0 - t*t) + 1.0) + startVal
  139. end
  140. function Tween.fEASE_IN_BOUNCE(t, startVal, cVal, endTime)
  141. t = t / endTime
  142. if t < 1/2.75 then
  143. return cVal*(7.5625*t*t) + startVal
  144. elseif t < (2/2.75) then
  145. t = t - (1.5/2.75)
  146. return cVal*(7.5625*(t)*t + .75) + startVal
  147. elseif (t < (2.5/2.75)) then
  148. t = t - (2.25/2.75)
  149. return cVal*(7.5625*(t)*t + .9375) + startVal
  150. else
  151. t = t - (2.625/2.75)
  152. return cVal*(7.5625*(t)*t + .984375) + startVal
  153. end
  154. end
  155. function Tween.fEASE_NONE(t, startVal, cVal, endTime)
  156. return cVal*t/endTime+startVal
  157. end
  158. Tween.fEASE_OUT_BOUNCE = Tween.fEASE_IN_BOUNCE
  159. Tween.fEASE_INOUT_BOUNCE = Tween.fEASE_IN_BOUNCE
  160. Tween.interpolateFunctions = {}
  161. Tween.interpolateFunctions[Tween.EASE_NONE] = Tween.fEASE_NONE
  162. Tween.interpolateFunctions[Tween.EASE_IN_QUAD] = Tween.fEASE_IN_QUAD
  163. Tween.interpolateFunctions[Tween.EASE_OUT_QUAD] = Tween.fEASE_OUT_QUAD
  164. Tween.interpolateFunctions[Tween.EASE_INOUT_QUAD] = Tween.fEASE_INOUT_QUAD
  165. Tween.interpolateFunctions[Tween.EASE_IN_CUBIC] = Tween.fEASE_IN_CUBIC
  166. Tween.interpolateFunctions[Tween.EASE_OUT_CUBIC] = Tween.fEASE_OUT_CUBIC
  167. Tween.interpolateFunctions[Tween.EASE_INOUT_CUBIC] = Tween.fEASE_INOUT_CUBIC
  168. Tween.interpolateFunctions[Tween.EASE_IN_QUART] = Tween.fEASE_IN_QUART
  169. Tween.interpolateFunctions[Tween.EASE_OUT_QUART] = Tween.fEASE_OUT_QUART
  170. Tween.interpolateFunctions[Tween.EASE_INOUT_QUART] = Tween.fEASE_INOUT_QUART
  171. Tween.interpolateFunctions[Tween.EASE_IN_QUINT] = Tween.fEASE_IN_QUINT
  172. Tween.interpolateFunctions[Tween.EASE_OUT_QUINT] = Tween.fEASE_OUT_QUINT
  173. Tween.interpolateFunctions[Tween.EASE_INOUT_QUINT] = Tween.fEASE_INOUT_QUINT
  174. Tween.interpolateFunctions[Tween.EASE_IN_SINE] = Tween.fEASE_IN_SINE
  175. Tween.interpolateFunctions[Tween.EASE_OUT_SINE] = Tween.fEASE_OUT_SINE
  176. Tween.interpolateFunctions[Tween.EASE_INOUT_SINE] = Tween.fEASE_INOUT_SINE
  177. Tween.interpolateFunctions[Tween.EASE_IN_EXPO] = Tween.fEASE_IN_EXPO
  178. Tween.interpolateFunctions[Tween.EASE_OUT_EXPO] = Tween.fEASE_OUT_EXPO
  179. Tween.interpolateFunctions[Tween.EASE_INOUT_EXPO] = Tween.fEASE_INOUT_EXPO
  180. Tween.interpolateFunctions[Tween.EASE_IN_CIRC] = Tween.fEASE_IN_CIRC
  181. Tween.interpolateFunctions[Tween.EASE_OUT_CIRC] = Tween.fEASE_OUT_CIRC
  182. Tween.interpolateFunctions[Tween.EASE_INOUT_CIRC] = Tween.fEASE_INOUT_CIRC
  183. Tween.interpolateFunctions[Tween.EASE_IN_BOUNCE] = Tween.fEASE_IN_BOUNCE
  184. Tween.interpolateFunctions[Tween.EASE_OUT_BOUNCE] = Tween.fEASE_OUT_BOUNCE
  185. Tween.interpolateFunctions[Tween.EASE_INOUT_BOUNCE] = Tween.fEASE_INOUT_BOUNCE
  186. function Tween.fEASE_NONE(t, startVal, cVal, endTime)
  187. return cVal*t/endTime+startVal
  188. end
  189. function Tween:Tween(target, key, easeType, startVal, endVal, time, repeating, waitTime)
  190. EventDispatcher.EventDispatcher(self)
  191. self.target = target
  192. self.key = key
  193. self.easeType = easeType
  194. self.startVal = startVal
  195. self.endVal = endVal
  196. self.endTime = time
  197. self.repeating = repeating
  198. self.waitTime =waitTime
  199. self.cVal = endVal - startVal
  200. self.tweenTime = 0
  201. self.complete = false
  202. Services.TweenManager:addTween(self)
  203. end
  204. function Tween:Reset()
  205. self.tweenTime = 0
  206. self.complete = false
  207. end
  208. function Tween:Update(elapsed)
  209. if self.tweenTime >= self.endTime + self.waitTime then
  210. if self.repeating == true then
  211. self:Reset()
  212. else
  213. self.target[self.key] = self.endVal
  214. self.complete = true
  215. return
  216. end
  217. end
  218. if self.tweenTime > self.waitTime then
  219. self.target[self.key] = self:interpolateTween()
  220. end
  221. self.tweenTime = self.tweenTime + elapsed
  222. end
  223. function Tween:interpolateTween()
  224. return Tween.interpolateFunctions[self.easeType](self.tweenTime - self.waitTime, self.startVal, self.cVal, self.endTime)
  225. end