From a2ee6c37613e7dc2a62f626bfc7bd8977823b0ae Mon Sep 17 00:00:00 2001 From: Rohan Kuruvilla Date: Fri, 26 Oct 2012 17:11:43 -0700 Subject: [PATCH] Changing Touch API in cocos2d-x. Also change ScriptEngine callbacks for targetted Touches --- .../CCLayer.cpp | 108 +++++++++++++++--- .../layers_scenes_transitions_nodes/CCLayer.h | 21 +++- 2 files changed, 110 insertions(+), 19 deletions(-) diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index ac2966fecb..b41df21c3f 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -50,6 +50,8 @@ CCLayer::CCLayer() { setAnchorPoint(ccp(0.5f, 0.5f)); m_bIgnoreAnchorPointForPosition = true; + m_bTouchMode = kCCTouchesAllAtOnce; + m_bTouchPriority = 0; } CCLayer::~CCLayer() @@ -61,7 +63,7 @@ bool CCLayer::init() { bool bRet = false; do - { + { CCDirector * pDirector; CC_BREAK_IF(!(pDirector = CCDirector::sharedDirector())); this->setContentSize(pDirector->getWinSize()); @@ -99,24 +101,36 @@ void CCLayer::registerWithTouchDispatcher() { CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher(); - if (m_pScriptHandlerEntry) - { - if (m_pScriptHandlerEntry->isMultiTouches()) - { - pDispatcher->addStandardDelegate(this, 0); - LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptHandlerEntry->getHandler()); - } - else - { - pDispatcher->addTargetedDelegate(this, - m_pScriptHandlerEntry->getPriority(), - m_pScriptHandlerEntry->getSwallowsTouches()); - LUALOG("[LUA] Add touch event handler: %d", m_pScriptHandlerEntry->getHandler()); - } - return; - } + if (m_pScriptHandlerEntry) + { + CCScriptEngineProtocol* pEngine = CCScriptEngineManager::sharedManager()->getScriptEngine(); + if(pEngine->getScriptType() == kScriptTypeJavascript) { - pDispatcher->addStandardDelegate(this, 0); + if( m_bTouchMode == kCCTouchesAllAtOnce ) { + pDispatcher->addStandardDelegate(this, 0); + } else { + pDispatcher->addTargetedDelegate(this, m_bTouchPriority, true); + } + return; + + } else { + if (m_pScriptHandlerEntry->isMultiTouches()) + { + pDispatcher->addStandardDelegate(this, 0); + LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptHandlerEntry->getHandler()); + } + else + { + pDispatcher->addTargetedDelegate(this, + m_pScriptHandlerEntry->getPriority(), + m_pScriptHandlerEntry->getSwallowsTouches()); + LUALOG("[LUA] Add touch event handler: %d", m_pScriptHandlerEntry->getHandler()); + } + return; + } + } + + pDispatcher->addStandardDelegate(this, 0); } void CCLayer::registerScriptTouchHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches) @@ -167,6 +181,40 @@ void CCLayer::setTouchEnabled(bool enabled) } } + +void CCLayer::setTouchMode(ccTouchesMode mode) { + if(m_bTouchMode != mode) { + m_bTouchMode = mode; + + if( m_bIsTouchEnabled) { + setTouchEnabled(false); + setTouchEnabled(true); + } + } +} + +void CCLayer::setTouchPriority(int priority) { + if(m_bTouchPriority != priority) { + m_bTouchPriority = priority; + + if( m_bIsTouchEnabled) { + setTouchEnabled(false); + setTouchEnabled(true); + } + } +} + +int CCLayer::getTouchPriority() { + return m_bTouchPriority; +} + +int CCLayer::getTouchMode() { + return m_bTouchMode; +} + + + + /// isAccelerometerEnabled getter bool CCLayer::isAccelerometerEnabled() { @@ -194,6 +242,30 @@ void CCLayer::setAccelerometerEnabled(bool enabled) } } + +void CCLayer::setAccelerometerInterval(double interval) { + if (m_bIsAccelerometerEnabled) + { + if (m_bIsRunning) + { + CCDirector* pDirector = CCDirector::sharedDirector(); +// pDirector->getAccelerometer()->setAccelerometerInterval(interval); + } + } +} + + +void CCLayer::didAccelerate(CCAcceleration* pAccelerationValue) { + CC_UNUSED_PARAM(pAccelerationValue); +// +// if ( m_eScriptType != kScriptTypeNone) +// { +// CCScriptEngineManager::sharedManager()->getScriptEngine()->executeAccelerometerEvent(this, pAccelerationValue); +// } + +} + + /// isKeypadEnabled getter bool CCLayer::isKeypadEnabled() { diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h index ecff165334..7c3caf0d96 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.h @@ -36,6 +36,12 @@ THE SOFTWARE. NS_CC_BEGIN +typedef enum { + kCCTouchesAllAtOnce, + kCCTouchesOneByOne, +} ccTouchesMode; + + /** * @addtogroup layer * @{ @@ -80,7 +86,7 @@ public: virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent); - virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);} + virtual void didAccelerate(CCAcceleration* pAccelerationValue); /** If isTouchEnabled, this method is called onEnter. Override it to change the way CCLayer receives touch events. @@ -106,12 +112,21 @@ public: */ bool isTouchEnabled(); void setTouchEnabled(bool value); + + void setTouchMode(ccTouchesMode mode); + void setTouchPriority(int priority); + int getTouchPriority(); + int getTouchMode(); + + /** whether or not it will receive Accelerometer events You can enable / disable accelerometer events with this property. @since v0.8.1 */ bool isAccelerometerEnabled(); void setAccelerometerEnabled(bool value); + void setAccelerometerInterval(double interval); + /** whether or not it will receive keypad events You can enable / disable accelerometer events with this property. it's new in cocos2d-x @@ -128,6 +143,10 @@ protected: private: // Script touch events handler CCTouchScriptHandlerEntry* m_pScriptHandlerEntry; + + int m_bTouchPriority; + ccTouchesMode m_bTouchMode; + int excuteScriptTouchHandler(int nEventType, CCTouch *pTouch); int excuteScriptTouchHandler(int nEventType, CCSet *pTouches); };