void LuaTouchEventManager::onTouchesCancelled(const std::vector<Touch*>& touches, Event *event) { dispatchingTouchEvent(touches, event, CCTOUCHCANCELLED); // remove all touching nodes // CCLOG("TOUCH CANCELLED, REMOVE ALL TOUCH TARGETS"); _touchingTargets.clear(); }
void LuaTouchEventManager::onTouchesEnded(const std::vector<Touch*>& touches, Event *event) { for (auto it = touches.begin(); it != touches.end(); ++it) { m_touchingIds.erase(((Touch*)*it)->getID()); } if (m_touchingIds.size()) { dispatchingTouchEvent(touches, event, CCTOUCHREMOVED); } else { dispatchingTouchEvent(touches, event, CCTOUCHENDED); // remove all touching nodes //CCLOG("TOUCH ENDED, REMOVE ALL TOUCH TARGETS"); _touchingTargets.clear(); } }
void LuaTouchEventManager::onTouchesMoved(const std::vector<Touch*>& touches, Event *event) { dispatchingTouchEvent(touches, event, CCTOUCHMOVED); }
void LuaTouchEventManager::onTouchesBegan(const std::vector<Touch*>& touches, Event *event) { if (!m_touchDispatchingEnabled || _touchableNodes.size() < 1) return; // save touches id for (auto it = touches.begin(); it != touches.end(); ++it) { m_touchingIds.insert(((Touch*)*it)->getID()); } // check current in touching if (_touchingTargets.size()) { dispatchingTouchEvent(touches, event, CCTOUCHADDED); return; } // start new touching event // sort touchable nodes sortAllTouchableNodes(_touchableNodes); // find touching target bool isTouchable = true; LuaEventNode *node = nullptr; LuaEventNode *checkTouchableNode = nullptr; LuaTouchTargetNode *touchTarget = nullptr; for (auto iter = _touchableNodes.begin(); iter != _touchableNodes.end(); ++iter) { checkTouchableNode = node = *iter; // check node is visible and capturing enabled isTouchable = true; do { isTouchable = isTouchable && checkTouchableNode->isRunning() && checkTouchableNode->isVisible() && checkTouchableNode->isTouchCaptureEnabled(); checkTouchableNode = checkTouchableNode->getParent(); } while (checkTouchableNode && isTouchable); if (!isTouchable) continue; // prepare for touch testing touchTarget = nullptr; Node *activeNode = node->getActiveNode(); if (!activeNode) continue; const Rect boundingBox = utils::getCascadeBoundingBox(activeNode); // set touch target Touch *touch = nullptr; for (auto it = touches.begin(); it != touches.end(); ++it) { touch = (Touch*)*it; const Point touchPoint = touch->getLocation(); if (boundingBox.containsPoint(touchPoint)) { if (!touchTarget) { touchTarget = LuaTouchTargetNode::create(node); } if (touchTarget->getTouchMode() == LuaEventNode::modeTouchesOneByOne) { touchTarget->setTouchId(touch->getID()); break; } } } if (!touchTarget) { // touch points not in current target, try to next continue; } // try to dispatching event Vector<LuaEventNode*> path(10); node = touchTarget->getNode(); do { path.pushBack(node); node = node->getParent(); } while (node != nullptr /*&& node != this*/); // phase: capturing // from parent to child bool dispatchingContinue = true; int touchMode = touchTarget->getTouchMode(); for (long i = path.size() - 1; dispatchingContinue && i >= 0; --i) { node = path.at(i); if (touchMode == LuaEventNode::modeTouchesAllAtOnce) { node->ccTouchesCaptureBegan(touches, touchTarget->getNode()); } else { dispatchingContinue = node->ccTouchCaptureBegan(touchTarget->findTouch(touches), touchTarget->getNode()); } } if (!dispatchingContinue) { // the target stop dispatching, try to next continue; } // phase: targeting node = touchTarget->getNode(); bool ret = true; if (touchMode == LuaEventNode::modeTouchesAllAtOnce) { node->ccTouchesBegan(touches, event); } else { ret = node->ccTouchBegan(touchTarget->findTouch(touches), event); } if (ret) { _touchingTargets.pushBack(touchTarget); //CCLOG("ADD TOUCH TARGET [%p]", touchTarget->getNode()->getNode()); } if (node->isTouchSwallowEnabled()) { // target swallow touch event, stop dispatching break; } // continue dispatching, try to next } }
void CCScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) { if (!m_touchDispatchingEnabled) return; // save touches id for (CCSetIterator it = pTouches->begin(); it != pTouches->end(); ++it) { m_touchingIds.insert(((CCTouch*)*it)->getID()); } // check current in touching if (m_touchingTargets->count()) { dispatchingTouchEvent(pTouches, pEvent, CCTOUCHADDED); return; } // start new touching event // sort touchable nodes sortAllTouchableNodes(m_touchableNodes); // find touching target bool isTouchable = true; CCObject *obj = NULL; CCNode *node = NULL; CCNode *checkTouchableNode = NULL; CCTouchTargetNode *touchTarget = NULL; CCARRAY_FOREACH(m_touchableNodes, obj) { checkTouchableNode = node = dynamic_cast<CCNode*>(obj); // check node is visible and capturing enabled isTouchable = true; do { isTouchable = isTouchable && checkTouchableNode->isRunning() && checkTouchableNode->isVisible() && checkTouchableNode->isTouchCaptureEnabled(); checkTouchableNode = checkTouchableNode->getParent(); } while (checkTouchableNode && isTouchable); if (!isTouchable) continue; // prepare for touch testing touchTarget = NULL; const CCRect boundingBox = node->getCascadeBoundingBox(); // set touch target CCTouch *touch = NULL; for (CCSetIterator it = pTouches->begin(); it != pTouches->end(); ++it) { touch = (CCTouch*)*it; const CCPoint touchPoint = touch->getLocation(); if (boundingBox.containsPoint(touchPoint)) { if (!touchTarget) { touchTarget = CCTouchTargetNode::create(node); } if (touchTarget->getTouchMode() == kCCTouchesOneByOne) { touchTarget->setTouchId(touch->getID()); break; } } } if (!touchTarget) { // touch points not in current target, try to next continue; } // try to dispatching event CCArray *path = CCArray::createWithCapacity(10); node = touchTarget->getNode(); do { path->addObject(node); node = node->getParent(); } while (node != NULL && node != this); // phase: capturing // from parent to child bool dispatchingContinue = true; int touchMode = touchTarget->getTouchMode(); for (int i = path->count() - 1; dispatchingContinue && i >= 0; --i) { node = dynamic_cast<CCNode*>(path->objectAtIndex(i)); if (touchMode == kCCTouchesAllAtOnce) { node->ccTouchesCaptureBegan(pTouches, touchTarget->getNode()); } else { dispatchingContinue = node->ccTouchCaptureBegan(touchTarget->findTouch(pTouches), touchTarget->getNode()); } } if (!dispatchingContinue) { // the target stop dispatching, try to next continue; } // phase: targeting node = touchTarget->getNode(); bool ret = true; if (touchMode == kCCTouchesAllAtOnce) { node->ccTouchesBegan(pTouches, pEvent); } else { ret = node->ccTouchBegan(touchTarget->findTouch(pTouches), pEvent); } if (ret) { m_touchingTargets->addObject(touchTarget); // CCLOG("ADD TOUCH TARGET [%p]", touchTarget); } if (node->isTouchSwallowEnabled()) { // target swallow touch event, stop dispatching break; } // continue dispatching, try to next }