给cocos2dx增加windows右键事件

前端之家收集整理的这篇文章主要介绍了给cocos2dx增加windows右键事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

  1. case WM_RBUTTONDOWN:
  2.  
  3. #if(_MSC_VER >= 1600)
  4. // Don't process message generated by Windows Touch
  5. if (m_bSupportTouch && (s_pfGetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;
  6. #endif /* #if(_MSC_VER >= 1600) */
  7.  
  8. if (m_pDelegate && MK_RBUTTON == wParam)
  9. {
  10. POINT point = {(short)LOWORD(lParam),(short)HIWORD(lParam)};
  11. CCPoint pt(point.x,point.y);
  12. pt.x /= m_fFrameZoomFactor;
  13. pt.y /= m_fFrameZoomFactor;
  14. CCPoint tmp = ccp(pt.x,m_obScreenSize.height - pt.y);
  15. if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp))
  16. {
  17. m_bCaptured = true;
  18. SetCapture(m_hWnd);
  19. int id = 0;
  20. handleRTouchesBegin(1,&id,&pt.x,&pt.y);
  21. }
  22. }
  23. break;
2.修改CCEGLViewProtocol,添加右键的handle,代码如下
  1. void CCEGLViewProtocol::handleRTouchesBegin(int num,int ids[],float xs[],float ys[])
  2. {
  3. CCSet set;
  4. for (int i = 0; i < num; ++i)
  5. {
  6. int id = ids[i];
  7. float x = xs[i];
  8. float y = ys[i];
  9.  
  10. CCInteger* pIndex = (CCInteger*)s_TouchesIntergerDict.objectForKey(id);
  11. int nUnusedIndex = 0;
  12.  
  13. // it is a new touch
  14. if (pIndex == NULL)
  15. {
  16. nUnusedIndex = getUnUsedIndex();
  17.  
  18. // The touches is more than MAX_TOUCHES ?
  19. if (nUnusedIndex == -1) {
  20. CCLOG("The touches is more than MAX_TOUCHES,nUnusedIndex = %d",nUnusedIndex);
  21. continue;
  22. }
  23.  
  24. CCTouch* pTouch = s_pTouches[nUnusedIndex] = new CCTouch();
  25. pTouch->setTouchInfo(nUnusedIndex,(x - m_obViewPortRect.origin.x) / m_fScaleX,(y - m_obViewPortRect.origin.y) / m_fScaleY);
  26. //CCLOG("x = %f y = %f",pTouch->getLocationInView().x,pTouch->getLocationInView().y);
  27. CCInteger* pInterObj = new CCInteger(nUnusedIndex);
  28. s_TouchesIntergerDict.setObject(pInterObj,id);
  29. set.addObject(pTouch);
  30. pInterObj->release();
  31. }
  32. }
  33.  
  34. if (set.count() == 0)
  35. {
  36. CCLOG("touchesBegan: count = 0");
  37. return;
  38. }
  39.  
  40. m_pDelegate->touchesRBegan(&set,NULL);
  41. }
3.ccTouchDispatcher中添加分发事件,代码如下
  1. void CCTouchDispatcher::touchesRBegan(CCSet *touches,CCEvent *pEvent)
  2. {
  3. if (m_bDispatchEvents)
  4. {
  5. this->touches(touches,pEvent,CCRTOUCHBEGAN);
  6. }
  7. }
4.修改ccTouchDispatcher的touch,添加右键
  1. if (uIndex == CCTOUCHBEGAN)
  2. {
  3. bClaimed = pHandler->getDelegate()->ccTouchBegan(pTouch,pEvent);
  4.  
  5. if (bClaimed)
  6. {
  7. pHandler->getClaimedTouches()->addObject(pTouch);
  8. }
  9. } else if(uIndex == CCRTOUCHBEGAN){
  10. bClaimed = pHandler->getDelegate()->ccRTouchBegan(pTouch,pEvent);
  11. if (bClaimed)
  12. {
  13. pHandler->getClaimedTouches()->addObject(pTouch);
  14. }
  15. }else
  16. if (pHandler->getClaimedTouches()->containsObject(pTouch))
  17. {
  18. // moved ended canceled
  19. bClaimed = true;
  20.  
  21. switch (sHelper.m_type)
  22. {
  23. case CCTOUCHMOVED:
  24. pHandler->getDelegate()->ccTouchMoved(pTouch,pEvent);
  25. break;
  26. case CCTOUCHENDED:
  27. pHandler->getDelegate()->ccTouchEnded(pTouch,pEvent);
  28. pHandler->getClaimedTouches()->removeObject(pTouch);
  29. break;
  30. case CCTOUCHCANCELLED:
  31. pHandler->getDelegate()->ccTouchCancelled(pTouch,pEvent);
  32. pHandler->getClaimedTouches()->removeObject(pTouch);
  33. break;
  34. case CCRTOUCHENDED:
  35. pHandler->getDelegate()->ccRTouchEnded(pTouch,pEvent);
  36. pHandler->getClaimedTouches()->removeObject(pTouch);
  37. break;
  38. }
  39. }
5.修改添加CCTouchDelegate右键事件:
  1. virtual int ccRTouchBegan(CCTouch *pTouch,CCEvent *pEvent) {
  2. CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return 0;};
  3. virtual void ccRTouchEnded(CCTouch *pTouch,CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}

6,.为ccnode添加右键事件
  1. int CCNode::ccRTouchBegan(CCTouch *pTouch,CCEvent *pEvent)
  2. {
  3. if (kScriptTypeNone != m_eScriptType)
  4. {
  5. return excuteScriptTouchHandler(CCRTOUCHBEGAN,pTouch);
  6. }
  7.  
  8.  
  9. CC_UNUSED_PARAM(pTouch);
  10. CC_UNUSED_PARAM(pEvent);
  11. CCAssert(false,"Layer#ccTouchBegan override me");
  12. <span style="white-space:pre"> </span>printf("the kCCTouchBegan is %d\n\n",kCCTouchBegan);
  13. return kCCTouchBegan;
  14. }
7.千万别忘了在ccScene中重写右键事件,因为ccScene的作用是分发触摸。
  1. int CCScene::ccRTouchBegan(CCTouch *pTouch,CCEvent *pEvent){
  2. // remove all touch targets
  3. m_touchTargets->removeAllObjects();
  4.  
  5. // check touch targets
  6. const CCPoint p = pTouch->getLocation();
  7. CCObject *node;
  8. CCNode *touchNode = NULL;
  9. CCNode *checkVisibleNode = NULL;
  10. bool visible = true;
  11. sortAllTouchableNodes(m_touchableNodes);
  12. CCARRAY_FOREACH(m_touchableNodes,node)
  13. {
  14. checkVisibleNode = touchNode = dynamic_cast<CCNode*>(node);
  15.  
  16. // check node is visible
  17. visible = true;
  18. do
  19. {
  20. visible = visible && checkVisibleNode->isVisible();
  21. checkVisibleNode = checkVisibleNode->getParent();
  22. } while (checkVisibleNode && visible);
  23. if (!visible) continue;
  24.  
  25. const CCRect boundingBox = touchNode->getCascadeBoundingBox();
  26. if (touchNode->isRunning() && boundingBox.containsPoint(p))
  27. {
  28. touchNode->retain();
  29. int ret = touchNode->ccRTouchBegan(pTouch,pEvent);
  30. if (ret == kCCTouchBegan || ret == kCCTouchBeganNoSwallows)
  31. {
  32. m_touchTargets->addObject(touchNode);
  33. if (ret == kCCTouchBegan)
  34. {
  35. touchNode->release();
  36. break;
  37. }
  38. }
  39. touchNode->release();
  40. }
  41. }
  42.  
  43. sortAllTouchableNodes(m_touchTargets);
  44. return kCCTouchBegan;
  45.  
  46. }
8.添加传递给lua的事件
  1. int CCLuaEngine::executeNodeTouchEvent(CCNode* pNode,int eventType,CCTouch *pTouch)
  2. {
  3. CCTouchScriptHandlerEntry* pScriptHandlerEntry = pNode->getScriptTouchHandlerEntry();
  4. if (!pScriptHandlerEntry){
  5. return 0;
  6. }
  7.  
  8. int nHandler = pScriptHandlerEntry->getHandler();
  9. if (!nHandler){
  10. return 0;
  11. }
  12.  
  13. switch (eventType)
  14. {
  15. case CCTOUCHBEGAN:
  16. m_stack->pushString("began");
  17. break;
  18.  
  19. case CCTOUCHMOVED:
  20. m_stack->pushString("moved");
  21. break;
  22.  
  23. case CCTOUCHENDED:
  24. m_stack->pushString("ended");
  25. break;
  26.  
  27. case CCTOUCHCANCELLED:
  28. m_stack->pushString("cancelled");
  29. break;
  30.  
  31. case CCRTOUCHBEGAN:
  32. m_stack->pushString("rbegan");
  33. break;
  34.  
  35. case CCRTOUCHENDED:
  36. m_stack->pushString("rended");
  37. break;
  38.  
  39. default:
  40. return 0;
  41. }
  42.  
  43. const CCPoint pt = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
  44. const CCPoint prev = CCDirector::sharedDirector()->convertToGL(pTouch->getPrevIoUsLocationInView());
  45. m_stack->pushFloat(pt.x);
  46. m_stack->pushFloat(pt.y);
  47. m_stack->pushFloat(prev.x);
  48. m_stack->pushFloat(prev.y);
  49. int ret = m_stack->executeFunctionByHandler(nHandler,5);
  50. m_stack->clean();
  51.  
  52. return ret;
  53. }

猜你在找的Cocos2d-x相关文章