From 9a12e848f597c7c7d5057ba493fb06e92a74f71b Mon Sep 17 00:00:00 2001 From: Ming Date: Fri, 3 Sep 2010 01:32:13 +0000 Subject: [PATCH] fixed #118 --- cocos2dx/include/CCLayer.h | 2 +- cocos2dx/include/CCTouchDelegateProtocol.h | 67 +++++++++--------- cocos2dx/include/CCTouchDispatcher.h | 4 +- .../CCLayer.cpp | 8 +-- .../touch_dispatcher/CCTouchDispatcher.cpp | 20 +++--- cocos2dx/touch_dispatcher/CCTouchHandler.cpp | 68 ++++++++++++++----- cocos2dx/touch_dispatcher/CCTouchHandler.h | 8 +-- 7 files changed, 107 insertions(+), 70 deletions(-) diff --git a/cocos2dx/include/CCLayer.h b/cocos2dx/include/CCLayer.h index 6e0218a038..891652671c 100644 --- a/cocos2dx/include/CCLayer.h +++ b/cocos2dx/include/CCLayer.h @@ -42,7 +42,7 @@ All features from CCNode are valid, plus the following new features: - It can receive Accelerometer input */ /// @todo public UIAccelerometerDelegate -class CCX_DLL CCLayer : public CCNode, public CCStandardTouchDelegate, public CCTargetedTouchDelegate//, public UIAccelerometerDelegate +class CCX_DLL CCLayer : public CCNode, public CCTouchDelegate//, public UIAccelerometerDelegate { public: CCLayer(); diff --git a/cocos2dx/include/CCTouchDelegateProtocol.h b/cocos2dx/include/CCTouchDelegateProtocol.h index ea235964fa..4ac2e9f161 100644 --- a/cocos2dx/include/CCTouchDelegateProtocol.h +++ b/cocos2dx/include/CCTouchDelegateProtocol.h @@ -29,8 +29,6 @@ THE SOFTWARE. #include "ccxCommon.h" namespace cocos2d { -class CCTouch; -class UIEvent; typedef enum { @@ -39,12 +37,11 @@ typedef enum ccTouchDeletateAllBit = (ccTouchDelegateStandardBit | ccTouchDelegateTargetedBit), } ccTouchDelegateFlag; -//class CCX_DLL CCTouchDelegate : virtual public NSObject +class CCTouch; +class UIEvent; +class NSSet; class CCX_DLL CCTouchDelegate { -// public: -// // for RTTI support -// virtual void v() {}; protected: ccTouchDelegateFlag m_eTouchDelegateType; @@ -54,41 +51,49 @@ public: virtual void destroy(void) {} // call the retain() in child (layer or menu) virtual void keep(void) {} -}; -class CCX_DLL CCTargetedTouchDelegate : public CCTouchDelegate -{ -public: - CCTargetedTouchDelegate() { m_eTouchDelegateType = ccTouchDelegateTargetedBit; } - /** Return YES to claim the touch. - @since v0.8 - */ virtual bool ccTouchBegan(CCTouch *pTouch, UIEvent *pEvent) { return false;}; // optional virtual void ccTouchMoved(CCTouch *pTouch, UIEvent *pEvent) {} virtual void ccTouchEnded(CCTouch *pTouch, UIEvent *pEvent) {} virtual void ccTouchCancelled(CCTouch *pTouch, UIEvent *pEvent) {} -}; -/** - CCStandardTouchDelegate. - - This type of delegate is the same one used by CocoaTouch. You will receive all the events (Began,Moved,Ended,Cancelled). - @since v0.8 -*/ - -class NSSet; -class CCX_DLL CCStandardTouchDelegate : public CCTouchDelegate -{ -public: - CCStandardTouchDelegate() { m_eTouchDelegateType = ccTouchDelegateTargetedBit; } // optional - virtual void ccTouchesBegan(NSSet *pTouches, UIEvent *pEvent) {} - virtual void ccTouchesMoved(NSSet *pTouches, UIEvent *pEvent) {} - virtual void ccTouchesEnded(NSSet *pTouches, UIEvent *pEvent) {} - virtual void ccTouchesCancelled(NSSet *pTouches, UIEvent *pEvent) {} + virtual void ccTouchesBegan(NSSet *pTouches, UIEvent *pEvent) {} + virtual void ccTouchesMoved(NSSet *pTouches, UIEvent *pEvent) {} + virtual void ccTouchesEnded(NSSet *pTouches, UIEvent *pEvent) {} + virtual void ccTouchesCancelled(NSSet *pTouches, UIEvent *pEvent) {} }; + +class CCX_DLL CCTargetedTouchDelegate : public CCTouchDelegate + { + public: + CCTargetedTouchDelegate() { m_eTouchDelegateType = ccTouchDelegateTargetedBit; } + // Return YES to claim the touch. + // @since v0 + virtual bool ccTouchBegan(CCTouch *pTouch, UIEvent *pEvent) { return false;}; + + // optional + virtual void ccTouchMoved(CCTouch *pTouch, UIEvent *pEvent) {} + virtual void ccTouchEnded(CCTouch *pTouch, UIEvent *pEvent) {} + virtual void ccTouchCancelled(CCTouch *pTouch, UIEvent *pEvent) {} + }; + + // CCStandardTouchDelegate. + // This type of delegate is the same one used by CocoaTouch. You will receive all the events (Began,Moved,Ended,Cancelled). + // @since v0.8 + class CCX_DLL CCStandardTouchDelegate : public CCTouchDelegate + { + public: + CCStandardTouchDelegate() { m_eTouchDelegateType = ccTouchDelegateTargetedBit; } + // optional + virtual void ccTouchesBegan(NSSet *pTouches, UIEvent *pEvent) {} + virtual void ccTouchesMoved(NSSet *pTouches, UIEvent *pEvent) {} + virtual void ccTouchesEnded(NSSet *pTouches, UIEvent *pEvent) {} + virtual void ccTouchesCancelled(NSSet *pTouches, UIEvent *pEvent) {} + }; + }//namespace cocos2d #endif // __TOUCH_DISPATHCHER_CCTOUCH_DELEGATE_PROTOCOL_H__ diff --git a/cocos2dx/include/CCTouchDispatcher.h b/cocos2dx/include/CCTouchDispatcher.h index f530848d3b..46d923f5aa 100644 --- a/cocos2dx/include/CCTouchDispatcher.h +++ b/cocos2dx/include/CCTouchDispatcher.h @@ -103,13 +103,13 @@ public: See StandardTouchDelegate description. IMPORTANT: The delegate will be retained. */ - void addStandardDelegate(CCStandardTouchDelegate *pDelegate, int nPriority); + void addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority); /** Adds a targeted touch delegate to the dispatcher's list. See TargetedTouchDelegate description. IMPORTANT: The delegate will be retained. */ - void addTargetedDelegate(CCTargetedTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches); + void addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches); /** Removes a touch delegate. The delegate will be released diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp index 43aabee584..ff8e39b8eb 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp @@ -34,7 +34,7 @@ CCLayer::CCLayer() :m_bIsTouchEnabled(false) ,m_bIsAccelerometerEnabled(false) { - CCTouchDelegate::m_eTouchDelegateType = ccTouchDeletateAllBit; + m_eTouchDelegateType = ccTouchDeletateAllBit; m_tAnchorPoint = ccp(0.5f, 0.5f); m_bIsRelativeAnchorPoint = false; } @@ -107,8 +107,7 @@ void CCLayer::setIsTouchEnabled(bool enabled) else { // have problems? - CCTouchDispatcher::getSharedDispatcher()->removeDelegate((CCTargetedTouchDelegate*)(this)); - CCTouchDispatcher::getSharedDispatcher()->removeDelegate((CCStandardTouchDelegate*)(this)); + CCTouchDispatcher::getSharedDispatcher()->removeDelegate(this); } } } @@ -157,8 +156,7 @@ void CCLayer::onExit() { if( m_bIsTouchEnabled ) { - CCTouchDispatcher::getSharedDispatcher()->removeDelegate((CCTargetedTouchDelegate*)(this)); - CCTouchDispatcher::getSharedDispatcher()->removeDelegate((CCStandardTouchDelegate*)(this)); + CCTouchDispatcher::getSharedDispatcher()->removeDelegate(this); } /** if( isAccelerometerEnabled ) diff --git a/cocos2dx/touch_dispatcher/CCTouchDispatcher.cpp b/cocos2dx/touch_dispatcher/CCTouchDispatcher.cpp index 41b160d4d7..811b37e5f8 100644 --- a/cocos2dx/touch_dispatcher/CCTouchDispatcher.cpp +++ b/cocos2dx/touch_dispatcher/CCTouchDispatcher.cpp @@ -133,7 +133,7 @@ void CCTouchDispatcher::forceAddHandler(CCTouchHandler *pHandler, NSMutableArray pHandler->retain(); } -void CCTouchDispatcher::addStandardDelegate(CCStandardTouchDelegate *pDelegate, int nPriority) +void CCTouchDispatcher::addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority) { CCTouchHandler *pHandler = CCStandardTouchHandler::handlerWithDelegate(pDelegate, nPriority); if (! m_bLocked) @@ -147,7 +147,7 @@ void CCTouchDispatcher::addStandardDelegate(CCStandardTouchDelegate *pDelegate, } } -void CCTouchDispatcher::addTargetedDelegate(CCTargetedTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches) +void CCTouchDispatcher::addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches) { CCTouchHandler *pHandler = CCTargetedTouchHandler::handlerWithDelegate(pDelegate, nPriority, bSwallowsTouches); if (! m_bLocked) @@ -282,7 +282,7 @@ void CCTouchDispatcher::touches(NSSet *pTouches, UIEvent *pEvent, unsigned int u bool bClaimed = false; if (uIndex == ccTouchBegan) { - bClaimed = ((CCTargetedTouchDelegate *)(pHandler->getDelegate()))->ccTouchBegan(pTouch, pEvent); + bClaimed = pHandler->getDelegate()->ccTouchBegan(pTouch, pEvent); if (bClaimed) { pHandler->getClaimedTouches()->addObject(pTouch); @@ -296,14 +296,14 @@ void CCTouchDispatcher::touches(NSSet *pTouches, UIEvent *pEvent, unsigned int u switch (sHelper.m_type) { case ccTouchMoved: - ((CCTargetedTouchDelegate*)(pHandler->getDelegate()))->ccTouchMoved(pTouch, pEvent); + pHandler->getDelegate()->ccTouchMoved(pTouch, pEvent); break; case ccTouchEnded: - ((CCTargetedTouchDelegate*)(pHandler->getDelegate()))->ccTouchEnded(pTouch, pEvent); + pHandler->getDelegate()->ccTouchEnded(pTouch, pEvent); pHandler->getClaimedTouches()->removeObject(pTouch); break; case ccTouchCancelled: - ((CCTargetedTouchDelegate*)(pHandler->getDelegate()))->ccTouchCancelled(pTouch, pEvent); + pHandler->getDelegate()->ccTouchCancelled(pTouch, pEvent); pHandler->getClaimedTouches()->removeObject(pTouch); break; } @@ -341,16 +341,16 @@ void CCTouchDispatcher::touches(NSSet *pTouches, UIEvent *pEvent, unsigned int u switch (sHelper.m_type) { case ccTouchBegan: - ((CCStandardTouchDelegate*)(pHandler->getDelegate()))->ccTouchesBegan(pMutableTouches, pEvent); + pHandler->getDelegate()->ccTouchesBegan(pMutableTouches, pEvent); break; case ccTouchMoved: - ((CCStandardTouchDelegate*)(pHandler->getDelegate()))->ccTouchesMoved(pMutableTouches, pEvent); + pHandler->getDelegate()->ccTouchesMoved(pMutableTouches, pEvent); break; case ccTouchEnded: - ((CCStandardTouchDelegate*)(pHandler->getDelegate()))->ccTouchesEnded(pMutableTouches, pEvent); + pHandler->getDelegate()->ccTouchesEnded(pMutableTouches, pEvent); break; case ccTouchCancelled: - ((CCStandardTouchDelegate*)(pHandler->getDelegate()))->ccTouchesCancelled(pMutableTouches, pEvent); + pHandler->getDelegate()->ccTouchesCancelled(pMutableTouches, pEvent); break; } } diff --git a/cocos2dx/touch_dispatcher/CCTouchHandler.cpp b/cocos2dx/touch_dispatcher/CCTouchHandler.cpp index f3f847fdfd..7c7f19f15f 100644 --- a/cocos2dx/touch_dispatcher/CCTouchHandler.cpp +++ b/cocos2dx/touch_dispatcher/CCTouchHandler.cpp @@ -24,6 +24,8 @@ THE SOFTWARE. #include "CCTouchHandler.h" #include "ccMacros.h" +#include "Cocos2dDefine.h" + #include namespace cocos2d { @@ -60,12 +62,23 @@ void CCTouchHandler::setEnalbedSelectors(int nValue) CCTouchHandler* CCTouchHandler::handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority) { CCTouchHandler *pHandler = new CCTouchHandler(); - pHandler->autorelease(); - return pHandler->initWithDelegate(pDelegate, nPriority); + if (pHandler) + { + if (pHandler->initWithDelegate(pDelegate, nPriority)) + { + pHandler->autorelease(); + } + else + { + CCX_SAFE_RELEASE_NULL(pHandler); + } + } + + return pHandler; } -CCTouchHandler* CCTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, int nPriority) +bool CCTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, int nPriority) { assert(pDelegate != NULL); @@ -73,7 +86,7 @@ CCTouchHandler* CCTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, int m_nPriority = nPriority; m_nEnabledSelectors = 0; - return this; + return true; } CCTouchHandler::~CCTouchHandler(void) @@ -82,7 +95,7 @@ CCTouchHandler::~CCTouchHandler(void) } // implementation of CCStandardTouchHandler -CCTouchHandler* CCStandardTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, int nPriority) +bool CCStandardTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, int nPriority) { if (CCTouchHandler::initWithDelegate(pDelegate, nPriority)) { @@ -97,19 +110,30 @@ CCTouchHandler* CCStandardTouchHandler::initWithDelegate(CCTouchDelegate *pDeleg if( [del respondsToSelector:@selector(ccTouchesCancelled:withEvent:)] ) enabledSelectors_ |= ccTouchSelectorCancelledBit; */ + + return true; } - return this; + return false; } -CCStandardTouchHandler* CCStandardTouchHandler::handlerWithDelegate(CCStandardTouchDelegate *pDelegate, int nPriority) +CCStandardTouchHandler* CCStandardTouchHandler::handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority) { - CCStandardTouchHandler* pTouchHandler = new CCStandardTouchHandler(); + CCStandardTouchHandler* pHandler = new CCStandardTouchHandler(); - pTouchHandler->initWithDelegate(pDelegate, nPriority); - pTouchHandler->autorelease(); + if (pHandler) + { + if (pHandler->initWithDelegate(pDelegate, nPriority)) + { + pHandler->autorelease(); + } + else + { + CCX_SAFE_RELEASE_NULL(pHandler); + } + } - return pTouchHandler; + return pHandler; } // implementation of CCTargetedTouchHandler @@ -131,14 +155,23 @@ NSMutableSet* CCTargetedTouchHandler::getClaimedTouches(void) CCTargetedTouchHandler* CCTargetedTouchHandler::handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow) { - CCTargetedTouchHandler *pTargetedHandler = new CCTargetedTouchHandler(); - pTargetedHandler->initWithDelegate(pDelegate, nPriority, bSwallow); - pTargetedHandler->autorelease(); + CCTargetedTouchHandler *pHandler = new CCTargetedTouchHandler(); + if (pHandler) + { + if (pHandler->initWithDelegate(pDelegate, nPriority, bSwallow)) + { + pHandler->autorelease(); + } + else + { + CCX_SAFE_RELEASE_NULL(pHandler); + } + } - return pTargetedHandler; + return pHandler; } -CCTouchHandler* CCTargetedTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow) +bool CCTargetedTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow) { if (CCTouchHandler::initWithDelegate(pDelegate, nPriority)) { @@ -155,9 +188,10 @@ CCTouchHandler* CCTargetedTouchHandler::initWithDelegate(CCTouchDelegate *pDeleg if( [aDelegate respondsToSelector:@selector(ccTouchCancelled:withEvent:)] ) enabledSelectors_ |= ccTouchSelectorCancelledBit; */ + return true; } - return this; + return false; } CCTargetedTouchHandler::~CCTargetedTouchHandler(void) diff --git a/cocos2dx/touch_dispatcher/CCTouchHandler.h b/cocos2dx/touch_dispatcher/CCTouchHandler.h index b74a19aa2a..4752da6c17 100644 --- a/cocos2dx/touch_dispatcher/CCTouchHandler.h +++ b/cocos2dx/touch_dispatcher/CCTouchHandler.h @@ -53,7 +53,7 @@ public: void setEnalbedSelectors(int nValue); // initializes a TouchHandler with a delegate and a priority - virtual CCTouchHandler* initWithDelegate(CCTouchDelegate *pDelegate, int nPriority); + virtual bool initWithDelegate(CCTouchDelegate *pDelegate, int nPriority); public: // allocates a TouchHandler with a delegate and a priority @@ -71,10 +71,10 @@ protected: class CCStandardTouchHandler : public CCTouchHandler { public: - virtual CCTouchHandler* initWithDelegate(CCTouchDelegate *pDelegate, int nPriority); + virtual bool initWithDelegate(CCTouchDelegate *pDelegate, int nPriority); public: - static CCStandardTouchHandler* handlerWithDelegate(CCStandardTouchDelegate *pDelegate, int nPriority); + static CCStandardTouchHandler* handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority); }; /** @@ -94,7 +94,7 @@ public: // MutableSet that contains the claimed touches NSMutableSet* getClaimedTouches(void); - CCTouchHandler* initWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow); + bool initWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow); public: static CCTargetedTouchHandler* handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow);