2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2009 Valentin Milea
|
|
|
|
|
|
|
|
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"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "cocoa/CCObject.h"
|
|
|
|
#include "cocoa/CCArray.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup input
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
enum {
|
|
|
|
CCTOUCHBEGAN,
|
|
|
|
CCTOUCHMOVED,
|
|
|
|
CCTOUCHENDED,
|
|
|
|
CCTOUCHCANCELLED,
|
|
|
|
|
|
|
|
ccTouchMax,
|
|
|
|
};
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class Set;
|
|
|
|
class Event;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
struct ccTouchHandlerHelperData {
|
|
|
|
// we only use the type
|
2013-06-20 14:13:12 +08:00
|
|
|
// void (StandardTouchDelegate::*touchesSel)(Set*, Event*);
|
|
|
|
// void (TargetedTouchDelegate::*touchSel)(NSTouch*, Event*);
|
2013-06-15 14:03:30 +08:00
|
|
|
int _type;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CC_DLL EGLTouchDelegate
|
|
|
|
{
|
|
|
|
public:
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void touchesBegan(Set* touches, Event* pEvent) = 0;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void touchesMoved(Set* touches, Event* pEvent) = 0;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void touchesEnded(Set* touches, Event* pEvent) = 0;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void touchesCancelled(Set* touches, Event* pEvent) = 0;
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
virtual ~EGLTouchDelegate() {}
|
|
|
|
};
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class TouchHandler;
|
2012-04-19 14:35:52 +08:00
|
|
|
struct _ccCArray;
|
2013-06-20 14:13:12 +08:00
|
|
|
/** @brief TouchDispatcher.
|
2012-04-19 14:35:52 +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
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL TouchDispatcher : public Object, public EGLTouchDelegate
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
~TouchDispatcher();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
bool init(void);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
|
|
|
TouchDispatcher()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _targetedHandlers(NULL)
|
|
|
|
, _standardHandlers(NULL)
|
|
|
|
, _handlersToAdd(NULL)
|
|
|
|
, _handlersToRemove(NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
public:
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Whether or not the events are going to be dispatched. Default: true
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
bool isDispatchEvents(void);
|
|
|
|
void setDispatchEvents(bool bDispatchEvents);
|
|
|
|
|
|
|
|
/** Adds a standard touch delegate to the dispatcher's list.
|
|
|
|
See StandardTouchDelegate description.
|
|
|
|
IMPORTANT: The delegate will be retained.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void addStandardDelegate(TouchDelegate *pDelegate, int nPriority);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Adds a targeted touch delegate to the dispatcher's list.
|
|
|
|
See TargetedTouchDelegate description.
|
|
|
|
IMPORTANT: The delegate will be retained.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void addTargetedDelegate(TouchDelegate *pDelegate, int nPriority, bool bSwallowsTouches);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/** Removes a touch delegate.
|
|
|
|
The delegate will be released
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void removeDelegate(TouchDelegate *pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/** Removes all touch delegates, releasing all the delegates
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
void removeAllDelegates(void);
|
|
|
|
|
|
|
|
/** Changes the priority of a previously added delegate. The lower the number,
|
2013-09-13 11:41:20 +08:00
|
|
|
the higher the priority
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void setPriority(int nPriority, TouchDelegate *pDelegate);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void touches(Set *pTouches, Event *pEvent, unsigned int uIndex);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void touchesBegan(Set* touches, Event* pEvent);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void touchesMoved(Set* touches, Event* pEvent);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void touchesEnded(Set* touches, Event* pEvent);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual void touchesCancelled(Set* touches, Event* pEvent);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
public:
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
TouchHandler* findHandler(TouchDelegate *pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
protected:
|
2013-06-20 14:13:12 +08:00
|
|
|
void forceRemoveDelegate(TouchDelegate *pDelegate);
|
|
|
|
void forceAddHandler(TouchHandler *pHandler, Array* pArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
void forceRemoveAllDelegates(void);
|
2013-06-20 14:13:12 +08:00
|
|
|
void rearrangeHandlers(Array* pArray);
|
|
|
|
TouchHandler* findHandler(Array* pArray, TouchDelegate *pDelegate);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
protected:
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* _targetedHandlers;
|
|
|
|
Array* _standardHandlers;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _locked;
|
|
|
|
bool _toAdd;
|
|
|
|
bool _toRemove;
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* _handlersToAdd;
|
2013-06-15 14:03:30 +08:00
|
|
|
struct _ccCArray *_handlersToRemove;
|
|
|
|
bool _toQuit;
|
|
|
|
bool _dispatchEvents;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// 4, 1 for each type of event
|
2013-06-15 14:03:30 +08:00
|
|
|
struct ccTouchHandlerHelperData _handlerHelperData[ccTouchMax];
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of input group
|
|
|
|
/// @}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif // __TOUCH_DISPATCHER_CCTOUCH_DISPATCHER_H__
|