Пример #1
0
void PlayerSprite::transferArrow() {
    CCPoint start = this->getAttackPoint();
    CCPoint end = this->getPosition();
    // adjust scale
    if (_arrow->isVisible()) {
        float distance = ccpDistance(start, end);
        CCSize size = _arrow->boundingBox().size;
        float scale = 0;
        
        if (distance > 0) {
            scale = distance / _screenSize.height / 2 * MAX_SCALE;
        }
        
        _arrow->setScaleX(scale);
        
        // adjust angle
        float diffx = end.x - start.x;
        float diffy = end.y - start.y;
        
        float radian = -atan2(diffy, diffx);
        float angle = CC_RADIANS_TO_DEGREES(radian);
        
        _arrow->setRotation(angle);
        
        // adjust position
        _arrow->setPosition(ccpMidpoint(start, end));
    }

}
Пример #2
0
void EXZoomController::beginZoom(CCPoint pt, CCPoint pt2) {
    //initialize our zoom vars
	_firstLength = ccpDistance(pt, pt2);
	_oldScale = _node->getScale();
    
    //get the mid point of pinch
    _firstTouch = _node->convertToNodeSpace(ccpMidpoint(pt, pt2));
}
Пример #3
0
void VRope::createRope(cocos2d::CCPoint pointA, cocos2d::CCPoint pointB, float ropeLenght) {
    
    float distance;
    if (ropeLenght < 0) {
        distance = ccpDistance(pointA,pointB);
    }else{
        distance = ropeLenght;
    }
    
	int segmentFactor = RopeSegmentFactor; //increase value to have less segments per rope, decrease to have more segments
	numPoints = distance/segmentFactor;
    
    mRopeLength = segmentFactor * numPoints;
    
	CCPoint diffVector = ccpSub(pointB,pointA);
	float multiplier = distance / (numPoints-1);
	antiSagHack = 0.1f; //HACK: scale down rope points to cheat sag. set to 0 to disable, max suggested value 0.1
    
	for(int i=0;i<numPoints;i++) {
		CCPoint tmpVector = ccpAdd(pointA, ccpMult(ccpNormalize(diffVector),multiplier*i*(1-antiSagHack)));
		VPoint* tmpPoint = new VPoint;
        tmpPoint->setPos(tmpVector.x, tmpVector.y);
        vPoints.insert(vPoints.end(), tmpPoint);
	}
    
	for(int i=0;i<numPoints-1;i++) {
        VStick tmpStick;
        tmpStick.initWith(vPoints[i], vPoints[i+1]);
		vSticks.insert(vSticks.end(), tmpStick);
	}
    
	if(spriteSheet!=NULL) {
		for(int i=0;i<numPoints-1;i++) {
            VPoint* point1 = vSticks[i].getPointA();
            VPoint* point2 = vSticks[i].getPointB();
			CCPoint stickVector = ccpSub(ccp(point1->x,point1->y),ccp(point2->x,point2->y));
			float stickAngle = ccpToAngle(stickVector);
            
            CCTexture2D* texture = spriteSheet->getTexture();
            RecordSprite* tmpSprite = new RecordSprite;
			tmpSprite->setTag(Tag_Box_RopeBatchSprite);
			tmpSprite->autorelease();
			tmpSprite->initWithTexture(texture, CCRectMake(0,0,multiplier,texture->getContentSize().height));
			ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
            tmpSprite->getTexture()->setTexParameters(&params);
            tmpSprite->setPosition(ccpMidpoint(ccp(point1->x,point1->y),ccp(point2->x,point2->y)));
            tmpSprite->setRotation(-1 * CC_RADIANS_TO_DEGREES(stickAngle));
            spriteSheet->addChild(tmpSprite);
            ropeSprites.insert(ropeSprites.end(), tmpSprite);
		}

		//// LiFeng 添加
		//ropeSprites[ropeSprites.size()-1]->setOpacity(100);
	}else{
        CCAssert(false, "not init");
    }
    
}
Пример #4
0
void EXZoomController::centerOnPoint(CCPoint pt, float duration, float rate) {
    CCPoint mid = _node->convertToNodeSpace(ccpMidpoint(_winTr, _winBl));
    CCPoint diff = ccpSub(mid, pt);
    
    CCPoint final = boundPos(ccpAdd(_node->getPosition(), diff));
    
    CCMoveTo* moveTo = CCMoveTo::create(duration, final);
    CCEaseOut* ease = CCEaseOut::create(moveTo, rate);
    
    _node->runAction(ease);
}
Пример #5
0
void EXZoomController::centerOnPoint(CCPoint pt, float damping) {
    CCPoint mid = _node->convertToNodeSpace(ccpMidpoint(_winTr, _winBl));
    CCPoint diff = ccpMult(ccpSub(mid, pt), damping);
    
    CCLog("_winTr %f,%f",_winTr.x,_winTr.y);
    CCLog("_winBl %f,%f",_winBl.x,_winBl.y);
    CCLog("mid %f,%f",mid.x,mid.y);
    CCLog("diff %f,%f",diff.x,diff.y);
    CCLog("_node->getPosition() %f,%f",_node->getPosition().x,_node->getPosition().y);
    
    updatePosition(ccpAdd(_node->getPosition(), diff));
}
Пример #6
0
bool CAScrollView::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    do
    {
        
        CCPoint point = this->convertTouchToNodeSpace(pTouch);
        CC_BREAK_IF(m_bscrollEnabled == false);
        CC_BREAK_IF(!this->isVisible());
        CC_BREAK_IF(!getBounds().containsPoint(point));
        CC_BREAK_IF(m_pTouches->count() > 2);

        if (!m_pTouches->containsObject(pTouch))
        {
            m_pTouches->addObject(pTouch);
        }

        if (m_pTouches->count() == 1)
        {
            CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(schedule_selector(CAScrollView::deaccelerateScrolling), this);
            m_tInertia = CCPointZero;
            CCDirector::sharedDirector()->getScheduler()->unscheduleSelector(schedule_selector(CAScrollView::closeToPoint), this);
            m_tCloseToPoint = CCPoint(-1, -1);
            m_pContainer->setAnchorPoint(CCPoint(0.5f, 0.5f));
        }
        else if (m_pTouches->count() == 2)
        {
            CCTouch* touch0 = dynamic_cast<CCTouch*>(m_pTouches->objectAtIndex(0));
            CCTouch* touch1 = dynamic_cast<CCTouch*>(m_pTouches->objectAtIndex(1));
           
            m_fTouchLength = ccpDistance(touch0->getLocation(), touch1->getLocation());
            
            CCPoint mid_point = ccpMidpoint(touch0->getLocation(), touch1->getLocation());
            
            CCPoint p = m_pContainer->convertToNodeSpace(mid_point);
            m_pContainer->setAnchorPointInPoints(p);

            if (m_pScrollViewDelegate)
            {
                m_pScrollViewDelegate->scrollViewDidZoom(this);
            }
            m_bZooming = true;
        }
        
        return true;
    }
    while (0);

    return false;
}
Пример #7
0
bool CAScrollView::ccTouchBegan(CATouch *pTouch, CAEvent *pEvent)
{
    do
    {
        CC_BREAK_IF(m_vTouches.size() > 2);
        
        if (!m_vTouches.contains(pTouch))
        {
            m_vTouches.pushBack(pTouch);
        }
        
        if (m_vTouches.size() == 1)
        {
            CAAnimation::unschedule(CAAnimation_selector(CAScrollView::closeToPoint), this);
            this->stopDeaccelerateScroll();
            m_tCloseToPoint = this->getViewSize();
            m_tInitialPoint = m_tCloseToPoint;
            m_pContainer->setAnchorPoint(DPoint(0.5f, 0.5f));
            CAScheduler::schedule(schedule_selector(CAScrollView::updatePointOffset), this, 0);
        }
        else if (m_vTouches.size() == 2)
        {
            CATouch* touch0 = dynamic_cast<CATouch*>(m_vTouches.at(0));
            CATouch* touch1 = dynamic_cast<CATouch*>(m_vTouches.at(1));

            m_fTouchLength = ccpDistance(this->convertToNodeSpace(touch0->getLocation()) ,
                                         this->convertToNodeSpace(touch1->getLocation()));
            
            DPoint mid_point = ccpMidpoint(m_pContainer->convertToNodeSpace(touch0->getLocation()),
                                            m_pContainer->convertToNodeSpace(touch1->getLocation()));
            
            m_pContainer->setAnchorPointInPoints(mid_point);

            if (m_pScrollViewDelegate)
            {
                m_pScrollViewDelegate->scrollViewDidZoom(this);
            }
            m_bZooming = true;
            m_tPointOffset.clear();
            CAScheduler::unschedule(schedule_selector(CAScrollView::updatePointOffset), this);
        }
        
        return true;
    }
    while (0);

    return false;
}
Пример #8
0
bool CAScrollView::ccTouchBegan(CATouch *pTouch, CAEvent *pEvent)
{
    do
    {
        CC_BREAK_IF(m_pTouches->count() > 2);
        
        if (m_bscrollEnabled == false)
            return true;

        if (!m_pTouches->containsObject(pTouch))
        {
            m_pTouches->addObject(pTouch);
        }

        if (m_pTouches->count() == 1)
        {
            CAScheduler::unschedule(schedule_selector(CAScrollView::closeToPoint), this);
            this->stopDeaccelerateScroll();
            m_tCloseToPoint = this->getViewSize();
            m_pContainer->setAnchorPoint(CCPoint(0.5f, 0.5f));
        }
        else if (m_pTouches->count() == 2)
        {
            CATouch* touch0 = dynamic_cast<CATouch*>(m_pTouches->objectAtIndex(0));
            CATouch* touch1 = dynamic_cast<CATouch*>(m_pTouches->objectAtIndex(1));

            m_fTouchLength = ccpDistance(this->convertToNodeSpace(touch0->getLocation()) ,
                                         this->convertToNodeSpace(touch1->getLocation()));
            
            CCPoint mid_point = ccpMidpoint(this->convertToNodeSpace(touch0->getLocation()),
                                            this->convertToNodeSpace(touch1->getLocation()));
            
            CCPoint p = m_pContainer->convertToNodeSpace(mid_point);
            m_pContainer->setAnchorPointInPoints(p);

            if (m_pScrollViewDelegate)
            {
                m_pScrollViewDelegate->scrollViewDidZoom(this);
            }
            m_bZooming = true;
        }
        
        return true;
    }
    while (0);

    return false;
}
Пример #9
0
void HFViewport::beginZoom(const CCPoint& point1, const CCPoint& point2)
{
    CC_ASSERT(this->m_TargetNode);
    mZoomFingersDistance = ccpDistance(point1, point2);
    if (mForceStopScroll || !mMovable) {
        mWSZoomCenter = ccp(CCDirector::sharedDirector()->getWinSize().width/2, CCDirector::sharedDirector()->getWinSize().height/2);
    }
    else {
        mWSZoomCenter = ccpMidpoint(point1, point2);
    }
    mNSZoomCenter = this->m_TargetNode->convertToNodeSpace(mWSZoomCenter);
    
    mZoomOldScale = this->m_TargetNode->getScale();
    mOperationStartPostition = this->m_TargetNode->getPosition();
    
    if (mTouchDelegate) {
        mTouchDelegate->onBeginZoom(point1, point2);
    }
}
Пример #10
0
void VRope::updateSprites() {
	if(spriteSheet!=NULL) {
		for(int i=0;i<numPoints-1;i++) {
            VPoint* point1 = vSticks[i].getPointA();
			//VPoint *point1 = [[vSticks objectAtIndex:i] getPointA];
            VPoint* point2 = vSticks[i].getPointB();
			//VPoint *point2 = [[vSticks objectAtIndex:i] getPointB];
			CCPoint point1_ = ccp(point1->x,point1->y);
			CCPoint point2_ = ccp(point2->x,point2->y);
			float stickAngle = ccpToAngle(ccpSub(point1_,point2_));
            RecordSprite* tmpSprite = ropeSprites[i];
			//CCSprite *tmpSprite = [ropeSprites objectAtIndex:i];

            tmpSprite->setPosition(ccpMidpoint(point1_,point2_));
			//[tmpSprite setPosition:ccpMidpoint(point1_,point2_)];
            tmpSprite->setRotation(-CC_RADIANS_TO_DEGREES(stickAngle));
			//[tmpSprite setRotation: -CC_RADIANS_TO_DEGREES(stickAngle)];
		}
	}	
}
Пример #11
0
NS_CC_BEGIN

void ccVertexLineToPolygon(DPoint *points, float stroke, ccVertex2F *vertices, unsigned int offset, unsigned int nuPoints)
{
    nuPoints += offset;
    if(nuPoints<=1) return;

    stroke *= 0.5f;

    unsigned int idx;
    unsigned int nuPointsMinus = nuPoints-1;

    for(unsigned int i = offset; i<nuPoints; i++)
    {
        idx = i*2;
        DPoint p1 = points[i];
        DPoint perpVector;

        if(i == 0)
            perpVector = ccpPerp(ccpNormalize(ccpSub(p1, points[i+1])));
        else if(i == nuPointsMinus)
            perpVector = ccpPerp(ccpNormalize(ccpSub(points[i-1], p1)));
        else
        {
            DPoint p2 = points[i+1];
            DPoint p0 = points[i-1];

            DPoint p2p1 = ccpNormalize(ccpSub(p2, p1));
            DPoint p0p1 = ccpNormalize(ccpSub(p0, p1));

            // Calculate angle between vectors
            float angle = acosf(ccpDot(p2p1, p0p1));

            if(angle < CC_DEGREES_TO_RADIANS(70))
                perpVector = ccpPerp(ccpNormalize(ccpMidpoint(p2p1, p0p1)));
            else if(angle < CC_DEGREES_TO_RADIANS(170))
                perpVector = ccpNormalize(ccpMidpoint(p2p1, p0p1));
            else
                perpVector = ccpPerp(ccpNormalize(ccpSub(p2, p0)));
        }
        perpVector = ccpMult(perpVector, stroke);

        vertices[idx] = vertex2(p1.x+perpVector.x, p1.y+perpVector.y);
        vertices[idx+1] = vertex2(p1.x-perpVector.x, p1.y-perpVector.y);

    }

    // Validate vertexes
    offset = (offset==0) ? 0 : offset-1;
    for(unsigned int i = offset; i<nuPointsMinus; i++)
    {
        idx = i*2;
        const unsigned int idx1 = idx+2;

        ccVertex2F p1 = vertices[idx];
        ccVertex2F p2 = vertices[idx+1];
        ccVertex2F p3 = vertices[idx1];
        ccVertex2F p4 = vertices[idx1+1];

        float s;
        //BOOL fixVertex = !ccpLineIntersect(DPoint(p1.x, p1.y), DPoint(p4.x, p4.y), DPoint(p2.x, p2.y), DPoint(p3.x, p3.y), &s, &t);
        bool fixVertex = !ccVertexLineIntersect(p1.x, p1.y, p4.x, p4.y, p2.x, p2.y, p3.x, p3.y, &s);
        if(!fixVertex)
            if (s<0.0f || s>1.0f)
                fixVertex = true;

        if(fixVertex)
        {
            vertices[idx1] = p4;
            vertices[idx1+1] = p3;
        }
    }
}
Пример #12
0
void CAScrollView::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
    CCPoint p_container = m_pContainer->getCenter().origin;
    CCPoint p_off = CCPointZero;
    
    if (m_pTouches->count() == 1)
    {
        p_off = ccpSub(pTouch->getLocation(), pTouch->getPreviousLocation());
    }
    else if (m_pTouches->count() == 2)
    {
        CCTouch* touch0 = dynamic_cast<CCTouch*>(m_pTouches->objectAtIndex(0));
        CCTouch* touch1 = dynamic_cast<CCTouch*>(m_pTouches->objectAtIndex(1));
        CCPoint mid_point = ccpMidpoint(touch0->getLocation(), touch1->getLocation());
        
        if (m_fMinimumZoomScale < m_fMaximumZoomScale)
        {
            float touch_lenght = ccpDistance(touch0->getLocation(), touch1->getLocation());
            float scale_off = touch_lenght - m_fTouchLength;
            
            m_fZoomScale = m_pContainer->getScale();
            m_fZoomScale += m_fZoomScale * scale_off * 0.0015f;
            
            m_fZoomScale = MIN(m_fZoomScale, m_fMaximumZoomScale);
            m_fZoomScale = MAX(m_fZoomScale, m_fMinimumZoomScale);
            
            m_pContainer->setScale(m_fZoomScale);
            m_fTouchLength = touch_lenght;
        }
        
        p_off = ccpSub(this->convertToNodeSpace(mid_point), m_pContainer->getPosition());
    }
    
    
    if (m_bBounces)
    {
        CCSize size = this->getContentSize();
        CCRect rect = m_pContainer->getCenter();
        rect.size.width = MAX(rect.size.width, size.width);
        rect.size.height = MAX(rect.size.height, size.height);
        CCPoint scale = CCPoint(1.0f, 1.0f);
        
        if (p_container.x - rect.size.width / 2 > 0)
        {
            scale.x = MAX(0, 0.5f - (rect.origin.x - rect.size.width/2) / size.width);
            p_off.x *= scale.x;
        }
        
        if (p_container.y - rect.size.height / 2 > 0)
        {
            scale.y = MAX(0, 0.5f - (rect.origin.y - rect.size.height/2) / size.height);
            p_off.y *= scale.y;
        }
        
        if ((p_container.x + rect.size.width / 2 - size.width) < 0)
        {
            scale.x = MAX(0, (rect.size.width/2 + rect.origin.x) / size.width - 0.5f);
            p_off.x *= scale.x;
        }
        
        if ((p_container.y + rect.size.height / 2 - size.height) < 0)
        {
            scale.y = MAX(0, (rect.size.height/2 + rect.origin.y) / size.height - 0.5f);
            p_off.y *= scale.y;
        }
    }
    
    p_container = ccpAdd(p_container, p_off);
    
    m_tPointOffset.push_back(p_off);

    if (m_tPointOffset.size() > 3)
    {
        m_tPointOffset.pop_front();
    }
    
    if (m_pScrollViewDelegate)
    {
        m_pScrollViewDelegate->scrollViewWillBeginDragging(this);
    }
    m_bTracking = true;
    
    if (m_bBounces == false)
    {
        p_container = this->getScrollWindowNotOutPoint(p_container);
    }
    else
    {
        if (m_bBounceHorizontal == false)
        {
            p_container.x = this->getScrollWindowNotOutHorizontal(p_container.x);
        }
        
        if (m_bBounceVertical == false)
        {
            p_container.y = this->getScrollWindowNotOutVertical(p_container.y);
        }
    }
    
    if (m_pScrollViewDelegate && p_container.equals(m_pContainer->getCenter().origin) == false)
    {
        m_pScrollViewDelegate->scrollViewDidScroll(this);
    }
    
    m_pContainer->setCenter(CCRect(p_container.x, p_container.y, 0, 0));
}
Пример #13
0
bool Geometry::isSegmentLineInPoly( const cocos2d::CCPoint& a0, const cocos2d::CCPoint& a1, std::vector<cocos2d::CCPoint>* point )
{
		/*
	if 线端PQ的端点不都在多边形内 
		then return false;
	点集pointSet初始化为空;
	for 多边形的每条边s
		do if 线段的某个端点在s上
			then 将该端点加入pointSet;
		else if s的某个端点在线段PQ上
			then 将该端点加入pointSet;
		else if s和线段PQ相交 // 这时候已经可以肯定是内交了
			then return false;
		将pointSet中的点按照X-Y坐标排序;
		for pointSet中每两个相邻点 pointSet[i] , pointSet[ i+1]
		do if pointSet[i] , pointSet[ i+1] 的中点不在多边形中
			then return false;
		return true;
		*/
	if (!pointIsInPolygon(a0, point) || !pointIsInPolygon(a1, point))		///< 首先判断线段a0a1
	{
		return false;
	}
	
	/// 判断每条边
	unsigned int numberOfPoints = point->size();
	std::vector<CCPoint> pointSet;
	for (unsigned int i = 0; i < numberOfPoints-1; i++)
	{
		/// 先判断点p0是否在边s上;
		CCPoint s0 = point->at(i);
		CCPoint s1 = point->at((i+1)%(numberOfPoints));
		do 
		{
			/// 判断线段a0a1端点是否在s0s1上
			if (pointIsAtSegment(a0, s0, s1))
			{
				pointSet.push_back(a0);
				break;
			}
			if (pointIsAtSegment(a1, s0, s1))
			{
				pointSet.push_back(a1);
				break;
			}
			/// 判断线段s0s1是否在a0a1上
			if (pointIsAtSegment(s0, a0, a1))
			{
				pointSet.push_back(s0);
				break;
			}
			if (pointIsAtSegment(s1, a0, a1))
			{
				pointSet.push_back(s1);
				break;
			}
			/// 判断a0a1是否与s0s1相交,相交---则必然内交,必然判定线段a0a1不在多边形内
			if (segmentLineIsIntersect(a0, a1, s0, s1))
			{
				return false;
			}
		} while (0);
	}

	unsigned int numOfPointSet = pointSet.size();
	if (0 == numOfPointSet )
	{
		return true;
	}

	/// 排序poisntSet, 有小到大,优先级为x
	CCAssert(0 == numOfPointSet%2, "The pointSet's points num should be 2 times!");
	for (unsigned int i = 0; i < numOfPointSet-1; ++i)
	{
		/// 冒泡排序
		for (unsigned int j = 0; j < numOfPointSet; ++j)
		{
			CCPoint p0 = pointSet.at(i);
			CCPoint p1 = pointSet.at(j);
			if (p0.x > p1.x)
			{
				std::swap(pointSet.at(i), pointSet.at(j));
			}
			else if (p0.x == p1.x)
			{
				if (p0.y > p1.y)
				{
					std::swap(pointSet.at(i), pointSet.at(j));
				}
			}

		}
	}

	

	/// 判断相邻交点集的中点是否在多边形内
	for (unsigned int i = 0; i < numOfPointSet-1; ++i)
	{
		CCPoint p0 = pointSet.at(i);
		CCPoint p1 = pointSet.at((i+1)%numOfPointSet);
		CCPoint midP = ccpMidpoint(p0, p1);
		if (!pointIsInPolygon(midP, point))
		{
			return false;
		}
	}
	return true;
}
Пример #14
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);
         }
     }
 }
Пример #15
0
void CAScrollView::ccTouchMoved(CATouch *pTouch, CAEvent *pEvent)
{
    CC_RETURN_IF(m_bPCMode);
    CC_RETURN_IF(m_vTouches.contains(pTouch) == false);
    DPoint p_container = m_pContainer->getFrameOrigin();
    DPoint p_off = DPointZero;
    
    if (m_vTouches.size() == 1)
    {
        p_off = ccpSub(this->convertToNodeSpace(pTouch->getLocation()),
                       this->convertToNodeSpace(pTouch->getPreviousLocation()));
        
        DPoint off = p_off;
        if (off.getLength() <= 5)
        {
            off = DPointZero;
        }
        
        m_tPointOffset.push_back(off);
    }
    else if (m_vTouches.size() == 2)
    {
        CATouch* touch0 = dynamic_cast<CATouch*>(m_vTouches.at(0));
        CATouch* touch1 = dynamic_cast<CATouch*>(m_vTouches.at(1));
        DPoint mid_point = ccpMidpoint(this->convertToNodeSpace(touch0->getLocation()),
                                        this->convertToNodeSpace(touch1->getLocation()));
        p_off = ccpSub(mid_point, ccpAdd(p_container, m_pContainer->getAnchorPointInPoints() * m_fZoomScale));
        
        if (m_fMinimumZoomScale < m_fMaximumZoomScale)
        {
            float touch_lenght = ccpDistance(this->convertToNodeSpace(touch0->getLocation()) ,
                                             this->convertToNodeSpace(touch1->getLocation()));
            float scale_off = (touch_lenght - m_fTouchLength) * 0.0020f;
            
            m_fZoomScale = m_pContainer->getScale();
            m_fZoomScale += m_fZoomScale * scale_off;
            
            m_fZoomScale = MIN(m_fZoomScale, m_fMaximumZoomScale);
            m_fZoomScale = MAX(m_fZoomScale, m_fMinimumZoomScale);
            
            m_pContainer->setScale(m_fZoomScale);
            m_fTouchLength = touch_lenght;
        }
    }

    if (m_bBounces)
    {
        DSize size = this->getBounds().size;
        DPoint curr_point = m_pContainer->getFrameOrigin();
        DPoint relust_point = curr_point;
        this->getScrollWindowNotOutPoint(relust_point);
        
        float lenght_x = fabsf(curr_point.x - relust_point.x);
        float lenght_y = fabsf(curr_point.y - relust_point.y);
        
        DPoint scale = DPoint(1.0f, 1.0f);
        
        if (!(lenght_x < FLT_EPSILON))
        {
            scale.x = (0.5f - MIN(lenght_x / size.width, 0.5f));
            p_off.x *= scale.x;
        }
        
        if (!(lenght_y < FLT_EPSILON))
        {
            scale.y = (0.5f - MIN(lenght_y / size.height, 0.5f));
            p_off.y *= scale.y;
        }
    }
    
    p_container = ccpAdd(p_container, p_off);

    if (m_bBounces == false)
    {
        this->getScrollWindowNotOutPoint(p_container);
    }
    else
    {
        if (m_bBounceHorizontal == false)
        {
            p_container.x = this->getScrollWindowNotOutHorizontal(p_container.x);
        }
        
        if (m_bBounceVertical == false)
        {
            p_container.y = this->getScrollWindowNotOutVertical(p_container.y);
        }
    }
    
    if (p_container.equals(m_pContainer->getFrameOrigin()) == false)
    {
        if (m_bTouchEnabledAtSubviews)
        {
            m_pContainer->setTouchEnabled(false);
        }
        this->setContainerFrame(p_container);
        this->showIndicator();
        
        if (m_bTracking == false)
        {
            if (m_pScrollViewDelegate)
            {
                m_pScrollViewDelegate->scrollViewWillBeginDragging(this);
            }
            m_bTracking = true;
        }
        
        if (m_pScrollViewDelegate)
        {
            m_pScrollViewDelegate->scrollViewDidScroll(this);
            m_pScrollViewDelegate->scrollViewDragging(this);
            m_pScrollViewDelegate->scrollViewDidMoved(this);
        }
    }
    
    this->changedFromPullToRefreshView();
}
Пример #16
0
void CAScrollView::ccTouchMoved(CATouch *pTouch, CAEvent *pEvent)
{
    CC_RETURN_IF(m_bscrollEnabled == false);
    
    CCPoint p_container = m_pContainer->getFrameOrigin();
    CCPoint p_off = CCPointZero;
    
    if (m_pTouches->count() == 1)
    {
        p_off = ccpSub(this->convertToNodeSpace(pTouch->getLocation()),
                       this->convertToNodeSpace(pTouch->getPreviousLocation()));
    }
    else if (m_pTouches->count() == 2)
    {
        CATouch* touch0 = dynamic_cast<CATouch*>(m_pTouches->objectAtIndex(0));
        CATouch* touch1 = dynamic_cast<CATouch*>(m_pTouches->objectAtIndex(1));
        CCPoint mid_point = ccpMidpoint(this->convertToNodeSpace(touch0->getLocation()),
                                        this->convertToNodeSpace(touch1->getLocation()));
        
        if (m_fMinimumZoomScale < m_fMaximumZoomScale)
        {
            float touch_lenght = ccpDistance(touch0->getLocation(), touch1->getLocation());
            float scale_off = _px(touch_lenght - m_fTouchLength) * 0.0015f;
            
            m_fZoomScale = m_pContainer->getScale();
            m_fZoomScale += m_fZoomScale * scale_off;
            
            m_fZoomScale = MIN(m_fZoomScale, m_fMaximumZoomScale);
            m_fZoomScale = MAX(m_fZoomScale, m_fMinimumZoomScale);
            
            m_pContainer->setScale(m_fZoomScale);
            m_fTouchLength = touch_lenght;
        }
        
        p_off = ccpSub(mid_point, ccpAdd(m_pContainer->getFrameOrigin(),
                                         m_pContainer->getAnchorPointInPoints() * m_fZoomScale));
    }
    
    
    if (m_bBounces)
    {
        CCSize size = this->getBounds().size;
        CCRect rect = m_pContainer->getFrame();
        rect.size.width = MAX(rect.size.width, size.width);
        rect.size.height = MAX(rect.size.height, size.height);
        CCPoint scale = CCPoint(1.0f, 1.0f);
        
        if (p_container.x > 0)
        {
            scale.x = MAX(0, 0.5f - rect.getMinX() / size.width);
            p_off.x *= scale.x;
        }
        
        if (p_container.y > 0)
        {
            scale.y = MAX(0, 0.5f - rect.getMinY() / size.height);
            p_off.y *= scale.y;
        }
        
        if ((p_container.x + rect.size.width - size.width) < 0)
        {
            scale.x = MAX(0, rect.getMaxX() / size.width - 0.5f);
            p_off.x *= scale.x;
        }
        
        if ((p_container.y + rect.size.height - size.height) < 0)
        {
            scale.y = MAX(0, rect.getMaxY() / size.height - 0.5f);
            p_off.y *= scale.y;
        }
    }
    
    p_container = ccpAdd(p_container, p_off);
    
    m_tPointOffset.push_back(p_off);

    if (m_tPointOffset.size() > 3)
    {
        m_tPointOffset.pop_front();
    }
    
    if (m_bBounces == false)
    {
        p_container = this->getScrollWindowNotOutPoint(p_container);
    }
    else
    {
        if (m_bBounceHorizontal == false)
        {
            p_container.x = this->getScrollWindowNotOutHorizontal(p_container.x);
        }
        
        if (m_bBounceVertical == false)
        {
            p_container.y = this->getScrollWindowNotOutVertical(p_container.y);
        }
    }
    
    if (p_container.equals(m_pContainer->getFrameOrigin()) == false)
    {
        this->setContainerFrame(p_container);
        this->showIndicator();
        
        if (m_bTracking == false)
        {
            if (m_pScrollViewDelegate)
            {
                m_pScrollViewDelegate->scrollViewWillBeginDragging(this);
            }
            m_bTracking = true;
        }
        
        if (m_pScrollViewDelegate)
        {
            m_pScrollViewDelegate->scrollViewDidScroll(this);
            m_pScrollViewDelegate->scrollViewDidMoved(this);
        }
    }
    
    this->changedFromPullToRefreshView();
}