anim.luadoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. --[[ Animation library.
  2. <br/>This library gives you functions to animate a variable from one value to another.
  3. <br/>You can group and chain multiple animations, for instance grouping two animations that
  4. <br/>animate the x and y positions of an object.
  5. <br/>How anim interpolates between the two values are set by animation styles such as linear, quadIn etc.
  6. <br/>To load do the following.
  7. <pre class="example">
  8. anim = require 'anim/anim'<br/>
  9. function love.update(dt)<br/>
  10. anim:update(dt)<br/>
  11. end
  12. </pre>
  13. @release 2010-02-26 Version 1
  14. ]]
  15. module 'anim'
  16. --[[ An animation table
  17. @class table
  18. @name anim_table
  19. @field table The table to pass to the anim object
  20. @field key The key to animate. (table[key] will be animated).
  21. @field start The value the animation starts at. (if nil current table[key] value will be used).
  22. @field finish The value the animation finishes at. (if nil current table[key] value will be used).
  23. @field time The time it takes for the animation to complete. (in seconds, default 1).
  24. @field delay The time it takes for the animation to start after calling anim:play(). (in seconds, default 0).
  25. @field style The style of the animation. (a string, default 'linear'). <a href='#styles'>See style</a>.
  26. @field styleargs Some styles such as 'elastic' can take extra parameters in the form of a table {arg1,arg2}.
  27. @see styles
  28. ]]
  29. --[[ List of animation styles you can use.
  30. @class table
  31. @name styles
  32. @field linear
  33. @field quartIn
  34. @field quartOut
  35. @field quartInOut
  36. @field quadIn
  37. @field quadOut
  38. @field quadInOut
  39. @field expoIn
  40. @field expoOut
  41. @field expoInOut
  42. @field elastic
  43. ]]
  44. --[[ Create a new animation instance.
  45. @name anim:new
  46. @class function
  47. @param anim_table A table of animation paremeters.
  48. @return table: An animation instance you can call anim methods on.
  49. @see anim_table, Sliding text
  50. @usage <pre class="example">
  51. new_animation = anim:new{
  52. table = myTable,
  53. key = 'x_position',
  54. start = 12,
  55. finish = 120,
  56. time = 4,
  57. style = 'linear'
  58. }
  59. </pre>
  60. ]]
  61. --[[ Updates all animations, use inside love.update().
  62. @param dt:number delta time
  63. ]]
  64. function anim:update(dt)
  65. --[[ Plays or resumes the animation ]]
  66. function anim:play()
  67. --[[ Reverse the animation. ]]
  68. function anim:reverse()
  69. --[[ Pause the animation. ]]
  70. function anim:pause()
  71. --[[ Finish the animation. (Instantaneus) ]]
  72. function anim:finish()
  73. --[[ Callback function when the animation is finished.
  74. @usage <pre class="example">
  75. new_anim = anim:new( anim_table )
  76. function new_anim:onFinish()
  77. print('animation finished')
  78. end
  79. </pre>
  80. ]]
  81. function anim:onFinish()
  82. --[[ Callback function when anim:play() is called
  83. @see anim:onFinish
  84. ]]
  85. function anim:onPlay()
  86. --[[ Callback function when anim:pause() is called
  87. @see anim:onFinish
  88. ]]
  89. function anim:onPause()
  90. --[[ Convenience function for anim:new(). Creates an animation instance.
  91. <br/>The animation will automatically play.
  92. @param table:table The table holding the value you wish to animate.
  93. @param key:string The key's value you wish to animate.
  94. @param start:number The starting value.
  95. @param finish:number The finishing value.
  96. @param time:number The time it takes for the animation to complete. (In seconds)
  97. @param style:string The style of the animation.
  98. @return table: An animation instance.
  99. @see styles, anim_table, anim:new
  100. ]]
  101. function anim:easy( table, key, start, finish, time, style )
  102. --[[ Animates an object from it's current position to another.
  103. <br/>The object must have x and y keys!
  104. <br/>The animation will automatically play.
  105. @param object:table An object with 'x' and 'y' keys.
  106. @param x:number The target x position.
  107. @param y:number The target y position.
  108. @param time:number The time it takes for the animation to complete. (In seconds)
  109. @param style:string The style of the animation.
  110. @param delay:number The time it waits before starting the animation. (In seconds)
  111. @return table: An animation instance.
  112. @see anim_table, styles
  113. ]]
  114. function anim:moveTo( object, x, y, time, style, delay )
  115. --[[ Animate text to slide in from offscreen.
  116. @name Sliding text
  117. @class example
  118. @usage Here's how:
  119. <pre class="example">
  120. anim = require 'anim/anim'
  121. function love.load()
  122. text = { x = -100, str = 'Woo!' }
  123. new_anim = anim:new{
  124. table = text,
  125. key = 'x',
  126. start = -100
  127. finish = 100,
  128. time = 2,
  129. style = 'quadInOut'
  130. }
  131. new_anim:play()
  132. end
  133. function love.update(dt)
  134. anim:update(dt)
  135. end
  136. function love.draw()
  137. love.graphics.print(text.str, text.x, 100)
  138. end
  139. </pre>
  140. ]]
  141. --------------- anim.group
  142. --[[ Creates an animation group which can hold individual animations.
  143. @param ... multiple animations to group.
  144. @return table: an animation group.
  145. ]]
  146. function anim.group:new( ... )
  147. --[[ Plays/resumes all the animations in the group together ]]
  148. function anim.group:play()
  149. --[[ Pauses all the animations in the group ]]
  150. function anim.group:pause()
  151. --[[ Reverses all the animations in the group ]]
  152. function anim.group:reverse()
  153. --[[ Callback function when all the animations in the group have finished ]]
  154. function anim.group:onFinish()
  155. --[[ Sets the time period of every animation in the group
  156. @param time:number time in seconds.
  157. ]]
  158. function anim.group:setTime( time )
  159. ----------------- anim.chain
  160. --[[ Creates an animation chain that plays multiple animations one after another.
  161. @param ... a list of animations to chain, in order.
  162. @return table: an animation chain.
  163. ]]
  164. function anim.chain:new(...)
  165. --[[ Plays/resumes the animation chain. ]]
  166. function anim.chain:play()
  167. --[[ Pauses the animation chain ]]
  168. function anim.chain:pause()
  169. --[[ Callback function when the last animation in the chain finishes ]]
  170. function anim.chain:onFinish(...)