transition.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. --[[
  2. Copyright (c) 2011-2014 chukong-inc.com
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. ]]
  19. local transition = {}
  20. local ACTION_EASING = {}
  21. ACTION_EASING["BACKIN"] = {cc.EaseBackIn, 1}
  22. ACTION_EASING["BACKINOUT"] = {cc.EaseBackInOut, 1}
  23. ACTION_EASING["BACKOUT"] = {cc.EaseBackOut, 1}
  24. ACTION_EASING["BOUNCE"] = {cc.EaseBounce, 1}
  25. ACTION_EASING["BOUNCEIN"] = {cc.EaseBounceIn, 1}
  26. ACTION_EASING["BOUNCEINOUT"] = {cc.EaseBounceInOut, 1}
  27. ACTION_EASING["BOUNCEOUT"] = {cc.EaseBounceOut, 1}
  28. ACTION_EASING["ELASTIC"] = {cc.EaseElastic, 2, 0.3}
  29. ACTION_EASING["ELASTICIN"] = {cc.EaseElasticIn, 2, 0.3}
  30. ACTION_EASING["ELASTICINOUT"] = {cc.EaseElasticInOut, 2, 0.3}
  31. ACTION_EASING["ELASTICOUT"] = {cc.EaseElasticOut, 2, 0.3}
  32. ACTION_EASING["EXPONENTIALIN"] = {cc.EaseExponentialIn, 1}
  33. ACTION_EASING["EXPONENTIALINOUT"] = {cc.EaseExponentialInOut, 1}
  34. ACTION_EASING["EXPONENTIALOUT"] = {cc.EaseExponentialOut, 1}
  35. ACTION_EASING["IN"] = {cc.EaseIn, 2, 1}
  36. ACTION_EASING["INOUT"] = {cc.EaseInOut, 2, 1}
  37. ACTION_EASING["OUT"] = {cc.EaseOut, 2, 1}
  38. ACTION_EASING["RATEACTION"] = {cc.EaseRateAction, 2, 1}
  39. ACTION_EASING["SINEIN"] = {cc.EaseSineIn, 1}
  40. ACTION_EASING["SINEINOUT"] = {cc.EaseSineInOut, 1}
  41. ACTION_EASING["SINEOUT"] = {cc.EaseSineOut, 1}
  42. local actionManager = cc.Director:getInstance():getActionManager()
  43. function transition.newEasing(action, easingName, more)
  44. local key = string.upper(tostring(easingName))
  45. local easing
  46. if ACTION_EASING[key] then
  47. local cls, count, default = unpack(ACTION_EASING[key])
  48. if count == 2 then
  49. easing = cls:create(action, more or default)
  50. else
  51. easing = cls:create(action)
  52. end
  53. end
  54. return easing or action
  55. end
  56. function transition.create(action, args)
  57. args = checktable(args)
  58. if args.easing then
  59. if type(args.easing) == "table" then
  60. action = transition.newEasing(action, unpack(args.easing))
  61. else
  62. action = transition.newEasing(action, args.easing)
  63. end
  64. end
  65. local actions = {}
  66. local delay = checknumber(args.delay)
  67. if delay > 0 then
  68. actions[#actions + 1] = cc.DelayTime:create(delay)
  69. end
  70. actions[#actions + 1] = action
  71. local onComplete = args.onComplete
  72. if type(onComplete) ~= "function" then onComplete = nil end
  73. if onComplete then
  74. actions[#actions + 1] = cc.CallFunc:create(onComplete)
  75. end
  76. if args.removeSelf then
  77. actions[#actions + 1] = cc.RemoveSelf:create()
  78. end
  79. if #actions > 1 then
  80. return transition.sequence(actions)
  81. else
  82. return actions[1]
  83. end
  84. end
  85. function transition.execute(target, action, args)
  86. assert(not tolua.isnull(target), "transition.execute() - target is not cc.Node")
  87. local action = transition.create(action, args)
  88. target:runAction(action)
  89. return action
  90. end
  91. function transition.moveTo(target, args)
  92. assert(not tolua.isnull(target), "transition.moveTo() - target is not cc.Node")
  93. local x = args.x or target:getPositionX()
  94. local y = args.y or target:getPositionY()
  95. local action = cc.MoveTo:create(args.time, cc.p(x, y))
  96. return transition.execute(target, action, args)
  97. end
  98. function transition.moveBy(target, args)
  99. assert(not tolua.isnull(target), "transition.moveBy() - target is not cc.Node")
  100. local x = args.x or 0
  101. local y = args.y or 0
  102. local action = cc.MoveBy:create(args.time, cc.p(x, y))
  103. return transition.execute(target, action, args)
  104. end
  105. function transition.fadeIn(target, args)
  106. assert(not tolua.isnull(target), "transition.fadeIn() - target is not cc.Node")
  107. local action = cc.FadeIn:create(args.time)
  108. return transition.execute(target, action, args)
  109. end
  110. function transition.fadeOut(target, args)
  111. assert(not tolua.isnull(target), "transition.fadeOut() - target is not cc.Node")
  112. local action = cc.FadeOut:create(args.time)
  113. return transition.execute(target, action, args)
  114. end
  115. function transition.fadeTo(target, args)
  116. assert(not tolua.isnull(target), "transition.fadeTo() - target is not cc.Node")
  117. local opacity = checkint(args.opacity)
  118. if opacity < 0 then
  119. opacity = 0
  120. elseif opacity > 255 then
  121. opacity = 255
  122. end
  123. local action = cc.FadeTo:create(args.time, opacity)
  124. return transition.execute(target, action, args)
  125. end
  126. function transition.scaleTo(target, args)
  127. assert(not tolua.isnull(target), "transition.scaleTo() - target is not cc.Node")
  128. local action
  129. if args.scale then
  130. action = cc.ScaleTo:create(checknumber(args.time), checknumber(args.scale))
  131. elseif args.scaleX or args.scaleY then
  132. local scaleX, scaleY
  133. if args.scaleX then
  134. scaleX = checknumber(args.scaleX)
  135. else
  136. scaleX = target:getScaleX()
  137. end
  138. if args.scaleY then
  139. scaleY = checknumber(args.scaleY)
  140. else
  141. scaleY = target:getScaleY()
  142. end
  143. action = cc.ScaleTo:create(checknumber(args.time), scaleX, scaleY)
  144. end
  145. return transition.execute(target, action, args)
  146. end
  147. function transition.rotateTo(target, args)
  148. assert(not tolua.isnull(target), "transition.rotateTo() - target is not cc.Node")
  149. local rotation = args.rotation or target:getRotation()
  150. local action = cc.RotateTo:create(args.time, rotation)
  151. return transition.execute(target, action, args)
  152. end
  153. function transition.rotateBy(target, args)
  154. assert(not tolua.isnull(target), "transition.rotateTo() - target is not cc.Node")
  155. local rotation = args.rotation or 0
  156. local action = cc.RotateBy:create(args.time, rotation)
  157. return transition.execute(target, action, args)
  158. end
  159. function transition.sequence(actions)
  160. if #actions < 1 then return end
  161. if #actions < 2 then return actions[1] end
  162. return cc.Sequence:create(actions)
  163. end
  164. function transition.removeAction(action)
  165. if not tolua.isnull(action) then
  166. actionManager:removeAction(action)
  167. end
  168. end
  169. function transition.stopTarget(target)
  170. if not tolua.isnull(target) then
  171. actionManager:removeAllActionsFromTarget(target)
  172. end
  173. end
  174. function transition.pauseTarget(target)
  175. if not tolua.isnull(target) then
  176. actionManager:pauseTarget(target)
  177. end
  178. end
  179. function transition.resumeTarget(target)
  180. if not tolua.isnull(target) then
  181. actionManager:resumeTarget(target)
  182. end
  183. end
  184. return transition