From 6f196e1470b24704a4b3a3db5e7fbf10f8ff4d51 Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 18 Sep 2013 14:24:21 +0800 Subject: [PATCH 01/20] upstream to js tests --- samples/Javascript/Shared | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/Javascript/Shared b/samples/Javascript/Shared index 022292761c..6d8e58221b 160000 --- a/samples/Javascript/Shared +++ b/samples/Javascript/Shared @@ -1 +1 @@ -Subproject commit 022292761c6a9d056e25cc6e844430650208513f +Subproject commit 6d8e58221b9753981e2ee65738d8b93671835b28 From 64c5e183cedcedbbcb7dab79b02be535075f7bcb Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Wed, 18 Sep 2013 14:49:19 +0800 Subject: [PATCH 02/20] Add setKeypadEnabled and isKeypadEnabled deprecated functions --- scripting/lua/script/Deprecated.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/scripting/lua/script/Deprecated.lua b/scripting/lua/script/Deprecated.lua index 82be56c5c6..0b2bc4662f 100644 --- a/scripting/lua/script/Deprecated.lua +++ b/scripting/lua/script/Deprecated.lua @@ -117,6 +117,11 @@ function CCMenuDeprecated.createWithItem(self,...) return self:create(...) end rawset(CCMenu,"createWithItem",CCMenuDeprecated.createWithItem) + +function CCMenuDeprecated.setHandlerPriority(self) + print("\n********** \n".."setHandlerPriority was deprecated in 3.0. \n**********") +end +rawset(CCMenu,"setHandlerPriority",CCMenuDeprecated.setHandlerPriority) --functions of CCMenu will be deprecated end --functions of CCNode will be deprecated begin @@ -1015,3 +1020,17 @@ function CCSpriteDeprecated.setFlipY(self,flag) end rawset(cc.Sprite, "setFlipY", CCSpriteDeprecated.setFlipY) --functions of Sprite will be deprecated end + + +--functions of Layer will be deprecated begin +local CCLayerDeprecated = {} +function CCLayerDeprecated.setKeypadEnabled( self, enabled) + return self:setKeyboardEnabled(enabled) +end +rawset(cc.Layer, "setKeypadEnabled", CCLayerDeprecated.setKeypadEnabled ) + +function CCLayerDeprecated.isKeypadEnabled(self) + return self:isKeyboardEnabled() +end +rawset(cc.Layer, "isKeypadEnabled", CCLayerDeprecated.isKeypadEnabled ) +--functions of Layer will be deprecated end From cfb5de2900997b10b7f10c7cac85a84beafa2f6a Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 16:06:39 +0800 Subject: [PATCH 03/20] Fixing crash after changing Dictionary::createWithContentOfFile() to return autorelease object. [ci skip]. --- cocos2dx/platform/apple/CCFileUtilsApple.mm | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/cocos2dx/platform/apple/CCFileUtilsApple.mm b/cocos2dx/platform/apple/CCFileUtilsApple.mm index 5aae811b52..34de65fee6 100644 --- a/cocos2dx/platform/apple/CCFileUtilsApple.mm +++ b/cocos2dx/platform/apple/CCFileUtilsApple.mm @@ -307,18 +307,20 @@ std::string FileUtilsApple::getFullPathForDirectoryAndFilename(const std::string Dictionary* FileUtilsApple::createDictionaryWithContentsOfFile(const std::string& filename) { std::string fullPath = fullPathForFilename(filename); - NSString* pPath = [NSString stringWithUTF8String:fullPath.c_str()]; - NSDictionary* pDict = [NSDictionary dictionaryWithContentsOfFile:pPath]; + NSString* path = [NSString stringWithUTF8String:fullPath.c_str()]; + NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:path]; - if (pDict != nil) + if (dict != nil) { - Dictionary* pRet = Dictionary::create(); - for (id key in [pDict allKeys]) { - id value = [pDict objectForKey:key]; - addValueToDict(key, value, pRet); + auto ret = new Dictionary(); + ret->init(); + + for (id key in [dict allKeys]) { + id value = [dict objectForKey:key]; + addValueToDict(key, value, ret); } - return pRet; + return ret; } else { From 3515a4f286e9782a0d96d13fa2f9ea9fda44eb1d Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 18 Sep 2013 16:36:16 +0800 Subject: [PATCH 04/20] limited action frame index --- extensions/CocoStudio/Action/CCActionNode.cpp | 15 ++++++++++++--- extensions/CocoStudio/Action/CCActionObject.cpp | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/extensions/CocoStudio/Action/CCActionNode.cpp b/extensions/CocoStudio/Action/CCActionNode.cpp index 9b92ccf016..6cadffdbcf 100644 --- a/extensions/CocoStudio/Action/CCActionNode.cpp +++ b/extensions/CocoStudio/Action/CCActionNode.cpp @@ -362,6 +362,7 @@ void ActionNode::stopAction() int ActionNode::getFirstFrameIndex() { int frameindex = 99999; + bool bFindFrame = false; for (int n = 0; n < _frameArrayNum; n++) { Array* cArray = (Array*)(_frameArray->getObjectAtIndex(n)); @@ -369,7 +370,7 @@ int ActionNode::getFirstFrameIndex() { continue; } - + bFindFrame = true; ActionFrame* frame = (ActionFrame*)(cArray->getObjectAtIndex(0)); int iFrameIndex = frame->getFrameIndex(); @@ -378,13 +379,17 @@ int ActionNode::getFirstFrameIndex() frameindex = iFrameIndex; } } - + if (!bFindFrame) + { + frameindex = 0; + } return frameindex; } int ActionNode::getLastFrameIndex() { int frameindex = -1; + bool bFindFrame = false; for (int n = 0; n < _frameArrayNum; n++) { Array* cArray = (Array*)(_frameArray->getObjectAtIndex(n)); @@ -392,6 +397,7 @@ int ActionNode::getLastFrameIndex() { continue; } + bFindFrame = true; int lastInex = cArray->count() - 1; ActionFrame* frame = (ActionFrame*)(cArray->getObjectAtIndex(lastInex)); int iFrameIndex = frame->getFrameIndex(); @@ -401,7 +407,10 @@ int ActionNode::getLastFrameIndex() frameindex = iFrameIndex; } } - + if (!bFindFrame) + { + frameindex = 0; + } return frameindex; } bool ActionNode::updateActionToTimeLine(float fTime) diff --git a/extensions/CocoStudio/Action/CCActionObject.cpp b/extensions/CocoStudio/Action/CCActionObject.cpp index f3ea0dc7e6..4456d805d0 100644 --- a/extensions/CocoStudio/Action/CCActionObject.cpp +++ b/extensions/CocoStudio/Action/CCActionObject.cpp @@ -118,6 +118,7 @@ void ActionObject::addActionNode(ActionNode* node) return; } _actionNodeList->addObject(node); + node->setUnitTime(_fUnitTime); } void ActionObject::removeActionNode(ActionNode* node) { From 567dd007103a18c40eea552df33b3adca9dceace Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Wed, 18 Sep 2013 16:44:05 +0800 Subject: [PATCH 05/20] Modify Accelerometer lua test sample --- .../AccelerometerTest/AccelerometerTest.lua | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua b/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua index 88c7b0eee6..709e1fbc6c 100644 --- a/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua +++ b/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua @@ -27,29 +27,27 @@ local function AccelerometerMainLayer() local szBall = pBall:getContentSize() local ptNowX,ptNowY = pBall:getPosition() - local ptTmp = pDir:convertToUI(cc.p(ptNowX,ptNowY)) - ptTmp.x = ptTmp.x + x * 9.81 - ptTmp.y = ptTmp.y - y * 9.81 + ptNowX = ptNowX - x + ptNowY = ptNowY - y - local ptNext = pDir:convertToGL(cc.p(ptTmp.x,ptTmp.y)) - local nMinX = math.floor(VisibleRect:left().x + szBall.width / 2.0) - local nMaxX = math.floor(VisibleRect:right().x - szBall.width / 2.0) - if ptNext.x < nMinX then - ptNext.x = nMinX - elseif ptNext.x > nMaxX then - ptNext.x = nMaxX + local minX = math.floor(VisibleRect:left().x + szBall.width / 2.0) + local maxX = math.floor(VisibleRect:right().x - szBall.width / 2.0) + if ptNowX < minX then + ptNowX = minX + elseif ptNowX > maxX then + ptNowX = maxX end - local nMinY = math.floor(VisibleRect:bottom().y + szBall.height / 2.0) - local nMaxY = math.floor(VisibleRect:top().y - szBall.height / 2.0) - if ptNext.y < nMinY then - ptNext.y = nMinY - elseif ptNext.y > nMaxY then - ptNext.y = nMaxY + local minY = math.floor(VisibleRect:bottom().y + szBall.height / 2.0) + local maxY = math.floor(VisibleRect:top().y - szBall.height / 2.0) + if ptNowY < minY then + ptNowY = minY + elseif ptNowY > maxY then + ptNowY = maxY end - pBall:setPosition(cc.p(ptNext.x,ptNext.y)) + pBall:setPosition(cc.p(ptNowX, ptNowY )) end From a2dee37b0e488846ad0f157db3d019df97fac731 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Wed, 18 Sep 2013 17:00:21 +0800 Subject: [PATCH 06/20] glfw EGLView set alloc on the stack to prevent memory leak --- samples/Cpp/AssetsManagerTest/proj.win32/main.cpp | 4 ++-- samples/Cpp/HelloCpp/proj.linux/main.cpp | 4 ++-- samples/Cpp/HelloCpp/proj.mac/main.cpp | 4 ++-- samples/Cpp/HelloCpp/proj.win32/main.cpp | 4 ++-- samples/Cpp/SimpleGame/proj.linux/main.cpp | 4 ++-- samples/Cpp/SimpleGame/proj.mac/main.cpp | 4 ++-- samples/Cpp/TestCpp/proj.linux/main.cpp | 4 ++-- samples/Cpp/TestCpp/proj.mac/main.cpp | 4 ++-- samples/Cpp/TestCpp/proj.win32/main.cpp | 4 ++-- samples/Javascript/CocosDragonJS/proj.mac/main.cpp | 4 ++-- samples/Javascript/CocosDragonJS/proj.win32/main.cpp | 4 ++-- samples/Javascript/CrystalCraze/proj.mac/main.cpp | 4 ++-- samples/Javascript/CrystalCraze/proj.win32/main.cpp | 4 ++-- samples/Javascript/MoonWarriors/proj.mac/main.cpp | 4 ++-- samples/Javascript/MoonWarriors/proj.win32/main.cpp | 4 ++-- samples/Javascript/TestJavascript/proj.mac/main.cpp | 4 ++-- samples/Javascript/TestJavascript/proj.win32/main.cpp | 4 ++-- samples/Javascript/WatermelonWithMe/proj.mac/main.cpp | 4 ++-- samples/Javascript/WatermelonWithMe/proj.win32/main.cpp | 4 ++-- samples/Lua/HelloLua/proj.linux/main.cpp | 4 ++-- samples/Lua/HelloLua/proj.mac/main.cpp | 4 ++-- samples/Lua/HelloLua/proj.win32/main.cpp | 4 ++-- samples/Lua/TestLua/proj.linux/main.cpp | 4 ++-- samples/Lua/TestLua/proj.mac/main.cpp | 4 ++-- samples/Lua/TestLua/proj.win32/main.cpp | 4 ++-- template/multi-platform-cpp/proj.linux/main.cpp | 4 ++-- template/multi-platform-cpp/proj.mac/main.cpp | 4 ++-- template/multi-platform-cpp/proj.win32/main.cpp | 4 ++-- template/multi-platform-js/proj.win32/main.cpp | 4 ++-- template/multi-platform-lua/proj.linux/main.cpp | 4 ++-- template/multi-platform-lua/proj.mac/main.cpp | 4 ++-- template/multi-platform-lua/proj.win32/main.cpp | 4 ++-- 32 files changed, 64 insertions(+), 64 deletions(-) diff --git a/samples/Cpp/AssetsManagerTest/proj.win32/main.cpp b/samples/Cpp/AssetsManagerTest/proj.win32/main.cpp index f15aa09b20..e9fef1d86d 100644 --- a/samples/Cpp/AssetsManagerTest/proj.win32/main.cpp +++ b/samples/Cpp/AssetsManagerTest/proj.win32/main.cpp @@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Cpp/HelloCpp/proj.linux/main.cpp b/samples/Cpp/HelloCpp/proj.linux/main.cpp index 2c1830bfaf..135ddbb2f1 100644 --- a/samples/Cpp/HelloCpp/proj.linux/main.cpp +++ b/samples/Cpp/HelloCpp/proj.linux/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char **argv) { // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/HelloCpp/proj.mac/main.cpp b/samples/Cpp/HelloCpp/proj.mac/main.cpp index a8560f50cd..976460501e 100644 --- a/samples/Cpp/HelloCpp/proj.mac/main.cpp +++ b/samples/Cpp/HelloCpp/proj.mac/main.cpp @@ -30,8 +30,8 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/HelloCpp/proj.win32/main.cpp b/samples/Cpp/HelloCpp/proj.win32/main.cpp index 0bf8bf10ee..ec96f44bca 100644 --- a/samples/Cpp/HelloCpp/proj.win32/main.cpp +++ b/samples/Cpp/HelloCpp/proj.win32/main.cpp @@ -14,7 +14,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/SimpleGame/proj.linux/main.cpp b/samples/Cpp/SimpleGame/proj.linux/main.cpp index 651d619dc1..4e9455bc04 100644 --- a/samples/Cpp/SimpleGame/proj.linux/main.cpp +++ b/samples/Cpp/SimpleGame/proj.linux/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char **argv) { // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/SimpleGame/proj.mac/main.cpp b/samples/Cpp/SimpleGame/proj.mac/main.cpp index a8560f50cd..976460501e 100644 --- a/samples/Cpp/SimpleGame/proj.mac/main.cpp +++ b/samples/Cpp/SimpleGame/proj.mac/main.cpp @@ -30,8 +30,8 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/TestCpp/proj.linux/main.cpp b/samples/Cpp/TestCpp/proj.linux/main.cpp index a2e9b8c38d..fe28f03278 100644 --- a/samples/Cpp/TestCpp/proj.linux/main.cpp +++ b/samples/Cpp/TestCpp/proj.linux/main.cpp @@ -13,7 +13,7 @@ int main(int argc, char **argv) { // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/TestCpp/proj.mac/main.cpp b/samples/Cpp/TestCpp/proj.mac/main.cpp index d8074883bc..061f7b6874 100644 --- a/samples/Cpp/TestCpp/proj.mac/main.cpp +++ b/samples/Cpp/TestCpp/proj.mac/main.cpp @@ -30,7 +30,7 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/TestCpp/proj.win32/main.cpp b/samples/Cpp/TestCpp/proj.win32/main.cpp index 26ba4e9292..8411246122 100644 --- a/samples/Cpp/TestCpp/proj.win32/main.cpp +++ b/samples/Cpp/TestCpp/proj.win32/main.cpp @@ -14,7 +14,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",960,640); + EGLView eglView; + eglView.init("TestCPP",960,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/CocosDragonJS/proj.mac/main.cpp b/samples/Javascript/CocosDragonJS/proj.mac/main.cpp index d8074883bc..061f7b6874 100644 --- a/samples/Javascript/CocosDragonJS/proj.mac/main.cpp +++ b/samples/Javascript/CocosDragonJS/proj.mac/main.cpp @@ -30,7 +30,7 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/CocosDragonJS/proj.win32/main.cpp b/samples/Javascript/CocosDragonJS/proj.win32/main.cpp index f15aa09b20..e9fef1d86d 100644 --- a/samples/Javascript/CocosDragonJS/proj.win32/main.cpp +++ b/samples/Javascript/CocosDragonJS/proj.win32/main.cpp @@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Javascript/CrystalCraze/proj.mac/main.cpp b/samples/Javascript/CrystalCraze/proj.mac/main.cpp index d8074883bc..061f7b6874 100644 --- a/samples/Javascript/CrystalCraze/proj.mac/main.cpp +++ b/samples/Javascript/CrystalCraze/proj.mac/main.cpp @@ -30,7 +30,7 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/CrystalCraze/proj.win32/main.cpp b/samples/Javascript/CrystalCraze/proj.win32/main.cpp index f15aa09b20..e9fef1d86d 100644 --- a/samples/Javascript/CrystalCraze/proj.win32/main.cpp +++ b/samples/Javascript/CrystalCraze/proj.win32/main.cpp @@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Javascript/MoonWarriors/proj.mac/main.cpp b/samples/Javascript/MoonWarriors/proj.mac/main.cpp index a8560f50cd..976460501e 100644 --- a/samples/Javascript/MoonWarriors/proj.mac/main.cpp +++ b/samples/Javascript/MoonWarriors/proj.mac/main.cpp @@ -30,8 +30,8 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/MoonWarriors/proj.win32/main.cpp b/samples/Javascript/MoonWarriors/proj.win32/main.cpp index f15aa09b20..e9fef1d86d 100644 --- a/samples/Javascript/MoonWarriors/proj.win32/main.cpp +++ b/samples/Javascript/MoonWarriors/proj.win32/main.cpp @@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Javascript/TestJavascript/proj.mac/main.cpp b/samples/Javascript/TestJavascript/proj.mac/main.cpp index a8560f50cd..976460501e 100644 --- a/samples/Javascript/TestJavascript/proj.mac/main.cpp +++ b/samples/Javascript/TestJavascript/proj.mac/main.cpp @@ -30,8 +30,8 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/TestJavascript/proj.win32/main.cpp b/samples/Javascript/TestJavascript/proj.win32/main.cpp index f15aa09b20..e9fef1d86d 100644 --- a/samples/Javascript/TestJavascript/proj.win32/main.cpp +++ b/samples/Javascript/TestJavascript/proj.win32/main.cpp @@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Javascript/WatermelonWithMe/proj.mac/main.cpp b/samples/Javascript/WatermelonWithMe/proj.mac/main.cpp index a8560f50cd..976460501e 100644 --- a/samples/Javascript/WatermelonWithMe/proj.mac/main.cpp +++ b/samples/Javascript/WatermelonWithMe/proj.mac/main.cpp @@ -30,8 +30,8 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/WatermelonWithMe/proj.win32/main.cpp b/samples/Javascript/WatermelonWithMe/proj.win32/main.cpp index f15aa09b20..e9fef1d86d 100644 --- a/samples/Javascript/WatermelonWithMe/proj.win32/main.cpp +++ b/samples/Javascript/WatermelonWithMe/proj.win32/main.cpp @@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Lua/HelloLua/proj.linux/main.cpp b/samples/Lua/HelloLua/proj.linux/main.cpp index 2c1830bfaf..135ddbb2f1 100644 --- a/samples/Lua/HelloLua/proj.linux/main.cpp +++ b/samples/Lua/HelloLua/proj.linux/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char **argv) { // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Lua/HelloLua/proj.mac/main.cpp b/samples/Lua/HelloLua/proj.mac/main.cpp index d8074883bc..061f7b6874 100644 --- a/samples/Lua/HelloLua/proj.mac/main.cpp +++ b/samples/Lua/HelloLua/proj.mac/main.cpp @@ -30,7 +30,7 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Lua/HelloLua/proj.win32/main.cpp b/samples/Lua/HelloLua/proj.win32/main.cpp index 46349a8c5a..3706b8cfdb 100644 --- a/samples/Lua/HelloLua/proj.win32/main.cpp +++ b/samples/Lua/HelloLua/proj.win32/main.cpp @@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Lua/TestLua/proj.linux/main.cpp b/samples/Lua/TestLua/proj.linux/main.cpp index a2e9b8c38d..fe28f03278 100644 --- a/samples/Lua/TestLua/proj.linux/main.cpp +++ b/samples/Lua/TestLua/proj.linux/main.cpp @@ -13,7 +13,7 @@ int main(int argc, char **argv) { // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Lua/TestLua/proj.mac/main.cpp b/samples/Lua/TestLua/proj.mac/main.cpp index d8074883bc..061f7b6874 100644 --- a/samples/Lua/TestLua/proj.mac/main.cpp +++ b/samples/Lua/TestLua/proj.mac/main.cpp @@ -30,7 +30,7 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/samples/Lua/TestLua/proj.win32/main.cpp b/samples/Lua/TestLua/proj.win32/main.cpp index 8f302d1f1a..4dfce357f9 100644 --- a/samples/Lua/TestLua/proj.win32/main.cpp +++ b/samples/Lua/TestLua/proj.win32/main.cpp @@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); int ret = Application::getInstance()->run(); diff --git a/template/multi-platform-cpp/proj.linux/main.cpp b/template/multi-platform-cpp/proj.linux/main.cpp index a2e9b8c38d..fe28f03278 100644 --- a/template/multi-platform-cpp/proj.linux/main.cpp +++ b/template/multi-platform-cpp/proj.linux/main.cpp @@ -13,7 +13,7 @@ int main(int argc, char **argv) { // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/template/multi-platform-cpp/proj.mac/main.cpp b/template/multi-platform-cpp/proj.mac/main.cpp index d8074883bc..061f7b6874 100644 --- a/template/multi-platform-cpp/proj.mac/main.cpp +++ b/template/multi-platform-cpp/proj.mac/main.cpp @@ -30,7 +30,7 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/template/multi-platform-cpp/proj.win32/main.cpp b/template/multi-platform-cpp/proj.win32/main.cpp index c7a804c96a..3e192c981c 100644 --- a/template/multi-platform-cpp/proj.win32/main.cpp +++ b/template/multi-platform-cpp/proj.win32/main.cpp @@ -14,7 +14,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/template/multi-platform-js/proj.win32/main.cpp b/template/multi-platform-js/proj.win32/main.cpp index f15aa09b20..e9fef1d86d 100644 --- a/template/multi-platform-js/proj.win32/main.cpp +++ b/template/multi-platform-js/proj.win32/main.cpp @@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); int ret = Application::getInstance()->run(); diff --git a/template/multi-platform-lua/proj.linux/main.cpp b/template/multi-platform-lua/proj.linux/main.cpp index a2e9b8c38d..fe28f03278 100644 --- a/template/multi-platform-lua/proj.linux/main.cpp +++ b/template/multi-platform-lua/proj.linux/main.cpp @@ -13,7 +13,7 @@ int main(int argc, char **argv) { // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } diff --git a/template/multi-platform-lua/proj.mac/main.cpp b/template/multi-platform-lua/proj.mac/main.cpp index 2d3a9b1eb1..ab23e39856 100644 --- a/template/multi-platform-lua/proj.mac/main.cpp +++ b/template/multi-platform-lua/proj.mac/main.cpp @@ -30,7 +30,7 @@ USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); return Application::getInstance()->run(); } \ No newline at end of file diff --git a/template/multi-platform-lua/proj.win32/main.cpp b/template/multi-platform-lua/proj.win32/main.cpp index 8f302d1f1a..4dfce357f9 100644 --- a/template/multi-platform-lua/proj.win32/main.cpp +++ b/template/multi-platform-lua/proj.win32/main.cpp @@ -24,8 +24,8 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; - EGLView* eglView = new EGLView(); - eglView->init("TestCPP",900,640); + EGLView eglView; + eglView.init("TestCPP",900,640); int ret = Application::getInstance()->run(); From 1ee7f722765cfc8c2c50e941f35cbc07ddf1b4e1 Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Wed, 18 Sep 2013 17:02:32 +0800 Subject: [PATCH 07/20] Modify handle_key_input function for responding the onKeyReleased --- cocos2dx/platform/android/nativeactivity.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2dx/platform/android/nativeactivity.cpp b/cocos2dx/platform/android/nativeactivity.cpp index 323cb7811f..231fcf0ad4 100644 --- a/cocos2dx/platform/android/nativeactivity.cpp +++ b/cocos2dx/platform/android/nativeactivity.cpp @@ -402,16 +402,16 @@ static int32_t handle_key_input(AInputEvent *event) { cocos2d::KeyboardEvent event; event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_BACKSPACE; + event._isPressed = false; cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); - LOGI("AKEYCODE_BACK"); } return 1; case AKEYCODE_MENU: { cocos2d::KeyboardEvent event; event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_MENU; + event._isPressed = false; cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); - LOGI("AKEYCODE_MENU"); } return 1; default: From 71b03ecdb9fdd86fe7bbc80d084a8794c34640ba Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Wed, 18 Sep 2013 17:09:08 +0800 Subject: [PATCH 08/20] replace tabs with spaces --- cocos2dx/platform/android/nativeactivity.cpp | 46 ++++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/cocos2dx/platform/android/nativeactivity.cpp b/cocos2dx/platform/android/nativeactivity.cpp index 231fcf0ad4..e2e22cbb30 100644 --- a/cocos2dx/platform/android/nativeactivity.cpp +++ b/cocos2dx/platform/android/nativeactivity.cpp @@ -394,31 +394,31 @@ static int32_t handle_touch_input(AInputEvent *event) { */ static int32_t handle_key_input(AInputEvent *event) { - if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_UP) - { - switch (AKeyEvent_getKeyCode(event)) - { - case AKEYCODE_BACK: - { - cocos2d::KeyboardEvent event; - event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_BACKSPACE; + if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_UP) + { + switch (AKeyEvent_getKeyCode(event)) + { + case AKEYCODE_BACK: + { + cocos2d::KeyboardEvent event; + event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_BACKSPACE; event._isPressed = false; - cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); - } - return 1; - case AKEYCODE_MENU: - { - cocos2d::KeyboardEvent event; - event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_MENU; + cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); + } + return 1; + case AKEYCODE_MENU: + { + cocos2d::KeyboardEvent event; + event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_MENU; event._isPressed = false; - cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); - } - return 1; - default: - break; - } - } - return 0; + cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); + } + return 1; + default: + break; + } + } + return 0; } /** From 768828ec295e16e6ebd1c3ad4d8a132b0f0006e8 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Wed, 18 Sep 2013 17:16:31 +0800 Subject: [PATCH 09/20] add term logic of application to prevent memory leak (for desktop such as linux, mac, windows) --- cocos2dx/platform/linux/CCApplication.cpp | 8 +++++++- cocos2dx/platform/mac/CCApplication.mm | 8 ++++++++ cocos2dx/platform/win32/CCApplication.cpp | 9 ++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cocos2dx/platform/linux/CCApplication.cpp b/cocos2dx/platform/linux/CCApplication.cpp index a9cfc080cc..0534b83208 100644 --- a/cocos2dx/platform/linux/CCApplication.cpp +++ b/cocos2dx/platform/linux/CCApplication.cpp @@ -60,7 +60,13 @@ int Application::run() usleep((_animationInterval - iCurTime+iLastTime)*1000); } } - + /* Only work on Desktop + * Director::mainLoop is really one frame logic + * when we want to close the window, we should call Director::end(); + * then call Director::mainLoop to do release of internal resources + */ + Director::getInstance()->end(); + Director::getInstance()->mainLoop(); return -1; } diff --git a/cocos2dx/platform/mac/CCApplication.mm b/cocos2dx/platform/mac/CCApplication.mm index 460574169b..5959a2774e 100644 --- a/cocos2dx/platform/mac/CCApplication.mm +++ b/cocos2dx/platform/mac/CCApplication.mm @@ -60,6 +60,14 @@ int Application::run() Director::getInstance()->mainLoop(); pMainWnd->pollEvents(); } + + /* Only work on Desktop + * Director::mainLoop is really one frame logic + * when we want to close the window, we should call Director::end(); + * then call Director::mainLoop to do release of internal resources + */ + Director::getInstance()->end(); + Director::getInstance()->mainLoop(); return true; } diff --git a/cocos2dx/platform/win32/CCApplication.cpp b/cocos2dx/platform/win32/CCApplication.cpp index 5f52412406..a29e842143 100644 --- a/cocos2dx/platform/win32/CCApplication.cpp +++ b/cocos2dx/platform/win32/CCApplication.cpp @@ -64,7 +64,14 @@ int Application::run() Sleep(0); } } - + + /* Only work on Desktop + * Director::mainLoop is really one frame logic + * when we want to close the window, we should call Director::end(); + * then call Director::mainLoop to do release of internal resources + */ + Director::getInstance()->end(); + Director::getInstance()->mainLoop(); return true; } From d4669f3ad44ffce8a52c5a478d16489dedd10a53 Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Wed, 18 Sep 2013 17:19:34 +0800 Subject: [PATCH 10/20] replace tabs with spaces --- .../AccelerometerTest/AccelerometerTest.lua | 85 +++++++++---------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua b/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua index 709e1fbc6c..89739511f4 100644 --- a/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua +++ b/samples/Lua/TestLua/Resources/luaScript/AccelerometerTest/AccelerometerTest.lua @@ -1,60 +1,57 @@ local function AccelerometerMainLayer() - local function title() - return "AccelerometerTest" - end - local pLayer = cc.Layer:create() - - pLayer:setAccelerometerEnabled(true) - - local pLabel = cc.LabelTTF:create(title(), "Arial", 32) - pLayer:addChild(pLabel, 1) - pLabel:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 50) ) + local function title() + return "AccelerometerTest" + end + local layer = cc.Layer:create() - local pBall = cc.Sprite:create("Images/ball.png") - pBall:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y)) - pLayer:addChild(pBall) + layer:setAccelerometerEnabled(true) - pBall:retain() + local label = cc.LabelTTF:create(title(), "Arial", 32) + layer:addChild(label, 1) + label:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 50) ) + + local ball = cc.Sprite:create("Images/ball.png") + ball:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y)) + layer:addChild(ball) + + ball:retain() local function didAccelerate(x,y,z,timestamp) - local pDir = cc.Director:getInstance() - if nil == pBall then - return - end + if nil == ball then + return + end - local szBall = pBall:getContentSize() - local ptNowX,ptNowY = pBall:getPosition() - - ptNowX = ptNowX - x - ptNowY = ptNowY - y + local szBall = ball:getContentSize() + local ptNowX,ptNowY = ball:getPosition() + + ptNowX = ptNowX - x + ptNowY = ptNowY - y - local minX = math.floor(VisibleRect:left().x + szBall.width / 2.0) - local maxX = math.floor(VisibleRect:right().x - szBall.width / 2.0) - if ptNowX < minX then - ptNowX = minX - elseif ptNowX > maxX then - ptNowX = maxX - end - - local minY = math.floor(VisibleRect:bottom().y + szBall.height / 2.0) - local maxY = math.floor(VisibleRect:top().y - szBall.height / 2.0) - if ptNowY < minY then - ptNowY = minY - elseif ptNowY > maxY then - ptNowY = maxY - end - - pBall:setPosition(cc.p(ptNowX, ptNowY )) - - + local minX = math.floor(VisibleRect:left().x + szBall.width / 2.0) + local maxX = math.floor(VisibleRect:right().x - szBall.width / 2.0) + if ptNowX < minX then + ptNowX = minX + elseif ptNowX > maxX then + ptNowX = maxX + end + + local minY = math.floor(VisibleRect:bottom().y + szBall.height / 2.0) + local maxY = math.floor(VisibleRect:top().y - szBall.height / 2.0) + if ptNowY < minY then + ptNowY = minY + elseif ptNowY > maxY then + ptNowY = maxY + end + + ball:setPosition(cc.p(ptNowX, ptNowY )) end - pLayer:registerScriptAccelerateHandler(didAccelerate) + layer:registerScriptAccelerateHandler(didAccelerate) - return pLayer + return layer end From f7fa92aea972c1dc32112265e2cb95dbaca5e0c9 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 17:45:25 +0800 Subject: [PATCH 11/20] issue #2087: Sorts listeners only when flag is dirty. setPriority is only valid in fixed priority listener. --- cocos2dx/base_nodes/CCNode.cpp | 14 +- cocos2dx/base_nodes/CCNode.h | 12 +- .../event_dispatcher/CCEventDispatcher.cpp | 186 +++++++++--------- cocos2dx/event_dispatcher/CCEventDispatcher.h | 17 +- cocos2dx/event_dispatcher/CCEventListener.h | 1 + .../NewEventDispatcherTest.cpp | 6 +- .../PerformanceTouchesTest.cpp | 2 +- 7 files changed, 133 insertions(+), 105 deletions(-) diff --git a/cocos2dx/base_nodes/CCNode.cpp b/cocos2dx/base_nodes/CCNode.cpp index 220bbe564e..5310d0312a 100644 --- a/cocos2dx/base_nodes/CCNode.cpp +++ b/cocos2dx/base_nodes/CCNode.cpp @@ -825,7 +825,7 @@ void Node::visit() } // self draw this->draw(); - _eventPriority = ++_globalEventPriorityIndex; + updateEventPriorityIndex(); for( ; i < _children->count(); i++ ) { @@ -837,7 +837,7 @@ void Node::visit() else { this->draw(); - _eventPriority = ++_globalEventPriorityIndex; + updateEventPriorityIndex(); } // reset for next frame @@ -1317,6 +1317,16 @@ void Node::removeAllEventListeners() } } +void Node::setDirtyForAllEventListeners() +{ + auto dispatcher = EventDispatcher::getInstance(); + + for (auto& listener : _eventlisteners) + { + dispatcher->setDirtyForEventType(listener->type, true); + } +} + // NodeRGBA NodeRGBA::NodeRGBA() : _displayedOpacity(255) diff --git a/cocos2dx/base_nodes/CCNode.h b/cocos2dx/base_nodes/CCNode.h index 8c7032675d..5801f35cbe 100644 --- a/cocos2dx/base_nodes/CCNode.h +++ b/cocos2dx/base_nodes/CCNode.h @@ -1382,11 +1382,21 @@ private: protected: /// Upates event priority for this node. - inline void updateEventPriorityIndex() { _eventPriority = ++_globalEventPriorityIndex; }; + inline void updateEventPriorityIndex() { + _oldEventPriority = _eventPriority; + _eventPriority = ++_globalEventPriorityIndex; + if (_oldEventPriority != _eventPriority) + { + setDirtyForAllEventListeners(); + } + }; /// Removes all event listeners that associated with this node. void removeAllEventListeners(); + /// Sets dirty for event listener. + void setDirtyForAllEventListeners(); + /// lazy allocs void childrenAlloc(void); diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp index 7aa26aede9..4be2cc89c0 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp @@ -67,7 +67,6 @@ EventDispatcher::EventListenerItem::~EventListenerItem() EventDispatcher::EventDispatcher() : _inDispatch(0) -, _listeners(nullptr) , _isEnabled(true) { _toAddedListeners.reserve(50); @@ -95,28 +94,25 @@ void EventDispatcher::destroyInstance() void EventDispatcher::addEventListenerWithItem(EventListenerItem* item) { - if (!_listeners) - { - _listeners = new std::map*>(); - } - if (_inDispatch == 0) { std::vector* listenerList = nullptr; - auto itr = _listeners->find(item->listener->type); - if (itr == _listeners->end()) + auto iter = _listeners.find(item->listener->type); + if (iter == _listeners.end()) { listenerList = new std::vector(); listenerList->reserve(100); - _listeners->insert(std::make_pair(item->listener->type, listenerList)); + _listeners.insert(std::make_pair(item->listener->type, listenerList)); } else { - listenerList = itr->second; + listenerList = iter->second; } listenerList->insert(listenerList->begin(), item); + + setDirtyForEventType(item->listener->type, true); } else { @@ -166,17 +162,19 @@ void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, void EventDispatcher::removeEventListener(EventListener* listener) { - if (_listeners == nullptr || listener == nullptr) + if (listener == nullptr) return; bool isFound = false; - for (auto iter = _listeners->begin(); iter != _listeners->end();) + for (auto iter = _listeners.begin(); iter != _listeners.end();) { for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter) { if ((*itemIter)->listener == listener) { + listener->retain(); + if (_inDispatch == 0) { (*itemIter)->listener->release(); @@ -196,8 +194,10 @@ void EventDispatcher::removeEventListener(EventListener* listener) if (iter->second->empty()) { auto list = iter->second; - iter = _listeners->erase(iter); + iter = _listeners.erase(iter); CC_SAFE_DELETE(list); + + _priorityDirtyFlagMap.erase(listener->type); } else { @@ -208,52 +208,30 @@ void EventDispatcher::removeEventListener(EventListener* listener) break; } - if (_listeners->empty()) + if (isFound) { - CC_SAFE_DELETE(_listeners); + listener->release(); } } -void EventDispatcher::setPriorityWithSceneGraph(EventListener* listener, Node* node) +void EventDispatcher::setPriority(EventListener* listener, int fixedPriority) { - if (_listeners == nullptr || listener == nullptr || node == nullptr) + if (listener == nullptr) return; - - for (auto iter = _listeners->begin(); iter != _listeners->end(); ++iter) + + for (auto iter = _listeners.begin(); iter != _listeners.end(); ++iter) { for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter) { auto item = *itemIter; if (item->listener == listener) { - item->fixedPriority = 0; + CCASSERT(item->node, "Can't set fixed priority with scene graph based listener."); - CC_SAFE_RETAIN(node); - CC_SAFE_RELEASE(item->node); - item->node = node; - return; - } - } - } -} - -void EventDispatcher::setPriorityWithFixedValue(EventListener* listener, int fixedPriority) -{ - if (_listeners == nullptr || listener == nullptr) - return; - - for (auto iter = _listeners->begin(); iter != _listeners->end(); ++iter) - { - for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter) - { - auto item = *itemIter; - if (item->listener == listener) - { - item->fixedPriority = fixedPriority; - if (item->node != nullptr) + if (item->fixedPriority != fixedPriority) { - item->node->dissociateEventListener(listener); - CC_SAFE_RELEASE_NULL(item->node); + item->fixedPriority = fixedPriority; + setDirtyForEventType(listener->type, true); } return; } @@ -263,12 +241,24 @@ void EventDispatcher::setPriorityWithFixedValue(EventListener* listener, int fix void EventDispatcher::dispatchEvent(Event* event, bool forceSortListeners) { - if (_listeners == nullptr || !_isEnabled) + if (!_isEnabled) return; - - if (forceSortListeners) + + bool isDirty = false; + auto dirtyIter = _priorityDirtyFlagMap.find(event->_type); + if (dirtyIter != _priorityDirtyFlagMap.end()) + { + isDirty = dirtyIter->second; + } + + if (forceSortListeners || isDirty) { sortAllEventListenerItemsForType(event->_type); + // Sets the dirty flag to false + if (isDirty) + { + dirtyIter->second = false; + } } DispatchGuard guard(_inDispatch); @@ -279,22 +269,19 @@ void EventDispatcher::dispatchEvent(Event* event, bool forceSortListeners) return; } - if (_listeners) + auto iter = _listeners.find(event->getType()); + if (iter != _listeners.end()) { - auto iter = _listeners->find(event->getType()); - if (iter != _listeners->end()) + auto listenerList = iter->second; + for (auto& item : *listenerList) { - auto listenerList = iter->second; - for (auto& item : *listenerList) - { - CCASSERT(item, "listener item is invalid."); + CCASSERT(item, "listener item is invalid."); - event->setCurrentTarget(item->node); - item->listener->onEvent(event); + event->setCurrentTarget(item->node); + item->listener->onEvent(event); - if (event->isStopped()) - break; - } + if (event->isStopped()) + break; } } @@ -500,11 +487,8 @@ void EventDispatcher::dispatchTouchEvent(TouchEvent* event) void EventDispatcher::updateListenerItems() { - if (!_listeners) - return; - - auto listenerItemIter = _listeners->begin(); - while (listenerItemIter != _listeners->end()) + auto listenerItemIter = _listeners.begin(); + while (listenerItemIter != _listeners.end()) { for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end();) { @@ -521,8 +505,9 @@ void EventDispatcher::updateListenerItems() if (listenerItemIter->second->empty()) { + _priorityDirtyFlagMap.erase(listenerItemIter->first); delete listenerItemIter->second; - listenerItemIter = _listeners->erase(listenerItemIter); + listenerItemIter = _listeners.erase(listenerItemIter); } else { @@ -536,12 +521,12 @@ void EventDispatcher::updateListenerItems() for (auto& item : _toAddedListeners) { - auto itr = _listeners->find(item->listener->type); - if (itr == _listeners->end()) + auto itr = _listeners.find(item->listener->type); + if (itr == _listeners.end()) { listenerList = new std::vector(); listenerList->reserve(100); - _listeners->insert(std::make_pair(item->listener->type, listenerList)); + _listeners.insert(std::make_pair(item->listener->type, listenerList)); } else { @@ -549,20 +534,16 @@ void EventDispatcher::updateListenerItems() } listenerList->push_back(item); + + setDirtyForEventType(item->listener->type, true); } _toAddedListeners.clear(); } - - if (_listeners->empty()) - { - delete _listeners; - _listeners = nullptr; - } } void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventType) { - if (_listeners == nullptr) + if (eventType.empty()) return; auto listenerList = getListenerItemsForType(eventType); @@ -610,13 +591,10 @@ void EventDispatcher::sortAllEventListenerItemsForType(const std::string &eventT std::vector* EventDispatcher::getListenerItemsForType(const std::string &eventType) { - if (_listeners != nullptr) + auto iter = _listeners.find(eventType); + if (iter != _listeners.end()) { - auto iter = _listeners->find(eventType); - if (iter != _listeners->end()) - { - return iter->second; - } + return iter->second; } return nullptr; @@ -624,11 +602,11 @@ std::vector* EventDispatcher::getListenerIt void EventDispatcher::removeListenersForEventType(const std::string& eventType) { - if (_listeners == nullptr) + if (eventType.empty()) return; - auto listenerItemIter = _listeners->find(eventType); - if (listenerItemIter != _listeners->end()) + auto listenerItemIter = _listeners.find(eventType); + if (listenerItemIter != _listeners.end()) { for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter) { @@ -647,17 +625,15 @@ void EventDispatcher::removeListenersForEventType(const std::string& eventType) { listenerItemIter->second->clear(); delete listenerItemIter->second; - _listeners->erase(listenerItemIter); + _listeners.erase(listenerItemIter); + _priorityDirtyFlagMap.erase(eventType); } } } void EventDispatcher::removeAllListeners() { - if (_listeners == nullptr) - return; - - for (auto listenerItemIter = _listeners->begin(); listenerItemIter != _listeners->end(); ++listenerItemIter) + for (auto listenerItemIter = _listeners.begin(); listenerItemIter != _listeners.end(); ++listenerItemIter) { for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter) { @@ -676,13 +652,14 @@ void EventDispatcher::removeAllListeners() { listenerItemIter->second->clear(); delete listenerItemIter->second; + + _priorityDirtyFlagMap.clear(); } } if (!_inDispatch) { - delete _listeners; - _listeners = nullptr; + _listeners.clear(); } } @@ -697,4 +674,29 @@ bool EventDispatcher::isEnabled() const return _isEnabled; } +void EventDispatcher::setDirtyForEventType(const std::string& eventType, bool isDirty) +{ + auto iter = _priorityDirtyFlagMap.find(eventType); + if (iter == _priorityDirtyFlagMap.end()) + { + _priorityDirtyFlagMap.insert(std::make_pair(eventType, isDirty)); + } + else + { + iter->second = isDirty; + } +} + +bool EventDispatcher::isDirtyForEventType(const std::string& eventType) +{ + bool isDirty = false; + auto iter = _priorityDirtyFlagMap.find(eventType); + if (iter != _priorityDirtyFlagMap.end()) + { + isDirty = iter->second; + } + + return isDirty; +} + NS_CC_END diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.h b/cocos2dx/event_dispatcher/CCEventDispatcher.h index 8d11d4e620..b5648b0629 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.h +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.h @@ -84,12 +84,9 @@ public: /** Removes all listeners */ void removeAllListeners(); - - /** Sets listener's priority with node's draw order. */ - void setPriorityWithSceneGraph(EventListener* listener, Node* node); /** Sets listener's priority with fixed value. */ - void setPriorityWithFixedValue(EventListener* listener, int fixedPriority); + void setPriority(EventListener* listener, int fixedPriority); /** Whether to enable dispatching events */ void setEnabled(bool isEnabled); @@ -101,8 +98,12 @@ public: * Also removes all EventListeners marked for deletion from the * event dispatcher list. */ - void dispatchEvent(Event* event, bool forceSortListeners = true); + void dispatchEvent(Event* event, bool forceSortListeners = false); + void setDirtyForEventType(const std::string& eventType, bool isDirty); + + bool isDirtyForEventType(const std::string& eventType); + public: /** Destructor of EventDispatcher */ ~EventDispatcher(); @@ -141,7 +142,11 @@ private: /** * Listeners map. */ - std::map*>* _listeners; + std::map*> _listeners; + + /// Priority dirty flag + std::map _priorityDirtyFlagMap; + std::vector _toAddedListeners; int _inDispatch; ///< Whether it's in dispatching event diff --git a/cocos2dx/event_dispatcher/CCEventListener.h b/cocos2dx/event_dispatcher/CCEventListener.h index b8bca5e336..10e9210f4b 100644 --- a/cocos2dx/event_dispatcher/CCEventListener.h +++ b/cocos2dx/event_dispatcher/CCEventListener.h @@ -87,6 +87,7 @@ protected: bool _isRegistered; /// Whether the listener has been added to dispatcher. friend class EventDispatcher; + friend class Node; }; NS_CC_END diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index 0a1f8fccbc..093758227a 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -213,7 +213,7 @@ public: auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE); listener->setSwallowTouches(true); - listener->onTouchBegan = [&](Touch* touch, Event* event){ + listener->onTouchBegan = [=](Touch* touch, Event* event){ Point locationInNode = this->convertToNodeSpace(touch->getLocation()); Size s = this->getContentSize(); @@ -227,11 +227,11 @@ public: return false; }; - listener->onTouchMoved = [&](Touch* touch, Event* event){ + listener->onTouchMoved = [=](Touch* touch, Event* event){ //this->setPosition(this->getPosition() + touch->getDelta()); }; - listener->onTouchEnded = [&](Touch* touch, Event* event){ + listener->onTouchEnded = [=](Touch* touch, Event* event){ this->setColor(Color3B::WHITE); }; diff --git a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp index 17dd5720c1..4fe99f2323 100644 --- a/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp +++ b/samples/Cpp/TestCpp/Classes/PerformanceTest/PerformanceTouchesTest.cpp @@ -260,7 +260,7 @@ void TouchesPerformTest3::onEnter() { CC_PROFILER_START(TOUCH_PROFILER_NAME); - dispatcher->dispatchEvent(&event, true); + dispatcher->dispatchEvent(&event, false); CC_PROFILER_STOP(TOUCH_PROFILER_NAME); } From 6f53dcb49f2ac79d8f96d11cbd7cb3a695a3aba3 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 18:15:18 +0800 Subject: [PATCH 12/20] issue #2087: Adding Event::setUserData and Event::getUserData, making some methods inline. --- cocos2dx/event_dispatcher/CCEvent.cpp | 9 --------- cocos2dx/event_dispatcher/CCEvent.h | 23 +++++++++++++++-------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/cocos2dx/event_dispatcher/CCEvent.cpp b/cocos2dx/event_dispatcher/CCEvent.cpp index 2d9b89f471..1dc9294d83 100644 --- a/cocos2dx/event_dispatcher/CCEvent.cpp +++ b/cocos2dx/event_dispatcher/CCEvent.cpp @@ -37,14 +37,5 @@ Event::~Event() { } -Node* Event::getCurrentTarget() -{ - return _currentTarget; -} - -void Event::setCurrentTarget(Node* target) -{ - _currentTarget = target; -} NS_CC_END diff --git a/cocos2dx/event_dispatcher/CCEvent.h b/cocos2dx/event_dispatcher/CCEvent.h index 151f288fb6..fb31f7e6e0 100644 --- a/cocos2dx/event_dispatcher/CCEvent.h +++ b/cocos2dx/event_dispatcher/CCEvent.h @@ -48,28 +48,35 @@ public: virtual ~Event(); /** Gets the event type */ - const std::string& getType() const { return _type; }; + inline const std::string& getType() const { return _type; }; /** Stops propagation for current event */ - void stopPropagation() { _isStopped = true; }; + inline void stopPropagation() { _isStopped = true; }; /** Checks whether the event has been stopped */ - bool isStopped() const { return _isStopped; }; + inline bool isStopped() const { return _isStopped; }; /** @brief Gets current target of the event * @return The target with which the event associates. * @note It onlys be available when the event listener is associated with node. * It returns 0 when the listener is associated with fixed priority. */ - Node* getCurrentTarget(); + inline Node* getCurrentTarget() { return _currentTarget; }; + + /** Set user data */ + inline void setUserData(void* data) { _userData = data; }; + + /** Get user data */ + inline void* getUserData() const { return _userData; }; protected: /** Sets current target */ - void setCurrentTarget(Node* target); + inline void setCurrentTarget(Node* target) { _currentTarget = target; }; - std::string _type; /// Event type - bool _isStopped; /// whether the event has been stopped. - Node* _currentTarget; /// Current target + std::string _type; ///< Event type + bool _isStopped; ///< whether the event has been stopped. + Node* _currentTarget; ///< Current target + void* _userData; ///< User data friend class EventDispatcher; }; From e697b0fc0bd45d79e7f3cd8fe4930a528e1c6e22 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 18:16:14 +0800 Subject: [PATCH 13/20] issue #2087: Updating the constructor of XXXEvent. --- .../event_dispatcher/CCAccelerationEvent.cpp | 6 +- .../event_dispatcher/CCAccelerationEvent.h | 6 +- cocos2dx/event_dispatcher/CCKeyboardEvent.h | 8 +- cocos2dx/platform/android/nativeactivity.cpp | 31 +- cocos2dx/platform/ios/CCDevice.mm | 3 +- cocos2dx/platform/linux/CCEGLView.cpp | 4 +- cocos2dx/platform/mac/CCEGLView.mm | 4 +- cocos2dx/platform/win32/CCEGLView.cpp | 270 +++++++++--------- 8 files changed, 167 insertions(+), 165 deletions(-) diff --git a/cocos2dx/event_dispatcher/CCAccelerationEvent.cpp b/cocos2dx/event_dispatcher/CCAccelerationEvent.cpp index 6eede1469b..73d114eb3d 100644 --- a/cocos2dx/event_dispatcher/CCAccelerationEvent.cpp +++ b/cocos2dx/event_dispatcher/CCAccelerationEvent.cpp @@ -28,8 +28,10 @@ NS_CC_BEGIN const char* AccelerationEvent::EVENT_TYPE = "AccelerometerEvent"; -AccelerationEvent::AccelerationEvent() +AccelerationEvent::AccelerationEvent(Acceleration acc) : Event(EVENT_TYPE) -{} +, _acc(acc) +{ +} NS_CC_END diff --git a/cocos2dx/event_dispatcher/CCAccelerationEvent.h b/cocos2dx/event_dispatcher/CCAccelerationEvent.h index c7e628d977..75168ce4b3 100644 --- a/cocos2dx/event_dispatcher/CCAccelerationEvent.h +++ b/cocos2dx/event_dispatcher/CCAccelerationEvent.h @@ -35,8 +35,10 @@ class AccelerationEvent : public Event public: static const char* EVENT_TYPE; - AccelerationEvent(); - Acceleration acc; + AccelerationEvent(Acceleration acc); + +private: + Acceleration _acc; }; NS_CC_END diff --git a/cocos2dx/event_dispatcher/CCKeyboardEvent.h b/cocos2dx/event_dispatcher/CCKeyboardEvent.h index e5b753b6bc..5af871f68f 100644 --- a/cocos2dx/event_dispatcher/CCKeyboardEvent.h +++ b/cocos2dx/event_dispatcher/CCKeyboardEvent.h @@ -198,7 +198,13 @@ public: static const char* EVENT_TYPE; - KeyboardEvent() : Event(EVENT_TYPE) {}; + KeyboardEvent(KeyCode keyCode, bool isPressed) + : Event(EVENT_TYPE) + , _keyCode(keyCode) + , _isPressed(isPressed) + {}; + +private: KeyCode _keyCode; bool _isPressed; }; diff --git a/cocos2dx/platform/android/nativeactivity.cpp b/cocos2dx/platform/android/nativeactivity.cpp index e2e22cbb30..da63f353de 100644 --- a/cocos2dx/platform/android/nativeactivity.cpp +++ b/cocos2dx/platform/android/nativeactivity.cpp @@ -400,17 +400,13 @@ static int32_t handle_key_input(AInputEvent *event) { case AKEYCODE_BACK: { - cocos2d::KeyboardEvent event; - event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_BACKSPACE; - event._isPressed = false; + cocos2d::KeyboardEvent event(cocos2d::KeyboardEvent::KeyCode::KEY_BACKSPACE, false); cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); } return 1; case AKEYCODE_MENU: { - cocos2d::KeyboardEvent event; - event._keyCode = cocos2d::KeyboardEvent::KeyCode::KEY_MENU; - event._isPressed = false; + cocos2d::KeyboardEvent event(cocos2d::KeyboardEvent::KeyCode::KEY_MENU, false); cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); } return 1; @@ -601,21 +597,24 @@ void android_main(struct android_app* state) { // ACONFIGURATION_ORIENTATION_ANY // ACONFIGURATION_ORIENTATION_PORT // ACONFIGURATION_ORIENTATION_SQUARE - cocos2d::AccelerationEvent accEvent; - accEvent.acc.x = event.acceleration.x; - accEvent.acc.y = event.acceleration.y; - accEvent.acc.z = event.acceleration.z; - accEvent.acc.timestamp = 0; + cocos2d::Acceleration acc; + acc.x = event.acceleration.x; + acc.y = event.acceleration.y; + acc.z = event.acceleration.z; + acc.timestamp = 0; + cocos2d::AccelerationEvent accEvent(acc); + cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent); } else { // ACONFIGURATION_ORIENTATION_LAND // swap x and y parameters + cocos2d::Acceleration acc; + acc.x = -event.acceleration.y; + acc.y = event.acceleration.x; + acc.z = event.acceleration.z; + acc.timestamp = 0; + cocos2d::AccelerationEvent accEvent(acc); - cocos2d::AccelerationEvent accEvent; - accEvent.acc.x = -event.acceleration.y; - accEvent.acc.y = event.acceleration.x; - accEvent.acc.z = event.acceleration.z; - accEvent.acc.timestamp = 0; cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent); } } diff --git a/cocos2dx/platform/ios/CCDevice.mm b/cocos2dx/platform/ios/CCDevice.mm index 5fdfa39cf9..3191af79ec 100644 --- a/cocos2dx/platform/ios/CCDevice.mm +++ b/cocos2dx/platform/ios/CCDevice.mm @@ -95,8 +95,7 @@ static CCAccelerometerDispatcher* s_pAccelerometerDispatcher; break; } - cocos2d::AccelerationEvent event; - event.acc = *_acceleration; + cocos2d::AccelerationEvent event(*_acceleration); cocos2d::EventDispatcher::getInstance()->dispatchEvent(&event); } diff --git a/cocos2dx/platform/linux/CCEGLView.cpp b/cocos2dx/platform/linux/CCEGLView.cpp index a0d31caa6a..d557e24854 100644 --- a/cocos2dx/platform/linux/CCEGLView.cpp +++ b/cocos2dx/platform/linux/CCEGLView.cpp @@ -222,9 +222,7 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { - KeyboardEvent event; - event._keyCode = g_keyCodeMap[key]; - event._isPressed = (GLFW_PRESS == action); + KeyboardEvent event(g_keyCodeMap[key], GLFW_PRESS == action); EventDispatcher::getInstance()->dispatchEvent(&event); } diff --git a/cocos2dx/platform/mac/CCEGLView.mm b/cocos2dx/platform/mac/CCEGLView.mm index eddf3e80d1..ba839a9e3a 100644 --- a/cocos2dx/platform/mac/CCEGLView.mm +++ b/cocos2dx/platform/mac/CCEGLView.mm @@ -238,9 +238,7 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { - KeyboardEvent event; - event._keyCode = g_keyCodeMap[key]; - event._isPressed = (GLFW_PRESS == action); + KeyboardEvent event(g_keyCodeMap[key], GLFW_PRESS == action); EventDispatcher::getInstance()->dispatchEvent(&event); } diff --git a/cocos2dx/platform/win32/CCEGLView.cpp b/cocos2dx/platform/win32/CCEGLView.cpp index 8aec9c89f4..58da40a0c9 100644 --- a/cocos2dx/platform/win32/CCEGLView.cpp +++ b/cocos2dx/platform/win32/CCEGLView.cpp @@ -28,8 +28,8 @@ THE SOFTWARE. #include "CCDirector.h" #include "text_input_node/CCIMEDispatcher.h" #include "CCApplication.h" -#include "event_dispatcher/CCTouch.h" -#include "event_dispatcher/CCEventDispatcher.h" +#include "event_dispatcher/CCTouch.h" +#include "event_dispatcher/CCEventDispatcher.h" #include "event_dispatcher/CCKeyboardEvent.h" @@ -41,137 +41,137 @@ struct keyCodeItem KeyboardEvent::KeyCode keyCode; }; -static std::map g_keyCodeMap; - -static keyCodeItem g_keyCodeStructArray[] = { - /* The unknown key */ - { GLFW_KEY_UNKNOWN , KeyboardEvent::KeyCode::KEY_NONE }, - - /* Printable keys */ - - { GLFW_KEY_SPACE , KeyboardEvent::KeyCode::KEY_SPACE }, - { GLFW_KEY_APOSTROPHE , KeyboardEvent::KeyCode::KEY_APOSTROPHE }, - { GLFW_KEY_COMMA , KeyboardEvent::KeyCode::KEY_COMMA }, - { GLFW_KEY_MINUS , KeyboardEvent::KeyCode::KEY_MINUS }, - { GLFW_KEY_PERIOD , KeyboardEvent::KeyCode::KEY_PERIOD }, - { GLFW_KEY_SLASH , KeyboardEvent::KeyCode::KEY_SLASH }, - { GLFW_KEY_0 , KeyboardEvent::KeyCode::KEY_0 }, - { GLFW_KEY_1 , KeyboardEvent::KeyCode::KEY_1 }, - { GLFW_KEY_2 , KeyboardEvent::KeyCode::KEY_2 }, - { GLFW_KEY_3 , KeyboardEvent::KeyCode::KEY_3 }, - { GLFW_KEY_4 , KeyboardEvent::KeyCode::KEY_4 }, - { GLFW_KEY_5 , KeyboardEvent::KeyCode::KEY_5 }, - { GLFW_KEY_6 , KeyboardEvent::KeyCode::KEY_6 }, - { GLFW_KEY_7 , KeyboardEvent::KeyCode::KEY_7 }, - { GLFW_KEY_8 , KeyboardEvent::KeyCode::KEY_8 }, - { GLFW_KEY_9 , KeyboardEvent::KeyCode::KEY_9 }, - { GLFW_KEY_SEMICOLON , KeyboardEvent::KeyCode::KEY_SEMICOLON }, - { GLFW_KEY_EQUAL , KeyboardEvent::KeyCode::KEY_EQUAL }, - { GLFW_KEY_A , KeyboardEvent::KeyCode::KEY_A }, - { GLFW_KEY_B , KeyboardEvent::KeyCode::KEY_B }, - { GLFW_KEY_C , KeyboardEvent::KeyCode::KEY_C }, - { GLFW_KEY_D , KeyboardEvent::KeyCode::KEY_D }, - { GLFW_KEY_E , KeyboardEvent::KeyCode::KEY_E }, - { GLFW_KEY_F , KeyboardEvent::KeyCode::KEY_F }, - { GLFW_KEY_G , KeyboardEvent::KeyCode::KEY_G }, - { GLFW_KEY_H , KeyboardEvent::KeyCode::KEY_H }, - { GLFW_KEY_I , KeyboardEvent::KeyCode::KEY_I }, - { GLFW_KEY_J , KeyboardEvent::KeyCode::KEY_J }, - { GLFW_KEY_K , KeyboardEvent::KeyCode::KEY_K }, - { GLFW_KEY_L , KeyboardEvent::KeyCode::KEY_L }, - { GLFW_KEY_M , KeyboardEvent::KeyCode::KEY_M }, - { GLFW_KEY_N , KeyboardEvent::KeyCode::KEY_N }, - { GLFW_KEY_O , KeyboardEvent::KeyCode::KEY_O }, - { GLFW_KEY_P , KeyboardEvent::KeyCode::KEY_P }, - { GLFW_KEY_Q , KeyboardEvent::KeyCode::KEY_Q }, - { GLFW_KEY_R , KeyboardEvent::KeyCode::KEY_R }, - { GLFW_KEY_S , KeyboardEvent::KeyCode::KEY_S }, - { GLFW_KEY_T , KeyboardEvent::KeyCode::KEY_T }, - { GLFW_KEY_U , KeyboardEvent::KeyCode::KEY_U }, - { GLFW_KEY_V , KeyboardEvent::KeyCode::KEY_V }, - { GLFW_KEY_W , KeyboardEvent::KeyCode::KEY_W }, - { GLFW_KEY_X , KeyboardEvent::KeyCode::KEY_X }, - { GLFW_KEY_Y , KeyboardEvent::KeyCode::KEY_Y }, - { GLFW_KEY_Z , KeyboardEvent::KeyCode::KEY_Z }, - { GLFW_KEY_LEFT_BRACKET , KeyboardEvent::KeyCode::KEY_LEFT_BRACKET }, - { GLFW_KEY_BACKSLASH , KeyboardEvent::KeyCode::KEY_BACK_SLASH }, - { GLFW_KEY_RIGHT_BRACKET , KeyboardEvent::KeyCode::KEY_RIGHT_BRACKET }, - { GLFW_KEY_GRAVE_ACCENT , KeyboardEvent::KeyCode::KEY_GRAVE }, - { GLFW_KEY_WORLD_1 , KeyboardEvent::KeyCode::KEY_GRAVE }, - { GLFW_KEY_WORLD_2 , KeyboardEvent::KeyCode::KEY_NONE }, - - /* Function keys */ - { GLFW_KEY_ESCAPE , KeyboardEvent::KeyCode::KEY_ESCAPE }, - { GLFW_KEY_ENTER , KeyboardEvent::KeyCode::KEY_KP_ENTER }, - { GLFW_KEY_TAB , KeyboardEvent::KeyCode::KEY_TAB }, - { GLFW_KEY_BACKSPACE , KeyboardEvent::KeyCode::KEY_BACKSPACE }, - { GLFW_KEY_INSERT , KeyboardEvent::KeyCode::KEY_INSERT }, - { GLFW_KEY_DELETE , KeyboardEvent::KeyCode::KEY_DELETE }, - { GLFW_KEY_RIGHT , KeyboardEvent::KeyCode::KEY_RIGHT_ARROW }, - { GLFW_KEY_LEFT , KeyboardEvent::KeyCode::KEY_LEFT_ARROW }, - { GLFW_KEY_DOWN , KeyboardEvent::KeyCode::KEY_DOWN_ARROW }, - { GLFW_KEY_UP , KeyboardEvent::KeyCode::KEY_UP_ARROW }, - { GLFW_KEY_PAGE_UP , KeyboardEvent::KeyCode::KEY_KP_PG_UP }, - { GLFW_KEY_PAGE_DOWN , KeyboardEvent::KeyCode::KEY_KP_PG_DOWN }, - { GLFW_KEY_HOME , KeyboardEvent::KeyCode::KEY_KP_HOME }, - { GLFW_KEY_END , KeyboardEvent::KeyCode::KEY_END }, - { GLFW_KEY_CAPS_LOCK , KeyboardEvent::KeyCode::KEY_CAPS_LOCK }, - { GLFW_KEY_SCROLL_LOCK , KeyboardEvent::KeyCode::KEY_SCROLL_LOCK }, - { GLFW_KEY_NUM_LOCK , KeyboardEvent::KeyCode::KEY_NUM_LOCK }, - { GLFW_KEY_PRINT_SCREEN , KeyboardEvent::KeyCode::KEY_PRINT }, - { GLFW_KEY_PAUSE , KeyboardEvent::KeyCode::KEY_PAUSE }, - { GLFW_KEY_F1 , KeyboardEvent::KeyCode::KEY_F1 }, - { GLFW_KEY_F2 , KeyboardEvent::KeyCode::KEY_F2 }, - { GLFW_KEY_F3 , KeyboardEvent::KeyCode::KEY_F3 }, - { GLFW_KEY_F4 , KeyboardEvent::KeyCode::KEY_F4 }, - { GLFW_KEY_F5 , KeyboardEvent::KeyCode::KEY_F5 }, - { GLFW_KEY_F6 , KeyboardEvent::KeyCode::KEY_F6 }, - { GLFW_KEY_F7 , KeyboardEvent::KeyCode::KEY_F7 }, - { GLFW_KEY_F8 , KeyboardEvent::KeyCode::KEY_F8 }, - { GLFW_KEY_F9 , KeyboardEvent::KeyCode::KEY_F9 }, - { GLFW_KEY_F10 , KeyboardEvent::KeyCode::KEY_F10 }, - { GLFW_KEY_F11 , KeyboardEvent::KeyCode::KEY_F11 }, - { GLFW_KEY_F12 , KeyboardEvent::KeyCode::KEY_F12 }, - { GLFW_KEY_F13 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F14 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F15 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F16 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F17 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F18 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F19 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F20 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F21 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F22 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F23 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F24 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_F25 , KeyboardEvent::KeyCode::KEY_NONE }, - { GLFW_KEY_KP_0 , KeyboardEvent::KeyCode::KEY_0 }, - { GLFW_KEY_KP_1 , KeyboardEvent::KeyCode::KEY_1 }, - { GLFW_KEY_KP_2 , KeyboardEvent::KeyCode::KEY_2 }, - { GLFW_KEY_KP_3 , KeyboardEvent::KeyCode::KEY_3 }, - { GLFW_KEY_KP_4 , KeyboardEvent::KeyCode::KEY_4 }, - { GLFW_KEY_KP_5 , KeyboardEvent::KeyCode::KEY_5 }, - { GLFW_KEY_KP_6 , KeyboardEvent::KeyCode::KEY_6 }, - { GLFW_KEY_KP_7 , KeyboardEvent::KeyCode::KEY_7 }, - { GLFW_KEY_KP_8 , KeyboardEvent::KeyCode::KEY_8 }, - { GLFW_KEY_KP_9 , KeyboardEvent::KeyCode::KEY_9 }, - { GLFW_KEY_KP_DECIMAL , KeyboardEvent::KeyCode::KEY_PERIOD }, - { GLFW_KEY_KP_DIVIDE , KeyboardEvent::KeyCode::KEY_KP_DIVIDE }, - { GLFW_KEY_KP_MULTIPLY , KeyboardEvent::KeyCode::KEY_KP_MULTIPLY }, - { GLFW_KEY_KP_SUBTRACT , KeyboardEvent::KeyCode::KEY_KP_MINUS }, - { GLFW_KEY_KP_ADD , KeyboardEvent::KeyCode::KEY_KP_PLUS }, - { GLFW_KEY_KP_ENTER , KeyboardEvent::KeyCode::KEY_KP_ENTER }, - { GLFW_KEY_KP_EQUAL , KeyboardEvent::KeyCode::KEY_EQUAL }, - { GLFW_KEY_LEFT_SHIFT , KeyboardEvent::KeyCode::KEY_SHIFT }, - { GLFW_KEY_LEFT_CONTROL , KeyboardEvent::KeyCode::KEY_CTRL }, - { GLFW_KEY_LEFT_ALT , KeyboardEvent::KeyCode::KEY_ALT }, - { GLFW_KEY_LEFT_SUPER , KeyboardEvent::KeyCode::KEY_HYPER }, - { GLFW_KEY_RIGHT_SHIFT , KeyboardEvent::KeyCode::KEY_SHIFT }, - { GLFW_KEY_RIGHT_CONTROL , KeyboardEvent::KeyCode::KEY_CTRL }, - { GLFW_KEY_RIGHT_ALT , KeyboardEvent::KeyCode::KEY_ALT }, - { GLFW_KEY_RIGHT_SUPER , KeyboardEvent::KeyCode::KEY_HYPER }, - { GLFW_KEY_MENU , KeyboardEvent::KeyCode::KEY_MENU }, - { GLFW_KEY_LAST , KeyboardEvent::KeyCode::KEY_NONE } +static std::map g_keyCodeMap; + +static keyCodeItem g_keyCodeStructArray[] = { + /* The unknown key */ + { GLFW_KEY_UNKNOWN , KeyboardEvent::KeyCode::KEY_NONE }, + + /* Printable keys */ + + { GLFW_KEY_SPACE , KeyboardEvent::KeyCode::KEY_SPACE }, + { GLFW_KEY_APOSTROPHE , KeyboardEvent::KeyCode::KEY_APOSTROPHE }, + { GLFW_KEY_COMMA , KeyboardEvent::KeyCode::KEY_COMMA }, + { GLFW_KEY_MINUS , KeyboardEvent::KeyCode::KEY_MINUS }, + { GLFW_KEY_PERIOD , KeyboardEvent::KeyCode::KEY_PERIOD }, + { GLFW_KEY_SLASH , KeyboardEvent::KeyCode::KEY_SLASH }, + { GLFW_KEY_0 , KeyboardEvent::KeyCode::KEY_0 }, + { GLFW_KEY_1 , KeyboardEvent::KeyCode::KEY_1 }, + { GLFW_KEY_2 , KeyboardEvent::KeyCode::KEY_2 }, + { GLFW_KEY_3 , KeyboardEvent::KeyCode::KEY_3 }, + { GLFW_KEY_4 , KeyboardEvent::KeyCode::KEY_4 }, + { GLFW_KEY_5 , KeyboardEvent::KeyCode::KEY_5 }, + { GLFW_KEY_6 , KeyboardEvent::KeyCode::KEY_6 }, + { GLFW_KEY_7 , KeyboardEvent::KeyCode::KEY_7 }, + { GLFW_KEY_8 , KeyboardEvent::KeyCode::KEY_8 }, + { GLFW_KEY_9 , KeyboardEvent::KeyCode::KEY_9 }, + { GLFW_KEY_SEMICOLON , KeyboardEvent::KeyCode::KEY_SEMICOLON }, + { GLFW_KEY_EQUAL , KeyboardEvent::KeyCode::KEY_EQUAL }, + { GLFW_KEY_A , KeyboardEvent::KeyCode::KEY_A }, + { GLFW_KEY_B , KeyboardEvent::KeyCode::KEY_B }, + { GLFW_KEY_C , KeyboardEvent::KeyCode::KEY_C }, + { GLFW_KEY_D , KeyboardEvent::KeyCode::KEY_D }, + { GLFW_KEY_E , KeyboardEvent::KeyCode::KEY_E }, + { GLFW_KEY_F , KeyboardEvent::KeyCode::KEY_F }, + { GLFW_KEY_G , KeyboardEvent::KeyCode::KEY_G }, + { GLFW_KEY_H , KeyboardEvent::KeyCode::KEY_H }, + { GLFW_KEY_I , KeyboardEvent::KeyCode::KEY_I }, + { GLFW_KEY_J , KeyboardEvent::KeyCode::KEY_J }, + { GLFW_KEY_K , KeyboardEvent::KeyCode::KEY_K }, + { GLFW_KEY_L , KeyboardEvent::KeyCode::KEY_L }, + { GLFW_KEY_M , KeyboardEvent::KeyCode::KEY_M }, + { GLFW_KEY_N , KeyboardEvent::KeyCode::KEY_N }, + { GLFW_KEY_O , KeyboardEvent::KeyCode::KEY_O }, + { GLFW_KEY_P , KeyboardEvent::KeyCode::KEY_P }, + { GLFW_KEY_Q , KeyboardEvent::KeyCode::KEY_Q }, + { GLFW_KEY_R , KeyboardEvent::KeyCode::KEY_R }, + { GLFW_KEY_S , KeyboardEvent::KeyCode::KEY_S }, + { GLFW_KEY_T , KeyboardEvent::KeyCode::KEY_T }, + { GLFW_KEY_U , KeyboardEvent::KeyCode::KEY_U }, + { GLFW_KEY_V , KeyboardEvent::KeyCode::KEY_V }, + { GLFW_KEY_W , KeyboardEvent::KeyCode::KEY_W }, + { GLFW_KEY_X , KeyboardEvent::KeyCode::KEY_X }, + { GLFW_KEY_Y , KeyboardEvent::KeyCode::KEY_Y }, + { GLFW_KEY_Z , KeyboardEvent::KeyCode::KEY_Z }, + { GLFW_KEY_LEFT_BRACKET , KeyboardEvent::KeyCode::KEY_LEFT_BRACKET }, + { GLFW_KEY_BACKSLASH , KeyboardEvent::KeyCode::KEY_BACK_SLASH }, + { GLFW_KEY_RIGHT_BRACKET , KeyboardEvent::KeyCode::KEY_RIGHT_BRACKET }, + { GLFW_KEY_GRAVE_ACCENT , KeyboardEvent::KeyCode::KEY_GRAVE }, + { GLFW_KEY_WORLD_1 , KeyboardEvent::KeyCode::KEY_GRAVE }, + { GLFW_KEY_WORLD_2 , KeyboardEvent::KeyCode::KEY_NONE }, + + /* Function keys */ + { GLFW_KEY_ESCAPE , KeyboardEvent::KeyCode::KEY_ESCAPE }, + { GLFW_KEY_ENTER , KeyboardEvent::KeyCode::KEY_KP_ENTER }, + { GLFW_KEY_TAB , KeyboardEvent::KeyCode::KEY_TAB }, + { GLFW_KEY_BACKSPACE , KeyboardEvent::KeyCode::KEY_BACKSPACE }, + { GLFW_KEY_INSERT , KeyboardEvent::KeyCode::KEY_INSERT }, + { GLFW_KEY_DELETE , KeyboardEvent::KeyCode::KEY_DELETE }, + { GLFW_KEY_RIGHT , KeyboardEvent::KeyCode::KEY_RIGHT_ARROW }, + { GLFW_KEY_LEFT , KeyboardEvent::KeyCode::KEY_LEFT_ARROW }, + { GLFW_KEY_DOWN , KeyboardEvent::KeyCode::KEY_DOWN_ARROW }, + { GLFW_KEY_UP , KeyboardEvent::KeyCode::KEY_UP_ARROW }, + { GLFW_KEY_PAGE_UP , KeyboardEvent::KeyCode::KEY_KP_PG_UP }, + { GLFW_KEY_PAGE_DOWN , KeyboardEvent::KeyCode::KEY_KP_PG_DOWN }, + { GLFW_KEY_HOME , KeyboardEvent::KeyCode::KEY_KP_HOME }, + { GLFW_KEY_END , KeyboardEvent::KeyCode::KEY_END }, + { GLFW_KEY_CAPS_LOCK , KeyboardEvent::KeyCode::KEY_CAPS_LOCK }, + { GLFW_KEY_SCROLL_LOCK , KeyboardEvent::KeyCode::KEY_SCROLL_LOCK }, + { GLFW_KEY_NUM_LOCK , KeyboardEvent::KeyCode::KEY_NUM_LOCK }, + { GLFW_KEY_PRINT_SCREEN , KeyboardEvent::KeyCode::KEY_PRINT }, + { GLFW_KEY_PAUSE , KeyboardEvent::KeyCode::KEY_PAUSE }, + { GLFW_KEY_F1 , KeyboardEvent::KeyCode::KEY_F1 }, + { GLFW_KEY_F2 , KeyboardEvent::KeyCode::KEY_F2 }, + { GLFW_KEY_F3 , KeyboardEvent::KeyCode::KEY_F3 }, + { GLFW_KEY_F4 , KeyboardEvent::KeyCode::KEY_F4 }, + { GLFW_KEY_F5 , KeyboardEvent::KeyCode::KEY_F5 }, + { GLFW_KEY_F6 , KeyboardEvent::KeyCode::KEY_F6 }, + { GLFW_KEY_F7 , KeyboardEvent::KeyCode::KEY_F7 }, + { GLFW_KEY_F8 , KeyboardEvent::KeyCode::KEY_F8 }, + { GLFW_KEY_F9 , KeyboardEvent::KeyCode::KEY_F9 }, + { GLFW_KEY_F10 , KeyboardEvent::KeyCode::KEY_F10 }, + { GLFW_KEY_F11 , KeyboardEvent::KeyCode::KEY_F11 }, + { GLFW_KEY_F12 , KeyboardEvent::KeyCode::KEY_F12 }, + { GLFW_KEY_F13 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F14 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F15 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F16 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F17 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F18 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F19 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F20 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F21 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F22 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F23 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F24 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_F25 , KeyboardEvent::KeyCode::KEY_NONE }, + { GLFW_KEY_KP_0 , KeyboardEvent::KeyCode::KEY_0 }, + { GLFW_KEY_KP_1 , KeyboardEvent::KeyCode::KEY_1 }, + { GLFW_KEY_KP_2 , KeyboardEvent::KeyCode::KEY_2 }, + { GLFW_KEY_KP_3 , KeyboardEvent::KeyCode::KEY_3 }, + { GLFW_KEY_KP_4 , KeyboardEvent::KeyCode::KEY_4 }, + { GLFW_KEY_KP_5 , KeyboardEvent::KeyCode::KEY_5 }, + { GLFW_KEY_KP_6 , KeyboardEvent::KeyCode::KEY_6 }, + { GLFW_KEY_KP_7 , KeyboardEvent::KeyCode::KEY_7 }, + { GLFW_KEY_KP_8 , KeyboardEvent::KeyCode::KEY_8 }, + { GLFW_KEY_KP_9 , KeyboardEvent::KeyCode::KEY_9 }, + { GLFW_KEY_KP_DECIMAL , KeyboardEvent::KeyCode::KEY_PERIOD }, + { GLFW_KEY_KP_DIVIDE , KeyboardEvent::KeyCode::KEY_KP_DIVIDE }, + { GLFW_KEY_KP_MULTIPLY , KeyboardEvent::KeyCode::KEY_KP_MULTIPLY }, + { GLFW_KEY_KP_SUBTRACT , KeyboardEvent::KeyCode::KEY_KP_MINUS }, + { GLFW_KEY_KP_ADD , KeyboardEvent::KeyCode::KEY_KP_PLUS }, + { GLFW_KEY_KP_ENTER , KeyboardEvent::KeyCode::KEY_KP_ENTER }, + { GLFW_KEY_KP_EQUAL , KeyboardEvent::KeyCode::KEY_EQUAL }, + { GLFW_KEY_LEFT_SHIFT , KeyboardEvent::KeyCode::KEY_SHIFT }, + { GLFW_KEY_LEFT_CONTROL , KeyboardEvent::KeyCode::KEY_CTRL }, + { GLFW_KEY_LEFT_ALT , KeyboardEvent::KeyCode::KEY_ALT }, + { GLFW_KEY_LEFT_SUPER , KeyboardEvent::KeyCode::KEY_HYPER }, + { GLFW_KEY_RIGHT_SHIFT , KeyboardEvent::KeyCode::KEY_SHIFT }, + { GLFW_KEY_RIGHT_CONTROL , KeyboardEvent::KeyCode::KEY_CTRL }, + { GLFW_KEY_RIGHT_ALT , KeyboardEvent::KeyCode::KEY_ALT }, + { GLFW_KEY_RIGHT_SUPER , KeyboardEvent::KeyCode::KEY_HYPER }, + { GLFW_KEY_MENU , KeyboardEvent::KeyCode::KEY_MENU }, + { GLFW_KEY_LAST , KeyboardEvent::KeyCode::KEY_NONE } }; #if(_MSC_VER >= 1600) // Visual Studio 2010 or higher version. @@ -340,9 +340,7 @@ void EGLViewEventHandler::OnGLFWMouseMoveCallBack(GLFWwindow* window, double x, void EGLViewEventHandler::OnGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { - KeyboardEvent event; - event._keyCode = g_keyCodeMap[key]; - event._isPressed = (GLFW_PRESS == action); + KeyboardEvent event(g_keyCodeMap[key], GLFW_PRESS == action); EventDispatcher::getInstance()->dispatchEvent(&event); } From 19e37d284e71c1824e956ce2f414335d844699fb Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Wed, 18 Sep 2013 18:26:03 +0800 Subject: [PATCH 14/20] rename test case to correct name --- samples/Cpp/AssetsManagerTest/proj.win32/main.cpp | 2 +- samples/Cpp/HelloCpp/proj.linux/main.cpp | 2 +- samples/Cpp/HelloCpp/proj.mac/main.cpp | 2 +- samples/Cpp/HelloCpp/proj.win32/main.cpp | 2 +- samples/Cpp/SimpleGame/proj.linux/main.cpp | 2 +- samples/Cpp/SimpleGame/proj.mac/main.cpp | 2 +- samples/Javascript/CocosDragonJS/proj.mac/main.cpp | 2 +- samples/Javascript/CocosDragonJS/proj.win32/main.cpp | 2 +- samples/Javascript/CrystalCraze/proj.mac/main.cpp | 2 +- samples/Javascript/CrystalCraze/proj.win32/main.cpp | 2 +- samples/Javascript/MoonWarriors/proj.mac/main.cpp | 2 +- samples/Javascript/MoonWarriors/proj.win32/main.cpp | 2 +- samples/Javascript/TestJavascript/proj.mac/main.cpp | 2 +- samples/Javascript/TestJavascript/proj.win32/main.cpp | 2 +- samples/Javascript/WatermelonWithMe/proj.mac/main.cpp | 2 +- samples/Javascript/WatermelonWithMe/proj.win32/main.cpp | 2 +- samples/Lua/HelloLua/proj.linux/main.cpp | 2 +- samples/Lua/HelloLua/proj.mac/main.cpp | 2 +- samples/Lua/HelloLua/proj.win32/main.cpp | 2 +- samples/Lua/TestLua/proj.linux/main.cpp | 2 +- samples/Lua/TestLua/proj.mac/main.cpp | 2 +- samples/Lua/TestLua/proj.win32/main.cpp | 2 +- 22 files changed, 22 insertions(+), 22 deletions(-) diff --git a/samples/Cpp/AssetsManagerTest/proj.win32/main.cpp b/samples/Cpp/AssetsManagerTest/proj.win32/main.cpp index e9fef1d86d..48e2af43a1 100644 --- a/samples/Cpp/AssetsManagerTest/proj.win32/main.cpp +++ b/samples/Cpp/AssetsManagerTest/proj.win32/main.cpp @@ -25,7 +25,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("AssetsManagerTest",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Cpp/HelloCpp/proj.linux/main.cpp b/samples/Cpp/HelloCpp/proj.linux/main.cpp index 135ddbb2f1..8a0736999a 100644 --- a/samples/Cpp/HelloCpp/proj.linux/main.cpp +++ b/samples/Cpp/HelloCpp/proj.linux/main.cpp @@ -13,6 +13,6 @@ int main(int argc, char **argv) // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("HelloCpp",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/HelloCpp/proj.mac/main.cpp b/samples/Cpp/HelloCpp/proj.mac/main.cpp index 976460501e..19faf80e3b 100644 --- a/samples/Cpp/HelloCpp/proj.mac/main.cpp +++ b/samples/Cpp/HelloCpp/proj.mac/main.cpp @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) { AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("HelloCpp",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/HelloCpp/proj.win32/main.cpp b/samples/Cpp/HelloCpp/proj.win32/main.cpp index ec96f44bca..bcc0e091d4 100644 --- a/samples/Cpp/HelloCpp/proj.win32/main.cpp +++ b/samples/Cpp/HelloCpp/proj.win32/main.cpp @@ -15,6 +15,6 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("HelloCpp",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/SimpleGame/proj.linux/main.cpp b/samples/Cpp/SimpleGame/proj.linux/main.cpp index 4e9455bc04..33e1052ebd 100644 --- a/samples/Cpp/SimpleGame/proj.linux/main.cpp +++ b/samples/Cpp/SimpleGame/proj.linux/main.cpp @@ -13,6 +13,6 @@ int main(int argc, char **argv) // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("SimpleGame",900,640); return Application::getInstance()->run(); } diff --git a/samples/Cpp/SimpleGame/proj.mac/main.cpp b/samples/Cpp/SimpleGame/proj.mac/main.cpp index 976460501e..d9432ebfe8 100644 --- a/samples/Cpp/SimpleGame/proj.mac/main.cpp +++ b/samples/Cpp/SimpleGame/proj.mac/main.cpp @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) { AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("SimpleGame",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/CocosDragonJS/proj.mac/main.cpp b/samples/Javascript/CocosDragonJS/proj.mac/main.cpp index 061f7b6874..41101ef8c9 100644 --- a/samples/Javascript/CocosDragonJS/proj.mac/main.cpp +++ b/samples/Javascript/CocosDragonJS/proj.mac/main.cpp @@ -31,6 +31,6 @@ int main(int argc, char *argv[]) { AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("CocosDragonJS",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/CocosDragonJS/proj.win32/main.cpp b/samples/Javascript/CocosDragonJS/proj.win32/main.cpp index e9fef1d86d..bf419888d8 100644 --- a/samples/Javascript/CocosDragonJS/proj.win32/main.cpp +++ b/samples/Javascript/CocosDragonJS/proj.win32/main.cpp @@ -25,7 +25,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("CocosDragonJS",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Javascript/CrystalCraze/proj.mac/main.cpp b/samples/Javascript/CrystalCraze/proj.mac/main.cpp index 061f7b6874..f07211b479 100644 --- a/samples/Javascript/CrystalCraze/proj.mac/main.cpp +++ b/samples/Javascript/CrystalCraze/proj.mac/main.cpp @@ -31,6 +31,6 @@ int main(int argc, char *argv[]) { AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("CrystalCraze",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/CrystalCraze/proj.win32/main.cpp b/samples/Javascript/CrystalCraze/proj.win32/main.cpp index e9fef1d86d..44dd20eada 100644 --- a/samples/Javascript/CrystalCraze/proj.win32/main.cpp +++ b/samples/Javascript/CrystalCraze/proj.win32/main.cpp @@ -25,7 +25,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("CrystalCraze",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Javascript/MoonWarriors/proj.mac/main.cpp b/samples/Javascript/MoonWarriors/proj.mac/main.cpp index 976460501e..5edd15794c 100644 --- a/samples/Javascript/MoonWarriors/proj.mac/main.cpp +++ b/samples/Javascript/MoonWarriors/proj.mac/main.cpp @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) { AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("MoonWarriors",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/MoonWarriors/proj.win32/main.cpp b/samples/Javascript/MoonWarriors/proj.win32/main.cpp index e9fef1d86d..98127c7b0f 100644 --- a/samples/Javascript/MoonWarriors/proj.win32/main.cpp +++ b/samples/Javascript/MoonWarriors/proj.win32/main.cpp @@ -25,7 +25,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("MoonWarriors",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Javascript/TestJavascript/proj.mac/main.cpp b/samples/Javascript/TestJavascript/proj.mac/main.cpp index 976460501e..01efc8b89b 100644 --- a/samples/Javascript/TestJavascript/proj.mac/main.cpp +++ b/samples/Javascript/TestJavascript/proj.mac/main.cpp @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) { AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("TestJavascript",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/TestJavascript/proj.win32/main.cpp b/samples/Javascript/TestJavascript/proj.win32/main.cpp index e9fef1d86d..0f4c16d7c8 100644 --- a/samples/Javascript/TestJavascript/proj.win32/main.cpp +++ b/samples/Javascript/TestJavascript/proj.win32/main.cpp @@ -25,7 +25,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("TestJavascript",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Javascript/WatermelonWithMe/proj.mac/main.cpp b/samples/Javascript/WatermelonWithMe/proj.mac/main.cpp index 976460501e..f492ffc573 100644 --- a/samples/Javascript/WatermelonWithMe/proj.mac/main.cpp +++ b/samples/Javascript/WatermelonWithMe/proj.mac/main.cpp @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) { AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("WatermelonWithMe",900,640); return Application::getInstance()->run(); } diff --git a/samples/Javascript/WatermelonWithMe/proj.win32/main.cpp b/samples/Javascript/WatermelonWithMe/proj.win32/main.cpp index e9fef1d86d..3c69549eac 100644 --- a/samples/Javascript/WatermelonWithMe/proj.win32/main.cpp +++ b/samples/Javascript/WatermelonWithMe/proj.win32/main.cpp @@ -25,7 +25,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("WatermelonWithMe",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Lua/HelloLua/proj.linux/main.cpp b/samples/Lua/HelloLua/proj.linux/main.cpp index 135ddbb2f1..1c4adf55ea 100644 --- a/samples/Lua/HelloLua/proj.linux/main.cpp +++ b/samples/Lua/HelloLua/proj.linux/main.cpp @@ -13,6 +13,6 @@ int main(int argc, char **argv) // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("HelloLua",900,640); return Application::getInstance()->run(); } diff --git a/samples/Lua/HelloLua/proj.mac/main.cpp b/samples/Lua/HelloLua/proj.mac/main.cpp index 061f7b6874..d3a6801ac6 100644 --- a/samples/Lua/HelloLua/proj.mac/main.cpp +++ b/samples/Lua/HelloLua/proj.mac/main.cpp @@ -31,6 +31,6 @@ int main(int argc, char *argv[]) { AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("HelloLua",900,640); return Application::getInstance()->run(); } diff --git a/samples/Lua/HelloLua/proj.win32/main.cpp b/samples/Lua/HelloLua/proj.win32/main.cpp index 3706b8cfdb..b07e50bc66 100644 --- a/samples/Lua/HelloLua/proj.win32/main.cpp +++ b/samples/Lua/HelloLua/proj.win32/main.cpp @@ -25,7 +25,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("HelloLua",900,640); int ret = Application::getInstance()->run(); diff --git a/samples/Lua/TestLua/proj.linux/main.cpp b/samples/Lua/TestLua/proj.linux/main.cpp index fe28f03278..4255905fec 100644 --- a/samples/Lua/TestLua/proj.linux/main.cpp +++ b/samples/Lua/TestLua/proj.linux/main.cpp @@ -14,6 +14,6 @@ int main(int argc, char **argv) // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("TestLua",900,640); return Application::getInstance()->run(); } diff --git a/samples/Lua/TestLua/proj.mac/main.cpp b/samples/Lua/TestLua/proj.mac/main.cpp index 061f7b6874..45f24c946a 100644 --- a/samples/Lua/TestLua/proj.mac/main.cpp +++ b/samples/Lua/TestLua/proj.mac/main.cpp @@ -31,6 +31,6 @@ int main(int argc, char *argv[]) { AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("TestLua",900,640); return Application::getInstance()->run(); } diff --git a/samples/Lua/TestLua/proj.win32/main.cpp b/samples/Lua/TestLua/proj.win32/main.cpp index 4dfce357f9..2554f5981a 100644 --- a/samples/Lua/TestLua/proj.win32/main.cpp +++ b/samples/Lua/TestLua/proj.win32/main.cpp @@ -25,7 +25,7 @@ int APIENTRY _tWinMain(HINSTANCE hInstance, // create the application instance AppDelegate app; EGLView eglView; - eglView.init("TestCPP",900,640); + eglView.init("TestLua",900,640); int ret = Application::getInstance()->run(); From a2d9f2d7cfd00f3a06cd8dcef2d44c61b7d3a6f3 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 18:35:42 +0800 Subject: [PATCH 15/20] Make XXXEventListener as friend class of XXXEvent. Move the test of eventdispatchertest. --- cocos2dx/event_dispatcher/CCAccelerationEvent.h | 1 + cocos2dx/event_dispatcher/CCAccelerationEventListener.cpp | 2 +- cocos2dx/event_dispatcher/CCKeyboardEvent.h | 2 ++ samples/Cpp/TestCpp/Classes/controller.cpp | 3 +-- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cocos2dx/event_dispatcher/CCAccelerationEvent.h b/cocos2dx/event_dispatcher/CCAccelerationEvent.h index 75168ce4b3..b72f7ac808 100644 --- a/cocos2dx/event_dispatcher/CCAccelerationEvent.h +++ b/cocos2dx/event_dispatcher/CCAccelerationEvent.h @@ -39,6 +39,7 @@ public: private: Acceleration _acc; + friend class AccelerationEventListener; }; NS_CC_END diff --git a/cocos2dx/event_dispatcher/CCAccelerationEventListener.cpp b/cocos2dx/event_dispatcher/CCAccelerationEventListener.cpp index 8b2d9dbf9c..7fa8d6d9a5 100644 --- a/cocos2dx/event_dispatcher/CCAccelerationEventListener.cpp +++ b/cocos2dx/event_dispatcher/CCAccelerationEventListener.cpp @@ -56,7 +56,7 @@ bool AccelerationEventListener::init(std::function(event); - this->onAccelerationEvent(&accEvent->acc, event); + this->onAccelerationEvent(&accEvent->_acc, event); }; if (EventListener::init(AccelerationEvent::EVENT_TYPE, listener)) diff --git a/cocos2dx/event_dispatcher/CCKeyboardEvent.h b/cocos2dx/event_dispatcher/CCKeyboardEvent.h index 5af871f68f..f7407fbbf8 100644 --- a/cocos2dx/event_dispatcher/CCKeyboardEvent.h +++ b/cocos2dx/event_dispatcher/CCKeyboardEvent.h @@ -207,6 +207,8 @@ public: private: KeyCode _keyCode; bool _isPressed; + + friend class KeyboardEventListener; }; NS_CC_END diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index af088f5af6..7717917111 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -14,8 +14,6 @@ struct { const char *test_name; std::function callback; } g_aTestNames[] = { - - { "NewEventDispatcherTest", []() { return new EventDispatcherTestScene(); } }, { "Accelerometer", []() { return new AccelerometerTestScene(); } }, { "ActionManagerTest", [](){return new ActionManagerTestScene(); } }, { "ActionsEaseTest", [](){return new ActionsEaseTestScene();} }, @@ -44,6 +42,7 @@ struct { #endif { "CurrentLanguageTest", []() { return new CurrentLanguageTestScene(); } }, { "DrawPrimitivesTest", [](){return new DrawPrimitivesTestScene();} }, + { "EventDispatcherTest", []() { return new EventDispatcherTestScene(); } }, { "EffectAdvancedTest", []() { return new EffectAdvanceScene(); } }, { "EffectsTest", [](){return new EffectTestScene();} }, { "ExtensionsTest", []() { return new ExtensionsTestScene(); } }, From 8fa969b39e9bf296d32c9828df8ce0e6d454cd84 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 22:20:20 +0800 Subject: [PATCH 16/20] issue #2087: Init Event::_userData. --- cocos2dx/event_dispatcher/CCEvent.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cocos2dx/event_dispatcher/CCEvent.cpp b/cocos2dx/event_dispatcher/CCEvent.cpp index 1dc9294d83..9dbe5030ca 100644 --- a/cocos2dx/event_dispatcher/CCEvent.cpp +++ b/cocos2dx/event_dispatcher/CCEvent.cpp @@ -30,6 +30,7 @@ Event::Event(const std::string& type) : _type(type) , _isStopped(false) , _currentTarget(nullptr) +, _userData(nullptr) { } From 3985574c302ecef1f38255c0610b2ae4832845a7 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 22:21:22 +0800 Subject: [PATCH 17/20] issue #2087: Bug fix in EventDispatcher: _isRegister fix. --- cocos2dx/event_dispatcher/CCEventDispatcher.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp index 4be2cc89c0..84b848ebe1 100644 --- a/cocos2dx/event_dispatcher/CCEventDispatcher.cpp +++ b/cocos2dx/event_dispatcher/CCEventDispatcher.cpp @@ -172,12 +172,11 @@ void EventDispatcher::removeEventListener(EventListener* listener) for (auto itemIter = iter->second->begin(); itemIter != iter->second->end(); ++itemIter) { if ((*itemIter)->listener == listener) - { - listener->retain(); - + { + (*itemIter)->listener->_isRegistered = false; + (*itemIter)->listener->release(); if (_inDispatch == 0) { - (*itemIter)->listener->release(); delete (*itemIter); iter->second->erase(itemIter); } @@ -207,11 +206,6 @@ void EventDispatcher::removeEventListener(EventListener* listener) if (isFound) break; } - - if (isFound) - { - listener->release(); - } } void EventDispatcher::setPriority(EventListener* listener, int fixedPriority) @@ -610,6 +604,7 @@ void EventDispatcher::removeListenersForEventType(const std::string& eventType) { for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter) { + (*iter)->listener->_isRegistered = false; (*iter)->listener->release(); if (_inDispatch) { @@ -637,6 +632,7 @@ void EventDispatcher::removeAllListeners() { for (auto iter = listenerItemIter->second->begin(); iter != listenerItemIter->second->end(); ++iter) { + (*iter)->listener->_isRegistered = false; (*iter)->listener->release(); if (_inDispatch) { From 32089fa517973db933102355fef60da56baebbfd Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 22:23:02 +0800 Subject: [PATCH 18/20] issue #2087: Adding new EventDispatcher Test. 1) RemoveListenerWhenDispatching 2) CustomEventTest 3) LabelKeyboardEventTest 4) SpriteAccelerationEventTest --- .../NewEventDispatcherTest.cpp | 237 +++++++++++++++++- .../NewEventDispatcherTest.h | 34 +++ 2 files changed, 267 insertions(+), 4 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index 093758227a..171b760536 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -7,13 +7,18 @@ // #include "NewEventDispatcherTest.h" +#include "testResource.h" namespace { std::function createFunctions[] = { CL(TouchableSpriteTest), - CL(FixedPriorityTest) + CL(FixedPriorityTest), + CL(RemoveListenerWhenDispatching), + CL(CustomEventTest), + CL(LabelKeyboardEventTest), + CL(SpriteAccelerationEventTest) }; unsigned int TEST_CASE_COUNT = sizeof(createFunctions) / sizeof(createFunctions[0]); @@ -148,7 +153,7 @@ void TouchableSpriteTest::onEnter() if (rect.containsPoint(locationInNode)) { - log("sprite tag %d, began... x = %f, y = %f", target->getTag(), locationInNode.x, locationInNode.y); + log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y); target->setOpacity(180); return true; } @@ -162,7 +167,7 @@ void TouchableSpriteTest::onEnter() listener1->onTouchEnded = [=](Touch* touch, Event* event){ auto target = static_cast(event->getCurrentTarget()); - log("sprite tag %d onTouchesEnded.. ", target->getTag()); + log("sprite onTouchesEnded.. "); target->setOpacity(255); if (target == sprite2) { @@ -186,7 +191,7 @@ std::string TouchableSpriteTest::title() std::string TouchableSpriteTest::subtitle() { - return ""; + return "Please drag the blocks"; } // FixedPriorityChangedTest @@ -299,3 +304,227 @@ std::string FixedPriorityTest::subtitle() { return "Fixed Priority, Blue: 30, Red: 20, Yellow: 10\n The lower value the higher priority will be."; } + +// RemoveListenerWhenDispatching +void RemoveListenerWhenDispatching::onEnter() +{ + EventDispatcherTestDemo::onEnter(); + + auto dispatcher = EventDispatcher::getInstance(); + + Point origin = Director::getInstance()->getVisibleOrigin(); + Size size = Director::getInstance()->getVisibleSize(); + + auto sprite1 = Sprite::create("Images/CyanSquare.png"); + sprite1->setPosition(origin+Point(size.width/2, size.height/2)); + addChild(sprite1, 10); + + // Make sprite1 touchable + auto listener1 = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE); + listener1->setSwallowTouches(true); + setUserObject(listener1); + + std::shared_ptr firstClick(new bool(true)); + + listener1->onTouchBegan = [=](Touch* touch, Event* event){ + Point locationInNode = sprite1->convertToNodeSpace(touch->getLocation()); + Size s = sprite1->getContentSize(); + Rect rect = Rect(0, 0, s.width, s.height); + + if (rect.containsPoint(locationInNode)) + { + sprite1->setColor(Color3B::RED); + return true; + } + return false; + }; + + listener1->onTouchEnded = [=](Touch* touch, Event* event){ + sprite1->setColor(Color3B::WHITE); + }; + + dispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); + + auto statusLabel = LabelTTF::create("The sprite could be touched!", "", 20); + statusLabel->setPosition(origin + Point(size.width/2, size.height-90)); + addChild(statusLabel); + std::shared_ptr enable(new bool(true)); + // Enable/Disable item + auto toggleItem = MenuItemToggle::createWithCallback([=](Object* sender){ + if (*enable) + { + dispatcher->removeEventListener(listener1); + statusLabel->setString("The sprite could not be touched!"); + + (*enable) = false; + } + else + { + dispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1); + statusLabel->setString("The sprite could be touched!"); + + (*enable) = true; + } + }, MenuItemFont::create("Enabled"), MenuItemFont::create("Disabled"), NULL); + + toggleItem->setPosition(origin + Point(size.width/2, 80)); + auto menu = Menu::create(toggleItem, nullptr); + menu->setPosition(Point(0, 0)); + menu->setAnchorPoint(Point(0, 0)); + addChild(menu, -1); +} + +std::string RemoveListenerWhenDispatching::title() +{ + return "Add and remove listener\n when dispatching event"; +} + +std::string RemoveListenerWhenDispatching::subtitle() +{ + return ""; +} + +// CustomEventTest +void CustomEventTest::onEnter() +{ + EventDispatcherTestDemo::onEnter(); + + auto dispatcher = EventDispatcher::getInstance(); + + Point origin = Director::getInstance()->getVisibleOrigin(); + Size size = Director::getInstance()->getVisibleSize(); + + auto statusLabel = LabelTTF::create("No custom event received!", "", 20); + statusLabel->setPosition(origin + Point(size.width/2, size.height-90)); + addChild(statusLabel); + + _listener = EventListener::create("game_custom_event", [=](Event* event){ + std::string str("Custom event received, "); + char* buf = static_cast(event->getUserData()); + str += buf; + str += " times"; + statusLabel->setString(str.c_str()); + delete[] buf; + }); + + dispatcher->addEventListenerWithFixedPriority(_listener, 1); + + auto sendItem = MenuItemFont::create("Send Custom Event", [=](Object* sender){ + static int count = 0; + ++count; + char* buf = new char[10]; + sprintf(buf, "%d", count); + Event event("game_custom_event"); + event.setUserData(buf); + dispatcher->dispatchEvent(&event); + }); + sendItem->setPosition(origin + Point(size.width/2, size.height/2)); + auto menu = Menu::create(sendItem, nullptr); + menu->setPosition(Point(0, 0)); + menu->setAnchorPoint(Point(0, 0)); + addChild(menu, -1); +} + +void CustomEventTest::onExit() +{ + EventDispatcher::getInstance()->removeEventListener(_listener); + EventDispatcherTestDemo::onExit(); +} + +std::string CustomEventTest::title() +{ + return "Send custom event"; +} + +std::string CustomEventTest::subtitle() +{ + return ""; +} + +// LabelKeyboardEventTest +void LabelKeyboardEventTest::onEnter() +{ + EventDispatcherTestDemo::onEnter(); + + auto dispatcher = EventDispatcher::getInstance(); + + Point origin = Director::getInstance()->getVisibleOrigin(); + Size size = Director::getInstance()->getVisibleSize(); + + auto statusLabel = LabelTTF::create("No keyboard event received!", "", 20); + statusLabel->setPosition(origin + Point(size.width/2, size.height/2)); + addChild(statusLabel); + + auto listener = KeyboardEventListener::create(); + listener->onKeyPressed = [](KeyboardEvent::KeyCode keyCode, Event* event){ + char buf[100] = {0}; + sprintf(buf, "Key %d was pressed!", (int)keyCode); + auto label = static_cast(event->getCurrentTarget()); + label->setString(buf); + }; + + listener->onKeyReleased = [](KeyboardEvent::KeyCode keyCode, Event* event){ + char buf[100] = {0}; + sprintf(buf, "Key %d was released!", (int)keyCode); + auto label = static_cast(event->getCurrentTarget()); + label->setString(buf); + }; + + dispatcher->addEventListenerWithSceneGraphPriority(listener, statusLabel); +} + +std::string LabelKeyboardEventTest::title() +{ + return "Label Receives Keyboard Event"; +} + +std::string LabelKeyboardEventTest::subtitle() +{ + return "Please click keyboard\n(Only available on Desktop and Android)"; +} + +// SpriteAccelerationEventTest +void SpriteAccelerationEventTest::onEnter() +{ +#define FIX_POS(_pos, _min, _max) \ +if (_pos < _min) \ +_pos = _min; \ +else if (_pos > _max) \ +_pos = _max; \ + + EventDispatcherTestDemo::onEnter(); + + auto dispatcher = EventDispatcher::getInstance(); + + Point origin = Director::getInstance()->getVisibleOrigin(); + Size size = Director::getInstance()->getVisibleSize(); + + auto sprite = Sprite::create(s_Ball); + sprite->setPosition(origin + Point(size.width/2, size.height/2)); + addChild(sprite); + + auto listener = AccelerationEventListener::create([=](Acceleration* acc, Event* event){ + auto ballSize = sprite->getContentSize(); + + auto ptNow = sprite->getPosition(); + + ptNow.x -= acc->x ; + ptNow.y -= acc->y ; + + FIX_POS(ptNow.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0)); + FIX_POS(ptNow.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0)); + sprite->setPosition(ptNow); + }); + + dispatcher->addEventListenerWithSceneGraphPriority(listener, sprite); +} + +std::string SpriteAccelerationEventTest::title() +{ + return "Sprite Receives Acceleration Event"; +} + +std::string SpriteAccelerationEventTest::subtitle() +{ + return "Please move your device\n(Only available on mobile)"; +} \ No newline at end of file diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h index a0a702215c..9a79ef8094 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h @@ -47,5 +47,39 @@ public: virtual std::string subtitle(); }; +class RemoveListenerWhenDispatching : public EventDispatcherTestDemo +{ +public: + virtual void onEnter(); + virtual std::string title(); + virtual std::string subtitle(); +}; + +class CustomEventTest : public EventDispatcherTestDemo +{ +public: + virtual void onEnter() override; + virtual void onExit() override; + virtual std::string title() override; + virtual std::string subtitle() override; +private: + EventListener* _listener; +}; + +class LabelKeyboardEventTest : public EventDispatcherTestDemo +{ +public: + virtual void onEnter() override; + virtual std::string title() override; + virtual std::string subtitle() override; +}; + +class SpriteAccelerationEventTest : public EventDispatcherTestDemo +{ +public: + virtual void onEnter() override; + virtual std::string title() override; + virtual std::string subtitle() override; +}; #endif /* defined(__samples__NewEventDispatcherTest__) */ From 8bb406a1ecdb106e9cecd2d17b3f299a9519ffb4 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 22:45:48 +0800 Subject: [PATCH 19/20] issue #2087: Enabling acc when testing it. --- .../NewEventDispatcherTest.cpp | 13 +++++++++++-- .../NewEventDispatcherTest/NewEventDispatcherTest.h | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp index 171b760536..8b22f3d6f5 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp @@ -499,6 +499,8 @@ _pos = _max; \ Point origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); + Device::setAccelerometerEnabled(true); + auto sprite = Sprite::create(s_Ball); sprite->setPosition(origin + Point(size.width/2, size.height/2)); addChild(sprite); @@ -508,8 +510,10 @@ _pos = _max; \ auto ptNow = sprite->getPosition(); - ptNow.x -= acc->x ; - ptNow.y -= acc->y ; + log("acc: x = %lf, y = %lf", acc->x, acc->y); + + ptNow.x += acc->x * 9.81f; + ptNow.y += acc->y * 9.81f; FIX_POS(ptNow.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0)); FIX_POS(ptNow.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0)); @@ -519,6 +523,11 @@ _pos = _max; \ dispatcher->addEventListenerWithSceneGraphPriority(listener, sprite); } +void SpriteAccelerationEventTest::onExit() +{ + Device::setAccelerometerEnabled(false); +} + std::string SpriteAccelerationEventTest::title() { return "Sprite Receives Acceleration Event"; diff --git a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h index 9a79ef8094..ff23ac330b 100644 --- a/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h +++ b/samples/Cpp/TestCpp/Classes/NewEventDispatcherTest/NewEventDispatcherTest.h @@ -78,6 +78,7 @@ class SpriteAccelerationEventTest : public EventDispatcherTestDemo { public: virtual void onEnter() override; + virtual void onExit() override; virtual std::string title() override; virtual std::string subtitle() override; }; From 4288160d509d439a4f113c913282c09e585ea60b Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 18 Sep 2013 22:47:50 +0800 Subject: [PATCH 20/20] issue #2087: Renaming EventDispatcherTest name to EventDispatcherTest(NEW) --- samples/Cpp/TestCpp/Classes/controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index 7717917111..fc2b73c985 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -42,7 +42,7 @@ struct { #endif { "CurrentLanguageTest", []() { return new CurrentLanguageTestScene(); } }, { "DrawPrimitivesTest", [](){return new DrawPrimitivesTestScene();} }, - { "EventDispatcherTest", []() { return new EventDispatcherTestScene(); } }, + { "EventDispatcherTest(NEW)", []() { return new EventDispatcherTestScene(); } }, { "EffectAdvancedTest", []() { return new EffectAdvanceScene(); } }, { "EffectsTest", [](){return new EffectTestScene();} }, { "ExtensionsTest", []() { return new ExtensionsTestScene(); } },