码迷,mamicode.com
首页 > Windows程序 > 详细

给cocos2dx增加windows右键事件

时间:2015-05-26 21:33:08      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:quick-cocos2d-x   windows模拟器   右键事件   

给quick-cocos2d-x增加windows下模拟器右键,步骤如下

1.修改LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam),增加右键按下和抬起事件,大体参照左键的抬起和按下,具体代码如下所示:

case WM_RBUTTONDOWN:

		#if(_MSC_VER >= 1600)
			// Don't process message generated by Windows Touch
			if (m_bSupportTouch && (s_pfGetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;
		#endif /* #if(_MSC_VER >= 1600) */

		if (m_pDelegate && MK_RBUTTON == wParam)
		{
			POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};
			CCPoint pt(point.x, point.y);
			pt.x /= m_fFrameZoomFactor;
			pt.y /= m_fFrameZoomFactor;
			CCPoint tmp = ccp(pt.x, m_obScreenSize.height - pt.y);
			if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp))
			{
				m_bCaptured = true;
				SetCapture(m_hWnd);
				int id = 0;
				handleRTouchesBegin(1, &id, &pt.x, &pt.y);
			}
		}
        break;
2.修改CCEGLViewProtocol,添加右键的handle,代码如下

void CCEGLViewProtocol::handleRTouchesBegin(int num, int ids[], float xs[], float ys[])
{
    CCSet set;
    for (int i = 0; i < num; ++i)
    {
        int id = ids[i];
        float x = xs[i];
        float y = ys[i];

        CCInteger* pIndex = (CCInteger*)s_TouchesIntergerDict.objectForKey(id);
        int nUnusedIndex = 0;

        // it is a new touch
        if (pIndex == NULL)
        {
            nUnusedIndex = getUnUsedIndex();

            // The touches is more than MAX_TOUCHES ?
            if (nUnusedIndex == -1) {
                CCLOG("The touches is more than MAX_TOUCHES, nUnusedIndex = %d", nUnusedIndex);
                continue;
            }

            CCTouch* pTouch = s_pTouches[nUnusedIndex] = new CCTouch();
			pTouch->setTouchInfo(nUnusedIndex, (x - m_obViewPortRect.origin.x) / m_fScaleX, 
                                     (y - m_obViewPortRect.origin.y) / m_fScaleY);
            
            //CCLOG("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y);
            
            CCInteger* pInterObj = new CCInteger(nUnusedIndex);
            s_TouchesIntergerDict.setObject(pInterObj, id);
            set.addObject(pTouch);
            pInterObj->release();
        }
    }

    if (set.count() == 0)
    {
        CCLOG("touchesBegan: count = 0");
        return;
    }

    m_pDelegate->touchesRBegan(&set, NULL);
}
3.ccTouchDispatcher中添加分发事件,代码如下

void CCTouchDispatcher::touchesRBegan(CCSet *touches, CCEvent *pEvent)
{
    if (m_bDispatchEvents)
    {
        this->touches(touches, pEvent, CCRTOUCHBEGAN);
    }
}
4.修改ccTouchDispatcher的touch,添加右键

 if (uIndex == CCTOUCHBEGAN)
                {
                    bClaimed = pHandler->getDelegate()->ccTouchBegan(pTouch, pEvent);

                    if (bClaimed)
                    {
                        pHandler->getClaimedTouches()->addObject(pTouch);
                    }
				} else if(uIndex == CCRTOUCHBEGAN){
					bClaimed = pHandler->getDelegate()->ccRTouchBegan(pTouch, pEvent);
                    if (bClaimed)
                    {
                        pHandler->getClaimedTouches()->addObject(pTouch);
                    }
				}else
                if (pHandler->getClaimedTouches()->containsObject(pTouch))
                {
                    // moved ended canceled
                    bClaimed = true;

                    switch (sHelper.m_type)
                    {
                    case CCTOUCHMOVED:
                        pHandler->getDelegate()->ccTouchMoved(pTouch, pEvent);
                        break;
                    case CCTOUCHENDED:
                        pHandler->getDelegate()->ccTouchEnded(pTouch, pEvent);
                        pHandler->getClaimedTouches()->removeObject(pTouch);
                        break;
                    case CCTOUCHCANCELLED:
                        pHandler->getDelegate()->ccTouchCancelled(pTouch, pEvent);
                        pHandler->getClaimedTouches()->removeObject(pTouch);
                        break;
					case CCRTOUCHENDED:
						pHandler->getDelegate()->ccRTouchEnded(pTouch, pEvent);
                        pHandler->getClaimedTouches()->removeObject(pTouch);
						break;
                    }
                }
5.修改添加CCTouchDelegate右键事件:

virtual int ccRTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {
		CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return 0;};
virtual void ccRTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}

6,.为ccnode添加右键事件

int CCNode::ccRTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
    if (kScriptTypeNone != m_eScriptType)
    {
        return excuteScriptTouchHandler(CCRTOUCHBEGAN, pTouch);
    }


    CC_UNUSED_PARAM(pTouch);
    CC_UNUSED_PARAM(pEvent);
    CCAssert(false, "Layer#ccTouchBegan override me");
<span style="white-space:pre">	</span>printf("the kCCTouchBegan is %d\n\n", kCCTouchBegan);
    return kCCTouchBegan;
}
7.千万别忘了在ccScene中重写右键事件,因为ccScene的作用是分发触摸。

int CCScene::ccRTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
	   // remove all touch targets
    m_touchTargets->removeAllObjects();

    // check touch targets
    const CCPoint p = pTouch->getLocation();
    CCObject *node;
    CCNode *touchNode = NULL;
    CCNode *checkVisibleNode = NULL;
    bool visible = true;
    sortAllTouchableNodes(m_touchableNodes);
    CCARRAY_FOREACH(m_touchableNodes, node)
    {
        checkVisibleNode = touchNode = dynamic_cast<CCNode*>(node);

        // check node is visible
        visible = true;
        do
        {
            visible = visible && checkVisibleNode->isVisible();
            checkVisibleNode = checkVisibleNode->getParent();
        } while (checkVisibleNode && visible);
        if (!visible) continue;

        const CCRect boundingBox = touchNode->getCascadeBoundingBox();
        if (touchNode->isRunning() && boundingBox.containsPoint(p))
        {
            touchNode->retain();
            int ret = touchNode->ccRTouchBegan(pTouch, pEvent);
            if (ret == kCCTouchBegan || ret == kCCTouchBeganNoSwallows)
            {
                m_touchTargets->addObject(touchNode);
                if (ret == kCCTouchBegan)
                {
                    touchNode->release();
                    break;
                }
            }
            touchNode->release();
        }
    }

    sortAllTouchableNodes(m_touchTargets);
    return kCCTouchBegan;

}
8.添加传递给lua的事件
int CCLuaEngine::executeNodeTouchEvent(CCNode* pNode, int eventType, CCTouch *pTouch)
{
    CCTouchScriptHandlerEntry* pScriptHandlerEntry = pNode->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;

		case CCRTOUCHBEGAN:
			m_stack->pushString("rbegan");
			break;

		 case CCRTOUCHENDED:
            m_stack->pushString("rended");
			break;

        default:
            return 0;
    }

    const CCPoint pt = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
    const CCPoint prev = CCDirector::sharedDirector()->convertToGL(pTouch->getPreviousLocationInView());
    m_stack->pushFloat(pt.x);
    m_stack->pushFloat(pt.y);
    m_stack->pushFloat(prev.x);
    m_stack->pushFloat(prev.y);
    int ret = m_stack->executeFunctionByHandler(nHandler, 5);
    m_stack->clean();

    return ret;
}




给cocos2dx增加windows右键事件

标签:quick-cocos2d-x   windows模拟器   右键事件   

原文地址:http://blog.csdn.net/helius_sun/article/details/46011423

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!