From 786aae219910e28bac1be211c68d74ee759d33ea Mon Sep 17 00:00:00 2001 From: heliclei Date: Tue, 25 Feb 2014 11:04:59 +0800 Subject: [PATCH 1/7] add touch move support --- cocos/base/CCConsole.cpp | 82 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/cocos/base/CCConsole.cpp b/cocos/base/CCConsole.cpp index e3b2d05c37..1553b71cee 100644 --- a/cocos/base/CCConsole.cpp +++ b/cocos/base/CCConsole.cpp @@ -82,7 +82,7 @@ static std::string &trim(std::string &s) { return ltrim(rtrim(s)); } -std::vector &split(const std::string &s, char delim, std::vector &elems) { +static std::vector &split(const std::string &s, char delim, std::vector &elems) { std::stringstream ss(s); std::string item; while (std::getline(ss, item, delim)) { @@ -92,13 +92,19 @@ std::vector &split(const std::string &s, char delim, std::vector split(const std::string &s, char delim) { +static std::vector split(const std::string &s, char delim) { std::vector elems; split(s, delim, elems); return elems; } - - +//isFloat taken from http://stackoverflow.com/questions/447206/c-isfloat-function +static bool isFloat( std::string myString ) { + std::istringstream iss(myString); + float f; + iss >> std::noskipws >> f; // noskipws considers leading whitespace invalid + // Check the entire string was consumed and if either failbit or badbit is set + return iss.eof() && !iss.fail(); +} // helper free functions // dprintf() is not defined in Android @@ -274,6 +280,7 @@ Console::Console() { "scenegraph", "Print the scene graph", std::bind(&Console::commandSceneGraph, this, std::placeholders::_1, std::placeholders::_2) }, { "texture", "Flush or print the TextureCache info. Args: [flush | ] ", std::bind(&Console::commandTextures, this, std::placeholders::_1, std::placeholders::_2) }, { "director", "director commands, type -h or [director help] to list supported directives", std::bind(&Console::commandDirector, this, std::placeholders::_1, std::placeholders::_2) }, + { "touch", "simulate touch event via console, type -h or [director help] to list supported directives", std::bind(&Console::commandTouch, this, std::placeholders::_1, std::placeholders::_2) }, }; @@ -575,7 +582,7 @@ void Console::commandDirector(int fd, const std::string& args) "\tresume, resume all scheduled timers\n" "\tstop, Stops the animation. Nothing will be drawn.\n" "\tstart, Restart the animation again, Call this function only if [director stop] was called earlier\n"; - write(fd, help, sizeof(help) - 1); + send(fd, help, sizeof(help) - 1,0); } else if(args == "pause") { @@ -605,6 +612,71 @@ void Console::commandDirector(int fd, const std::string& args) } +void Console::commandTouch(int fd, const std::string& args) +{ + if(args =="help" || args == "-h") + { + const char help[] = "available touch directives:\n" + "\tbegin x y: simulate touch begin event\n" + "\tend x y: simulate touch end event\n" + "\tswipe x1 y1 x2 y2: simulate touch swipe from (x1,y1) to (x2,y2).\n"; + send(fd, help, sizeof(help) - 1,0); + } + else + { + auto argv = split(args,' '); + + if(argv.size() == 0) + { + return; + } + + if(argv[0]=="begin") + { + if((argv.size() == 3) && (isFloat(argv[1]) && isFloat(argv[2]))) + { + + float x = std::stof(argv[1]); + float y = std::stof(argv[2]); + + srand (time(NULL)); + _touchId = rand(); + Scheduler *sched = Director::getInstance()->getScheduler(); + sched->performFunctionInCocosThread( [&](){ + Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &_touchId, &x, &y); + }); + } + else + { + const char msg[] = "touch: invalid arguments.\n"; + send(fd, msg, sizeof(msg) - 1, 0); + } + return; + } + + if(argv[0]=="end") + { + if((argv.size() == 3) && (isFloat(argv[1])) && (isFloat(argv[2]))) + { + + float x = std::stof(argv[1]); + float y = std::stof(argv[2]); + + Scheduler *sched = Director::getInstance()->getScheduler(); + sched->performFunctionInCocosThread( [&](){ + Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &x, &y); + }); + } + else + { + const char msg[] = "touch: invalid arguments.\n"; + send(fd, msg, sizeof(msg) - 1, 0); + } + + } + } +} + bool Console::parseCommand(int fd) { char buf[512]; From e09a72f26dd359aac6973e3f297e7ceb08dc0d5e Mon Sep 17 00:00:00 2001 From: heliclei Date: Tue, 25 Feb 2014 11:47:53 +0800 Subject: [PATCH 2/7] add touch swipe --- cocos/base/CCConsole.cpp | 35 +++++++++++++++++++++++++++++++++++ cocos/base/CCConsole.h | 2 ++ 2 files changed, 37 insertions(+) diff --git a/cocos/base/CCConsole.cpp b/cocos/base/CCConsole.cpp index 1553b71cee..c79a765e60 100644 --- a/cocos/base/CCConsole.cpp +++ b/cocos/base/CCConsole.cpp @@ -672,8 +672,43 @@ void Console::commandTouch(int fd, const std::string& args) const char msg[] = "touch: invalid arguments.\n"; send(fd, msg, sizeof(msg) - 1, 0); } + return; + } + + if(argv[0]=="swipe") + { + if((argv.size() == 5) + && (isFloat(argv[1])) && (isFloat(argv[2])) + && (isFloat(argv[3])) && (isFloat(argv[4]))) + { + + float x1 = std::stof(argv[1]); + float y1 = std::stof(argv[2]); + float x2 = std::stof(argv[3]); + float y2 = std::stof(argv[4]); + + srand (time(NULL)); + _touchId = rand(); + + Scheduler *sched = Director::getInstance()->getScheduler(); + sched->performFunctionInCocosThread( [&](){ + Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &_touchId, &x1, &y1); + }); + + float dx = std::abs(x1 - x2); + float dy = std::abs(y1 - y2); + + float dt = dx>dy?dx:dy; + + } + else + { + const char msg[] = "touch: invalid arguments.\n"; + send(fd, msg, sizeof(msg) - 1, 0); + } } + } } diff --git a/cocos/base/CCConsole.h b/cocos/base/CCConsole.h index 7f39a4b73b..b8013f0fe1 100644 --- a/cocos/base/CCConsole.h +++ b/cocos/base/CCConsole.h @@ -114,6 +114,7 @@ protected: void commandResolution(int fd, const std::string &args); void commandProjection(int fd, const std::string &args); void commandDirector(int fd, const std::string &args); + void commandTouch(int fd, const std::string &args); // file descriptor: socket, console, etc. int _listenfd; int _maxfd; @@ -132,6 +133,7 @@ protected: std::mutex _DebugStringsMutex; std::vector _DebugStrings; + int _touchId; private: CC_DISALLOW_COPY_AND_ASSIGN(Console); }; From a7d636f7bf14b3bac36814f5c5633b6192d2656a Mon Sep 17 00:00:00 2001 From: heliclei Date: Tue, 25 Feb 2014 15:52:37 +0800 Subject: [PATCH 3/7] support touch swipe directive --- cocos/base/CCConsole.cpp | 69 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/cocos/base/CCConsole.cpp b/cocos/base/CCConsole.cpp index c79a765e60..5bda8a3f3f 100644 --- a/cocos/base/CCConsole.cpp +++ b/cocos/base/CCConsole.cpp @@ -691,15 +691,76 @@ void Console::commandTouch(int fd, const std::string& args) _touchId = rand(); Scheduler *sched = Director::getInstance()->getScheduler(); - sched->performFunctionInCocosThread( [&](){ - Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &_touchId, &x1, &y1); + sched->performFunctionInCocosThread( [=](){ + float tempx = x1, tempy = y1; + Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &_touchId, &tempx, &tempy); }); float dx = std::abs(x1 - x2); float dy = std::abs(y1 - y2); + float _x_ = x1, _y_ = y1; + if(dx > dy) + { + while(dx > 1) + { + + if(x1 < x2) + { + _x_ += 1; + } + if(x1 > x2) + { + _x_ -= 1; + } + if(y1 < y2) + { + _y_ += dy/dx; + } + if(y1 > y2) + { + _y_ -= dy/dx; + } + sched->performFunctionInCocosThread( [=](){ + float tempx = _x_, tempy = _y_; + Director::getInstance()->getOpenGLView()->handleTouchesMove(1, &_touchId, &tempx, &tempy); + }); + dx -= 1; + } + + } + else + { + while(dy > 1) + { + if(x1 < x2) + { + _x_ += dx/dy; + } + if(x1 > x2) + { + _x_ -= dx/dy; + } + if(y1 < y2) + { + _y_ += 1; + } + if(y1 > y2) + { + _y_ -= 1; + } + sched->performFunctionInCocosThread( [=](){ + float tempx = _x_, tempy = _y_; + Director::getInstance()->getOpenGLView()->handleTouchesMove(1, &_touchId, &tempx, &tempy); + }); + dy -= 1; + } + + } + + sched->performFunctionInCocosThread( [&](){ + Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &x2, &y2); + }); - float dt = dx>dy?dx:dy; - } else { From f1376e7cafea60a51465be32fc3237d96f99ea7c Mon Sep 17 00:00:00 2001 From: heliclei Date: Tue, 25 Feb 2014 15:55:01 +0800 Subject: [PATCH 4/7] fix help typo --- cocos/base/CCConsole.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/base/CCConsole.cpp b/cocos/base/CCConsole.cpp index 5bda8a3f3f..08040fa95e 100644 --- a/cocos/base/CCConsole.cpp +++ b/cocos/base/CCConsole.cpp @@ -280,7 +280,7 @@ Console::Console() { "scenegraph", "Print the scene graph", std::bind(&Console::commandSceneGraph, this, std::placeholders::_1, std::placeholders::_2) }, { "texture", "Flush or print the TextureCache info. Args: [flush | ] ", std::bind(&Console::commandTextures, this, std::placeholders::_1, std::placeholders::_2) }, { "director", "director commands, type -h or [director help] to list supported directives", std::bind(&Console::commandDirector, this, std::placeholders::_1, std::placeholders::_2) }, - { "touch", "simulate touch event via console, type -h or [director help] to list supported directives", std::bind(&Console::commandTouch, this, std::placeholders::_1, std::placeholders::_2) }, + { "touch", "simulate touch event via console, type -h or [touch help] to list supported directives", std::bind(&Console::commandTouch, this, std::placeholders::_1, std::placeholders::_2) }, }; From 7f3aab2bfdd3b59a143a6b28621e9576a0f2eb19 Mon Sep 17 00:00:00 2001 From: heliclei Date: Tue, 25 Feb 2014 15:59:43 +0800 Subject: [PATCH 5/7] fix touchEnd lambda --- cocos/base/CCConsole.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cocos/base/CCConsole.cpp b/cocos/base/CCConsole.cpp index 08040fa95e..7a9628c6c4 100644 --- a/cocos/base/CCConsole.cpp +++ b/cocos/base/CCConsole.cpp @@ -757,8 +757,9 @@ void Console::commandTouch(int fd, const std::string& args) } - sched->performFunctionInCocosThread( [&](){ - Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &x2, &y2); + sched->performFunctionInCocosThread( [=](){ + float tempx = x2, tempy = y2; + Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &tempx, &tempy); }); } From 6848e6dde068b6c1280e504ae71a49751abe8385 Mon Sep 17 00:00:00 2001 From: heliclei Date: Tue, 25 Feb 2014 16:31:21 +0800 Subject: [PATCH 6/7] use atof instead of stof to fix compile error on android --- cocos/base/CCConsole.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cocos/base/CCConsole.cpp b/cocos/base/CCConsole.cpp index 7a9628c6c4..b1be933be4 100644 --- a/cocos/base/CCConsole.cpp +++ b/cocos/base/CCConsole.cpp @@ -636,8 +636,8 @@ void Console::commandTouch(int fd, const std::string& args) if((argv.size() == 3) && (isFloat(argv[1]) && isFloat(argv[2]))) { - float x = std::stof(argv[1]); - float y = std::stof(argv[2]); + float x = std::atof(argv[1].c_str()); + float y = std::atof(argv[2].c_str()); srand (time(NULL)); _touchId = rand(); @@ -659,8 +659,8 @@ void Console::commandTouch(int fd, const std::string& args) if((argv.size() == 3) && (isFloat(argv[1])) && (isFloat(argv[2]))) { - float x = std::stof(argv[1]); - float y = std::stof(argv[2]); + float x = std::atof(argv[1].c_str()); + float y = std::atof(argv[2].c_str()); Scheduler *sched = Director::getInstance()->getScheduler(); sched->performFunctionInCocosThread( [&](){ @@ -682,10 +682,10 @@ void Console::commandTouch(int fd, const std::string& args) && (isFloat(argv[3])) && (isFloat(argv[4]))) { - float x1 = std::stof(argv[1]); - float y1 = std::stof(argv[2]); - float x2 = std::stof(argv[3]); - float y2 = std::stof(argv[4]); + float x1 = std::atof(argv[1].c_str()); + float y1 = std::atof(argv[2].c_str()); + float x2 = std::atof(argv[3].c_str()); + float y2 = std::atof(argv[4].c_str()); srand (time(NULL)); _touchId = rand(); From 1dc6d1ad21726bfb51f01e419b84ea66069e1af2 Mon Sep 17 00:00:00 2001 From: heliclei Date: Tue, 25 Feb 2014 18:17:43 +0800 Subject: [PATCH 7/7] merge begin & end with one 'tap' command --- cocos/base/CCConsole.cpp | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/cocos/base/CCConsole.cpp b/cocos/base/CCConsole.cpp index b1be933be4..40e4ea075c 100644 --- a/cocos/base/CCConsole.cpp +++ b/cocos/base/CCConsole.cpp @@ -617,8 +617,7 @@ void Console::commandTouch(int fd, const std::string& args) if(args =="help" || args == "-h") { const char help[] = "available touch directives:\n" - "\tbegin x y: simulate touch begin event\n" - "\tend x y: simulate touch end event\n" + "\ttap x y: simulate touch tap at (x,y)\n" "\tswipe x1 y1 x2 y2: simulate touch swipe from (x1,y1) to (x2,y2).\n"; send(fd, help, sizeof(help) - 1,0); } @@ -631,7 +630,7 @@ void Console::commandTouch(int fd, const std::string& args) return; } - if(argv[0]=="begin") + if(argv[0]=="tap") { if((argv.size() == 3) && (isFloat(argv[1]) && isFloat(argv[2]))) { @@ -644,26 +643,6 @@ void Console::commandTouch(int fd, const std::string& args) Scheduler *sched = Director::getInstance()->getScheduler(); sched->performFunctionInCocosThread( [&](){ Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &_touchId, &x, &y); - }); - } - else - { - const char msg[] = "touch: invalid arguments.\n"; - send(fd, msg, sizeof(msg) - 1, 0); - } - return; - } - - if(argv[0]=="end") - { - if((argv.size() == 3) && (isFloat(argv[1])) && (isFloat(argv[2]))) - { - - float x = std::atof(argv[1].c_str()); - float y = std::atof(argv[2].c_str()); - - Scheduler *sched = Director::getInstance()->getScheduler(); - sched->performFunctionInCocosThread( [&](){ Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &x, &y); }); }