axmol/cocos/scripting/lua-bindings/script/framework/extends/NodeEx.lua

229 lines
5.0 KiB
Lua
Raw Normal View History

2014-12-29 01:58:16 +08:00
--[[
Copyright (c) 2011-2014 chukong-inc.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]
local Node = cc.Node
2014-12-29 05:41:13 +08:00
function Node:add(child, zorder, tag)
if tag then
self:addChild(child, zorder, tag)
elseif zorder then
self:addChild(child, zorder)
else
self:addChild(child)
end
return self
end
function Node:addTo(parent, zorder, tag)
if tag then
parent:addChild(self, zorder, tag)
elseif zorder then
parent:addChild(self, zorder)
else
parent:addChild(self)
end
2014-12-29 01:58:16 +08:00
return self
end
function Node:removeSelf()
self:removeFromParent()
return self
end
2014-12-29 05:41:13 +08:00
function Node:align(anchorPoint, x, y)
self:setAnchorPoint(anchorPoint)
return self:move(x, y)
end
2014-12-29 01:58:16 +08:00
function Node:show()
self:setVisible(true)
return self
end
function Node:hide()
self:setVisible(false)
return self
end
2014-12-29 05:41:13 +08:00
function Node:move(x, y)
if y then
self:setPosition(x, y)
else
self:setPosition(x)
end
return self
end
function Node:moveTo(args)
transition.moveTo(self, args)
return self
end
function Node:moveBy(args)
transition.moveBy(self, args)
return self
end
function Node:fadeIn(args)
transition.fadeIn(self, args)
return self
end
function Node:fadeOut(args)
transition.fadeOut(self, args)
return self
end
function Node:fadeTo(args)
transition.fadeTo(self, args)
return self
end
function Node:rotate(rotation)
self:setRotation(rotation)
return self
end
function Node:rotateTo(args)
transition.rotateTo(self, args)
return self
end
function Node:rotateBy(args)
transition.rotateBy(self, args)
return self
end
function Node:scaleTo(args)
transition.scaleTo(self, args)
return self
end
2015-01-05 14:02:29 +08:00
function Node:onUpdate(callback)
2014-12-29 05:41:13 +08:00
self:scheduleUpdateWithPriorityLua(callback, 0)
return self
end
2015-01-05 14:02:29 +08:00
Node.scheduleUpdate = Node.onUpdate
function Node:onNodeEvent(eventName, callback)
if "enter" == eventName then
self.onEnterCallback_ = callback
elseif "exit" == eventName then
self.onExitCallback_ = callback
elseif "enterTransitionFinish" == eventName then
self.onEnterTransitionFinishCallback_ = callback
elseif "exitTransitionStart" == eventName then
self.onExitTransitionStartCallback_ = callback
elseif "cleanup" == eventName then
self.onCleanupCallback_ = callback
end
self:enableNodeEvents()
end
2014-12-29 05:41:13 +08:00
function Node:enableNodeEvents()
2015-01-05 14:02:29 +08:00
if self.isNodeEventEnabled_ then
return self
end
2014-12-29 05:41:13 +08:00
self:registerScriptHandler(function(state)
if state == "enter" then
2015-01-05 14:02:29 +08:00
self:onEnter_()
2014-12-29 05:41:13 +08:00
elseif state == "exit" then
2015-01-05 14:02:29 +08:00
self:onExit_()
2014-12-29 05:41:13 +08:00
elseif state == "enterTransitionFinish" then
2015-01-05 14:02:29 +08:00
self:onEnterTransitionFinish_()
2014-12-29 05:41:13 +08:00
elseif state == "exitTransitionStart" then
2015-01-05 14:02:29 +08:00
self:onExitTransitionStart_()
2014-12-29 05:41:13 +08:00
elseif state == "cleanup" then
2015-01-05 14:02:29 +08:00
self:onCleanup_()
2014-12-29 05:41:13 +08:00
end
end)
2015-01-05 14:02:29 +08:00
self.isNodeEventEnabled_ = true
2014-12-29 05:41:13 +08:00
return self
end
function Node:disableNodeEvents()
self:unregisterScriptHandler()
2015-01-05 14:02:29 +08:00
self.isNodeEventEnabled_ = false
2014-12-29 05:41:13 +08:00
return self
end
2015-01-05 14:02:29 +08:00
2014-12-29 05:41:13 +08:00
function Node:onEnter()
end
function Node:onExit()
end
function Node:onEnterTransitionFinish()
end
function Node:onExitTransitionStart()
end
function Node:onCleanup()
end
2015-01-05 14:02:29 +08:00
function Node:onEnter_()
self:onEnter()
if not self.onEnterCallback_ then
return
end
self:onEnterCallback_()
end
function Node:onExit_()
self:onExit()
if not self.onExitCallback_ then
return
end
self:onExitCallback_()
end
function Node:onEnterTransitionFinish_()
self:onEnterTransitionFinish()
if not self.onEnterTransitionFinishCallback_ then
return
end
self:onEnterTransitionFinishCallback_()
end
function Node:onExitTransitionStart_()
self:onExitTransitionStart()
if not self.onExitTransitionStartCallback_ then
return
end
self:onExitTransitionStartCallback_()
end
function Node:onCleanup_()
self:onCleanup()
if not self.onCleanupCallback_ then
return
end
self:onCleanupCallback_()
end