From e8d1b0a0cfb36d9a6c38d8b2fc9086f714172ecf Mon Sep 17 00:00:00 2001 From: Ming Date: Fri, 16 Jul 2010 03:39:21 +0000 Subject: [PATCH] issue #10: add CCTouchHandler and CCTouchDelegateProtocol.h --- .../CCTouchDelegateProtocol.h | 80 +++++++++ cocos2dx/touch_dispatcher/CCTouchHandler.cpp | 165 ++++++++++++++++++ cocos2dx/touch_dispatcher/CCTouchHandler.h | 106 +++++++++++ 3 files changed, 351 insertions(+) create mode 100644 cocos2dx/touch_dispatcher/CCTouchDelegateProtocol.h create mode 100644 cocos2dx/touch_dispatcher/CCTouchHandler.cpp create mode 100644 cocos2dx/touch_dispatcher/CCTouchHandler.h diff --git a/cocos2dx/touch_dispatcher/CCTouchDelegateProtocol.h b/cocos2dx/touch_dispatcher/CCTouchDelegateProtocol.h new file mode 100644 index 0000000000..50977f07db --- /dev/null +++ b/cocos2dx/touch_dispatcher/CCTouchDelegateProtocol.h @@ -0,0 +1,80 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ + +#ifndef __TOUCH_DISPATHCHER_CCTOUCH_DELEGATE_PROTOCOL_H__ +#define __TOUCH_DISPATHCHER_CCTOUCH_DELEGATE_PROTOCOL_H__ + +#include "cocoa/NSObject.h" + +class UITouch; +class UIEvent; + +typedef enum { + kStandardTouchDelegate, + kTargetedTouchDelegate, +} eTouchDelegteType; + +class CCTouchDelegate : public NSObject +{ +public: + eTouchDelegteType m_eType; +}; + +class CCTargetedTouchDelegate : public CCTouchDelegate +{ +public: + CCTargetedTouchDelegate() {m_eType = kTargetedTouchDelegate;} + + /** Return YES to claim the touch. + @since v0.8 + */ + virtual bool ccTouchBegan(UITouch *pTouch, UIEvent *pEvent) { return false;}; + + // optional + virtual void ccTouchMoved(UITouch *pTouch, UIEvent *pEvent) {} + virtual void ccTouchEnded(UITouch *pTouch, UIEvent *pEvent) {} + virtual void ccTouchCancelled(UITouch *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 CCStandardTouchDelegate : public CCTouchDelegate +{ +public: + CCStandardTouchDelegate() {m_eType = kStandardTouchDelegate;} + + // 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) {} +}; + +#endif // __TOUCH_DISPATHCHER_CCTOUCH_DELEGATE_PROTOCOL_H__ diff --git a/cocos2dx/touch_dispatcher/CCTouchHandler.cpp b/cocos2dx/touch_dispatcher/CCTouchHandler.cpp new file mode 100644 index 0000000000..5b5eaf03ff --- /dev/null +++ b/cocos2dx/touch_dispatcher/CCTouchHandler.cpp @@ -0,0 +1,165 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ + +#include "CCTouchHandler.h" +#include "ccMacros.h" +#include + +CCTouchDelegate* CCTouchHandler::getDelegate(void) +{ + return m_pDelegate; +} + +void CCTouchHandler::setDelegate(CCTouchDelegate *pDelegate) +{ + m_pDelegate = pDelegate; +} + +INT32 CCTouchHandler::getPriority(void) +{ + return m_nPriority; +} + +void CCTouchHandler::setPriority(Int32 nPriority) +{ + m_nPriority = nPriority; +} + +INT32 CCTouchHandler::getEnabledSelectors(void) +{ + return m_nEnabledSelectors; +} + +void CCTouchHandler::setEnalbedSelectors(Int32 nValue) +{ + m_nEnabledSelectors = nValue; +} + +CCTouchHandler* CCTouchHandler::handlerWithDelegate(CCTouchDelegate *pDelegate, Int32 nPriority) +{ + CCTouchHandler *pHandler = new CCTouchHandler(); + pHandler->autorelease(); + + return pHandler->initWithDelegate(pDelegate, nPriority); +} + +CCTouchHandler* CCTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, Int32 nPriority) +{ + assert(pDelegate != NULL); + + m_pDelegate = pDelegate; + m_nPriority = nPriority; + m_nEnabledSelectors = 0; + + return this; +} + +CCTouchHandler::~CCTouchHandler(void) +{ + m_pDelegate->release(); +} + +// implementation of CCStandardTouchHandler +CCTouchHandler* CCStandardTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, Int32 nPriority) +{ + if (__super::initWithDelegate(pDelegate, nPriority)) + { + /* + * we can not do this in c++ + if( [del respondsToSelector:@selector(ccTouchesBegan:withEvent:)] ) + enabledSelectors_ |= ccTouchSelectorBeganBit; + if( [del respondsToSelector:@selector(ccTouchesMoved:withEvent:)] ) + enabledSelectors_ |= ccTouchSelectorMovedBit; + if( [del respondsToSelector:@selector(ccTouchesEnded:withEvent:)] ) + enabledSelectors_ |= ccTouchSelectorEndedBit; + if( [del respondsToSelector:@selector(ccTouchesCancelled:withEvent:)] ) + enabledSelectors_ |= ccTouchSelectorCancelledBit; + */ + } + + return this; +} + +CCStandardTouchHandler* CCStandardTouchHandler::handlerWithDelegate(CCStandardTouchDelegate *pDelegate, Int32 nPriority) +{ + CCStandardTouchHandler* pTouchHandler = new CCStandardTouchHandler(); + + pTouchHandler->initWithDelegate(pDelegate, nPriority); + pTouchHandler->autorelease(); + + return pTouchHandler; +} + +// implementation of CCTargetedTouchHandler + +bool CCTargetedTouchHandler::isSwallowsTouches(void) +{ + return m_bSwallowsTouches; +} + +void CCTargetedTouchHandler::setSwallowsTouches(bool bSwallowsTouches) +{ + m_bSwallowsTouches = bSwallowsTouches; +} + +NSMutableSet* CCTargetedTouchHandler::getClaimedTouches(void) +{ + return m_pClaimedTouches; +} + +CCTargetedTouchHandler* CCTargetedTouchHandler::handlerWithDelegate(CCTouchDelegate *pDelegate, Int32 nPriority, bool bSwallow) +{ + CCTargetedTouchHandler *pTargetedHandler = new CCTargetedTouchHandler(); + pTargetedHandler->initWithDelegate(pDelegate, nPriority, bSwallow); + pTargetedHandler->autorelease(); + + return pTargetedHandler; +} + +CCTouchHandler* CCTargetedTouchHandler::initWithDelegate(CCTouchDelegate *pDelegate, Int32 nPriority, bool bSwallow) +{ + if (__super::initWithDelegate(pDelegate, nPriority)) + { + m_pClaimedTouches = new NSMutableSet(); + m_bSwallowsTouches = bSwallow; + + /* + if( [aDelegate respondsToSelector:@selector(ccTouchBegan:withEvent:)] ) + enabledSelectors_ |= ccTouchSelectorBeganBit; + if( [aDelegate respondsToSelector:@selector(ccTouchMoved:withEvent:)] ) + enabledSelectors_ |= ccTouchSelectorMovedBit; + if( [aDelegate respondsToSelector:@selector(ccTouchEnded:withEvent:)] ) + enabledSelectors_ |= ccTouchSelectorEndedBit; + if( [aDelegate respondsToSelector:@selector(ccTouchCancelled:withEvent:)] ) + enabledSelectors_ |= ccTouchSelectorCancelledBit; + */ + } + + return this; +} + +CCTargetedTouchHandler::~CCTargetedTouchHandler(void) +{ + m_pClaimedTouches->release(); +} diff --git a/cocos2dx/touch_dispatcher/CCTouchHandler.h b/cocos2dx/touch_dispatcher/CCTouchHandler.h new file mode 100644 index 0000000000..3bc78222dc --- /dev/null +++ b/cocos2dx/touch_dispatcher/CCTouchHandler.h @@ -0,0 +1,106 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ + +#ifndef __TOUCH_DISPATCHER_CCTOUCH_HANDLER_H__ +#define __TOUCH_DISPATCHER_CCTOUCH_HANDLER_H__ + +#include "CCTouchDelegateProtocol.h" +#include "CCTouchDispatcher.h" +#include "cocoa/NSObject.h" +#include "cocoa/NSSet.h" + +/** + CCTouchHandler + Object than contains the delegate and priority of the event handler. +*/ +class CCTouchHandler : public NSObject +{ +public: + virtual ~CCTouchHandler(void); + + // delegate + CCTouchDelegate* getDelegate(); + void setDelegate(CCTouchDelegate *pDelegate); + + // priority + INT32 getPriority(void); + void setPriority(INT32 nPriority); + + // enabled selectors + INT32 getEnabledSelectors(void); + void setEnalbedSelectors(INT32 nValue); + + // initializes a TouchHandler with a delegate and a priority + virtual CCTouchHandler* initWithDelegate(CCTouchDelegate *pDelegate, INT32 nPriority); + +public: + // allocates a TouchHandler with a delegate and a priority + static CCTouchHandler* handlerWithDelegate(CCTouchDelegate *pDelegate, INT32 nPriority); + +protected: + CCTouchDelegate *m_pDelegate; + INT32 m_nPriority; + INT32 m_nEnabledSelectors; +}; + +/** CCStandardTouchHandler + It forwardes each event to the delegate. + */ +class CCStandardTouchHandler : public CCTouchHandler +{ +public: + virtual CCTouchHandler* initWithDelegate(CCTouchDelegate *pDelegate, INT32 nPriority); + +public: + static CCStandardTouchHandler* handlerWithDelegate(CCStandardTouchDelegate *pDelegate, INT32 nPriority); +}; + +/** + CCTargetedTouchHandler + Object than contains the claimed touches and if it swallos touches. + Used internally by TouchDispatcher + */ +class CCTargetedTouchHandler : public CCTouchHandler +{ +public: + ~CCTargetedTouchHandler(void); + + // whether or not the touches are swallowed + bool isSwallowsTouches(void); + void setSwallowsTouches(bool bSwallowsTouches); + + // MutableSet that contains the claimed touches + NSMutableSet* getClaimedTouches(void); + + CCTouchHandler* initWithDelegate(CCTouchDelegate *pDelegate, INT32 nPriority, bool bSwallow); + +public: + static CCTargetedTouchHandler* handlerWithDelegate(CCTouchDelegate *pDelegate, INT32 nPriority, bool bSwallow); + +protected: + bool m_bSwallowsTouches; + NSMutableSet *m_pClaimedTouches; +}; + +#endif // __TOUCH_DISPATCHER_CCTOUCH_HANDLER_H__