给cocos2dx增加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;
}

时间: 2024-10-10 09:33:10

给cocos2dx增加windows右键事件的相关文章

仿EXCEL插件,智表ZCELL产品V1.7 版本发布,增加自定义右键菜单功能

详细请移步 智表(ZCELL)官网www.zcell.net 更新说明  这次更新主要应用户要求,主要解决了自定义右键菜单事件的支持,并新增了公式中自定义函数传参.快捷键剪切等功能,欢迎大家体验使用. 本次版本更新内容如下: 版本: V1.7发布日期:2019-07-05 1.优化公式中的自定义函数,增加传入参数的支持. 2.增加剪切快捷键CTL+X支持功能. 3.增加自定义右键菜单功能,用户可以自定义开发右键功能,通过BindEvents接口实现. 4.新增获取单元格是否合并单元格接口GetC

安卓给button增加点击事件

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mmmm); //获取XML里面的button Button button=(Button)findViewById(R.id.button1); //给button增加点击事件 button.setOnClickListener(new OnClick

cocos2dx在windows下搭建环境android报错

报错:Program bash is not found in PATH (如果按照我的方法来的话是没有这个错误的,我之前用别的方法的时候有但是后来还是没解决,写出来放到这里做参考吧) 参考原文:http://blog.csdn.net/fuyongbing1986/article/details/11556149 方法: 1.把cygwin/bin加入系统环境变量Path下 2.右击工程-->properties-->C/C++ Build -->ToolChain Edit    确

Cocos2d-x for Windows Phone 用法总结

鉴于诺基亚(微软移动这个没人用的手机)开发者比较少,cocos2dx移植方面更是少的问题,总结一下WP8移植方面的资料,希望对大家有用,自己也当作笔记留念. 1.WP8方面有两种方式创建项目,HelloCpp和TestCpp就是这样,XAML方式和纯c++方式.最好选择xaml方式,因为你有可能会c++和c#进行交互.废话不说,有图有真相. 2.说到c++和c#交互,其实叫C++/CX(C++/CX其实是微软在Win8开发平台下,对C++语言的一种扩展),下面就讲述其用法,概念可以去百度. a)

关于ios苹果系统的中的右键事件,查遍了全网都没有的小技巧。

前阵子公司要求写一套手机端,兼容各种平台和系统,当然,pc端也没有放过. 我用了bootstrap框架和jq.在安卓中的右键事件只需要取消浏览器默认事件,然后长按就可以触发pc端的右键事件,非常好,一套js脚步就可以实现我的效果了. 但是,在ios系统中就不好使了. 没有办法,第一次的解决方法,用的手机专用事件touchend,可是尴尬的事情来了,请求数据的时候,因为id的原因,这里就不能这样干了. 肿么办,FQ去看看老外是怎么解决的. 好了,只需要一段css样式就行了.那就是把鼠标指针从箭头改

Android设备 cocos2dx 骨骼动画注册事件播放音效,退到后台再返回黑屏问题

最近遇到一个cocos2dx 骨骼动画注册事件播放音效,在骨骼动画播放的时候,按HOME键退到桌面,再次打开游戏的时候,会黑屏. 解决办法如下,可能不是太完美,至少解决了大部分问题. 1.在org.cocos2dx.lib下的 Cocos2dxRenderer.java 中添加native方法 public static native void setIsPause(boolean isPause); 2.在Cocos2dxRenderer.java对应的Java_org_cocos2dx_li

C#捕获windows关机事件,在系统关机前做一些自己想做的事

C#捕获windows关机事件,在系统关机前做一些自己想做的事: 有些时候我们可能想在Windows关机时记录或处理一些事情,这里提供几种方法. 方法一: /// <summary> /// 窗口过程的回调函数 /// </summary> /// <param name="m"></param> protected override void WndProc(ref Message m) { switch (m.Msg) { //此消息

WPF捕捉Windows关机事件

1 private const int SC_SCREENSAVE = 0xF140; 2 private const int WM_QUERYENDSESSION = 0x0011; 3 private bool IsScreenSave = false; 4 private bool IsWindowStopClosing = false; 变量 不废话,直接贴代码,构造函数中 public XX{ 1 // 获得窗口句柄 2 IntPtr hwnd = new WindowInteropH

在windows右键菜单中加入自己的程序 [转载]

原文链接: http://blog.csdn.net/marklr/article/details/4006356 在windows右键菜单中加入自己的程序 标签: windowsattributes扩展commandshell文档 2009-03-20 08:44 3369人阅读 评论(1) 收藏 举报  分类: BCB(12)  修改右键菜单应该在注册表中,具体的说,是在HKEY_CLASSES_ROOT根键下(这里可是右键菜单中所有命令的根源哦!).展开该根键,其下的子键主要有两种:扩展名