mirror of https://github.com/axmolengine/axmol.git
Merge pull request #5462 from heliclei/console-simulate-touch
closed #4146: Simulate touch events(tap,swipe) via CCConsole
This commit is contained in:
commit
a0192591fd
|
@ -82,7 +82,7 @@ static std::string &trim(std::string &s) {
|
|||
return ltrim(rtrim(s));
|
||||
}
|
||||
|
||||
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
|
||||
static std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
|
||||
std::stringstream ss(s);
|
||||
std::string item;
|
||||
while (std::getline(ss, item, delim)) {
|
||||
|
@ -92,13 +92,19 @@ std::vector<std::string> &split(const std::string &s, char delim, std::vector<st
|
|||
}
|
||||
|
||||
|
||||
std::vector<std::string> split(const std::string &s, char delim) {
|
||||
static std::vector<std::string> split(const std::string &s, char delim) {
|
||||
std::vector<std::string> 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 [touch help] to list supported directives", std::bind(&Console::commandTouch, this, std::placeholders::_1, std::placeholders::_2) },
|
||||
|
||||
};
|
||||
|
||||
|
@ -605,6 +612,147 @@ 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"
|
||||
"\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);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto argv = split(args,' ');
|
||||
|
||||
if(argv.size() == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(argv[0]=="tap")
|
||||
{
|
||||
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());
|
||||
|
||||
srand (time(NULL));
|
||||
_touchId = rand();
|
||||
Scheduler *sched = Director::getInstance()->getScheduler();
|
||||
sched->performFunctionInCocosThread( [&](){
|
||||
Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &_touchId, &x, &y);
|
||||
Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &x, &y);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
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::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();
|
||||
|
||||
Scheduler *sched = Director::getInstance()->getScheduler();
|
||||
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( [=](){
|
||||
float tempx = x2, tempy = y2;
|
||||
Director::getInstance()->getOpenGLView()->handleTouchesEnd(1, &_touchId, &tempx, &tempy);
|
||||
});
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
const char msg[] = "touch: invalid arguments.\n";
|
||||
send(fd, msg, sizeof(msg) - 1, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
bool Console::parseCommand(int fd)
|
||||
{
|
||||
char buf[512];
|
||||
|
|
|
@ -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<std::string> _DebugStrings;
|
||||
|
||||
int _touchId;
|
||||
private:
|
||||
CC_DISALLOW_COPY_AND_ASSIGN(Console);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue