void CCDrawLineLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) { CCTouch* touch = pTouch; CCPoint nextPoint = touch->getLocationInView(); nextPoint = CCDirector::sharedDirector()->convertToGL(nextPoint); CCPoint preMovePoint = touch->getPreviousLocationInView(); preMovePoint = CCDirector::sharedDirector()->convertToGL(preMovePoint); float distance = ccpDistance(nextPoint, preMovePoint); if (distance > 1) { int d = (int)distance; for (int i = 0; i < d; i++ ) { float distanceX = nextPoint.x - preMovePoint.x; float distanceY = nextPoint.y - preMovePoint.y; float percent = i / distance; CCPoint newPoint; newPoint.x = preMovePoint.x + (distanceX * percent); newPoint.y = preMovePoint.y + (distanceY * percent); m_pointList.push_back(newPoint); } } if(m_drawLayerDelegate) { m_drawLayerDelegate->ccDrawLineLayerTouchMove(this,touch); } }
void HelloWorld::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *event) { static CCSize s = CCDirector::sharedDirector()->getWinSize(); CCTouch *touch = (CCTouch*)touches->anyObject(); CCPoint pt0 = touch->getPreviousLocationInView(); CCPoint pt1 = touch->getLocationInView(); // Correct Y axis coordinates to cocos2d coordinates pt0.y = s.height - pt0.y; pt1.y = s.height - pt1.y; for (int i=0; i < _ropes.size(); ++i) { VRope *rope = _ropes[i]; vector<VStick*> stick = rope->getSticks(); for (int j=0; j < stick.size(); ++j) { VStick *pStick = stick[j]; CCPoint pa = pStick->getPointA()->getPoint(); CCPoint pb = pStick->getPointB()->getPoint(); if (this->checkLineIntersection(pt0, pt1, pa, pb)) { //cut the rope here b2Body *newBodyA = this->createRopeTipBody(); b2Body *newbodyB = this->createRopeTipBody(); VRope *newRope = rope->cutRopeInStick(pStick, newBodyA, newbodyB); _ropes.push_back(newRope); SimpleAudioEngine::sharedEngine()->playEffect(kCuttingSound); return; } } } }
void BattleField::xtTouchesMoved(cocos2d::CCSet *_touches, cocos2d::CCEvent *event) { if (_touchedTroop!=NULL) { return; } CCTouch *pTouch = (CCTouch *)_touches->anyObject(); CCPoint loc = this->convertTouchToNodeSpace(pTouch); CCPoint prevoiusLoc = pTouch->getPreviousLocationInView(); //屏幕坐标变成GL坐标 prevoiusLoc = CCDirector::sharedDirector()->convertToGL(prevoiusLoc); //然后转成layer相对坐标 prevoiusLoc = this->convertToNodeSpace(prevoiusLoc); CCPoint translation = ccpSub(loc, prevoiusLoc); CCPoint next = ccpAdd(this->getPosition(), translation); this->adjustViewBoundingPosition(next); }
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; }
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 CCLayerPanZoom::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) { _panningOrZooming = true; if(_panZoomEnabled) { if(pTouches->count() == 1 && !_dragging) // Drag the map { CCTouch* touch = (CCTouch*)(*pTouches->begin()); CCPoint touchLocation = touch->getLocationInView(); CCPoint prevLocation = touch->getPreviousLocationInView(); touchLocation = CCDirector::sharedDirector()->convertToGL( touchLocation ); prevLocation = CCDirector::sharedDirector()->convertToGL( prevLocation ); CCPoint diff = ccpSub(touchLocation, prevLocation); move(diff); //add the diff into the avgInertia _avgInertiaDir[_avgIdx] = diff; ++_avgIdx; if(_avgIdx >= NUM_INERTIA_SAMPLES) _avgIdx = 0; CCTime::gettimeofdayCocos2d(&_lastTouchMoved, NULL); } else if(pTouches->count() == 2 && _panZoomEnabled) // Pinch zoom in/out { // Get two touchess to handle the zoom CCSetIterator iter = pTouches->begin(); CCTouch* touchOne = (CCTouch*)(*iter); ++iter; CCTouch* touchTwo = (CCTouch*)(*iter); // Get the touches and previous touches locations CCPoint touchLocationOne = touchOne->getLocation(); CCPoint touchLocationTwo = touchTwo->getLocation(); CCPoint previousLocationOne = touchOne->getPreviousLocation(); CCPoint previousLocationTwo = touchTwo->getPreviousLocation(); // Position the camera to the middle of the pinch // Get the middle position of the pinch CCPoint pinchCenter = ccpMidpoint(touchLocationOne, touchLocationTwo); // Calculate new scale float newScale = getScale() * ccpDistance(touchLocationOne, touchLocationTwo) / ccpDistance(previousLocationOne, previousLocationTwo); if(newScale < _minScale) newScale = _minScale; else if(newScale > _maxScale) newScale = _maxScale; // Call the scale method to scale by the distanceDelta from pinchCenter scale(newScale, pinchCenter); // You can move using two fingers (like in Google Maps) touchLocationOne = CCDirector::sharedDirector()->convertToGL(touchLocationOne); touchLocationTwo = CCDirector::sharedDirector()->convertToGL(touchLocationTwo); previousLocationOne = CCDirector::sharedDirector()->convertToGL(previousLocationOne); previousLocationTwo = CCDirector::sharedDirector()->convertToGL(previousLocationTwo); CCPoint touchLocation = ccpMidpoint(touchLocationOne, touchLocationTwo); CCPoint prevtouchLocation = ccpMidpoint(previousLocationOne, previousLocationTwo); CCPoint diff = ccpSub(touchLocation, prevtouchLocation); diff.y = -diff.y; move(diff); } } }
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; } }