コード例 #1
0
ファイル: InGameScene.cpp プロジェクト: JungHanter/SWMTAMA
void InGameScene::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
    // 적절하지 않은 데이터 판단 - 클라우드가 있으면 확장이 안된다고??
    // 화면 확대 축소 이동
    if( !getChildByTag(THINK_CLOUD)->isVisible() )
    {
        float scale = getScale();
        if( pTouches->count() > 1)
        {
            static float fPrevGap = 0.f;
            CCSetIterator it = pTouches->begin();
            CCTouch *touch1 = static_cast<CCTouch*>(*it++);
            CCTouch *touch2 = static_cast<CCTouch*>(*it);
            CCPoint point1 = touch1->getLocation();
            CCPoint point2 = touch2->getLocation();
            
            float fGap = sqrtf((point2.x-point1.x)*(point2.x-point1.x)+(point2.y-point1.y)*(point2.y-point1.y));
            if( fGap > fPrevGap )
            {
                if( scale < 2.f )
                    scale *= 1.03f;
            }
            else
            {
                //if( scale > 0.6f )
                scale /= 1.03f;
            }
            fPrevGap = fGap;
            
            CCPoint location = getPosition();
            if( isInBackground(location, scale) )
                setScale(scale);
        }
        else
        {
            CCTouch *touch = static_cast<CCTouch*>(pTouches->anyObject());
            
            CCPoint ptGap = touch->getLocation();
            ptGap.x -= touch->getPreviousLocation().x;
            ptGap.y -= touch->getPreviousLocation().y;
            
            CCPoint damage = getPosition();
            damage.x += ptGap.x;
            damage.y += ptGap.y;
            
            if( isInBackground(damage, scale) )
                setPosition(damage);
        }
    }
	for( CCSetIterator it = pTouches->begin(); it != pTouches->end(); ++it)
	{
		CCTouch *touch = static_cast<CCTouch*>(*it);
        
		lastestTouch = touch->getLocation();
	}
	pUI->TouchesMoved(this, pTouches, pEvent);
}
コード例 #2
0
void PageScrollView::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	CCTouch *touch = (CCTouch *)pTouches->anyObject();
	CCPoint currentPoint = (CCPoint)touch->getLocation();
	CCPoint prePoint = (CCPoint)touch->getPreviousLocation();

	if(isTouchMoving){
		v = currentPoint.x - prePoint.x;
		content->setPositionX(content->getPositionX() + v);
	}
	
}
コード例 #3
0
void RoundPageScrollView::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	CCTouch *touch = (CCTouch *)pTouches->anyObject();
    
    CCPoint curLocalPoint = convertToNodeSpace((CCPoint)touch->getLocation());
    CCPoint preLocalPoint = convertToNodeSpace((CCPoint)touch->getPreviousLocation());
    
	if(isTouchMoving){
        v = atan(curLocalPoint.x/radius) - atan(preLocalPoint.x/radius);
        w += v;
	}
	
}
コード例 #4
0
KDvoid RenderTextureSave::ccTouchesMoved ( CCSet* pTouches, CCEvent* pEvent )
{
    CCTouch*	pTouch = (CCTouch*) pTouches->anyObject ( );
	CCPoint		tStart = this->convertToNodeSpace ( pTouch->getLocation ( ) );
	CCPoint		tEnd   = this->convertToNodeSpace ( pTouch->getPreviousLocation ( ) );

	// begin drawing to the render texture
	m_pTarget->begin ( );

	// for extra points, we'll draw this smoothly from the last position and vary the sprite's
	// scale/rotation/offset
	KDfloat  fDistance = ccpDistance ( tStart, tEnd );
	if ( fDistance > 1 )
	{
		KDint  d = (KDint) fDistance;
		for ( KDint i = 0; i < d; i++ )
		{
			CCPoint  tDiff  = ccpSub ( tEnd, tStart );
			KDfloat  fDelta = (KDfloat) i / fDistance;

			m_pBrush->setPosition ( ccp ( tStart.x + ( tDiff.x * fDelta ), tStart.y + ( tDiff.y * fDelta ) ) );
			m_pBrush->setRotation ( (KDfloat) ( kdRand ( ) % 360 ) );

			KDfloat  fScale = ( (KDfloat) ( kdRand ( ) % 50 ) / 50.f ) + 0.25f;

			m_pBrush->setScale ( fScale );
			m_pBrush->setColor ( ccc3 ( kdRand ( ) % 127 + 128, 255, 255 ) );

			// Call visit to draw the brush, don't call draw..
			m_pBrush->visit ( );
		}
	}

	// finish drawing and return context back to the screen
	m_pTarget->end ( );
}
コード例 #5
0
 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);
         }
     }
 }
コード例 #6
0
void MapScrollLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
    if (pTouches->count() > 1)
    {
        CCSetIterator it = pTouches->begin();
        CCTouch *touch1 = (CCTouch*)(*(it++));;
        CCTouch *touch2 = (CCTouch*)(*it);
        
        CCPoint curPosTouch1 = CCDirector::sharedDirector()->convertToGL(touch1->getLocationInView());
        CCPoint curPosTouch2 = CCDirector::sharedDirector()->convertToGL(touch2->getLocationInView());
        
        CCPoint prevPosTouch1 = CCDirector::sharedDirector()->convertToGL(touch1->getPreviousLocationInView());
        CCPoint prevPosTouch2 = CCDirector::sharedDirector()->convertToGL(touch2->getPreviousLocationInView());
        
        
        CCPoint curPosLayer = ccpMidpoint(curPosTouch1, curPosTouch2);
        CCPoint prevPosLayer = ccpMidpoint(prevPosTouch1, prevPosTouch2);
        
        float prevScale = this->getScale();
        float curScale = this->getScale() * (ccpDistance(curPosTouch1, curPosTouch2) / ccpDistance(prevPosTouch1, prevPosTouch2));
        
        if(!zoomStart){
            zoomStart = true;
            curScale = prevScale;
        }
        
        if (curScale < minScale || curScale > maxScale)
            curScale = prevScale;
        
        this->setScale( curScale);

        if (this->getScale() != prevScale)
        {
            CCPoint realCurPosLayer = this->convertToNodeSpace(curPosLayer);
            float deltaX = (this->getAnchorPoint().x * this->getContentSize().width) * (this->getScale() - prevScale);
            float deltaY = (this->getAnchorPoint().y * this->getContentSize().height) * (this->getScale() - prevScale);
            this->setPosition(ccp(this->getPosition().x - deltaX, this->getPosition().y - deltaY));
        }
        if (!prevPosLayer.equals(curPosLayer))
        {
            this->setPosition(ccp(this->getPosition().x + curPosLayer.x - prevPosLayer.x,
                                  this->getPosition().y + curPosLayer.y - prevPosLayer.y));
        }
        return;
    }
 
    CCTouch* touch = (CCTouch*)( pTouches->anyObject());
	CCPoint location = touch->getLocation();
    CCPoint prevLocation = touch->getPreviousLocation();
    CCPoint diff = ccpSub(location, prevLocation);
    CCPoint newPosition = ccpAdd(getPosition(), diff);
    
    float scale = this->getScale();
    float contentSizeWidth = this->getContentSize().width*this->getScale();
    float leftOffset = -(contentSizeWidth - WINSIZE.width);
    
    float contentSizeHeight = this->getContentSize().height*this->getScale();
    float downOffset = -(contentSizeHeight - WINSIZE.height);
    
    if (newPosition.x < leftOffset)
    {
        float offsetBorder =  leftOffset - newPosition.x;
        diff.x/=(offsetBorder/7.0f);
    }
    else if (newPosition.x > 0.0f)
    {
        float offsetBorder =  newPosition.x;
        diff.x/=(offsetBorder/7.0f);
    }
    
    if (newPosition.y < downOffset)
    {
        float offsetBorder =  downOffset - newPosition.y;
        diff.y/=(offsetBorder/15.0f);
    }
    else if (newPosition.y > 0.0f)
    {
        float offsetBorder =  newPosition.y;
        diff.y/=(offsetBorder/7.0f);
    }
    
    newPosition = ccpAdd(getPosition(), diff);
    this->setPosition(newPosition);
    
    lastScrolling.push_back(diff);
}
コード例 #7
0
void MapScrollLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
    if (pTouches->count() > 1)
    {
        zoomStart = false;
        return;
    }
    CCTouch* touch = (CCTouch*)( pTouches->anyObject());
	CCPoint location = touch->getLocation();
    CCPoint prevLocation = touch->getPreviousLocation();
    CCPoint diff = ccpSub(location, prevLocation);
    
    CCPoint newPosition = getPosition();
    
    float scale = this->getScale();
    float contentSizeWidth = this->getContentSize().width*this->getScale();
    
    float leftOffset = -(contentSizeWidth - WINSIZE.width);
    
    float contentSizeHeight = this->getContentSize().height*this->getScale();
    float downOffset = -(contentSizeHeight - WINSIZE.height);
    
    bool isTrespassing = false;
    
    if (newPosition.x < leftOffset)
    {
        float offsetBorder =  leftOffset - newPosition.x;
        this->runAction(CCMoveBy::create(0.3f, ccp(offsetBorder, 0.0f)));
        isTrespassing = true;
    }
    else if (newPosition.x > 0.0f)
    {
        float offsetBorder = newPosition.x;
        this->runAction(CCMoveBy::create(0.3f, ccp(-offsetBorder, 0.0f)));
        isTrespassing = true;
    }
    
    if (newPosition.y < downOffset)
    {
        float offsetBorder =  downOffset - newPosition.y;
        this->runAction(CCMoveBy::create(0.3f, ccp(0.0f, offsetBorder)));
        isTrespassing = true;
    }
    else if (newPosition.y > 0.0f)
    {
        float offsetBorder =  newPosition.y;
        this->runAction(CCMoveBy::create(0.3f, ccp(0.0f, -offsetBorder)));
        isTrespassing = true;
    }
    
    if (!isTrespassing)
    {
        lastScrolling.push_back(diff);
        float xDiff = 0.0f;
        float yDiff = 0.0f;
        float idx = 0;
        for (int i = lastScrolling.size(); i >= 0; i--)
        {
            if (fabs(lastScrolling[i].x) > 1000.0f || fabs(lastScrolling[i].y) > 1000.0f)
                continue;
            
            xDiff+=lastScrolling[i].x;
            yDiff+=lastScrolling[i].y;
            idx++;
            if (idx >= DEACCEL_COUNT)
                break;
        }
        xDiff/=idx;
        yDiff/=idx;
        
        deaccelDiff = CCPoint(xDiff, yDiff);
        
        lastScrolling.clear();
        this->schedule(schedule_selector(MapScrollLayer::deaccelerate));
    }
}