From 32ec256ffb482016017559fb75e61575bfe2d042 Mon Sep 17 00:00:00 2001 From: YuLei Liao Date: Thu, 2 Feb 2012 10:38:26 +0800 Subject: [PATCH] * add Touch event script support --- cocos2dx/include/CCLayer.h | 20 +++ .../CCLayer.cpp | 132 +++++++++++++++++- 2 files changed, 149 insertions(+), 3 deletions(-) diff --git a/cocos2dx/include/CCLayer.h b/cocos2dx/include/CCLayer.h index f17471691c..42a4d11d20 100755 --- a/cocos2dx/include/CCLayer.h +++ b/cocos2dx/include/CCLayer.h @@ -56,7 +56,12 @@ public: virtual void onEnter(); virtual void onExit(); virtual void onEnterTransitionDidFinish(); + + // default implements are used to call script callback if exist virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); + virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); + virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); + virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); // default implements are used to call script callback if exist virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); @@ -77,6 +82,13 @@ public: @since v0.8.0 */ virtual void registerWithTouchDispatcher(void); + +#if CC_LUA_ENGINE_ENABLED + /** Register script touch events handler */ + void registerScriptTouchHandler(unsigned int uFuncID, bool isMultiTouches, int nPriority, bool bSwallowsTouches); + /** Unregister script touch events handler */ + void unregisterScriptTouchHandler(void); +#endif virtual void touchDelegateRetain(); virtual void touchDelegateRelease(); @@ -97,6 +109,14 @@ public: it's new in cocos2d-x */ CC_PROPERTY(bool, m_bIsKeypadEnabled, IsKeypadEnabled) + +#if CC_LUA_ENGINE_ENABLED +private: + // Script touch events handler function reference ID + unsigned int m_uScriptHandlerFuncID; + int excuteScriptTouchHandler(int nEventType, CCTouch *pTouch); + int excuteScriptTouchHandler(int nEventType, CCSet *pTouches); +#endif }; // for the subclass of CCLayer, each has to implement the static "node" method diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index 1bec536dc5..cbe97303ca 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -38,6 +38,9 @@ CCLayer::CCLayer() :m_bIsTouchEnabled(false) ,m_bIsAccelerometerEnabled(false) ,m_bIsKeypadEnabled(false) +#if CC_LUA_ENGINE_ENABLED +,m_uScriptHandlerFuncID(0) +#endif { setAnchorPoint(ccp(0.5f, 0.5f)); m_bIsRelativeAnchorPoint = false; @@ -85,6 +88,45 @@ void CCLayer::registerWithTouchDispatcher() CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0); } +#if CC_LUA_ENGINE_ENABLED +void CCLayer::registerScriptTouchHandler(unsigned int uFuncID, bool isMultiTouches, int nPriority, bool bSwallowsTouches) +{ + unregisterScriptTouchHandler(); + if (isMultiTouches) + { + CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this, 0); + LUALOG("[LUA] Add multi-touches event handler: %u", uFuncID); + } + else + { + CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, nPriority, bSwallowsTouches); + LUALOG("[LUA] Add touch event handler: %u", uFuncID); + } + m_uScriptHandlerFuncID = uFuncID; +} + +void CCLayer::unregisterScriptTouchHandler(void) +{ + if (m_uScriptHandlerFuncID) + { + CCLuaEngine::sharedEngine()->removeLuaFuncID(m_uScriptHandlerFuncID); + CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); + LUALOG("[LUA] Remove touch event handler: %u", m_uScriptHandlerFuncID); + } + m_uScriptHandlerFuncID = 0; +} + +int CCLayer::excuteScriptTouchHandler(int nEventType, CCTouch *pTouch) +{ + return CCLuaEngine::sharedEngine()->executeTouchEvent(m_uScriptHandlerFuncID, nEventType, pTouch); +} + +int CCLayer::excuteScriptTouchHandler(int nEventType, CCSet *pTouches) +{ + return CCLuaEngine::sharedEngine()->executeTouchesEvent(m_uScriptHandlerFuncID, nEventType, pTouches); +} +#endif // CC_LUA_ENGINE_ENABLED + /// isTouchEnabled getter bool CCLayer::getIsTouchEnabled() { @@ -106,6 +148,9 @@ void CCLayer::setIsTouchEnabled(bool enabled) { // have problems? CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); +#if CC_LUA_ENGINE_ENABLED + unregisterScriptTouchHandler(); +#endif } } } @@ -194,6 +239,9 @@ void CCLayer::onExit() if( m_bIsTouchEnabled ) { CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); +#if CC_LUA_ENGINE_ENABLED + unregisterScriptTouchHandler(); +#endif } // remove this layer from the delegates who concern Accelerometer Sensor @@ -233,26 +281,104 @@ void CCLayer::touchDelegateRelease() bool CCLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { +#if CC_LUA_ENGINE_ENABLED + if (m_uScriptHandlerFuncID) + { + return excuteScriptTouchHandler(CCTOUCHBEGAN, pTouch); + } +#endif CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); CCAssert(false, "Layer#ccTouchBegan override me"); return true; } -void CCLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) -{ +void CCLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) { +#if CC_LUA_ENGINE_ENABLED + if (m_uScriptHandlerFuncID) + { + excuteScriptTouchHandler(CCTOUCHMOVED, pTouch); + return; + } +#endif + CC_UNUSED_PARAM(pTouch); + CC_UNUSED_PARAM(pEvent); +} + +void CCLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) { +#if CC_LUA_ENGINE_ENABLED + if (m_uScriptHandlerFuncID) + { + excuteScriptTouchHandler(CCTOUCHENDED, pTouch); + return; + } +#endif + CC_UNUSED_PARAM(pTouch); + CC_UNUSED_PARAM(pEvent); } -void CCLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) +void CCLayer::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) { +#if CC_LUA_ENGINE_ENABLED + if (m_uScriptHandlerFuncID) + { + excuteScriptTouchHandler(CCTOUCHCANCELLED, pTouch); + return; + } +#endif + CC_UNUSED_PARAM(pTouch); + CC_UNUSED_PARAM(pEvent); +} + +void CCLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) { +#if CC_LUA_ENGINE_ENABLED + if (m_uScriptHandlerFuncID) + { + excuteScriptTouchHandler(CCTOUCHBEGAN, pTouches); + return; + } +#endif + CC_UNUSED_PARAM(pTouches); + CC_UNUSED_PARAM(pEvent); } void CCLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) { +#if CC_LUA_ENGINE_ENABLED + if (m_uScriptHandlerFuncID) + { + excuteScriptTouchHandler(CCTOUCHMOVED, pTouches); + return; + } +#endif + CC_UNUSED_PARAM(pTouches); + CC_UNUSED_PARAM(pEvent); +} + +void CCLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) +{ +#if CC_LUA_ENGINE_ENABLED + if (m_uScriptHandlerFuncID) + { + excuteScriptTouchHandler(CCTOUCHENDED, pTouches); + return; + } +#endif + CC_UNUSED_PARAM(pTouches); + CC_UNUSED_PARAM(pEvent); } void CCLayer::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent) { +#if CC_LUA_ENGINE_ENABLED + if (m_uScriptHandlerFuncID) + { + excuteScriptTouchHandler(CCTOUCHCANCELLED, pTouches); + return; + } +#endif + CC_UNUSED_PARAM(pTouches); + CC_UNUSED_PARAM(pEvent); } /// ColorLayer