void RoleLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) { for (CCSetIterator it = pTouches->begin(); it != pTouches->end(); it++) { CCTouch *touch = (CCTouch *)*it; int id = touch->getID(); CCString * id_str = CCString::createWithFormat("moved: %d",id); ((CCLabelTTF *)this->getChildByTag(kDebugLabel))->setString(id_str->getCString()); CCLOGINFO("%s", id_str->getCString()); } }
void MutiTouchTestLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) { CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++) { CCTouch* pTouch = (CCTouch*)(*iter); TouchPoint* pTouchPoint = TouchPoint::touchPointWithParent(this); CCPoint location = pTouch->locationInView(); location = CCDirector::sharedDirector()->convertToGL(location); pTouchPoint->setTouchPos(location); pTouchPoint->setTouchColor(s_TouchColors[pTouch->getID()]); addChild(pTouchPoint); s_dic.setObject(pTouchPoint, pTouch->getID()); } }
CCTouch *CCTouchTargetNode::findTouchFromTouchesSet(CCSet *touches, int touchId) { CCTouch *touch = NULL; for (CCSetIterator it = touches->begin(); it != touches->end(); ++it) { touch = (CCTouch*)*it; if (touch->getID() == touchId) return touch; } return NULL; }
KDvoid TestMultiTouch::ccTouchesMoved ( CCSet* pTouches, CCEvent* pEvent ) { for ( CCSetIterator iter = pTouches->begin ( ); iter != pTouches->end ( ); iter++ ) { CCTouch* pTouch = (CCTouch*) ( *iter ); TouchPoint* pTouchPoint = (TouchPoint*) m_tDictionary.objectForKey ( pTouch->getID ( ) ); pTouchPoint->setTouchPos ( this->convertTouchToNodeSpace ( pTouch ) ); } }
KDvoid TestBlade::ccTouchesMoved ( CCSet* pTouches, CCEvent* pEvent ) { for ( CCSetIterator it = pTouches->begin ( ); it != pTouches->end ( ); it++ ) { CCTouch* pTouch = (CCTouch*) ( *it ); CCPoint tLocation = this->convertTouchToNodeSpace ( pTouch ); CCBlade* pBlade = (CCBlade*) m_pDictionary->objectForKey ( pTouch->getID ( ) ); pBlade->push ( tLocation ); } }
void MutiTouchTestLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) { CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++) { CCTouch* pTouch = (CCTouch*)(*iter); TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID()); CCPoint location = pTouch->getLocation(); pTP->setTouchPos(location); } }
void MutiTouchTestLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) { CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++) { CCTouch* pTouch = (CCTouch*)(*iter); TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID()); CCPoint location = pTouch->locationInView(); location = CCDirector::sharedDirector()->convertToGL(location); pTP->setTouchPos(location); } }
void SelectionRecognizer::checkForSelection(CCObject* obj) { CCTouch* touch = (CCTouch*)obj; //the touch could have been discarded in the meantime if(isTouchInSelection(touch)) { CCPoint currentLocation = Scene::touchPosition(touch); CCPoint touchOrigin = TOPOINT(storedTouches->objectForKey(touch->getID())); CCObject* target = mainLinker->linkedObjectOf(touch); if(ccpDistance(Scene::touchPosition(touch), touchOrigin) <= maxMovement) { CCNotificationCenter::sharedNotificationCenter()->postNotification("SelectionRecognized", DcreateP(touch, Screate("Touch"), target, Screate("Target"), NULL)); } else { CCNotificationCenter::sharedNotificationCenter()->postNotification("SelectionCanceled", DcreateP(touch, Screate("Touch"), storedTouches->objectForKey(touch->getID()), Screate("Origin"), target, Screate("Target"), NULL)); } storedTouches->removeObjectForKey(touch->getID()); } }
void BaseContentWrapLayer::ccTouchesMoved (CCSet *pTouches, CCEvent *pEvent) { if (!mBaseScene->getCollisionEnable()) { return; } if (mFixedScaling) { return; } CCTouch* touch = (CCTouch*)(*(pTouches->begin())); if(touch->getID() != 0 || pTouches->count() > 1) return; CCPoint currentPos = touch->getLocation(); mVCalculate->addTouchMoveRecord(currentPos); float x_offset = currentPos.x - mLastTouchPos.x; float y_offset = currentPos.y - mLastTouchPos.y; setPositionX(getPositionX() + x_offset); setPositionY(getPositionY() + y_offset); mLastTouchPos = currentPos; float min_x; float min_y; float max_x; float max_y; getBoarderPos(min_x,min_y,max_x,max_y); if (getPositionX() < min_x) { setPositionX(min_x); } if (getPositionX() > max_x) { setPositionX(max_x); } if (getPositionY() < min_y) { setPositionY(min_y); } if (getPositionY() > max_y) { setPositionY(max_y); } }
void EXZoomController::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent){ CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++){ CCTouch* pTouch = (CCTouch*)(*iter); // CCPoint location = pTouch->getLocation(); _touchesDic->setObject(pTouch, CCString::createWithFormat("%d",pTouch->getID())->getCString()); CCLog("touc id %s,",CCString::createWithFormat("%d",pTouch->getID())->getCString()); } bool multitouch = _touchesDic->count() > 1; if (multitouch){ //reset history so auto scroll doesn't happen _timePointStampCounter = 0; endScroll(_firstTouch); CCArray* keys = _touchesDic->allKeys(); CCTouch *touch1 = (CCTouch*)_touchesDic->objectForKey(((CCString*)keys->objectAtIndex(0))->getCString()); CCTouch *touch2 = (CCTouch*)_touchesDic->objectForKey(((CCString*)keys->objectAtIndex(1))->getCString()); CCPoint pt = touch1->getLocationInView(); CCPoint pt2 = touch2->getLocationInView(); beginZoom(pt, pt2); } else { //record the point for determining velocity CCArray* keys = _touchesDic->allKeys(); // ((CCString*)keys->objectAtIndex(0))->getCString() _touchesDic->objectForKey(((CCString*)keys->objectAtIndex(0))->getCString()); CCTouch *touch = (CCTouch*)_touchesDic->objectForKey(((CCString*)keys->objectAtIndex(0))->getCString()); recordScrollPoint(touch); beginScroll(_node->convertToNodeSpace(touch->getLocation())); } }
void BaseContentWrapLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) { if (!mBaseScene->getCollisionEnable()) { return; } CCTouch* touch = (CCTouch*)(*(pTouches->begin())); if(touch->getID() != 0 || pTouches->count() > 1) return; mScrollControler->stopScrolling(); mVCalculate->clearRecord(); mLastTouchPos = touch->getLocation(); }
int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches) { CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry(); if (!pScriptHandlerEntry) return 0; int nHandler = pScriptHandlerEntry->getHandler(); if (!nHandler) return 0; switch (eventType) { case CCTOUCHBEGAN: m_stack->pushString("began"); break; case CCTOUCHMOVED: m_stack->pushString("moved"); break; case CCTOUCHENDED: m_stack->pushString("ended"); break; case CCTOUCHCANCELLED: m_stack->pushString("cancelled"); break; default: return 0; } CCDirector* pDirector = CCDirector::sharedDirector(); lua_State *L = m_stack->getLuaState(); lua_newtable(L); int i = 1; for (CCSetIterator it = pTouches->begin(); it != pTouches->end(); ++it) { CCTouch* pTouch = (CCTouch*)*it; CCPoint pt = pDirector->convertToGL(pTouch->getLocationInView()); lua_pushnumber(L, pt.x); lua_rawseti(L, -2, i++); lua_pushnumber(L, pt.y); lua_rawseti(L, -2, i++); lua_pushinteger(L, pTouch->getID()); lua_rawseti(L, -2, i++); } int ret = m_stack->executeFunctionByHandler(nHandler, 2); m_stack->clean(); return ret; }
KDvoid TestBlade::ccTouchesBegan ( CCSet* pTouches, CCEvent* pEvent ) { for ( CCSetIterator it = pTouches->begin ( ); it != pTouches->end ( ); it++ ) { CCTouch* pTouch = (CCTouch*) ( *it ); CCPoint tLocation = this->convertTouchToNodeSpace ( pTouch ); CCBlade* pBlade = CCBlade::create ( 50 ); pBlade->setAutoDim ( KD_TRUE ); KDint nRand = kdRand ( ) % 3 + 1; CCTexture2D* pTexture = CCTextureCache::sharedTextureCache ( )->addImage ( ccszf ( "xm_supports/streak%d-hd.png", nRand ) ); pBlade->setTexture ( pTexture ); pBlade->push ( tLocation ); this->addChild ( pBlade ); m_pDictionary->setObject ( pBlade, pTouch->getID ( ) ); } }
void BaseContentWrapLayer::ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event) { if (!mBaseScene->getCollisionEnable()) { return; } CCTouch* touch = (CCTouch*)(*(touches->begin())); if(touch->getID() != 0 || touches->count() > 1) return; if (mFixedScaling) return; ScrollConfig config; config.decelerate = 0.95f; getBoarderPos(config.minPosX,config.minPosY,config.maxPosX,config.maxPosY); config.scrollSpeed = mVCalculate->getCurrentInstantaneousSpeed(); config.toScroll = this; mScrollControler->scrollWithNoRebound(config); }
void EXZoomController::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent){ bool multitouch = _touchesDic->count() > 1; if (multitouch) { CCArray* keys = _touchesDic->allKeys(); CCTouch *touch1 = (CCTouch*)_touchesDic->objectForKey(((CCString*)keys->objectAtIndex(0))->getCString()); CCTouch *touch2 = (CCTouch*)_touchesDic->objectForKey(((CCString*)keys->objectAtIndex(1))->getCString()); CCPoint pt1 = touch1->getLocationInView(); CCPoint pt2 = touch2->getLocationInView(); endZoom(pt1, pt2); //which touch remains? // if (touch == touch2) // beginScroll(_node->convertToNodeSpace(touch1->getLocation())); // else beginScroll(_node->convertToNodeSpace(touch2->getLocation())); } else { CCArray* keys = _touchesDic->allKeys(); CCTouch *touch = (CCTouch*)_touchesDic->objectForKey(((CCString*)keys->objectAtIndex(0))->getCString()); recordScrollPoint(touch); CCPoint pt = _node->convertToNodeSpace(touch->getLocation()); endScroll(pt); //handle double-tap zooming // if (zoomOnDoubleTap /**&& [touch tapCount] == 2*/) // handleDoubleTapAt(pt); } CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++){ CCTouch* pTouch = (CCTouch*)(*iter); _touchesDic->removeObjectForKey(CCString::createWithFormat("%d",pTouch->getID())->getCString()); } }
void DancingBoneLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) { CCSetIterator it; CCTouch* touch; for ( it = pTouches->begin(); it != pTouches->end(); it++) { touch = (CCTouch*)(*it); if(!touch) break; CCPoint location = touch->getLocation(); b2Vec2 pos = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); CCObject *obj = this->touchDic->objectForKey(touch->getID()); if (obj == NULL) { continue; } MouseJointObject *mouseJoint = (MouseJointObject*)obj; mouseJoint->mouseJoint->SetTarget(pos); } }
//=============================== Touches =============================== //Begin void DancingBoneLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) { CCSetIterator it; CCTouch* touch; for ( it = pTouches->begin(); it != pTouches->end(); it++) { touch = (CCTouch*)(*it); if(!touch) break; CCPoint location = touch->getLocation(); b2Vec2 pos = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); b2AABB aabb; b2Vec2 d; d.Set(0.001f, 0.001f); aabb.lowerBound = pos - d; aabb.upperBound = pos + d; if (this->isPushpinDown) { MultiBodyQueryCallback callback(pos); this->world->QueryAABB(&callback, aabb); //if query something if (callback.queryBodyArray) { b2Body *front = NULL; b2Body *end = NULL; int count = callback.queryBodyArray->count(); for (int i = 0 ; i < count; i++) { MultiBodyQueryNode *queryNode = (MultiBodyQueryNode*)callback.queryBodyArray->objectAtIndex(i); if (front == NULL) { front = queryNode->body; continue; } end = queryNode->body; //create joint here. b2RevoluteJointDef jointDef; jointDef.Initialize(front, end, pos); jointDef.enableMotor = true; jointDef.motorSpeed = 0.0F; jointDef.maxMotorTorque = 15.0F; jointDef.lowerAngle = -0.125f * b2_pi; jointDef.upperAngle = 0.125f * b2_pi; b2Joint *pushpinJoint = this->world->CreateJoint(&jointDef); //creat drawing pushpin here. CCSprite *pushpinSprite = CCSprite::create(); pushpinSprite->initWithTexture(this->pushpinDown->getTexture()); pushpinSprite->setScale(0.5); pushpinSprite->setUserData(pushpinJoint); this->pushpinSpriteArray->addObject(pushpinSprite); this->addChild(pushpinSprite); front = end; } } } else { PositionQueryCallback callback(pos); this->world->QueryAABB(&callback, aabb); //if query something if (callback.objFixture) { b2Body *body = callback.objFixture->GetBody(); b2MouseJointDef md; md.bodyA = this->groundBody; md.bodyB = body; md.target = pos; md.maxForce = 1000.0F * body->GetMass(); body->SetAwake(true); MouseJointObject *mouseJoint = new MouseJointObject(); mouseJoint->mouseJoint = (b2MouseJoint*)this->world->CreateJoint(&md); mouseJoint->autorelease(); mouseJoint->retain(); this->touchDic->setObject(mouseJoint, touch->getID()); } } } }
int CCLuaEngine::executeNodeTouchesEvent(CCNode* pNode, int eventType, CCSet *pTouches, int phase) { m_stack->clean(); CCLuaValueDict event; switch (eventType) { case CCTOUCHBEGAN: event["name"] = CCLuaValue::stringValue("began"); break; case CCTOUCHMOVED: event["name"] = CCLuaValue::stringValue("moved"); break; case CCTOUCHENDED: event["name"] = CCLuaValue::stringValue("ended"); break; case CCTOUCHCANCELLED: event["name"] = CCLuaValue::stringValue("cancelled"); break; case CCTOUCHADDED: event["name"] = CCLuaValue::stringValue("added"); break; case CCTOUCHREMOVED: event["name"] = CCLuaValue::stringValue("removed"); break; default: return 0; } event["mode"] = CCLuaValue::intValue(kCCTouchesAllAtOnce); switch (phase) { case NODE_TOUCH_CAPTURING_PHASE: event["phase"] = CCLuaValue::stringValue("capturing"); break; case NODE_TOUCH_TARGETING_PHASE: event["phase"] = CCLuaValue::stringValue("targeting"); break; default: event["phase"] = CCLuaValue::stringValue("unknown"); } CCLuaValueDict points; CCDirector* pDirector = CCDirector::sharedDirector(); char touchId[16]; for (CCSetIterator touchIt = pTouches->begin(); touchIt != pTouches->end(); ++touchIt) { CCLuaValueDict point; CCTouch* pTouch = (CCTouch*)*touchIt; sprintf(touchId, "%d", pTouch->getID()); point["id"] = CCLuaValue::stringValue(touchId); const CCPoint pt = pDirector->convertToGL(pTouch->getLocationInView()); point["x"] = CCLuaValue::floatValue(pt.x); point["y"] = CCLuaValue::floatValue(pt.y); const CCPoint prev = pDirector->convertToGL(pTouch->getPreviousLocationInView()); point["prevX"] = CCLuaValue::floatValue(prev.x); point["prevY"] = CCLuaValue::floatValue(prev.y); points[touchId] = CCLuaValue::dictValue(point); } event["points"] = CCLuaValue::dictValue(points); m_stack->pushCCLuaValueDict(event); int eventInt = (phase == NODE_TOUCH_CAPTURING_PHASE) ? NODE_TOUCH_CAPTURE_EVENT : NODE_TOUCH_EVENT; CCArray *listeners = pNode->getAllScriptEventListeners(); CCScriptHandlePair *p; for (int i = listeners->count() - 1; i >= 0; --i) { p = dynamic_cast<CCScriptHandlePair*>(listeners->objectAtIndex(i)); if (p->event != eventInt || p->removed) continue; m_stack->copyValue(1); m_stack->executeFunctionByHandler(p->listener, 1); m_stack->settop(1); } m_stack->clean(); return 1; }
void HFViewport::onTouchesBegan(const std::vector<Touch*>& pTouches, Event *pEvent) { unscheduleAll(); CC_ASSERT(this->m_TargetNode); //tao.yu 为解决batch不跟随自定义摄像机走的问题,增加此行代码 if (m_TargetNode->getTag() == IMPERIAL_SCENE_TOUCH_LAYER_TAG) { m_TargetNode->setCameraMask((unsigned short)CameraFlag::USER4, true); auto node3d = m_TargetNode->getChildByTag(JUST3D_NODE_TAG); if (node3d) { node3d->setCameraMask((unsigned short)CameraFlag::USER2, true); } } // CC_ASSERT(pTouches); if (mFingerMap.size() >= 2) { isMove = true; return; } if( this->mTouchMode == TouchMode_Fling ) { this->m_TargetNode->stopActionByTag(FLINGACTION_TAG); } this->mTouchMode = TouchMode_None; for ( auto &item: pTouches ) { CCTouch* curTouch = dynamic_cast<CCTouch*>(item); CC_ASSERT(curTouch); if (mFingerMap.size() <= 2) { mFingerMap[curTouch->getID()] = curTouch->getLocation(); } } // // CCSetIterator iter = pTouches->begin(); // // while( iter != pTouches->end() ) // { // CCTouch* curTouch = dynamic_cast<CCTouch*>(*iter); // CC_ASSERT(curTouch); // if (mFingerMap.size() <= 2) { // mFingerMap[curTouch->getID()] = curTouch->getLocation(); // } // iter++; // } switch(mFingerMap.size()) { case 1: { m_bTouchMoved = false; if (this->mTouchMode != TouchMode_ZoomFling) { this->mTouchMode = TouchMode_Scroll; } if (!mForceStopScroll && mMovable && !pTouches.empty()) { CCTouch* objTouch = dynamic_cast<CCTouch*>(*pTouches.begin()); CC_ASSERT(objTouch); // let "beginScroll" fired by first step scroll , make single touch not fire this // beginScroll(objTouch); if (mTouchDelegate) { mTouchDelegate->onSingleTouchBegin(this->m_TargetNode->convertToNodeSpace(objTouch->getLocation())); mTouchDelegate->onSingleTouchBegin(objTouch); } } CCTouch* ptouch =getAnyTouchObject(pTouches); mPreviewPos = ptouch->getLocation(); } break; case 2: if (this->mTouchMode != TouchMode_ZoomFling) { this->mTouchMode = TouchMode_Zoom; } if(!allowZoom){ return; } do { FingerMap::iterator iter = mFingerMap.begin(); CC_BREAK_IF(iter == mFingerMap.end()); CCPoint pos1 = (*iter).second; CC_BREAK_IF(++iter == mFingerMap.end()); CCPoint pos2 = (*iter).second; beginZoom(pos1, pos2); } while(0); isMove = true; break; default: isMove = true; CCLOG("begin:Touch not support with %d fingers", pTouches.size()); } }
void GameLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) { CCLOG("GameLayer::ccTouchesEnded"); for (CCSetIterator it = pTouches->begin(); it != pTouches->end(); it++) { CCTouch * pTouch = dynamic_cast<CCTouch *>(*it); CCPoint pos = this -> convertTouchToNodeSpace(pTouch); if (Stage::GetInstance()->isInScope(pos, true)) { CCPoint loc = Stage::GetInstance()->pos2loc(pos); int idx = Stage::GetInstance()->pos2idx(pos); CCLOGINFO("ccTouchesEnded pos:(%f,%f) loc:(%f,%f) idx:(%d) tID:(%d)", pos.x, pos.y, loc.x, loc.y, idx, pTouch->getID()); // USER OPERATE #if PROGRAM_MODE == PROGRAM_MODE_GAME // TODO: GameManager::GetInstance() -> touchAPoint(loc); #elif PROGRAM_MODE == PROGRAM_MODE_CREATER enumMapCellCode cell_code = SystemManager::GetInstance()->m_chosedCellCode; enumMapCellPollCode cell_poll_code = SystemManager::GetInstance()->m_chosedCellPollCode; Stage::GetInstance()->cell(idx).setCode(cell_code); Stage::GetInstance()->cell(idx).setPollCode(cell_poll_code); Stage::GetInstance()->refleshCellShow_adv_nine(loc); #else CCAssert(false, "PROGRAM_MODE unexpect."); #endif } } }
void CentralLayer::mapTouchesMoved(CCSet *pTouches, CCEvent *pEvent) { //CCSize mapSize = CCSizeMake(m_nodeArena->getContentSize().width,m_nodeArena->getContentSize().height); CCSize mapSize = CCSizeMake(MAPGLWIDTH,MAPGLHEIGHT); switch (pTouches->count()) { case 1: {//move // CCLog("move--------------------------"); CCSetIterator it; CCTouch* touch = NULL; for( it = pTouches->begin(); it != pTouches->end(); it++) { touch = (CCTouch*)(*it); if(!touch) break; int iViewID = touch->getID(); if( iViewID == 0 ){ break; } } if (touch != NULL) { initialDistance = 0.0; finalDistance = 0.0; CCPoint touchLocation = touch->getLocationInView(); CCPoint prevLocation = touch->getPreviousLocationInView(); touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation); prevLocation = CCDirector::sharedDirector()->convertToGL(prevLocation); CCPoint diff = ccpSub(touchLocation,prevLocation); CCPoint currentPos = this->getPosition(); float tempWidth = mapSize.width*this->getScale(); float tempWidth_Half = tempWidth/2; float tempHeight = mapSize.height*this->getScale(); float tempHeight_Half =tempHeight/2; CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); float result1= tempWidth_Half-screenSize.width/2; float result2 = tempHeight_Half-screenSize.height/2; CCPoint maxSelfPos = ccp(result1,result2); CCPoint minSelfPos; minSelfPos = ccp(result1-(tempWidth-480),result2-(tempHeight-320)); if (diff.x >=0) { diff.x = (this->getPositionX()+diff.x)<=maxSelfPos.x?diff.x:(maxSelfPos.x-this->getPositionX()); } else { diff.x = (this->getPositionX()+diff.x)>=minSelfPos.x?diff.x:(minSelfPos.x-this->getPositionX()); } if (diff.y>=0) { diff.y = (this->getPositionY()+diff.y)<=maxSelfPos.y?diff.y:(maxSelfPos.y-this->getPositionY()); } else { diff.y = (this->getPositionY()+diff.y)>=minSelfPos.y?diff.y:(minSelfPos.y-this->getPositionY()); } this->setPosition(ccpAdd(currentPos, diff)); } } break; case 3: case 4: case 5: case 2: {//zoom // CCLog("zoom--------------------------"); if ( initialDistance==0) { CCSetIterator it; CCTouch* touch1 = NULL; CCTouch* touch2 = NULL; for( it = pTouches->begin(); it != pTouches->end(); it++) { CCTouch *touch = (CCTouch*)(*it); if(!touch) break; int iViewID = touch->getID(); if( iViewID == 0 ){ touch1 = touch; } if (iViewID == 1) { touch2 = touch; } } if (touch1 != NULL && touch2 != NULL) { CCPoint touch1Location = touch1->getLocationInView(); CCPoint touch2Location = touch2->getLocationInView(); initialDistance = sqrt((touch1Location.x-touch2Location.x)*(touch1Location.x-touch2Location.x) +(touch1Location.y-touch2Location.y)*(touch1Location.y-touch2Location.y)); } } CCSetIterator it; CCTouch* touch1 = NULL; CCTouch* touch2 = NULL; for( it = pTouches->begin(); it != pTouches->end(); it++) { CCTouch *touch = (CCTouch*)(*it); if(!touch) break; int iViewID = touch->getID(); if( iViewID == 0 ){ touch1 = touch; } if (iViewID == 1) { touch2 = touch; } } if (touch1 != NULL && touch2 != NULL) { CCPoint touch1Location = touch1->getLocationInView(); CCPoint touch2Location = touch2->getLocationInView(); finalDistance = sqrt((touch1Location.x-touch2Location.x)*(touch1Location.x-touch2Location.x)+(touch1Location.y-touch2Location.y)*(touch1Location.y-touch2Location.y)); float scaleGesture = this->getScale() + (finalDistance - initialDistance)*0.005; scaleGesture = scaleGesture<3.0f?scaleGesture:3.0f; scaleGesture = scaleGesture>320.0/(MAPGLHEIGHT)?scaleGesture:320.0/(MAPGLHEIGHT); CCPoint newPos=this->getPosition(); float tempWidth = mapSize.width*scaleGesture; float tempWidth_Half = tempWidth/2; float tempHeight = mapSize.height*scaleGesture; float tempHeight_Half =tempHeight/2; CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); float result1= tempWidth_Half-screenSize.width/2; float result2 = tempHeight_Half-screenSize.height/2; CCPoint maxSelfPos = ccp(result1,result2); CCPoint minSelfPos; minSelfPos = ccp(result1-(tempWidth-480),result2-(tempHeight-320)); newPos.x = this->getPositionX()>=minSelfPos.x?newPos.x:minSelfPos.x; newPos.y = this->getPositionY()>=minSelfPos.y?newPos.y:minSelfPos.y; newPos.x = this->getPositionX()<=maxSelfPos.x?newPos.x:maxSelfPos.x; newPos.y = this->getPositionY()<=maxSelfPos.y?newPos.y:maxSelfPos.y; this->setPosition(newPos); this->setScale(scaleGesture); m_fCurrentScale = scaleGesture; initialDistance = finalDistance; } } break; } }
int CCLuaEngine::executeNodeTouchesEvent(CCNode* pNode, int eventType, CCSet *pTouches, int phase) { CCScriptEventListenersForEvent &listeners = pNode->getScriptEventListenersByEvent(phase == NODE_TOUCH_CAPTURING_PHASE ? NODE_TOUCH_CAPTURE_EVENT : NODE_TOUCH_EVENT); if (listeners.size() == 0) return 1; m_stack->clean(); CCLuaValueDict event; switch (eventType) { case CCTOUCHBEGAN: event["name"] = CCLuaValue::stringValue("began"); break; case CCTOUCHMOVED: event["name"] = CCLuaValue::stringValue("moved"); break; case CCTOUCHENDED: event["name"] = CCLuaValue::stringValue("ended"); break; case CCTOUCHCANCELLED: event["name"] = CCLuaValue::stringValue("cancelled"); break; default: return 0; } event["mode"] = CCLuaValue::intValue(kCCTouchesAllAtOnce); switch (phase) { case NODE_TOUCH_CAPTURING_PHASE: event["phase"] = CCLuaValue::stringValue("capturing"); break; case NODE_TOUCH_TARGETING_PHASE: event["phase"] = CCLuaValue::stringValue("targeting"); break; default: event["phase"] = CCLuaValue::stringValue("unknown"); } CCLuaValueDict points; CCDirector* pDirector = CCDirector::sharedDirector(); char touchId[16]; for (CCSetIterator touchIt = pTouches->begin(); touchIt != pTouches->end(); ++touchIt) { CCLuaValueDict point; CCTouch* pTouch = (CCTouch*)*touchIt; sprintf(touchId, "%d", pTouch->getID()); point["id"] = CCLuaValue::stringValue(touchId); const CCPoint pt = pDirector->convertToGL(pTouch->getLocationInView()); point["x"] = CCLuaValue::floatValue(pt.x); point["y"] = CCLuaValue::floatValue(pt.y); const CCPoint prev = pDirector->convertToGL(pTouch->getPreviousLocationInView()); point["prevX"] = CCLuaValue::floatValue(prev.x); point["prevY"] = CCLuaValue::floatValue(prev.y); points[touchId] = CCLuaValue::dictValue(point); } event["points"] = CCLuaValue::dictValue(points); m_stack->pushCCLuaValueDict(event); CCScriptEventListenersForEventIterator it = listeners.begin(); for (; it != listeners.end(); ++it) { m_stack->copyValue(1); m_stack->executeFunctionByHandler((*it).listener, 1); m_stack->settop(1); } m_stack->clean(); return 1; }
void CCScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) { if (!m_touchDispatchingEnabled) return; // cleanup m_touchingTargets->removeAllObjects(); // 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 }