NodeEx.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 Node = cc.Node
  20. function Node:add(child, zorder, tag)
  21. if tag then
  22. self:addChild(child, zorder, tag)
  23. elseif zorder then
  24. self:addChild(child, zorder)
  25. else
  26. self:addChild(child)
  27. end
  28. return self
  29. end
  30. function Node:addTo(parent, zorder, tag)
  31. if tag then
  32. parent:addChild(self, zorder, tag)
  33. elseif zorder then
  34. parent:addChild(self, zorder)
  35. else
  36. parent:addChild(self)
  37. end
  38. return self
  39. end
  40. function Node:removeSelf()
  41. self:removeFromParent()
  42. return self
  43. end
  44. function Node:align(anchorPoint, x, y)
  45. self:setAnchorPoint(anchorPoint)
  46. return self:move(x, y)
  47. end
  48. function Node:show()
  49. self:setVisible(true)
  50. return self
  51. end
  52. function Node:hide()
  53. self:setVisible(false)
  54. return self
  55. end
  56. function Node:move(x, y)
  57. if y then
  58. self:setPosition(x, y)
  59. else
  60. self:setPosition(x)
  61. end
  62. return self
  63. end
  64. function Node:moveTo(args)
  65. transition.moveTo(self, args)
  66. return self
  67. end
  68. function Node:moveBy(args)
  69. transition.moveBy(self, args)
  70. return self
  71. end
  72. function Node:fadeIn(args)
  73. transition.fadeIn(self, args)
  74. return self
  75. end
  76. function Node:fadeOut(args)
  77. transition.fadeOut(self, args)
  78. return self
  79. end
  80. function Node:fadeTo(args)
  81. transition.fadeTo(self, args)
  82. return self
  83. end
  84. function Node:rotate(rotation)
  85. self:setRotation(rotation)
  86. return self
  87. end
  88. function Node:rotateTo(args)
  89. transition.rotateTo(self, args)
  90. return self
  91. end
  92. function Node:rotateBy(args)
  93. transition.rotateBy(self, args)
  94. return self
  95. end
  96. function Node:scaleTo(args)
  97. transition.scaleTo(self, args)
  98. return self
  99. end
  100. function Node:onUpdate(callback)
  101. self:scheduleUpdateWithPriorityLua(callback, 0)
  102. return self
  103. end
  104. Node.scheduleUpdate = Node.onUpdate
  105. function Node:onNodeEvent(eventName, callback)
  106. if "enter" == eventName then
  107. self.onEnterCallback_ = callback
  108. elseif "exit" == eventName then
  109. self.onExitCallback_ = callback
  110. elseif "enterTransitionFinish" == eventName then
  111. self.onEnterTransitionFinishCallback_ = callback
  112. elseif "exitTransitionStart" == eventName then
  113. self.onExitTransitionStartCallback_ = callback
  114. elseif "cleanup" == eventName then
  115. self.onCleanupCallback_ = callback
  116. end
  117. self:enableNodeEvents()
  118. end
  119. function Node:enableNodeEvents()
  120. if self.isNodeEventEnabled_ then
  121. return self
  122. end
  123. self:registerScriptHandler(function(state)
  124. if state == "enter" then
  125. self:onEnter_()
  126. elseif state == "exit" then
  127. self:onExit_()
  128. elseif state == "enterTransitionFinish" then
  129. self:onEnterTransitionFinish_()
  130. elseif state == "exitTransitionStart" then
  131. self:onExitTransitionStart_()
  132. elseif state == "cleanup" then
  133. self:onCleanup_()
  134. end
  135. end)
  136. self.isNodeEventEnabled_ = true
  137. return self
  138. end
  139. function Node:disableNodeEvents()
  140. self:unregisterScriptHandler()
  141. self.isNodeEventEnabled_ = false
  142. return self
  143. end
  144. function Node:onEnter()
  145. end
  146. function Node:onExit()
  147. end
  148. function Node:onEnterTransitionFinish()
  149. end
  150. function Node:onExitTransitionStart()
  151. end
  152. function Node:onCleanup()
  153. end
  154. function Node:onEnter_()
  155. self:onEnter()
  156. if not self.onEnterCallback_ then
  157. return
  158. end
  159. self:onEnterCallback_()
  160. end
  161. function Node:onExit_()
  162. self:onExit()
  163. if not self.onExitCallback_ then
  164. return
  165. end
  166. self:onExitCallback_()
  167. end
  168. function Node:onEnterTransitionFinish_()
  169. self:onEnterTransitionFinish()
  170. if not self.onEnterTransitionFinishCallback_ then
  171. return
  172. end
  173. self:onEnterTransitionFinishCallback_()
  174. end
  175. function Node:onExitTransitionStart_()
  176. self:onExitTransitionStart()
  177. if not self.onExitTransitionStartCallback_ then
  178. return
  179. end
  180. self:onExitTransitionStartCallback_()
  181. end
  182. function Node:onCleanup_()
  183. self:onCleanup()
  184. if not self.onCleanupCallback_ then
  185. return
  186. end
  187. self:onCleanupCallback_()
  188. end