給cocos2dx增加windows右鍵事件

來源:互聯網
上載者:User

標籤: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 Touchif (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右鍵事件

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.