This commit is contained in:
Ming 2010-09-03 01:32:13 +00:00
parent 082b90ea06
commit 9a12e848f5
7 changed files with 107 additions and 70 deletions

View File

@ -42,7 +42,7 @@ All features from CCNode are valid, plus the following new features:
- It can receive Accelerometer input - It can receive Accelerometer input
*/ */
/// @todo public UIAccelerometerDelegate /// @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: public:
CCLayer(); CCLayer();

View File

@ -29,8 +29,6 @@ THE SOFTWARE.
#include "ccxCommon.h" #include "ccxCommon.h"
namespace cocos2d { namespace cocos2d {
class CCTouch;
class UIEvent;
typedef enum typedef enum
{ {
@ -39,12 +37,11 @@ typedef enum
ccTouchDeletateAllBit = (ccTouchDelegateStandardBit | ccTouchDelegateTargetedBit), ccTouchDeletateAllBit = (ccTouchDelegateStandardBit | ccTouchDelegateTargetedBit),
} ccTouchDelegateFlag; } ccTouchDelegateFlag;
//class CCX_DLL CCTouchDelegate : virtual public NSObject class CCTouch;
class UIEvent;
class NSSet;
class CCX_DLL CCTouchDelegate class CCX_DLL CCTouchDelegate
{ {
// public:
// // for RTTI support
// virtual void v() {};
protected: protected:
ccTouchDelegateFlag m_eTouchDelegateType; ccTouchDelegateFlag m_eTouchDelegateType;
@ -54,41 +51,49 @@ public:
virtual void destroy(void) {} virtual void destroy(void) {}
// call the retain() in child (layer or menu) // call the retain() in child (layer or menu)
virtual void keep(void) {} 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;}; virtual bool ccTouchBegan(CCTouch *pTouch, UIEvent *pEvent) { return false;};
// optional // optional
virtual void ccTouchMoved(CCTouch *pTouch, UIEvent *pEvent) {} virtual void ccTouchMoved(CCTouch *pTouch, UIEvent *pEvent) {}
virtual void ccTouchEnded(CCTouch *pTouch, UIEvent *pEvent) {} virtual void ccTouchEnded(CCTouch *pTouch, UIEvent *pEvent) {}
virtual void ccTouchCancelled(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 // optional
virtual void ccTouchesBegan(NSSet *pTouches, UIEvent *pEvent) {} virtual void ccTouchesBegan(NSSet *pTouches, UIEvent *pEvent) {}
virtual void ccTouchesMoved(NSSet *pTouches, UIEvent *pEvent) {} virtual void ccTouchesMoved(NSSet *pTouches, UIEvent *pEvent) {}
virtual void ccTouchesEnded(NSSet *pTouches, UIEvent *pEvent) {} virtual void ccTouchesEnded(NSSet *pTouches, UIEvent *pEvent) {}
virtual void ccTouchesCancelled(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 }//namespace cocos2d
#endif // __TOUCH_DISPATHCHER_CCTOUCH_DELEGATE_PROTOCOL_H__ #endif // __TOUCH_DISPATHCHER_CCTOUCH_DELEGATE_PROTOCOL_H__

View File

@ -103,13 +103,13 @@ public:
See StandardTouchDelegate description. See StandardTouchDelegate description.
IMPORTANT: The delegate will be retained. 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. /** Adds a targeted touch delegate to the dispatcher's list.
See TargetedTouchDelegate description. See TargetedTouchDelegate description.
IMPORTANT: The delegate will be retained. 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. /** Removes a touch delegate.
The delegate will be released The delegate will be released

View File

@ -34,7 +34,7 @@ CCLayer::CCLayer()
:m_bIsTouchEnabled(false) :m_bIsTouchEnabled(false)
,m_bIsAccelerometerEnabled(false) ,m_bIsAccelerometerEnabled(false)
{ {
CCTouchDelegate::m_eTouchDelegateType = ccTouchDeletateAllBit; m_eTouchDelegateType = ccTouchDeletateAllBit;
m_tAnchorPoint = ccp(0.5f, 0.5f); m_tAnchorPoint = ccp(0.5f, 0.5f);
m_bIsRelativeAnchorPoint = false; m_bIsRelativeAnchorPoint = false;
} }
@ -107,8 +107,7 @@ void CCLayer::setIsTouchEnabled(bool enabled)
else else
{ {
// have problems? // have problems?
CCTouchDispatcher::getSharedDispatcher()->removeDelegate((CCTargetedTouchDelegate*)(this)); CCTouchDispatcher::getSharedDispatcher()->removeDelegate(this);
CCTouchDispatcher::getSharedDispatcher()->removeDelegate((CCStandardTouchDelegate*)(this));
} }
} }
} }
@ -157,8 +156,7 @@ void CCLayer::onExit()
{ {
if( m_bIsTouchEnabled ) if( m_bIsTouchEnabled )
{ {
CCTouchDispatcher::getSharedDispatcher()->removeDelegate((CCTargetedTouchDelegate*)(this)); CCTouchDispatcher::getSharedDispatcher()->removeDelegate(this);
CCTouchDispatcher::getSharedDispatcher()->removeDelegate((CCStandardTouchDelegate*)(this));
} }
/** /**
if( isAccelerometerEnabled ) if( isAccelerometerEnabled )

View File

@ -133,7 +133,7 @@ void CCTouchDispatcher::forceAddHandler(CCTouchHandler *pHandler, NSMutableArray
pHandler->retain(); pHandler->retain();
} }
void CCTouchDispatcher::addStandardDelegate(CCStandardTouchDelegate *pDelegate, int nPriority) void CCTouchDispatcher::addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority)
{ {
CCTouchHandler *pHandler = CCStandardTouchHandler::handlerWithDelegate(pDelegate, nPriority); CCTouchHandler *pHandler = CCStandardTouchHandler::handlerWithDelegate(pDelegate, nPriority);
if (! m_bLocked) 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); CCTouchHandler *pHandler = CCTargetedTouchHandler::handlerWithDelegate(pDelegate, nPriority, bSwallowsTouches);
if (! m_bLocked) if (! m_bLocked)
@ -282,7 +282,7 @@ void CCTouchDispatcher::touches(NSSet *pTouches, UIEvent *pEvent, unsigned int u
bool bClaimed = false; bool bClaimed = false;
if (uIndex == ccTouchBegan) if (uIndex == ccTouchBegan)
{ {
bClaimed = ((CCTargetedTouchDelegate *)(pHandler->getDelegate()))->ccTouchBegan(pTouch, pEvent); bClaimed = pHandler->getDelegate()->ccTouchBegan(pTouch, pEvent);
if (bClaimed) if (bClaimed)
{ {
pHandler->getClaimedTouches()->addObject(pTouch); pHandler->getClaimedTouches()->addObject(pTouch);
@ -296,14 +296,14 @@ void CCTouchDispatcher::touches(NSSet *pTouches, UIEvent *pEvent, unsigned int u
switch (sHelper.m_type) switch (sHelper.m_type)
{ {
case ccTouchMoved: case ccTouchMoved:
((CCTargetedTouchDelegate*)(pHandler->getDelegate()))->ccTouchMoved(pTouch, pEvent); pHandler->getDelegate()->ccTouchMoved(pTouch, pEvent);
break; break;
case ccTouchEnded: case ccTouchEnded:
((CCTargetedTouchDelegate*)(pHandler->getDelegate()))->ccTouchEnded(pTouch, pEvent); pHandler->getDelegate()->ccTouchEnded(pTouch, pEvent);
pHandler->getClaimedTouches()->removeObject(pTouch); pHandler->getClaimedTouches()->removeObject(pTouch);
break; break;
case ccTouchCancelled: case ccTouchCancelled:
((CCTargetedTouchDelegate*)(pHandler->getDelegate()))->ccTouchCancelled(pTouch, pEvent); pHandler->getDelegate()->ccTouchCancelled(pTouch, pEvent);
pHandler->getClaimedTouches()->removeObject(pTouch); pHandler->getClaimedTouches()->removeObject(pTouch);
break; break;
} }
@ -341,16 +341,16 @@ void CCTouchDispatcher::touches(NSSet *pTouches, UIEvent *pEvent, unsigned int u
switch (sHelper.m_type) switch (sHelper.m_type)
{ {
case ccTouchBegan: case ccTouchBegan:
((CCStandardTouchDelegate*)(pHandler->getDelegate()))->ccTouchesBegan(pMutableTouches, pEvent); pHandler->getDelegate()->ccTouchesBegan(pMutableTouches, pEvent);
break; break;
case ccTouchMoved: case ccTouchMoved:
((CCStandardTouchDelegate*)(pHandler->getDelegate()))->ccTouchesMoved(pMutableTouches, pEvent); pHandler->getDelegate()->ccTouchesMoved(pMutableTouches, pEvent);
break; break;
case ccTouchEnded: case ccTouchEnded:
((CCStandardTouchDelegate*)(pHandler->getDelegate()))->ccTouchesEnded(pMutableTouches, pEvent); pHandler->getDelegate()->ccTouchesEnded(pMutableTouches, pEvent);
break; break;
case ccTouchCancelled: case ccTouchCancelled:
((CCStandardTouchDelegate*)(pHandler->getDelegate()))->ccTouchesCancelled(pMutableTouches, pEvent); pHandler->getDelegate()->ccTouchesCancelled(pMutableTouches, pEvent);
break; break;
} }
} }

View File

@ -24,6 +24,8 @@ THE SOFTWARE.
#include "CCTouchHandler.h" #include "CCTouchHandler.h"
#include "ccMacros.h" #include "ccMacros.h"
#include "Cocos2dDefine.h"
#include <assert.h> #include <assert.h>
namespace cocos2d { namespace cocos2d {
@ -60,12 +62,23 @@ void CCTouchHandler::setEnalbedSelectors(int nValue)
CCTouchHandler* CCTouchHandler::handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority) CCTouchHandler* CCTouchHandler::handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority)
{ {
CCTouchHandler *pHandler = new CCTouchHandler(); 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); assert(pDelegate != NULL);
@ -73,7 +86,7 @@ CCTouchHandler* CCTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, int
m_nPriority = nPriority; m_nPriority = nPriority;
m_nEnabledSelectors = 0; m_nEnabledSelectors = 0;
return this; return true;
} }
CCTouchHandler::~CCTouchHandler(void) CCTouchHandler::~CCTouchHandler(void)
@ -82,7 +95,7 @@ CCTouchHandler::~CCTouchHandler(void)
} }
// implementation of CCStandardTouchHandler // implementation of CCStandardTouchHandler
CCTouchHandler* CCStandardTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, int nPriority) bool CCStandardTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, int nPriority)
{ {
if (CCTouchHandler::initWithDelegate(pDelegate, nPriority)) if (CCTouchHandler::initWithDelegate(pDelegate, nPriority))
{ {
@ -97,19 +110,30 @@ CCTouchHandler* CCStandardTouchHandler::initWithDelegate(CCTouchDelegate *pDeleg
if( [del respondsToSelector:@selector(ccTouchesCancelled:withEvent:)] ) if( [del respondsToSelector:@selector(ccTouchesCancelled:withEvent:)] )
enabledSelectors_ |= ccTouchSelectorCancelledBit; 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); if (pHandler)
pTouchHandler->autorelease(); {
if (pHandler->initWithDelegate(pDelegate, nPriority))
{
pHandler->autorelease();
}
else
{
CCX_SAFE_RELEASE_NULL(pHandler);
}
}
return pTouchHandler; return pHandler;
} }
// implementation of CCTargetedTouchHandler // implementation of CCTargetedTouchHandler
@ -131,14 +155,23 @@ NSMutableSet* CCTargetedTouchHandler::getClaimedTouches(void)
CCTargetedTouchHandler* CCTargetedTouchHandler::handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow) CCTargetedTouchHandler* CCTargetedTouchHandler::handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow)
{ {
CCTargetedTouchHandler *pTargetedHandler = new CCTargetedTouchHandler(); CCTargetedTouchHandler *pHandler = new CCTargetedTouchHandler();
pTargetedHandler->initWithDelegate(pDelegate, nPriority, bSwallow); if (pHandler)
pTargetedHandler->autorelease(); {
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)) if (CCTouchHandler::initWithDelegate(pDelegate, nPriority))
{ {
@ -155,9 +188,10 @@ CCTouchHandler* CCTargetedTouchHandler::initWithDelegate(CCTouchDelegate *pDeleg
if( [aDelegate respondsToSelector:@selector(ccTouchCancelled:withEvent:)] ) if( [aDelegate respondsToSelector:@selector(ccTouchCancelled:withEvent:)] )
enabledSelectors_ |= ccTouchSelectorCancelledBit; enabledSelectors_ |= ccTouchSelectorCancelledBit;
*/ */
return true;
} }
return this; return false;
} }
CCTargetedTouchHandler::~CCTargetedTouchHandler(void) CCTargetedTouchHandler::~CCTargetedTouchHandler(void)

View File

@ -53,7 +53,7 @@ public:
void setEnalbedSelectors(int nValue); void setEnalbedSelectors(int nValue);
// initializes a TouchHandler with a delegate and a priority // initializes a TouchHandler with a delegate and a priority
virtual CCTouchHandler* initWithDelegate(CCTouchDelegate *pDelegate, int nPriority); virtual bool initWithDelegate(CCTouchDelegate *pDelegate, int nPriority);
public: public:
// allocates a TouchHandler with a delegate and a priority // allocates a TouchHandler with a delegate and a priority
@ -71,10 +71,10 @@ protected:
class CCStandardTouchHandler : public CCTouchHandler class CCStandardTouchHandler : public CCTouchHandler
{ {
public: public:
virtual CCTouchHandler* initWithDelegate(CCTouchDelegate *pDelegate, int nPriority); virtual bool initWithDelegate(CCTouchDelegate *pDelegate, int nPriority);
public: 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 // MutableSet that contains the claimed touches
NSMutableSet* getClaimedTouches(void); NSMutableSet* getClaimedTouches(void);
CCTouchHandler* initWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow); bool initWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow);
public: public:
static CCTargetedTouchHandler* handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow); static CCTargetedTouchHandler* handlerWithDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallow);