Add windows context events to cocos2dx

Source: Internet
Author: User

Add windows context events to cocos2dx

Right-click the simulator in windows to add the quick-cocos2d-x as follows:

1. Modify LRESULT CCEGLView: WindowProc (UINT message, WPARAM wParam, LPARAM lParam) and add the right-click and push events. For more information, see left-click lift and press. The specific code is as follows:

 

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. Modify CCEGLViewProtocol and add the right-click handle. The Code is as follows:

 

 

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. Add a dispatch event to ccTouchDispatcher. The Code is as follows:

 

 

void CCTouchDispatcher::touchesRBegan(CCSet *touches, CCEvent *pEvent){    if (m_bDispatchEvents)    {        this->touches(touches, pEvent, CCRTOUCHBEGAN);    }}
4. Modify the ccTouchDispatcher touch and right-click

 

 

 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. Modify the right-click CCTouchDelegate event:

 

 

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. Add a right-click event for 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");printf("the kCCTouchBegan is %d\n\n", kCCTouchBegan);    return kCCTouchBegan;}
7. Do not forget to re-write the right-click event in ccScene, because ccScene serves to distribute touch.

 

 

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
 
  (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. Add the event passed to 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;}


 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.