From f82404f9c9e0294b77cf58007e26fecb420dce62 Mon Sep 17 00:00:00 2001 From: htl Date: Mon, 5 Jan 2015 14:02:29 +0800 Subject: [PATCH] modify node register event interface --- .../script/framework/components/event.lua | 4 +- .../script/framework/extends/NodeEx.lua | 77 +++++++++++++++++-- .../lua-bindings/script/framework/init.lua | 3 + 3 files changed, 77 insertions(+), 7 deletions(-) diff --git a/cocos/scripting/lua-bindings/script/framework/components/event.lua b/cocos/scripting/lua-bindings/script/framework/components/event.lua index 9fc7f13693..1351ca647b 100644 --- a/cocos/scripting/lua-bindings/script/framework/components/event.lua +++ b/cocos/scripting/lua-bindings/script/framework/components/event.lua @@ -29,7 +29,7 @@ function Event:unbind(target) self:init_() end -function Event:addEventListener(eventName, listener, tag) +function Event:onEventListener(eventName, listener, tag) assert(type(eventName) == "string" and eventName ~= "", "Event:addEventListener() - invalid eventName") eventName = string.upper(eventName) @@ -50,6 +50,8 @@ function Event:addEventListener(eventName, listener, tag) return self.target_, handle end +Event.addEventListener = Event.onEventListener + function Event:dispatchEvent(event) event.name = string.upper(tostring(event.name)) local eventName = event.name diff --git a/cocos/scripting/lua-bindings/script/framework/extends/NodeEx.lua b/cocos/scripting/lua-bindings/script/framework/extends/NodeEx.lua index edd31adb50..a15807b8f6 100644 --- a/cocos/scripting/lua-bindings/script/framework/extends/NodeEx.lua +++ b/cocos/scripting/lua-bindings/script/framework/extends/NodeEx.lua @@ -120,33 +120,58 @@ function Node:scaleTo(args) return self end -function Node:scheduleUpdate(callback) +function Node:onUpdate(callback) self:scheduleUpdateWithPriorityLua(callback, 0) return self end +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 + function Node:enableNodeEvents() + if self.isNodeEventEnabled_ then + return self + end + self:registerScriptHandler(function(state) if state == "enter" then - self:onEnter() + self:onEnter_() elseif state == "exit" then - self:onExit() + self:onExit_() elseif state == "enterTransitionFinish" then - self:onEnterTransitionFinish() + self:onEnterTransitionFinish_() elseif state == "exitTransitionStart" then - self:onExitTransitionStart() + self:onExitTransitionStart_() elseif state == "cleanup" then - self:onCleanup() + self:onCleanup_() end end) + self.isNodeEventEnabled_ = true + return self end function Node:disableNodeEvents() self:unregisterScriptHandler() + self.isNodeEventEnabled_ = false return self end + function Node:onEnter() end @@ -161,3 +186,43 @@ end function Node:onCleanup() end + +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 diff --git a/cocos/scripting/lua-bindings/script/framework/init.lua b/cocos/scripting/lua-bindings/script/framework/init.lua index a33ec0995f..ebe4c86ac6 100644 --- a/cocos/scripting/lua-bindings/script/framework/init.lua +++ b/cocos/scripting/lua-bindings/script/framework/init.lua @@ -44,6 +44,8 @@ require("cocos.framework.extends.NodeEx") require("cocos.framework.extends.SpriteEx") require("cocos.framework.extends.LayerEx") require("cocos.framework.extends.MenuEx") + +if ccui then require("cocos.framework.extends.UIWidget") require("cocos.framework.extends.UICheckBox") require("cocos.framework.extends.UIEditBox") @@ -52,6 +54,7 @@ require("cocos.framework.extends.UIPageView") require("cocos.framework.extends.UIScrollView") require("cocos.framework.extends.UISlider") require("cocos.framework.extends.UITextField") +end require("cocos.framework.package_support")