2010-07-16 14:10:18 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 10:59:01 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2009 Valentin Milea
|
2010-07-16 14:10:18 +08:00
|
|
|
|
|
|
|
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_DISPATCHER_H__
|
|
|
|
#define __TOUCH_DISPATCHER_CCTOUCH_DISPATCHER_H__
|
|
|
|
|
|
|
|
#include "CCTouchDelegateProtocol.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCObject.h"
|
|
|
|
#include "CCMutableArray.h"
|
2010-08-04 15:46:12 +08:00
|
|
|
namespace cocos2d {
|
2010-07-16 14:10:18 +08:00
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
ccTouchSelectorBeganBit = 1 << 0,
|
|
|
|
ccTouchSelectorMovedBit = 1 << 1,
|
|
|
|
ccTouchSelectorEndedBit = 1 << 2,
|
|
|
|
ccTouchSelectorCancelledBit = 1 << 3,
|
|
|
|
ccTouchSelectorAllBits = ( ccTouchSelectorBeganBit | ccTouchSelectorMovedBit | ccTouchSelectorEndedBit | ccTouchSelectorCancelledBit),
|
|
|
|
} ccTouchSelectorFlag;
|
|
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ccTouchBegan,
|
|
|
|
ccTouchMoved,
|
|
|
|
ccTouchEnded,
|
|
|
|
ccTouchCancelled,
|
|
|
|
|
|
|
|
ccTouchMax,
|
|
|
|
};
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
class CCSet;
|
2011-03-18 09:39:34 +08:00
|
|
|
class CCEvent;
|
2010-08-02 10:58:00 +08:00
|
|
|
|
|
|
|
struct ccTouchHandlerHelperData {
|
|
|
|
// we only use the type
|
2011-03-18 09:39:34 +08:00
|
|
|
// void (StandardTouchDelegate::*touchesSel)(CCSet*, CCEvent*);
|
|
|
|
// void (TargetedTouchDelegate::*touchSel)(NSTouch*, CCEvent*);
|
2010-08-02 10:58:00 +08:00
|
|
|
int m_type;
|
2010-07-16 14:10:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL EGLTouchDelegate
|
2010-07-16 14:10:18 +08:00
|
|
|
{
|
|
|
|
public:
|
2011-03-18 09:39:34 +08:00
|
|
|
virtual void touchesBegan(CCSet* touches, CCEvent* pEvent) = 0;
|
|
|
|
virtual void touchesMoved(CCSet* touches, CCEvent* pEvent) = 0;
|
|
|
|
virtual void touchesEnded(CCSet* touches, CCEvent* pEvent) = 0;
|
|
|
|
virtual void touchesCancelled(CCSet* touches, CCEvent* pEvent) = 0;
|
2010-09-11 14:51:13 +08:00
|
|
|
|
|
|
|
virtual ~EGLTouchDelegate() {}
|
2010-07-16 14:10:18 +08:00
|
|
|
};
|
|
|
|
|
2010-09-28 18:27:33 +08:00
|
|
|
class CCTouchHandler;
|
|
|
|
struct _ccCArray;
|
|
|
|
/** @brief CCTouchDispatcher.
|
2010-08-02 10:58:00 +08:00
|
|
|
Singleton that handles all the touch events.
|
|
|
|
The dispatcher dispatches events to the registered TouchHandlers.
|
|
|
|
There are 2 different type of touch handlers:
|
|
|
|
- Standard Touch Handlers
|
|
|
|
- Targeted Touch Handlers
|
|
|
|
|
|
|
|
The Standard Touch Handlers work like the CocoaTouch touch handler: a set of touches is passed to the delegate.
|
|
|
|
On the other hand, the Targeted Touch Handlers only receive 1 touch at the time, and they can "swallow" touches (avoid the propagation of the event).
|
|
|
|
|
|
|
|
Firstly, the dispatcher sends the received touches to the targeted touches.
|
|
|
|
These touches can be swallowed by the Targeted Touch Handlers. If there are still remaining touches, then the remaining touches will be sent
|
|
|
|
to the Standard Touch Handlers.
|
|
|
|
|
|
|
|
@since v0.8.0
|
2010-07-16 14:10:18 +08:00
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCTouchDispatcher : public CCObject, public EGLTouchDelegate
|
2010-07-16 14:10:18 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
~CCTouchDispatcher();
|
2010-09-04 12:02:52 +08:00
|
|
|
bool init(void);
|
2011-04-01 16:06:53 +08:00
|
|
|
CCTouchDispatcher()
|
2011-06-10 17:51:37 +08:00
|
|
|
: m_pTargetedHandlers(NULL)
|
|
|
|
, m_pStandardHandlers(NULL)
|
|
|
|
, m_pHandlersToAdd(NULL)
|
2011-04-01 16:06:53 +08:00
|
|
|
, m_pHandlersToRemove(NULL)
|
2011-06-10 17:51:37 +08:00
|
|
|
|
2011-04-01 16:06:53 +08:00
|
|
|
{}
|
2010-07-16 14:10:18 +08:00
|
|
|
|
|
|
|
public:
|
2010-09-28 16:18:05 +08:00
|
|
|
/** Whether or not the events are going to be dispatched. Default: true */
|
2010-07-16 14:10:18 +08:00
|
|
|
bool isDispatchEvents(void);
|
|
|
|
void setDispatchEvents(bool bDispatchEvents);
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
/** Adds a standard touch delegate to the dispatcher's list.
|
|
|
|
See StandardTouchDelegate description.
|
|
|
|
IMPORTANT: The delegate will be retained.
|
2010-07-16 14:10:18 +08:00
|
|
|
*/
|
2010-09-03 09:32:13 +08:00
|
|
|
void addStandardDelegate(CCTouchDelegate *pDelegate, int nPriority);
|
2010-07-16 14:10:18 +08:00
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
/** Adds a targeted touch delegate to the dispatcher's list.
|
|
|
|
See TargetedTouchDelegate description.
|
|
|
|
IMPORTANT: The delegate will be retained.
|
2010-07-16 14:10:18 +08:00
|
|
|
*/
|
2010-09-03 09:32:13 +08:00
|
|
|
void addTargetedDelegate(CCTouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches);
|
2010-07-16 14:10:18 +08:00
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
/** Removes a touch delegate.
|
|
|
|
The delegate will be released
|
2010-07-16 14:10:18 +08:00
|
|
|
*/
|
|
|
|
void removeDelegate(CCTouchDelegate *pDelegate);
|
|
|
|
|
2010-09-28 16:18:05 +08:00
|
|
|
/** Removes all touch delegates, releasing all the delegates */
|
2010-07-16 14:10:18 +08:00
|
|
|
void removeAllDelegates(void);
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
/** Changes the priority of a previously added delegate. The lower the number,
|
2010-07-16 14:10:18 +08:00
|
|
|
the higher the priority */
|
|
|
|
void setPriority(int nPriority, CCTouchDelegate *pDelegate);
|
|
|
|
|
2011-03-18 09:39:34 +08:00
|
|
|
void touches(CCSet *pTouches, CCEvent *pEvent, unsigned int uIndex);
|
2010-07-16 14:10:18 +08:00
|
|
|
|
2011-03-18 09:39:34 +08:00
|
|
|
virtual void touchesBegan(CCSet* touches, CCEvent* pEvent);
|
|
|
|
virtual void touchesMoved(CCSet* touches, CCEvent* pEvent);
|
|
|
|
virtual void touchesEnded(CCSet* touches, CCEvent* pEvent);
|
|
|
|
virtual void touchesCancelled(CCSet* touches, CCEvent* pEvent);
|
2010-07-16 14:10:18 +08:00
|
|
|
|
|
|
|
public:
|
2010-09-28 16:18:05 +08:00
|
|
|
/** singleton of the CCTouchDispatcher */
|
2010-11-16 15:12:09 +08:00
|
|
|
static CCTouchDispatcher* sharedDispatcher();
|
2010-07-16 14:10:18 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void forceRemoveDelegate(CCTouchDelegate *pDelegate);
|
2011-03-07 17:11:57 +08:00
|
|
|
void forceAddHandler(CCTouchHandler *pHandler, CCMutableArray<CCTouchHandler*> *pArray);
|
2010-07-16 14:10:18 +08:00
|
|
|
void forceRemoveAllDelegates(void);
|
|
|
|
|
|
|
|
protected:
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray<CCTouchHandler*> *m_pTargetedHandlers;
|
|
|
|
CCMutableArray<CCTouchHandler*> *m_pStandardHandlers;
|
2010-07-16 14:10:18 +08:00
|
|
|
|
|
|
|
bool m_bLocked;
|
|
|
|
bool m_bToAdd;
|
|
|
|
bool m_bToRemove;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCMutableArray<CCTouchHandler*> *m_pHandlersToAdd;
|
2010-08-27 14:13:32 +08:00
|
|
|
struct _ccCArray *m_pHandlersToRemove;
|
2010-07-16 14:10:18 +08:00
|
|
|
bool m_bToQuit;
|
|
|
|
bool m_bDispatchEvents;
|
|
|
|
|
|
|
|
// 4, 1 for each type of event
|
|
|
|
struct ccTouchHandlerHelperData m_sHandlerHelperData[ccTouchMax];
|
|
|
|
};
|
2010-08-04 15:46:12 +08:00
|
|
|
}//namespace cocos2d
|
2010-07-16 14:10:18 +08:00
|
|
|
|
|
|
|
#endif // __TOUCH_DISPATCHER_CCTOUCH_DISPATCHER_H__
|