From 97f26176e8e74aea62b8fee9a5f8fb3e651c4391 Mon Sep 17 00:00:00 2001 From: samuele3hu <qingkui.hu@cocos2d-x.org> Date: Sun, 22 Dec 2013 22:10:21 +0800 Subject: [PATCH 1/3] issue #3403:Add the EventDispatcher lua binding and the releated test cases --- cocos/2d/CCEvent.h | 3 +- cocos/2d/CCEventListenerAcceleration.h | 2 + cocos/2d/CCEventListenerCustom.h | 2 + cocos/2d/CCScriptSupport.h | 6 + cocos/scripting/lua/bindings/CCLuaEngine.cpp | 255 +++++ cocos/scripting/lua/bindings/CCLuaEngine.h | 6 + cocos/scripting/lua/bindings/CCLuaStack.cpp | 1 + .../lua/bindings/LuaScriptHandlerMgr.h | 16 + .../lua_cocos2dx_manual.cpp.REMOVED.git-id | 2 +- .../lua/bindings/lua_cocos2dx_manual.hpp | 96 +- .../scripting/lua/script/Cocos2dConstants.lua | 16 + .../NewEventDispatcherTest.lua | 1008 +++++++++++++++++ .../TestLua/Resources/luaScript/mainMenu.lua | 2 + tools/tolua/cocos2dx.ini | 7 +- 14 files changed, 1417 insertions(+), 5 deletions(-) create mode 100644 samples/Lua/TestLua/Resources/luaScript/NewEventDispatcherTest/NewEventDispatcherTest.lua diff --git a/cocos/2d/CCEvent.h b/cocos/2d/CCEvent.h index 205ec443a7..b067ff3ce3 100644 --- a/cocos/2d/CCEvent.h +++ b/cocos/2d/CCEvent.h @@ -29,6 +29,7 @@ #include <string> #include <stdint.h> +#include "CCObject.h" #include "CCPlatformMacros.h" NS_CC_BEGIN @@ -38,7 +39,7 @@ class Node; /** * Base class of all kinds of events. */ -class Event +class Event : public Object { public: enum class Type diff --git a/cocos/2d/CCEventListenerAcceleration.h b/cocos/2d/CCEventListenerAcceleration.h index 59ce3d8439..b0719089ab 100644 --- a/cocos/2d/CCEventListenerAcceleration.h +++ b/cocos/2d/CCEventListenerAcceleration.h @@ -44,6 +44,8 @@ private: bool init(std::function<void(Acceleration*, Event* event)> callback); std::function<void(Acceleration*, Event*)> onAccelerationEvent; + + friend class LuaEventListenerAcceleration; }; NS_CC_END diff --git a/cocos/2d/CCEventListenerCustom.h b/cocos/2d/CCEventListenerCustom.h index e2e750ce56..8a7fe162e1 100644 --- a/cocos/2d/CCEventListenerCustom.h +++ b/cocos/2d/CCEventListenerCustom.h @@ -70,6 +70,8 @@ protected: bool init(ListenerID listenerId, std::function<void(EventCustom*)> callback); std::function<void(EventCustom*)> _onCustomEvent; + + friend class LuaEventListenerCustom; }; NS_CC_END diff --git a/cocos/2d/CCScriptSupport.h b/cocos/2d/CCScriptSupport.h index 60dda84147..64436b56cd 100644 --- a/cocos/2d/CCScriptSupport.h +++ b/cocos/2d/CCScriptSupport.h @@ -214,6 +214,12 @@ enum ScriptEventType kAssetsManagerEvent,//Now it's only used in Lua Binding kCocoStudioEventListener,//Now it's only used in Lua Binding kArmatureWrapper,//Now it's only used in Lua Binding + kEventListenerAcc,//Now it's only used in Lua Binding + kEventListenerKeyboard,//Now it's only used in Lua Binding + kEventListenerTouch,//Now it's only used in Lua Binding + kEventListenerTouches,//Now it's only used in Lua Binding + kEventListenerMouse,//Now it's only used in Lua Binding + kEventListenerCustom,////Now it's only used in Lua Binding }; struct BasicScriptData diff --git a/cocos/scripting/lua/bindings/CCLuaEngine.cpp b/cocos/scripting/lua/bindings/CCLuaEngine.cpp index 7671034043..e8261931c7 100644 --- a/cocos/scripting/lua/bindings/CCLuaEngine.cpp +++ b/cocos/scripting/lua/bindings/CCLuaEngine.cpp @@ -23,12 +23,14 @@ ****************************************************************************/ #include "CCLuaEngine.h" +#include "tolua_fix.h" #include "cocos2d.h" #include "CCArray.h" #include "CCScheduler.h" #include "LuaScriptHandlerMgr.h" #include "extensions/GUI/CCControlExtension/CCControl.h" #include "LuaOpengl.h" +#include "lua_cocos2dx_manual.hpp" #include "lua_cocos2dx_extension_manual.h" #include "lua_cocos2dx_coco_studio_manual.hpp" @@ -267,6 +269,36 @@ int LuaEngine::sendEvent(ScriptEvent* evt) return handleArmatureWrapper(evt->data); } break; + case kEventListenerAcc: + { + return handleEventListenerAcc(evt->data); + } + break; + case kEventListenerKeyboard: + { + return handleEventListenerKeyboard(evt->data); + } + break; + case kEventListenerTouch: + { + return handleEventListenerTouch(evt->data); + } + break; + case kEventListenerTouches: + { + return handleEventListenerTouches(evt->data); + } + break; + case kEventListenerMouse: + { + return handleEventListenerMouse(evt->data); + } + break; + case kEventListenerCustom: + { + return handleEventListenerCustom(evt->data); + }; + break; default: break; } @@ -858,4 +890,227 @@ int LuaEngine::handleArmatureWrapper(void* data) return 0; } +int LuaEngine::handleEventListenerAcc(void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data); + if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::EVENTLISTENER_ACC); + if (0 == handler) + return 0; + + lua_State* L = _stack->getLuaState(); + + LuaEventListenerAccelerationData* eventListennerAcc = static_cast<LuaEventListenerAccelerationData*>(basicScriptData->value); + toluafix_pushusertype_ccobject(L, eventListennerAcc->event->_ID, &(eventListennerAcc->event->_luaID), (void*)(eventListennerAcc->event),"Event"); + Acceleration* accleration = static_cast<Acceleration*>(eventListennerAcc->acc); + lua_pushnumber(L,accleration->x); + lua_pushnumber(L,accleration->y); + lua_pushnumber(L,accleration->z); + lua_pushnumber(L,accleration->timestamp); + int ret = _stack->executeFunctionByHandler(handler, 5); + _stack->clean(); + return ret; +} + +int LuaEngine::handleEventListenerKeyboard(void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data); + if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value) + return 0; + + LuaEventListenerKeyboarData* keyboardData = static_cast<LuaEventListenerKeyboarData*>(basicScriptData->value); + ScriptHandlerMgr::HandlerType type = ScriptHandlerMgr::HandlerType::EVENTLISTENER_KEYBOARD_PRESSED; + if (keyboardData->status == LuaEventListenerKeyboarData::KeyboardStatus::RELEASE) + type = ScriptHandlerMgr::HandlerType::EVENTLISTENER_KEYBOARD_RELEASE; + + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, type); + if (0 == handler) + return 0; + + lua_State* L = _stack->getLuaState(); + lua_pushinteger(L, keyboardData->keyCode); + toluafix_pushusertype_ccobject(L, keyboardData->event->_ID, &(keyboardData->event->_luaID), (void*)(keyboardData->event),"Event"); + int ret = _stack->executeFunctionByHandler(handler, 2); + _stack->clean(); + return ret; +} + +int LuaEngine::handleEventListenerTouch(void* data) +{ + if (nullptr == data) + return 0; + + LuaEventListenerTouchData* listenerData = static_cast<LuaEventListenerTouchData*>(data); + if (nullptr == listenerData->nativeObject || nullptr == listenerData->touch || nullptr == listenerData->event) + return 0; + + ScriptHandlerMgr::HandlerType handlerType; + switch (listenerData->eventCode) + { + case EventTouch::EventCode::BEGAN: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_BEGAN; + break; + + case EventTouch::EventCode::MOVED: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_MOVED; + break; + + case EventTouch::EventCode::ENDED: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_ENDED; + break; + case EventTouch::EventCode::CANCELLED: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_CANCELLED; + break; + default: + return 0; + } + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(listenerData->nativeObject, handlerType); + if (0 == handler) + return 0; + + int ret = 0; + + Touch* touch = listenerData->touch; + if (NULL != touch) { + lua_State* L = _stack->getLuaState(); + toluafix_pushusertype_ccobject(L, listenerData->touch->_ID, &(listenerData->touch->_luaID), (void*)(listenerData->touch),"Touch"); + toluafix_pushusertype_ccobject(L, listenerData->event->_ID, &(listenerData->event->_luaID), (void*)(listenerData->event),"Event"); + ret = _stack->executeFunctionByHandler(handler, 2); + } + _stack->clean(); + + return ret; +} + +int LuaEngine::handleEventListenerTouches(void* data) +{ + if (nullptr == data) + return 0; + + LuaEventListenerTouchesData * listenerData = static_cast<LuaEventListenerTouchesData*>(data); + if (NULL == listenerData->nativeObject || nullptr == listenerData->event || listenerData->touches.size() == 0) + return 0; + + ScriptHandlerMgr::HandlerType handlerType; + switch (listenerData->eventCode) + { + case EventTouch::EventCode::BEGAN: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_BEGAN; + break; + + case EventTouch::EventCode::MOVED: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_MOVED; + break; + + case EventTouch::EventCode::ENDED: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_ENDED; + break; + case EventTouch::EventCode::CANCELLED: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_CANCELLED; + break; + default: + return 0; + } + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)listenerData->nativeObject, handlerType); + + if (0 == handler) + return 0; + + lua_State *L = _stack->getLuaState(); + int ret = 0; + + lua_newtable(L); + int i = 1; + for (auto& touch : listenerData->touches) + { + lua_pushnumber(L, (lua_Number)i); + toluafix_pushusertype_ccobject(L, touch->_ID, &(touch->_luaID), (void*)(touch),"Touch"); + lua_rawset(L, -3); + ++i; + } + toluafix_pushusertype_ccobject(L, listenerData->event->_ID, &(listenerData->event->_luaID), (void*)(listenerData->event),"Event"); + + ret = _stack->executeFunctionByHandler(handler, 2); + _stack->clean(); + return ret; +} + +int LuaEngine::handleEventListenerMouse(void* data) +{ + if (nullptr == data) + return 0; + + LuaEventListenerMouseData * listenerData = static_cast<LuaEventListenerMouseData*>(data); + if (NULL == listenerData->nativeObject || nullptr == listenerData->event ) + return 0; + + ScriptHandlerMgr::HandlerType handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_DOWN; + switch (listenerData->eventType) + { + case EventMouse::MouseEventType::MOUSE_DOWN: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_DOWN; + break; + + case EventMouse::MouseEventType::MOUSE_UP: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_UP; + break; + + case EventMouse::MouseEventType::MOUSE_MOVE: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_MOVE; + break; + case EventMouse::MouseEventType::MOUSE_SCROLL: + handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_SCROLL; + break; + default: + break; + } + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)listenerData->nativeObject, handlerType); + + if (0 == handler) + return 0; + + lua_State* L = _stack->getLuaState(); + toluafix_pushusertype_ccobject(L, listenerData->event->_ID, &(listenerData->event->_luaID), (void*)(listenerData->event),"Event"); + + int ret = _stack->executeFunctionByHandler(handler, 1); + _stack->clean(); + + return ret; +} + +int LuaEngine::handleEventListenerCustom(void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData * listenerData = static_cast<BasicScriptData*>(data); + if (NULL == listenerData->nativeObject || nullptr == listenerData->value ) + return 0; + + EventCustom* eventCustom = static_cast<EventCustom*>(listenerData->value); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)listenerData->nativeObject, ScriptHandlerMgr::HandlerType::EVENTLISTENER_CUSTIOM); + + if (0 == handler) + return 0; + + lua_State* L = _stack->getLuaState(); + toluafix_pushusertype_ccobject(L, eventCustom->_ID, &(eventCustom->_luaID), (void*)(eventCustom),"EventCustom"); + int ret = _stack->executeFunctionByHandler(handler, 1); + _stack->clean(); + + return ret; +} + NS_CC_END diff --git a/cocos/scripting/lua/bindings/CCLuaEngine.h b/cocos/scripting/lua/bindings/CCLuaEngine.h index bcb1e2327c..12be7885d3 100644 --- a/cocos/scripting/lua/bindings/CCLuaEngine.h +++ b/cocos/scripting/lua/bindings/CCLuaEngine.h @@ -139,6 +139,12 @@ private: int handleAssetsManagerEvent(void* data); int handleCocoStudioEventListener(void* data); int handleArmatureWrapper(void* data); + int handleEventListenerAcc(void* data); + int handleEventListenerKeyboard(void* data); + int handleEventListenerTouch(void* data); + int handleEventListenerTouches(void* data); + int handleEventListenerMouse(void* data); + int handleEventListenerCustom(void* data); private: static LuaEngine* _defaultEngine; LuaStack *_stack; diff --git a/cocos/scripting/lua/bindings/CCLuaStack.cpp b/cocos/scripting/lua/bindings/CCLuaStack.cpp index 5b0925e376..af593cbbb0 100644 --- a/cocos/scripting/lua/bindings/CCLuaStack.cpp +++ b/cocos/scripting/lua/bindings/CCLuaStack.cpp @@ -144,6 +144,7 @@ bool LuaStack::init(void) register_all_cocos2dx_extension(_state); register_all_cocos2dx_deprecated(_state); register_cocos2dx_extension_CCBProxy(_state); + register_cocos2dx_event_releated(_state); tolua_opengl_open(_state); register_all_cocos2dx_studio(_state); register_all_cocos2dx_manual(_state); diff --git a/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.h b/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.h index dffe6f3276..7ab703b1a6 100644 --- a/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.h +++ b/cocos/scripting/lua/bindings/LuaScriptHandlerMgr.h @@ -110,6 +110,22 @@ public: EVENT_LISTENER, ARMATURE_EVENT, + + EVENTLISTENER_ACC, + EVENTLISTENER_CUSTIOM, + + EVENTLISTENER_KEYBOARD_PRESSED, + EVENTLISTENER_KEYBOARD_RELEASE, + + EVENTLISTENER_TOUCH_BEGAN, + EVENTLISTENER_TOUCH_MOVED, + EVENTLISTENER_TOUCH_ENDED, + EVENTLISTENER_TOUCH_CANCELLED, + + EVENTLISTENER_MOUSE_DOWN, + EVENTLISTENER_MOUSE_UP, + EVENTLISTENER_MOUSE_MOVE, + EVENTLISTENER_MOUSE_SCROLL, }; typedef int Handler; diff --git a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id index f6d3f63b06..61eb9dbd42 100644 --- a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id +++ b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id @@ -1 +1 @@ -c1e3182aeb1023c573f64314788fbccb8da2de43 \ No newline at end of file +8d902acd33947b88d5c849c3e36c0da7c7b1af8c \ No newline at end of file diff --git a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.hpp b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.hpp index 8000eaf4b5..8148a9e3f6 100644 --- a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.hpp +++ b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.hpp @@ -9,6 +9,100 @@ extern "C" { } #endif -int register_all_cocos2dx_manual(lua_State* tolua_S); +#include "cocos2d.h" + +NS_CC_BEGIN +class LuaEventListenerCustom +{ +public: + static EventListenerCustom* create(const std::string& eventName); +}; + +class LuaEventListenerAcceleration +{ +public: + static EventListenerAcceleration* create(); +}; +NS_CC_END + +USING_NS_CC; + +TOLUA_API int register_all_cocos2dx_manual(lua_State* tolua_S); + +TOLUA_API int register_cocos2dx_event_releated(lua_State* tolua_S); + +struct LuaEventListenerAccelerationData +{ + void* acc; + Event* event; + + LuaEventListenerAccelerationData(void* inAcc,Event* inEvent) + :acc(inAcc),event(inEvent) + { + } +}; + +struct LuaEventListenerKeyboarData +{ + enum class KeyboardStatus :int + { + PRESSED = 0, + RELEASE, + }; + int keyCode; + Event* event; + KeyboardStatus status; + + LuaEventListenerKeyboarData(int inKeyCode,Event* inEvent,KeyboardStatus inStatus) + :keyCode(inKeyCode),event(inEvent),status(inStatus) + { + } +}; + +struct LuaEventListenerTouchData +{ + EventTouch::EventCode eventCode; + void* nativeObject; + Touch* touch; + Event* event; + + LuaEventListenerTouchData(EventTouch::EventCode inEventCode, void* inNativeObject, Touch* inTouch, Event* inEvent) + : eventCode(inEventCode), + nativeObject(inNativeObject), + touch(inTouch), + event(inEvent) + { + } +}; + +struct LuaEventListenerTouchesData +{ + EventTouch::EventCode eventCode; + void* nativeObject; + std::vector<Touch*> touches; + Event* event; + + LuaEventListenerTouchesData(EventTouch::EventCode inEventCode, void* inNativeObject, std::vector<Touch*> inTouches, Event* inEvent) + : eventCode(inEventCode), + nativeObject(inNativeObject), + touches(inTouches), + event(inEvent) + { + } +}; + +struct LuaEventListenerMouseData +{ + EventMouse::MouseEventType eventType; + void* nativeObject; + Event* event; + + LuaEventListenerMouseData(EventMouse::MouseEventType inEventType, void* inNativeObject, Event* inEvent) + : eventType(inEventType), + nativeObject(inNativeObject), + event(inEvent) + { + } +}; #endif // #ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_GENERATED_LUA_COCOS2DX_MANUAL_H diff --git a/cocos/scripting/lua/script/Cocos2dConstants.lua b/cocos/scripting/lua/script/Cocos2dConstants.lua index 9d9a3c3580..117dafe867 100644 --- a/cocos/scripting/lua/script/Cocos2dConstants.lua +++ b/cocos/scripting/lua/script/Cocos2dConstants.lua @@ -331,4 +331,20 @@ cc.GLYPHCOLLECTION_DYNAMIC = 0 cc.GLYPHCOLLECTION_NEHE = 1 cc.GLYPHCOLLECTION_ASCII = 2 cc.GLYPHCOLLECTION_CUSTOM = 3 + +cc.EVENTLISTENER_UNKNOWN = 0 +cc.EVENTLISTENER_TOUCH_ONE_BY_ONE = 1 +cc.EVENTLISTENER_TOUCH_ALL_AT_ONCE = 2 +cc.EVENTLISTENER_KEYBOARD = 3 +cc.EVENTLISTENER_MOUSE = 4 +cc.EVENTLISTENER_ACCELERATION = 5 +cc.EVENTLISTENER_CUSTOM = 6 + +cc.TOUCH_BEGAN = 0 +cc.TOUCH_MOVED = 1 +cc.TOUCH_ENDED = 2 +cc.TOUCH_CANCELLED = 3 + +cc.KEYBOARD_PRESSED = 0 +cc.KEYBOARD_RELEASE = 1 \ No newline at end of file diff --git a/samples/Lua/TestLua/Resources/luaScript/NewEventDispatcherTest/NewEventDispatcherTest.lua b/samples/Lua/TestLua/Resources/luaScript/NewEventDispatcherTest/NewEventDispatcherTest.lua new file mode 100644 index 0000000000..2e4132d22d --- /dev/null +++ b/samples/Lua/TestLua/Resources/luaScript/NewEventDispatcherTest/NewEventDispatcherTest.lua @@ -0,0 +1,1008 @@ +local itemTagBasic = 1000 + +local testArray = +{ + TouchableSprite = 1, + FixedPriority = 2, + RemoveListenerWhenDispatching = 3, + CustomEvent = 4, + LabelKeyboardEvent = 5, + SpriteAccelerationEvent = 6, + RemoveAndRetainNode = 7, + RemoveListenerAfterAdding = 8, +} + +local curLayerIdx = testArray.TouchableSprite + +local EventDispatcherScene = class("EventDispatcherScene") +EventDispatcherScene.__index = EventDispatcherScene + +function EventDispatcherScene.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, EventDispatcherScene) + return target +end + +function EventDispatcherScene.create() + local scene = EventDispatcherScene.extend(cc.Scene:create()) + return scene +end + + +local EventDispatcherTestDemo = class("EventDispatcherTestDemo") +EventDispatcherTestDemo.__index = EventDispatcherTestDemo +EventDispatcherTestDemo._backItem = nil +EventDispatcherTestDemo._restarItem = nil +EventDispatcherTestDemo._nextItem = nil + +function EventDispatcherTestDemo:onEnter() + +end + +function EventDispatcherTestDemo.title(idx) + if testArray.TouchableSprite == idx then + return "Touchable Sprite Test" + elseif testArray.FixedPriority == idx then + return "Fixed priority test" + elseif testArray.RemoveListenerWhenDispatching == idx then + return "Add and remove listener\n when dispatching event" + elseif testArray.CustomEvent == idx then + return "Send custom event" + elseif testArray.LabelKeyboardEvent == idx then + return "Label Receives Keyboard Event" + elseif testArray.SpriteAccelerationEvent == idx then + return "Sprite Receives Acceleration Event" + elseif testArray.RemoveAndRetainNode == idx then + return "RemoveAndRetainNodeTest" + elseif testArray.RemoveListenerAfterAdding == idx then + return "RemoveListenerAfterAddingTest" + end +end + +function EventDispatcherTestDemo.subTitle(idx) + if testArray.TouchableSprite == idx then + return "Please drag the blocks" + elseif testArray.FixedPriority == idx then + return "Fixed Priority, Blue: 30, Red: 20, Yellow: 10\n The lower value the higher priority will be." + elseif testArray.RemoveListenerWhenDispatching == idx then + return "" + elseif testArray.CustomEvent == idx then + return "" + elseif testArray.LabelKeyboardEvent == idx then + return "Please click keyboard\n(Only available on Desktop and Android)" + elseif testArray.SpriteAccelerationEvent == idx then + return "Please move your device\n(Only available on mobile)" + elseif testArray.RemoveAndRetainNode == idx then + return "Sprite should be removed after 5s, add to scene again after 5s" + elseif testArray.RemoveListenerAfterAdding == idx then + return "Should not crash!" + end +end + +function EventDispatcherTestDemo.create() + local layer = EventDispatcherTestDemo.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:onEnter() + layer:creatTitleAndSubTitle(curLayerIdx) + end + + return layer +end + +function EventDispatcherTestDemo.backCallback() + local newScene = EventDispatcherScene.create() + newScene:addChild(backEventDispatcherTest()) + newScene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene(newScene) +end + +function EventDispatcherTestDemo.restartCallback() + local newScene = EventDispatcherScene.create() + newScene:addChild(restartEventDispatcherTest()) + newScene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene(newScene) +end + +function EventDispatcherTestDemo.nextCallback() + local newScene = EventDispatcherScene.create() + newScene:addChild(nextEventDispatcherTest()) + newScene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene(newScene) +end + +function EventDispatcherTestDemo:createMenu() + local menu = cc.Menu:create() + + self._backItem = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + self._backItem:registerScriptTapHandler(self.backCallback) + menu:addChild(self._backItem,itemTagBasic) + self._restarItem = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + self._restarItem:registerScriptTapHandler(self.restartCallback) + menu:addChild(self._restarItem,itemTagBasic) + self._nextItem = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + menu:addChild(self._nextItem,itemTagBasic) + self._nextItem:registerScriptTapHandler(self.nextCallback) + + local size = cc.Director:getInstance():getWinSize() + self._backItem:setPosition(cc.p(size.width / 2 - self._restarItem:getContentSize().width * 2, self._restarItem:getContentSize().height / 2)) + self._restarItem:setPosition(cc.p(size.width / 2, self._restarItem:getContentSize().height / 2)) + self._nextItem:setPosition(cc.p(size.width / 2 + self._restarItem:getContentSize().width * 2, self._restarItem:getContentSize().height / 2)) + + menu:setPosition(cc.p(0, 0)) + + self:addChild(menu) +end + +function EventDispatcherTestDemo:creatTitleAndSubTitle(idx) + local title = cc.LabelTTF:create(EventDispatcherTestDemo.title(idx),"Arial",18) + title:setColor(cc.c3b(128,128,0)) + self:addChild(title, 1, 10000) + title:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 30)) + local subTitle = nil + if "" ~= EventDispatcherTestDemo.subTitle(idx) then + local subTitle = cc.LabelTTF:create(EventDispatcherTestDemo.subTitle(idx), "Arial", 18) + subTitle:setColor(cc.c3b(128,128,0)) + self:addChild(subTitle, 1, 10001) + subTitle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 60) ) + end +end + + +local TouchableSpriteTest = class("TouchableSpriteTest",EventDispatcherTestDemo) +TouchableSpriteTest.__index = TouchableSpriteTest + +function TouchableSpriteTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TouchableSpriteTest) + return target +end + +function TouchableSpriteTest:onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + + local sprite1 = cc.Sprite:create("Images/CyanSquare.png") + sprite1:setPosition(cc.p(origin.x + size.width/2 - 80, origin.y + size.height/2 + 80)) + self:addChild(sprite1, 10) + + local sprite2 = cc.Sprite:create("Images/MagentaSquare.png") + sprite2:setPosition(cc.p(origin.x + size.width/2, origin.x + size.height/2)) + self:addChild(sprite2, 20) + + local sprite3 = cc.Sprite:create("Images/YellowSquare.png") + sprite3:setPosition(cc.p(0, 0)) + sprite2:addChild(sprite3, 1) + + local function onTouchBegan(touch, event) + local target = tolua.cast(event:getCurrentTarget(),"Sprite") + + local locationInNode = target:convertToNodeSpace(touch:getLocation()) + local s = target:getContentSize() + local rect = cc.rect(0, 0, s.width, s.height) + + if cc.rectContainsPoint(rect, locationInNode) then + print(string.format("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y)) + target:setOpacity(180) + return true + end + return false + end + + local function onTouchMoved(touch, event) + local target = tolua.cast(event:getCurrentTarget(), "Sprite") + local posX,posY = target:getPosition() + local delta = touch:getDelta() + target:setPosition(cc.p(posX + delta.x, posY + delta.y)) + end + + local function onTouchEnded(touch, event) + local target = tolua.cast(event:getCurrentTarget(), "Sprite") + print("sprite onTouchesEnded..") + target:setOpacity(255) + if target == sprite2 then + sprite1:setZOrder(100) + elseif target == sprite1 then + sprite1:setZOrder(0) + end + end + + local listener1 = cc.EventListenerTouchOneByOne:create() + listener1:setSwallowTouches(true) + listener1:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN ) + listener1:registerScriptHandler(onTouchMoved,cc.TOUCH_MOVED ) + listener1:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED ) + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, sprite1) + + local listener2 = listener1:clone() + listener2:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN ) + listener2:registerScriptHandler(onTouchMoved,cc.TOUCH_MOVED ) + listener2:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED ) + eventDispatcher:addEventListenerWithSceneGraphPriority(listener2, sprite2) + + + local listener3 = listener1:clone() + listener3:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN ) + listener3:registerScriptHandler(onTouchMoved,cc.TOUCH_MOVED ) + listener3:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED ) + eventDispatcher:addEventListenerWithSceneGraphPriority(listener3, sprite3) + + local function removeAllTouchItem(tag, sender) + sender:setString("Only Next item could be clicked") + eventDispatcher:removeEventListeners(cc.EVENTLISTENER_TOUCH_ONE_BY_ONE) + + local nextMenuItem = cc.MenuItemFont:create("Next") + nextMenuItem:setFontSizeObj(16) + nextMenuItem:setPosition(cc.p(VisibleRect:right().x - 100, VisibleRect:right().y - 30)) + nextMenuItem:registerScriptTapHandler(self.nextCallback) + + local menu2 = cc.Menu:create(nextMenuItem) + menu2:setPosition(cc.p(0, 0)) + menu2:setAnchorPoint(cc.p(0, 0)) + self:addChild(menu2) + end + + local menuItem = cc.MenuItemFont:create("Remove All Touch Listeners") + menuItem:setFontSizeObj(16) + menuItem:setPosition(cc.p(VisibleRect:right().x - 100, VisibleRect:right().y)) + menuItem:registerScriptTapHandler(removeAllTouchItem) + + local menu = cc.Menu:create(menuItem) + menu:setPosition(cc.p(0, 0)) + menu:setAnchorPoint(cc.p(0, 0)) + self:addChild(menu) +end + +function TouchableSpriteTest.create() + local layer = TouchableSpriteTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + +local TouchableSpriteWithFixedPriority = class("TouchableSpriteWithFixedPriority") +TouchableSpriteWithFixedPriority.__index = TouchableSpriteWithFixedPriority +TouchableSpriteWithFixedPriority._listener = nil +TouchableSpriteWithFixedPriority._fixedPriority = 0 +TouchableSpriteWithFixedPriority._useNodePriority = false + +function TouchableSpriteWithFixedPriority.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TouchableSpriteWithFixedPriority) + return target +end + +function TouchableSpriteWithFixedPriority:onEnter() + local function onTouchBegan(touch, event) + local locationInNode = self:convertToNodeSpace(touch:getLocation()) + local s = self:getContentSize() + local rect = cc.rect(0, 0, s.width, s.height) + + if cc.rectContainsPoint(rect, locationInNode) then + self:setColor(cc.c3b(255, 0, 0)) + return true + end + + return false + end + + local function onTouchMoved(touch, event) + + end + + local function onTouchEnded(touch, event) + self:setColor(cc.c3b(255, 255, 255)) + end + + local listener = cc.EventListenerTouchOneByOne:create() + self._listener = listener + listener:setSwallowTouches(true) + listener:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN ) + listener:registerScriptHandler(onTouchMoved,cc.TOUCH_MOVED ) + listener:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED ) + + local eventDispatcher = self:getEventDispatcher() + if self._useNodePriority then + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self) + else + eventDispatcher:addEventListenerWithFixedPriority(listener,self._fixedPriority) + end +end + +function TouchableSpriteWithFixedPriority:onExit() + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:removeEventListener(self._listener) +end + +function TouchableSpriteWithFixedPriority:setPriority(fixedPriority) + self._fixedPriority = fixedPriority + self._useNodePriority = false +end + +function TouchableSpriteWithFixedPriority:setPriorityWithThis(useNodePriority) + self._fixedPriority = 0 + self._useNodePriority = useNodePriority +end + +function TouchableSpriteWithFixedPriority.create() + local touchableSprite = TouchableSpriteWithFixedPriority.extend(cc.Sprite:create()) + + local function onNodeEvent(event) + if event == "enter" then + touchableSprite:onEnter() + elseif event == "exit" then + touchableSprite:onExit() + end + end + + touchableSprite:registerScriptHandler(onNodeEvent) + return touchableSprite +end + +local FixedPriorityTest = class("FixedPriorityTest",EventDispatcherTestDemo) +FixedPriorityTest.__index = FixedPriorityTest + +function FixedPriorityTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, FixedPriorityTest) + return target +end + +function FixedPriorityTest:onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + + local sprite1 = TouchableSpriteWithFixedPriority.create() + sprite1:setTexture("Images/CyanSquare.png") + sprite1:setPriority(30) + sprite1:setPosition(cc.p(origin.x + size.width/2 - 80, origin.y + size.height/2 + 40)) + self:addChild(sprite1, 10) + + local sprite2 = TouchableSpriteWithFixedPriority.create() + sprite2:setTexture("Images/MagentaSquare.png") + sprite2:setPriority(20) + sprite2:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2) ) + self:addChild(sprite2, 20) + + local sprite3 = TouchableSpriteWithFixedPriority:create() + sprite3:setTexture("Images/YellowSquare.png") + sprite3:setPriority(10) + sprite3:setPosition(cc.p(0, 0)) + sprite2:addChild(sprite3, 1) +end + +function FixedPriorityTest.create() + local layer = FixedPriorityTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + +local RemoveListenerWhenDispatchingTest = class("RemoveListenerWhenDispatchingTest",EventDispatcherTestDemo) +RemoveListenerWhenDispatchingTest.__index = RemoveListenerWhenDispatchingTest + +function RemoveListenerWhenDispatchingTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, RemoveListenerWhenDispatchingTest) + return target +end + +function RemoveListenerWhenDispatchingTest:onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + + local sprite1 = cc.Sprite:create("Images/CyanSquare.png") + sprite1:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2)) + self:addChild(sprite1, 10) + + local function onTouchBegan(touch, event) + local locationInNode = sprite1:convertToNodeSpace(touch:getLocation()) + local s = sprite1:getContentSize() + local rect = cc.rect(0, 0, s.width, s.height) + + if cc.rectContainsPoint(rect, locationInNode) then + sprite1:setColor(cc.c3b(255, 0, 0)) + return true + end + + return false + end + + local function onTouchEnded(touch, event) + sprite1:setColor(cc.c3b(255, 255, 255)) + end + + local listener1 = cc.EventListenerTouchOneByOne:create() + listener1:setSwallowTouches(true) + self:setUserObject(listener1) + + listener1:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN ) + listener1:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED ) + + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, sprite1) + + local statusLabel = cc.LabelTTF:create("The sprite could be touched!", "", 20) + statusLabel:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height - 90)) + self:addChild(statusLabel) + + local enable = true + local function toggleCallback(tag, sender) + if enable then + eventDispatcher:removeEventListener(listener1) + statusLabel:setString("The sprite could not be touched!") + enable = false + else + eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, sprite1) + statusLabel:setString("The sprite could be touched!") + enable = true + end + end + + local toggleItem = cc.MenuItemToggle:create(cc.MenuItemFont:create("Enabled"), cc.MenuItemFont:create("Disabled")) + toggleItem:setPosition(cc.p(origin.x + size.width/2, origin.y + 80)) + toggleItem:registerScriptTapHandler(toggleCallback) + local menu = cc.Menu:create(toggleItem) + menu:setPosition(cc.p(0, 0)) + menu:setAnchorPoint(cc.p(0, 0)) + self:addChild(menu, -1) +end + + +function RemoveListenerWhenDispatchingTest.create() + local layer = RemoveListenerWhenDispatchingTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + +local CustomEventTest = class("CustomEventTest",EventDispatcherTestDemo) +CustomEventTest.__index = CustomEventTest +CustomEventTest._listener1 = nil +CustomEventTest._listener2 = nil + +function CustomEventTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, CustomEventTest) + return target +end + +function CustomEventTest:onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + local count1 = 0 + local count2 = 0 + + cc.MenuItemFont:setFontSize(20) + + local statusLabel1 = cc.LabelTTF:create("No custom event 1 received!", "", 20) + statusLabel1:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height-90 )) + self:addChild(statusLabel1) + + local function eventCustomListener1(event) + local str = "Custom event 1 received, "..event._usedata.." times" + statusLabel1:setString(str) + end + + local listener1 = cc.LuaEventListenerCustom:create("game_custom_event1",eventCustomListener1) + self._listener1 = listener1 + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithFixedPriority(listener1, 1) + + local function sendCallback1(tag, sender) + count1 = count1 + 1 + + local event = cc.EventCustom:new("game_custom_event1") + event._usedata = string.format("%d",count1) + eventDispatcher:dispatchEvent(event) + end + local sendItem1 = cc.MenuItemFont:create("Send Custom Event 1") + sendItem1:registerScriptTapHandler(sendCallback1) + sendItem1:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2)) + + local statusLabel2 = cc.LabelTTF:create("No custom event 2 received!", "", 20) + statusLabel2:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height-120 )) + self:addChild(statusLabel2) + + local function eventCustomListener2(event) + local str = "Custom event 2 received, "..event._usedata.." times" + statusLabel2:setString(str) + end + + local listener2 = cc.LuaEventListenerCustom:create("game_custom_event2",eventCustomListener2) + CustomEventTest._listener2 = listener2 + eventDispatcher:addEventListenerWithFixedPriority(listener2, 1) + + local function sendCallback2(tag, sender) + count2 = count2 + 1 + + local event = cc.EventCustom:new("game_custom_event2") + event._usedata = string.format("%d",count2) + eventDispatcher:dispatchEvent(event) + end + local sendItem2 = cc.MenuItemFont:create("Send Custom Event 2") + sendItem2:registerScriptTapHandler(sendCallback2) + sendItem2:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2 - 40)) + + local menu = cc.Menu:create(sendItem1, sendItem2) + menu:setPosition(cc.p(0, 0)) + menu:setAnchorPoint(cc.p(0, 0)) + self:addChild(menu, -1) +end + +function CustomEventTest:onExit() + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:removeEventListener(self._listener1) + eventDispatcher:removeEventListener(self._listener2) +end + +function CustomEventTest.create() + local layer = CustomEventTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + layer:onExit() + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + + + +local LabelKeyboardEventTest = class("LabelKeyboardEventTest",EventDispatcherTestDemo) +LabelKeyboardEventTest.__index = LabelKeyboardEventTest + +function LabelKeyboardEventTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, LabelKeyboardEventTest) + return target +end + +function LabelKeyboardEventTest:onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + + local statusLabel = cc.LabelTTF:create("No keyboard event received!", "", 20) + statusLabel:setPosition(cc.p(origin.x + size.width/2,origin.y + size.height/2)) + self:addChild(statusLabel) + + local function onKeyPressed(keyCode, event) + local buf = string.format("Key %s was pressed!",string.char(keyCode)) + local label = event:getCurrentTarget() + label:setString(buf) + end + + local function onKeyReleased(keyCode, event) + local buf = string.format("Key %s was released!",string.char(keyCode)) + local label = event:getCurrentTarget() + label:setString(buf) + end + + local listener = cc.EventListenerKeyboard:create() + listener:registerScriptHandler(onKeyPressed, cc.KEYBOARD_PRESSED) + listener:registerScriptHandler(onKeyReleased, cc.KEYBOARD_RELEASE) + + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, statusLabel) +end + + +function LabelKeyboardEventTest.create() + local layer = LabelKeyboardEventTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + +local SpriteAccelerationEventTest = class("SpriteAccelerationEventTest",EventDispatcherTestDemo) +SpriteAccelerationEventTest.__index = SpriteAccelerationEventTest + +function SpriteAccelerationEventTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, SpriteAccelerationEventTest) + return target +end + +function SpriteAccelerationEventTest:onEnter() + self:setAccelerometerEnabled(true) + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + local sprite = cc.Sprite:create("Images/ball.png") + sprite:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y)) + self:addChild(sprite) + + local function accelerometerListener(event,x,y,z,timestamp) + local target = event:getCurrentTarget() + local ballSize = target:getContentSize() + local ptNowX,ptNowY = target:getPosition() + ptNowX = ptNowX + x * 9.81 + ptNowY = ptNowY + y * 9.81 + + local minX = math.floor(VisibleRect:left().x + ballSize.width / 2.0) + local maxX = math.floor(VisibleRect:right().x - ballSize.width / 2.0) + if ptNowX < minX then + ptNowX = minX + elseif ptNowX > maxX then + ptNowX = maxX + end + + local minY = math.floor(VisibleRect:bottom().y + ballSize.height / 2.0) + local maxY = math.floor(VisibleRect:top().y - ballSize.height / 2.0) + if ptNowY < minY then + ptNowY = minY + elseif ptNowY > maxY then + ptNowY = maxY + end + + target:setPosition(cc.p(ptNowX , ptNowY)) + end + + local listerner = cc.LuaEventListenerAcceleration:create(accelerometerListener) + + self:getEventDispatcher():addEventListenerWithSceneGraphPriority(listerner,sprite) +end + +function SpriteAccelerationEventTest:onExit() + self:setAccelerometerEnabled(false) +end + +function SpriteAccelerationEventTest.create() + local layer = SpriteAccelerationEventTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + layer:onExit() + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + + +local RemoveAndRetainNodeTest = class("RemoveAndRetainNodeTest",EventDispatcherTestDemo) +RemoveAndRetainNodeTest.__index = RemoveAndRetainNodeTest +RemoveAndRetainNodeTest._spriteSaved = false +RemoveAndRetainNodeTest._sprite = nil + +function RemoveAndRetainNodeTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, RemoveAndRetainNodeTest) + return target +end + +function RemoveAndRetainNodeTest:onEnter() + self._spriteSaved = false + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + + local sprite = cc.Sprite:create("Images/CyanSquare.png") + sprite:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2)) + self._sprite = sprite + self:addChild(sprite, 10) + + local function onTouchBegan(touch,event) + local target = event:getCurrentTarget() + local locationInNode = target:convertToNodeSpace(touch:getLocation()) + local s = target:getContentSize() + local rect = cc.rect(0, 0, s.width, s.height) + + if cc.rectContainsPoint(rect, locationInNode) then + print(string.format("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y)) + target:setOpacity(180) + return true + end + return false + end + + local function onTouchMoved(touch,event) + local target = event:getCurrentTarget() + local posX,posY = target:getPosition() + local delta = touch:getDelta() + target:setPosition(cc.p(posX + delta.x, posY + delta.y)) + end + + local function onTouchEnded(touch,event) + local target = event:getCurrentTarget() + print("sprite onTouchesEnded.. ") + target:setOpacity(255) + end + + local listener1 = cc.EventListenerTouchOneByOne:create() + listener1:setSwallowTouches(true) + listener1:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN) + listener1:registerScriptHandler(onTouchMoved,cc.TOUCH_MOVED) + listener1:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED) + self:getEventDispatcher():addEventListenerWithSceneGraphPriority(listener1, sprite) + + local function retainSprite() + self._spriteSaved = true + self._sprite:retain() + self._sprite:removeFromParent() + end + + local function releaseSprite() + self._spriteSaved = false + self:addChild(self._sprite) + self._sprite:release() + end + + self:runAction( cc.Sequence:create(cc.DelayTime:create(5.0), + cc.CallFunc:create(retainSprite), + cc.DelayTime:create(5.0), + cc.CallFunc:create(releaseSprite) + )) + +end + +function RemoveAndRetainNodeTest:onExit() + if self._spriteSaved then + self._sprite:release() + end +end + +function RemoveAndRetainNodeTest.create() + local layer = RemoveAndRetainNodeTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + layer:onExit() + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + +local RemoveListenerAfterAddingTest = class("RemoveListenerAfterAddingTest",EventDispatcherTestDemo) +RemoveListenerAfterAddingTest.__index = RemoveListenerAfterAddingTest + + +function RemoveListenerAfterAddingTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, RemoveListenerAfterAddingTest) + return target +end + +function RemoveListenerAfterAddingTest:onEnter() + + local eventDispatcher = self:getEventDispatcher() + + local function item1Callback(tag, sender) + + local function onTouchBegan(touch, event) + CCASSERT(false, "Should not come here!") + return true + end + + local listener = cc.EventListenerTouchOneByOne:create() + listener:registerScriptHandler(onTouchBegan, cc.TOUCH_BEGAN) + eventDispatcher:addEventListenerWithFixedPriority(listener, -1) + eventDispatcher:removeEventListener(listener) + end + + local item1 = cc.MenuItemFont:create("Click Me 1") + item1:registerScriptTapHandler(item1Callback) + item1:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y + 80)) + + local function addNextButton() + + local function nextButtonCallback(tag, sender) + self.restartCallback() + end + + local nextButton = cc.MenuItemFont:create("Please Click Me To Reset!") + nextButton:registerScriptTapHandler(nextButtonCallback) + nextButton:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y - 40)) + + local menu = cc.Menu:create(nextButton) + menu:setPosition(VisibleRect:leftBottom()) + menu:setAnchorPoint(cc.p(0,0)) + self:addChild(menu) + end + + local function item2Callback( tag, sender ) + + local function onTouchBegan(touch, event) + print("Should not come here!") + return true + end + + local listener = cc.EventListenerTouchOneByOne:create() + listener:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN) + + eventDispatcher:addEventListenerWithFixedPriority(listener, -1) + eventDispatcher:removeEventListeners(cc.EVENTLISTENER_TOUCH_ONE_BY_ONE) + + addNextButton() + + end + + local item2 = cc.MenuItemFont:create("Click Me 2") + item2:registerScriptTapHandler(item2Callback) + item2:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y + 40)) + + local function item3Callback( tag, sender ) + + local function onTouchBegan(touch, event) + print("Should not come here!") + return true + end + + local listener = cc.EventListenerTouchOneByOne:create() + listener:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN) + + eventDispatcher:addEventListenerWithFixedPriority(listener, -1) + eventDispatcher:removeAllEventListeners() + + addNextButton() + + end + + local item3 = cc.MenuItemFont:create("Click Me 3") + item3:registerScriptTapHandler(item3Callback) + item3:setPosition(VisibleRect:center()) + + local menu = cc.Menu:create(item1, item2, item3) + menu:setPosition(VisibleRect:leftBottom()) + menu:setAnchorPoint(cc.p(0,0)) + + self:addChild(menu) +end + +function RemoveListenerAfterAddingTest:onExit() + +end + +function RemoveListenerAfterAddingTest.create() + local layer = RemoveListenerAfterAddingTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + layer:onExit() + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + + +local createFunction = +{ + TouchableSpriteTest.create, + FixedPriorityTest.create, + RemoveListenerWhenDispatchingTest.create, + CustomEventTest.create, + LabelKeyboardEventTest.create, + SpriteAccelerationEventTest.create, + RemoveAndRetainNodeTest.create, + RemoveListenerAfterAddingTest.create, +} + +function nextEventDispatcherTest() + curLayerIdx = curLayerIdx + 1 + curLayerIdx = curLayerIdx % table.getn(createFunction) + if 0 == curLayerIdx then + curLayerIdx = table.getn(createFunction) + end + return createFunction[curLayerIdx]() +end + +function backEventDispatcherTest() + curLayerIdx = curLayerIdx - 1 + if curLayerIdx <= 0 then + curLayerIdx = curLayerIdx + table.getn(createFunction) + end + + return createFunction[curLayerIdx]() +end + +function restartEventDispatcherTest() + return createFunction[curLayerIdx]() +end + + +function NewEventDispatcherTest() + local scene = EventDispatcherScene.create() + curLayerIdx = testArray.TouchableSprite + scene:addChild(restartEventDispatcherTest()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua b/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua index ddab173d10..8eac7636f9 100644 --- a/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua +++ b/samples/Lua/TestLua/Resources/luaScript/mainMenu.lua @@ -32,6 +32,7 @@ require "luaScript/LabelTestNew/LabelTestNew" require "luaScript/LayerTest/LayerTest" require "luaScript/MenuTest/MenuTest" require "luaScript/MotionStreakTest/MotionStreakTest" +require "luaScript/NewEventDispatcherTest/NewEventDispatcherTest" require "luaScript/NodeTest/NodeTest" require "luaScript/OpenGLTest/OpenGLTest" require "luaScript/ParallaxTest/ParallaxTest" @@ -87,6 +88,7 @@ local _allTests = { { isSupported = true, name = "MenuTest" , create_func = MenuTestMain }, { isSupported = true, name = "MotionStreakTest" , create_func = MotionStreakTest }, { isSupported = false, name = "MutiTouchTest" , create_func= MutiTouchTestMain }, + { isSupported = true, name = "NewEventDispatcherTest" , create_func = NewEventDispatcherTest }, { isSupported = true, name = "NodeTest" , create_func = CocosNodeTest }, { isSupported = true, name = "OpenGLTest" , create_func= OpenGLTestMain }, { isSupported = true, name = "ParallaxTest" , create_func = ParallaxTestMain }, diff --git a/tools/tolua/cocos2dx.ini b/tools/tolua/cocos2dx.ini index bea7b1f326..02b9a3819e 100644 --- a/tools/tolua/cocos2dx.ini +++ b/tools/tolua/cocos2dx.ini @@ -26,7 +26,7 @@ headers = %(cocosdir)s/cocos/2d/cocos2d.h %(cocosdir)s/cocos/audio/include/Simpl # what classes to produce code for. You can use regular expressions here. When testing the regular # expression, it will be enclosed in "^$", like this: "^Menu*$". -classes = New.* Sprite.* Scene Node.* Director Layer.* Menu.* Touch .*Action.* Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn Animat.* Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* PointArray ToggleVisibility.* RemoveSelf Hide Particle.* Label.* Atlas.* TextureCache.* Texture2D Cardinal.* CatmullRom.* ParallaxNode TileMap.* TMX.* CallFunc RenderTexture GridAction Grid3DAction GridBase$ .+Grid Shaky3D Waves3D FlipX3D FlipY3D Speed ActionManager Set Data SimpleAudioEngine Scheduler Timer Orbit.* Follow.* Bezier.* CardinalSpline.* Camera.* DrawNode .*3D$ Liquid$ Waves$ ShuffleTiles$ TurnOffTiles$ Split.* Twirl$ FileUtils$ GLProgram ShaderCache Application ClippingNode MotionStreak ^Object$ UserDefault EGLViewProtocol EGLView Image +classes = New.* Sprite.* Scene Node.* Director Layer.* Menu.* Touch .*Action.* Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn Animat.* Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* PointArray ToggleVisibility.* RemoveSelf Hide Particle.* Label.* Atlas.* TextureCache.* Texture2D Cardinal.* CatmullRom.* ParallaxNode TileMap.* TMX.* CallFunc RenderTexture GridAction Grid3DAction GridBase$ .+Grid Shaky3D Waves3D FlipX3D FlipY3D Speed ActionManager Set Data SimpleAudioEngine Scheduler Timer Orbit.* Follow.* Bezier.* CardinalSpline.* Camera.* DrawNode .*3D$ Liquid$ Waves$ ShuffleTiles$ TurnOffTiles$ Split.* Twirl$ FileUtils$ GLProgram ShaderCache Application ClippingNode MotionStreak ^Object$ UserDefault EGLViewProtocol EGLView Image Event.* # what should we skip? in the format ClassName::[function function] # ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also @@ -79,6 +79,9 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS Dictionary::[*], Array::[*], Range::[*], + EventListenerVector::[*], + EventListener.*::[create], + EventTouch::[(s|g)etTouches], NotificationObserver::[*], Image::[initWithString initWithImageData initWithRawData], Sequence::[create], @@ -139,7 +142,7 @@ base_classes_to_skip = Clonable # classes that create no constructor # Set is special and we will use a hand-written constructor -abstract_classes = Action FiniteTimeAction ActionInterval ActionEase EaseRateAction EaseElastic EaseBounce ActionInstant GridAction Grid3DAction TiledGrid3DAction Director SpriteFrameCache TransitionEaseScene Set SimpleAudioEngine FileUtils Application ClippingNode Label EGLViewProtocol EGLView +abstract_classes = Action FiniteTimeAction ActionInterval ActionEase EaseRateAction EaseElastic EaseBounce ActionInstant GridAction Grid3DAction TiledGrid3DAction Director SpriteFrameCache TransitionEaseScene Set SimpleAudioEngine FileUtils Application ClippingNode Label EGLViewProtocol EGLView EventAcceleration # Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'. script_control_cpp = no From 71d5896d419d5087aaa03737d94a44f9f4dcf15b Mon Sep 17 00:00:00 2001 From: samuele3hu <qingkui.hu@cocos2d-x.org> Date: Mon, 23 Dec 2013 09:42:36 +0800 Subject: [PATCH 2/3] issue #3403:Delete Camera manual lua binding --- .../lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id index 61eb9dbd42..bda972a6e8 100644 --- a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id +++ b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id @@ -1 +1 @@ -8d902acd33947b88d5c849c3e36c0da7c7b1af8c \ No newline at end of file +557045812d83325acae1244501059ca76fff1485 \ No newline at end of file From f0dc7f0b15e237d2d65225232b1cc73f829c1cc1 Mon Sep 17 00:00:00 2001 From: samuele3hu <qingkui.hu@cocos2d-x.org> Date: Mon, 23 Dec 2013 11:19:55 +0800 Subject: [PATCH 3/3] issue #3403:Modify the registerScriptHandler functions inputparams and some EventData struct info --- cocos/scripting/lua/bindings/CCLuaEngine.cpp | 75 +------------------ .../lua_cocos2dx_manual.cpp.REMOVED.git-id | 2 +- .../lua/bindings/lua_cocos2dx_manual.hpp | 25 ++++--- .../scripting/lua/script/Cocos2dConstants.lua | 16 ++++ .../NewEventDispatcherTest.lua | 51 ++++++------- 5 files changed, 60 insertions(+), 109 deletions(-) diff --git a/cocos/scripting/lua/bindings/CCLuaEngine.cpp b/cocos/scripting/lua/bindings/CCLuaEngine.cpp index e8261931c7..7edc81b366 100644 --- a/cocos/scripting/lua/bindings/CCLuaEngine.cpp +++ b/cocos/scripting/lua/bindings/CCLuaEngine.cpp @@ -927,12 +927,8 @@ int LuaEngine::handleEventListenerKeyboard(void* data) return 0; LuaEventListenerKeyboarData* keyboardData = static_cast<LuaEventListenerKeyboarData*>(basicScriptData->value); - ScriptHandlerMgr::HandlerType type = ScriptHandlerMgr::HandlerType::EVENTLISTENER_KEYBOARD_PRESSED; - if (keyboardData->status == LuaEventListenerKeyboarData::KeyboardStatus::RELEASE) - type = ScriptHandlerMgr::HandlerType::EVENTLISTENER_KEYBOARD_RELEASE; - - int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, type); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, keyboardData->type); if (0 == handler) return 0; @@ -953,28 +949,7 @@ int LuaEngine::handleEventListenerTouch(void* data) if (nullptr == listenerData->nativeObject || nullptr == listenerData->touch || nullptr == listenerData->event) return 0; - ScriptHandlerMgr::HandlerType handlerType; - switch (listenerData->eventCode) - { - case EventTouch::EventCode::BEGAN: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_BEGAN; - break; - - case EventTouch::EventCode::MOVED: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_MOVED; - break; - - case EventTouch::EventCode::ENDED: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_ENDED; - break; - case EventTouch::EventCode::CANCELLED: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_CANCELLED; - break; - default: - return 0; - } - - int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(listenerData->nativeObject, handlerType); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(listenerData->nativeObject, listenerData->type); if (0 == handler) return 0; @@ -1001,28 +976,7 @@ int LuaEngine::handleEventListenerTouches(void* data) if (NULL == listenerData->nativeObject || nullptr == listenerData->event || listenerData->touches.size() == 0) return 0; - ScriptHandlerMgr::HandlerType handlerType; - switch (listenerData->eventCode) - { - case EventTouch::EventCode::BEGAN: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_BEGAN; - break; - - case EventTouch::EventCode::MOVED: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_MOVED; - break; - - case EventTouch::EventCode::ENDED: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_ENDED; - break; - case EventTouch::EventCode::CANCELLED: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_TOUCH_CANCELLED; - break; - default: - return 0; - } - - int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)listenerData->nativeObject, handlerType); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)listenerData->nativeObject, listenerData->type); if (0 == handler) return 0; @@ -1055,28 +1009,7 @@ int LuaEngine::handleEventListenerMouse(void* data) if (NULL == listenerData->nativeObject || nullptr == listenerData->event ) return 0; - ScriptHandlerMgr::HandlerType handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_DOWN; - switch (listenerData->eventType) - { - case EventMouse::MouseEventType::MOUSE_DOWN: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_DOWN; - break; - - case EventMouse::MouseEventType::MOUSE_UP: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_UP; - break; - - case EventMouse::MouseEventType::MOUSE_MOVE: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_MOVE; - break; - case EventMouse::MouseEventType::MOUSE_SCROLL: - handlerType = ScriptHandlerMgr::HandlerType::EVENTLISTENER_MOUSE_SCROLL; - break; - default: - break; - } - - int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)listenerData->nativeObject, handlerType); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)listenerData->nativeObject, listenerData->type); if (0 == handler) return 0; diff --git a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id index bda972a6e8..60b4dcf835 100644 --- a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id +++ b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.cpp.REMOVED.git-id @@ -1 +1 @@ -557045812d83325acae1244501059ca76fff1485 \ No newline at end of file +b633b49d700c36e38f74509d09c52255e2300ecf \ No newline at end of file diff --git a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.hpp b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.hpp index 8148a9e3f6..5dd2062010 100644 --- a/cocos/scripting/lua/bindings/lua_cocos2dx_manual.hpp +++ b/cocos/scripting/lua/bindings/lua_cocos2dx_manual.hpp @@ -10,6 +10,7 @@ extern "C" { #endif #include "cocos2d.h" +#include "LuaScriptHandlerMgr.h" NS_CC_BEGIN class LuaEventListenerCustom @@ -51,23 +52,23 @@ struct LuaEventListenerKeyboarData }; int keyCode; Event* event; - KeyboardStatus status; + ScriptHandlerMgr::HandlerType type; - LuaEventListenerKeyboarData(int inKeyCode,Event* inEvent,KeyboardStatus inStatus) - :keyCode(inKeyCode),event(inEvent),status(inStatus) + LuaEventListenerKeyboarData(int inKeyCode,Event* inEvent,ScriptHandlerMgr::HandlerType inType) + :keyCode(inKeyCode),event(inEvent),type(inType) { } }; struct LuaEventListenerTouchData { - EventTouch::EventCode eventCode; + ScriptHandlerMgr::HandlerType type; void* nativeObject; Touch* touch; Event* event; - LuaEventListenerTouchData(EventTouch::EventCode inEventCode, void* inNativeObject, Touch* inTouch, Event* inEvent) - : eventCode(inEventCode), + LuaEventListenerTouchData(ScriptHandlerMgr::HandlerType inType, void* inNativeObject, Touch* inTouch, Event* inEvent) + : type(inType), nativeObject(inNativeObject), touch(inTouch), event(inEvent) @@ -77,13 +78,13 @@ struct LuaEventListenerTouchData struct LuaEventListenerTouchesData { - EventTouch::EventCode eventCode; + ScriptHandlerMgr::HandlerType type; void* nativeObject; std::vector<Touch*> touches; Event* event; - LuaEventListenerTouchesData(EventTouch::EventCode inEventCode, void* inNativeObject, std::vector<Touch*> inTouches, Event* inEvent) - : eventCode(inEventCode), + LuaEventListenerTouchesData(ScriptHandlerMgr::HandlerType inType, void* inNativeObject, std::vector<Touch*> inTouches, Event* inEvent) + : type(inType), nativeObject(inNativeObject), touches(inTouches), event(inEvent) @@ -93,12 +94,12 @@ struct LuaEventListenerTouchesData struct LuaEventListenerMouseData { - EventMouse::MouseEventType eventType; + ScriptHandlerMgr::HandlerType type; void* nativeObject; Event* event; - LuaEventListenerMouseData(EventMouse::MouseEventType inEventType, void* inNativeObject, Event* inEvent) - : eventType(inEventType), + LuaEventListenerMouseData(ScriptHandlerMgr::HandlerType inType, void* inNativeObject, Event* inEvent) + : type(inType), nativeObject(inNativeObject), event(inEvent) { diff --git a/cocos/scripting/lua/script/Cocos2dConstants.lua b/cocos/scripting/lua/script/Cocos2dConstants.lua index 117dafe867..3cfd6295c5 100644 --- a/cocos/scripting/lua/script/Cocos2dConstants.lua +++ b/cocos/scripting/lua/script/Cocos2dConstants.lua @@ -326,6 +326,22 @@ cc.HANDLERTYPE_ASSETSMANAGER_SUCCESS = 33 cc.HANDLERTYPE_ASSETSMANAGER_ERROR = 34 cc.HANDLERTYPE_EVENT_LISTENER = 35 cc.HANDLERTYPE_ARMATURE_EVENT = 36 +cc.HANDLERTYPE_EVENTLISTENER_ACC = 37 +cc.HANDLERTYPE_EVENTLISTENER_CUSTIOM = 38 + +cc.HANDLERTYPE_EVENTLISTENER_KEYBOARD_PRESSED = 39 +cc.HANDLERTYPE_EVENTLISTENER_KEYBOARD_RELEASE = 40 + +cc.HANDLERTYPE_EVENTLISTENER_TOUCH_BEGAN = 41 +cc.HANDLERTYPE_EVENTLISTENER_TOUCH_MOVED = 42 +cc.HANDLERTYPE_EVENTLISTENER_TOUCH_ENDED = 43 +cc.HANDLERTYPE_EVENTLISTENER_TOUCH_CANCELLED = 44 + +cc.HANDLERTYPE_EVENTLISTENER_MOUSE_DOWN = 45 +cc.HANDLERTYPE_EVENTLISTENER_MOUSE_UP = 46 +cc.HANDLERTYPE_EVENTLISTENER_MOUSE_MOVE = 47 +cc.HANDLERTYPE_EVENTLISTENER_MOUSE_SCROLL = 48 + cc.GLYPHCOLLECTION_DYNAMIC = 0 cc.GLYPHCOLLECTION_NEHE = 1 diff --git a/samples/Lua/TestLua/Resources/luaScript/NewEventDispatcherTest/NewEventDispatcherTest.lua b/samples/Lua/TestLua/Resources/luaScript/NewEventDispatcherTest/NewEventDispatcherTest.lua index 2e4132d22d..1804d67f8e 100644 --- a/samples/Lua/TestLua/Resources/luaScript/NewEventDispatcherTest/NewEventDispatcherTest.lua +++ b/samples/Lua/TestLua/Resources/luaScript/NewEventDispatcherTest/NewEventDispatcherTest.lua @@ -218,23 +218,23 @@ function TouchableSpriteTest:onEnter() local listener1 = cc.EventListenerTouchOneByOne:create() listener1:setSwallowTouches(true) - listener1:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN ) - listener1:registerScriptHandler(onTouchMoved,cc.TOUCH_MOVED ) - listener1:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED ) + listener1:registerScriptHandler(onTouchBegan,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_BEGAN ) + listener1:registerScriptHandler(onTouchMoved,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_MOVED ) + listener1:registerScriptHandler(onTouchEnded,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_ENDED ) local eventDispatcher = self:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, sprite1) local listener2 = listener1:clone() - listener2:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN ) - listener2:registerScriptHandler(onTouchMoved,cc.TOUCH_MOVED ) - listener2:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED ) + listener2:registerScriptHandler(onTouchBegan,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_BEGAN) + listener2:registerScriptHandler(onTouchMoved,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_MOVED) + listener2:registerScriptHandler(onTouchEnded,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_ENDED ) eventDispatcher:addEventListenerWithSceneGraphPriority(listener2, sprite2) local listener3 = listener1:clone() - listener3:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN ) - listener3:registerScriptHandler(onTouchMoved,cc.TOUCH_MOVED ) - listener3:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED ) + listener3:registerScriptHandler(onTouchBegan,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_BEGAN ) + listener3:registerScriptHandler(onTouchMoved,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_MOVED ) + listener3:registerScriptHandler(onTouchEnded,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_ENDED ) eventDispatcher:addEventListenerWithSceneGraphPriority(listener3, sprite3) local function removeAllTouchItem(tag, sender) @@ -320,9 +320,10 @@ function TouchableSpriteWithFixedPriority:onEnter() local listener = cc.EventListenerTouchOneByOne:create() self._listener = listener listener:setSwallowTouches(true) - listener:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN ) - listener:registerScriptHandler(onTouchMoved,cc.TOUCH_MOVED ) - listener:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED ) + + listener:registerScriptHandler(onTouchBegan,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_BEGAN ) + listener:registerScriptHandler(onTouchMoved,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_MOVED ) + listener:registerScriptHandler(onTouchEnded,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_ENDED ) local eventDispatcher = self:getEventDispatcher() if self._useNodePriority then @@ -456,8 +457,8 @@ function RemoveListenerWhenDispatchingTest:onEnter() listener1:setSwallowTouches(true) self:setUserObject(listener1) - listener1:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN ) - listener1:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED ) + listener1:registerScriptHandler(onTouchBegan,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_BEGAN ) + listener1:registerScriptHandler(onTouchEnded,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_ENDED ) local eventDispatcher = self:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, sprite1) @@ -537,7 +538,7 @@ function CustomEventTest:onEnter() statusLabel1:setString(str) end - local listener1 = cc.LuaEventListenerCustom:create("game_custom_event1",eventCustomListener1) + local listener1 = cc.EventListenerCustom:create("game_custom_event1",eventCustomListener1) self._listener1 = listener1 local eventDispatcher = self:getEventDispatcher() eventDispatcher:addEventListenerWithFixedPriority(listener1, 1) @@ -562,7 +563,7 @@ function CustomEventTest:onEnter() statusLabel2:setString(str) end - local listener2 = cc.LuaEventListenerCustom:create("game_custom_event2",eventCustomListener2) + local listener2 = cc.EventListenerCustom:create("game_custom_event2",eventCustomListener2) CustomEventTest._listener2 = listener2 eventDispatcher:addEventListenerWithFixedPriority(listener2, 1) @@ -642,8 +643,8 @@ function LabelKeyboardEventTest:onEnter() end local listener = cc.EventListenerKeyboard:create() - listener:registerScriptHandler(onKeyPressed, cc.KEYBOARD_PRESSED) - listener:registerScriptHandler(onKeyReleased, cc.KEYBOARD_RELEASE) + listener:registerScriptHandler(onKeyPressed, cc.HANDLERTYPE_EVENTLISTENER_KEYBOARD_PRESSED ) + listener:registerScriptHandler(onKeyReleased, cc.HANDLERTYPE_EVENTLISTENER_KEYBOARD_RELEASE ) local eventDispatcher = self:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(listener, statusLabel) @@ -713,7 +714,7 @@ function SpriteAccelerationEventTest:onEnter() target:setPosition(cc.p(ptNowX , ptNowY)) end - local listerner = cc.LuaEventListenerAcceleration:create(accelerometerListener) + local listerner = cc.EventListenerAcceleration:create(accelerometerListener) self:getEventDispatcher():addEventListenerWithSceneGraphPriority(listerner,sprite) end @@ -794,9 +795,9 @@ function RemoveAndRetainNodeTest:onEnter() local listener1 = cc.EventListenerTouchOneByOne:create() listener1:setSwallowTouches(true) - listener1:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN) - listener1:registerScriptHandler(onTouchMoved,cc.TOUCH_MOVED) - listener1:registerScriptHandler(onTouchEnded,cc.TOUCH_ENDED) + listener1:registerScriptHandler(onTouchBegan,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_BEGAN) + listener1:registerScriptHandler(onTouchMoved,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_MOVED) + listener1:registerScriptHandler(onTouchEnded,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_ENDED) self:getEventDispatcher():addEventListenerWithSceneGraphPriority(listener1, sprite) local function retainSprite() @@ -868,7 +869,7 @@ function RemoveListenerAfterAddingTest:onEnter() end local listener = cc.EventListenerTouchOneByOne:create() - listener:registerScriptHandler(onTouchBegan, cc.TOUCH_BEGAN) + listener:registerScriptHandler(onTouchBegan, cc.HANDLERTYPE_EVENTLISTENER_TOUCH_BEGAN) eventDispatcher:addEventListenerWithFixedPriority(listener, -1) eventDispatcher:removeEventListener(listener) end @@ -901,7 +902,7 @@ function RemoveListenerAfterAddingTest:onEnter() end local listener = cc.EventListenerTouchOneByOne:create() - listener:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN) + listener:registerScriptHandler(onTouchBegan,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_BEGAN) eventDispatcher:addEventListenerWithFixedPriority(listener, -1) eventDispatcher:removeEventListeners(cc.EVENTLISTENER_TOUCH_ONE_BY_ONE) @@ -922,7 +923,7 @@ function RemoveListenerAfterAddingTest:onEnter() end local listener = cc.EventListenerTouchOneByOne:create() - listener:registerScriptHandler(onTouchBegan,cc.TOUCH_BEGAN) + listener:registerScriptHandler(onTouchBegan,cc.HANDLERTYPE_EVENTLISTENER_TOUCH_BEGAN) eventDispatcher:addEventListenerWithFixedPriority(listener, -1) eventDispatcher:removeAllEventListeners()