axmol/cocos2dx/include/CCTouchDelegateProtocol.h

165 lines
6.7 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
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_DISPATHCHER_CCTOUCH_DELEGATE_PROTOCOL_H__
#define __TOUCH_DISPATHCHER_CCTOUCH_DELEGATE_PROTOCOL_H__
2011-06-20 17:31:38 +08:00
#include <string>
#include <map>
#include "CCObject.h"
2011-05-31 14:04:14 +08:00
#include "ccConfig.h"
2011-06-20 17:31:38 +08:00
#include "CCScriptSupport.h"
2010-08-04 15:46:12 +08:00
namespace cocos2d {
2010-09-03 09:32:13 +08:00
class CCTouch;
2011-03-18 09:39:34 +08:00
class CCEvent;
class CCSet;
class CCTouchDispatcher;
class CC_DLL CCTouchDelegate
{
protected:
2011-06-20 17:31:38 +08:00
std::map<int, std::string> *m_pEventTypeFuncMap;
public:
2011-06-20 17:31:38 +08:00
CCTouchDelegate() : m_pEventTypeFuncMap(NULL) {}
virtual ~CCTouchDelegate()
{
CC_SAFE_DELETE(m_pEventTypeFuncMap);
}
2011-06-10 17:51:37 +08:00
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false;};
// optional
2011-04-19 09:32:33 +08:00
2011-06-10 17:51:37 +08:00
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
2010-09-03 09:32:13 +08:00
// optional
2011-06-10 17:51:37 +08:00
virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
2011-05-31 14:04:14 +08:00
/*
* In TouchesTest, class Padle inherits from CCSprite and CCTargetedTouchDelegate.
* When it invoke CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true),
* it will crash in CCTouchHandler::initWithDelegate() because of dynamic_cast() on android.
* I don't know why, so add these functions for the subclass to invoke it's know retain() and
* release().
* More detain info please refer issue #926(cocos2d-x).
*/
virtual void touchDelegateRetain(){}
virtual void touchDelegateRelease(){}
2011-06-20 17:31:38 +08:00
// functions for script call back
inline void registerScriptTouchHandler(int eventType, const char* pszScriptFunctionName)
2011-05-31 14:04:14 +08:00
{
2011-06-20 17:31:38 +08:00
if (m_pEventTypeFuncMap == NULL)
2011-05-31 14:04:14 +08:00
{
2011-06-20 17:31:38 +08:00
m_pEventTypeFuncMap = new std::map<int, std::string>();
2011-05-31 14:04:14 +08:00
}
2011-06-20 17:31:38 +08:00
(*m_pEventTypeFuncMap)[eventType] = pszScriptFunctionName;
2011-05-31 14:04:14 +08:00
}
2011-06-20 17:31:38 +08:00
inline bool isScriptHandlerExist(int eventType)
2011-05-31 14:04:14 +08:00
{
2011-06-20 17:31:38 +08:00
if (m_pEventTypeFuncMap)
2011-05-31 14:04:14 +08:00
{
2011-06-20 17:31:38 +08:00
return (*m_pEventTypeFuncMap)[eventType].size() != 0;
2011-05-31 14:04:14 +08:00
}
2011-06-20 17:31:38 +08:00
return false;
2011-05-31 14:04:14 +08:00
}
2011-06-20 17:31:38 +08:00
inline void excuteScriptTouchHandler(int eventType, CCTouch *pTouch)
2011-05-31 14:04:14 +08:00
{
if (m_pEventTypeFuncMap && CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine())
2011-05-31 14:04:14 +08:00
{
2011-06-21 10:18:43 +08:00
CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeTouchEvent((*m_pEventTypeFuncMap)[eventType].c_str(),
2011-06-20 17:31:38 +08:00
pTouch);
2011-05-31 14:04:14 +08:00
}
2011-06-20 17:31:38 +08:00
2011-05-31 14:04:14 +08:00
}
2011-06-20 17:31:38 +08:00
inline void excuteScriptTouchesHandler(int eventType, CCSet *pTouches)
2011-05-31 14:04:14 +08:00
{
if (m_pEventTypeFuncMap && CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine())
2011-05-31 14:04:14 +08:00
{
2011-06-21 10:18:43 +08:00
CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeTouchesEvent((*m_pEventTypeFuncMap)[eventType].c_str(),
pTouches);
2011-05-31 14:04:14 +08:00
}
}
};
/**
@brief
Using this type of delegate results in two benefits:
- 1. You don't need to deal with CCSets, the dispatcher does the job of splitting
them. You get exactly one UITouch per call.
- 2. You can *claim* a UITouch by returning YES in ccTouchBegan. Updates of claimed
touches are sent only to the delegate(s) that claimed them. So if you get a move/
ended/cancelled update you're sure it's your touch. This frees you from doing a
lot of checks when doing multi-touch.
(The name TargetedTouchDelegate relates to updates "targeting" their specific
handler, without bothering the other handlers.)
@since v0.8
2010-09-29 16:33:52 +08:00
*/
class CC_DLL CCTargetedTouchDelegate : public CCTouchDelegate
2010-09-03 09:32:13 +08:00
{
public:
/** Return YES to claim the touch.
@since v0
*/
2011-06-10 17:51:37 +08:00
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);return false;};
2010-09-03 09:32:13 +08:00
// optional
2011-06-10 17:51:37 +08:00
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
2010-09-03 09:32:13 +08:00
};
2010-09-29 16:33:52 +08:00
/** @brief
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 CC_DLL CCStandardTouchDelegate : public CCTouchDelegate
2010-09-03 09:32:13 +08:00
{
public:
// optional
2011-06-10 17:51:37 +08:00
virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
2010-09-03 09:32:13 +08:00
};
2010-08-04 15:46:12 +08:00
}//namespace cocos2d
#endif // __TOUCH_DISPATHCHER_CCTOUCH_DELEGATE_PROTOCOL_H__