void GLView::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, intptr_t ids[], float xs[], float ys[]) { intptr_t id = 0; float x = 0.0f; float y = 0.0f; EventTouch touchEvent; for (int i = 0; i < num; ++i) { id = ids[i]; x = xs[i]; y = ys[i]; auto iter = g_touchIdReorderMap.find(id); if (iter == g_touchIdReorderMap.end()) { CCLOG("if the index doesn't exist, it is an error"); continue; } /* Add to the set to send to the director */ Touch* touch = g_touches[iter->second]; if (touch) { CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", id, x, y); touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX, (y - _viewPortRect.origin.y) / _scaleY); touchEvent._touches.push_back(touch); g_touches[iter->second] = nullptr; removeUsedIndexBit(iter->second); g_touchIdReorderMap.erase(id); } else { CCLOG("Ending touches with id: %ld error", static_cast<long>(id)); return; } } if (touchEvent._touches.size() == 0) { CCLOG("touchesEnded or touchesCancel: size = 0"); return; } touchEvent._eventCode = eventCode; auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->dispatchEvent(&touchEvent); for (auto& touch : touchEvent._touches) { // release the touch object. touch->release(); } }
void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[], float fs[], float ms[]) { intptr_t id = 0; float x = 0.0f; float y = 0.0f; float force = 0.0f; float maxForce = 0.0f; EventTouch touchEvent; for (int i = 0; i < num; ++i) { id = ids[i]; x = xs[i]; y = ys[i]; force = fs ? fs[i] : 0.0f; maxForce = ms ? ms[i] : 0.0f; auto iter = g_touchIdReorderMap.find(id); if (iter == g_touchIdReorderMap.end()) { CCLOG("if the index doesn't exist, it is an error"); continue; } CCLOGINFO("Moving touches with id: %d, x=%f, y=%f, force=%f, maxFource=%f", id, x, y, force, maxForce); Touch* touch = g_touches[iter->second]; if (touch) { touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX, (y - _viewPortRect.origin.y) / _scaleY, force, maxForce); touchEvent._touches.push_back(touch); } else { // It is error, should return. CCLOG("Moving touches with id: %ld error", (long int)id); return; } } if (touchEvent._touches.size() == 0) { CCLOG("touchesMoved: size = 0"); return; } touchEvent._eventCode = EventTouch::EventCode::MOVED; auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->dispatchEvent(&touchEvent); }
void EGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[]) { int id = 0; float x = 0.0f; float y = 0.0f; int unusedIndex = 0; EventTouch touchEvent; for (int i = 0; i < num; ++i) { id = ids[i]; x = xs[i]; y = ys[i]; auto iter = g_touchIdReorderMap.find(id); unusedIndex = 0; // it is a new touch if (iter == g_touchIdReorderMap.end()) { unusedIndex = getUnUsedIndex(); // The touches is more than MAX_TOUCHES ? if (unusedIndex == -1) { CCLOG("The touches is more than MAX_TOUCHES, unusedIndex = %d", unusedIndex); continue; } Touch* touch = g_touches[unusedIndex] = new Touch(); touch->setTouchInfo(unusedIndex, (x - _viewPortRect.origin.x) / _scaleX, (y - _viewPortRect.origin.y) / _scaleY); CCLOGINFO("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y); g_touchIdReorderMap.insert(std::make_pair(id, unusedIndex)); touchEvent._touches.push_back(touch); } } if (touchEvent._touches.size() == 0) { CCLOG("touchesBegan: size = 0"); return; } touchEvent._eventCode = EventTouch::EventCode::BEGAN; auto dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->dispatchEvent(&touchEvent); }
void EGLViewProtocol::getSetOfTouchesEndOrCancel(Set& set, int num, int ids[], float xs[], float ys[]) { for (int i = 0; i < num; ++i) { int id = ids[i]; float x = xs[i]; float y = ys[i]; Integer* pIndex = (Integer*)s_TouchesIntergerDict.objectForKey(id); if (pIndex == NULL) { CCLOG("if the index doesn't exist, it is an error"); continue; } /* Add to the set to send to the director */ Touch* pTouch = s_pTouches[pIndex->getValue()]; if (pTouch) { CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", id, x, y); pTouch->setTouchInfo(pIndex->getValue(), (x - _viewPortRect.origin.x) / _scaleX, (y - _viewPortRect.origin.y) / _scaleY); set.addObject(pTouch); // release the object pTouch->release(); s_pTouches[pIndex->getValue()] = NULL; removeUsedIndexBit(pIndex->getValue()); s_TouchesIntergerDict.removeObjectForKey(id); } else { CCLOG("Ending touches with id: %d error", id); return; } } if (set.count() == 0) { CCLOG("touchesEnded or touchesCancel: count = 0"); return; } }
void EGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float ys[]) { Set set; for (int i = 0; i < num; ++i) { int id = ids[i]; float x = xs[i]; float y = ys[i]; Integer* pIndex = (Integer*)s_TouchesIntergerDict.objectForKey(id); int nUnusedIndex = 0; // it is a new touch if (pIndex == NULL) { nUnusedIndex = getUnUsedIndex(); // The touches is more than MAX_TOUCHES ? if (nUnusedIndex == -1) { CCLOG("The touches is more than MAX_TOUCHES, nUnusedIndex = %d", nUnusedIndex); continue; } Touch* pTouch = s_pTouches[nUnusedIndex] = new Touch(); pTouch->setTouchInfo(nUnusedIndex, (x - _viewPortRect.origin.x) / _scaleX, (y - _viewPortRect.origin.y) / _scaleY); //CCLOG("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y); Integer* pInterObj = new Integer(nUnusedIndex); s_TouchesIntergerDict.setObject(pInterObj, id); set.addObject(pTouch); pInterObj->release(); } } if (set.count() == 0) { CCLOG("touchesBegan: count = 0"); return; } _delegate->touchesBegan(&set, NULL); }
void EGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float ys[]) { Set set; for (int i = 0; i < num; ++i) { int id = ids[i]; float x = xs[i]; float y = ys[i]; Integer* pIndex = (Integer*)s_TouchesIntergerDict.objectForKey(id); if (pIndex == NULL) { CCLOG("if the index doesn't exist, it is an error"); continue; } CCLOGINFO("Moving touches with id: %d, x=%f, y=%f", id, x, y); Touch* pTouch = s_pTouches[pIndex->getValue()]; if (pTouch) { pTouch->setTouchInfo(pIndex->getValue(), (x - _viewPortRect.origin.x) / _scaleX, (y - _viewPortRect.origin.y) / _scaleY); set.addObject(pTouch); } else { // It is error, should return. CCLOG("Moving touches with id: %d error", id); return; } } if (set.count() == 0) { CCLOG("touchesMoved: count = 0"); return; } _delegate->touchesMoved(&set, NULL); }
void TouchEventDispatchingPerfTest::generateTestFunctions() { TestFunction testFunctions[] = { { "OneByOne-scenegraph", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [](Touch* touch, Event* event){ return false; }; listener->onTouchMoved = [](Touch* touch, Event* event){}; listener->onTouchEnded = [](Touch* touch, Event* event){}; // Create new touchable nodes for (int i = 0; i < this->_quantityOfNodes; ++i) { auto node = Node::create(); node->setTag(1000 + i); this->addChild(node); this->_nodes.push_back(node); dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , { "OneByOne-fixed", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [](Touch* touch, Event* event){ return false; }; listener->onTouchMoved = [](Touch* touch, Event* event){}; listener->onTouchEnded = [](Touch* touch, Event* event){}; for (int i = 0; i < this->_quantityOfNodes; ++i) { auto l = listener->clone(); this->_fixedPriorityListeners.push_back(l); dispatcher->addEventListenerWithFixedPriority(l, i+1); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , { "AllAtOnce-scenegraph", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){}; listener->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){}; listener->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){}; // Create new touchable nodes for (int i = 0; i < this->_quantityOfNodes; ++i) { auto node = Node::create(); node->setTag(1000 + i); this->addChild(node); this->_nodes.push_back(node); dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , { "AllAtOnce-fixed", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){}; listener->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){}; listener->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){}; for (int i = 0; i < this->_quantityOfNodes; ++i) { auto l = listener->clone(); this->_fixedPriorityListeners.push_back(l); dispatcher->addEventListenerWithFixedPriority(l, i+1); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , { "TouchModeMix-scenegraph", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listenerOneByOne = EventListenerTouchOneByOne::create(); listenerOneByOne->onTouchBegan = [](Touch* touch, Event* event){ return false; }; listenerOneByOne->onTouchMoved = [](Touch* touch, Event* event){}; listenerOneByOne->onTouchEnded = [](Touch* touch, Event* event){}; auto listenerAllAtOnce = EventListenerTouchAllAtOnce::create(); listenerAllAtOnce->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){}; listenerAllAtOnce->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){}; listenerAllAtOnce->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){}; int i = 0; // Create new touchable nodes for (; i < this->_quantityOfNodes/2; ++i) { auto node = Node::create(); node->setTag(1000 + i); this->addChild(node); this->_nodes.push_back(node); dispatcher->addEventListenerWithSceneGraphPriority(listenerOneByOne->clone(), node); } for (; i < this->_quantityOfNodes; ++i) { auto node = Node::create(); node->setTag(1000 + i); this->addChild(node); this->_nodes.push_back(node); dispatcher->addEventListenerWithSceneGraphPriority(listenerAllAtOnce->clone(), node); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , { "TouchModeMix-fixed", [=](){ auto dispatcher = Director::getInstance()->getEventDispatcher(); if (_quantityOfNodes != _lastRenderedCount) { auto listenerOneByOne = EventListenerTouchOneByOne::create(); listenerOneByOne->onTouchBegan = [](Touch* touch, Event* event){ return false; }; listenerOneByOne->onTouchMoved = [](Touch* touch, Event* event){}; listenerOneByOne->onTouchEnded = [](Touch* touch, Event* event){}; auto listenerAllAtOnce = EventListenerTouchAllAtOnce::create(); listenerAllAtOnce->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){}; listenerAllAtOnce->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){}; listenerAllAtOnce->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){}; int i = 0; for (; i < this->_quantityOfNodes/2; ++i) { auto l = listenerOneByOne->clone(); this->_fixedPriorityListeners.push_back(l); dispatcher->addEventListenerWithFixedPriority(l, i+1); } for (; i < this->_quantityOfNodes; ++i) { auto l = listenerAllAtOnce->clone(); this->_fixedPriorityListeners.push_back(l); dispatcher->addEventListenerWithFixedPriority(l, i+1); } _lastRenderedCount = _quantityOfNodes; } Size size = Director::getInstance()->getWinSize(); EventTouch touchEvent; touchEvent.setEventCode(EventTouch::EventCode::BEGAN); std::vector<Touch*> touches; for (int i = 0; i < 4; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->autorelease(); touch->setTouchInfo(i, rand() % 200, rand() % 200); touches.push_back(touch); } touchEvent.setTouches(touches); CC_PROFILER_START(this->profilerName()); dispatcher->dispatchEvent(&touchEvent); CC_PROFILER_STOP(this->profilerName()); } } , }; for (const auto& func : testFunctions) { _testFunctions.push_back(func); } }
void TouchesPerformTest3::onEnter() { PerformBasicLayer::onEnter(); auto s = Director::getInstance()->getWinSize(); // add title auto label = Label::createWithTTF(title().c_str(), "fonts/arial.ttf", 32); addChild(label, 1); label->setPosition(Vec2(s.width/2, s.height-50)); #define TOUCH_PROFILER_NAME "TouchProfileName" #define TOUCHABLE_NODE_NUM 1000 srand((unsigned)time(nullptr)); for (int i = 0; i < TOUCHABLE_NODE_NUM; ++i) { int zorder = rand() % TOUCHABLE_NODE_NUM; auto layer = new (std::nothrow) TouchableLayer(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(TouchableLayer::onTouchBegan, layer); listener->onTouchMoved = CC_CALLBACK_2(TouchableLayer::onTouchMoved, layer); listener->onTouchEnded = CC_CALLBACK_2(TouchableLayer::onTouchEnded, layer); listener->onTouchCancelled = CC_CALLBACK_2(TouchableLayer::onTouchCancelled, layer); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, layer); addChild(layer, zorder); layer->release(); } auto emitEventlabel = Label::createWithSystemFont("Emit Touch Event", "", 24); auto menuItem = MenuItemLabel::create(emitEventlabel, [this](Ref* sender){ CC_PROFILER_PURGE_ALL(); std::vector<Touch*> touches; for (int i = 0; i < EventTouch::MAX_TOUCHES; ++i) { Touch* touch = new (std::nothrow) Touch(); touch->setTouchInfo(i, 10, (i+1) * 10); touches.push_back(touch); } EventTouch event; event.setEventCode(EventTouch::EventCode::BEGAN); event.setTouches(touches); for (int i = 0; i < 100; ++i) { CC_PROFILER_START(TOUCH_PROFILER_NAME); _eventDispatcher->dispatchEvent(&event); CC_PROFILER_STOP(TOUCH_PROFILER_NAME); } CC_PROFILER_DISPLAY_TIMERS(); for (auto& touch : touches) { touch->release(); } }); menuItem->setPosition(Vec2(0, -20)); auto menu = Menu::create(menuItem, nullptr); addChild(menu); }
bool TouchPool::handleTouch(TouchTrigger::TouchAction action,int num,intptr_t ids[],float xs[],float ys[]) { intptr_t id = 0; float x = 0.0f; float y = 0.0f; int unusedIndex = 0; TouchTrigger touchTrigger; for(int i=0;i<num;i++) { id = ids[i]; x = xs[i]; y = ys[i]; auto iter = _touchIdReorderMap.find(id); // it is a new touch if (iter == _touchIdReorderMap.end()) { if(action != TouchTrigger::TouchAction::DOWN) { FKLOG("if the index doesn't exist, it is an error"); continue; } FKLOG("id = %ld",(long int) id); unusedIndex = getUnUsedIndex(); // The touches is more than MAX_TOUCHES ? if (unusedIndex == -1) { FKLOG("The touches is more than MAX_TOUCHES, unusedIndex = %d", unusedIndex); continue; } Touch* touch = _touches[unusedIndex] = new (std::nothrow) Touch(); touch->setTouchInfo(unusedIndex, x, y); FKLOG("x = %f y = %f", touch->getLocationInView().x, touch->getLocationInView().y); _touchIdReorderMap.insert(std::make_pair(id, unusedIndex)); touchTrigger._touches.push_back(touch); } else { FKLOG("unusedindex = %d", iter->second); FKLOG("other x = %f y = %f", x, y); Touch* touch = _touches[iter->second]; if (touch) { touch->setTouchInfo(iter->second, x, y); touchTrigger._touches.push_back(touch); if(action == TouchTrigger::TouchAction::UP || action == TouchTrigger::TouchAction::CANCEL) { _touches[iter->second] = nullptr; removeUsedIndexBit(iter->second); _touchIdReorderMap.erase(id); } } else { // It is error, should return. FKLOG("Moving touches with id: %ld error", (long int)id); return false; } } } if (touchTrigger._touches.size() == 0) { FKLOG("touches: size = 0"); return false; } touchTrigger.setAction(action); bool result = dispatchTouch(&touchTrigger); if(action == TouchTrigger::TouchAction::UP || action == TouchTrigger::TouchAction::CANCEL) { for (auto& touch : touchTrigger._touches) { // release the touch object. touch->release(); } } return result; }
void TouchesPerformTest3::onEnter() { PerformBasicLayer::onEnter(); auto s = Director::getInstance()->getWinSize(); // add title auto label = LabelTTF::create(title().c_str(), "Arial", 32); addChild(label, 1); label->setPosition(Point(s.width/2, s.height-50)); #define TOUCH_PROFILER_NAME "TouchProfileName" #define TOUCHABLE_NODE_NUM 1000 srand(time(nullptr)); for (int i = 0; i < TOUCHABLE_NODE_NUM; ++i) { int zorder = rand() % TOUCHABLE_NODE_NUM; auto layer = new TouchableLayer(); layer->setTouchEnabled(true); layer->setTouchMode(Touch::DispatchMode::ONE_BY_ONE); addChild(layer, zorder); layer->release(); } auto emitEventlabel = LabelTTF::create("Emit Touch Event", "", 24); auto menuItem = MenuItemLabel::create(emitEventlabel, [](Object* sender){ CC_PROFILER_PURGE_ALL(); std::vector<Touch*> touches; for (int i = 0; i < EventTouch::MAX_TOUCHES; ++i) { Touch* touch = new Touch(); touch->setTouchInfo(i, 10, (i+1) * 10); touches.push_back(touch); } EventTouch event; event.setEventCode(EventTouch::EventCode::BEGAN); event.setTouches(touches); auto dispatcher = EventDispatcher::getInstance(); for (int i = 0; i < 100; ++i) { CC_PROFILER_START(TOUCH_PROFILER_NAME); dispatcher->dispatchEvent(&event, false); CC_PROFILER_STOP(TOUCH_PROFILER_NAME); } CC_PROFILER_DISPLAY_TIMERS(); for (auto& touch : touches) { touch->release(); } }); menuItem->setPosition(Point(0, -20)); auto menu = Menu::create(menuItem, NULL); addChild(menu); }