2010-08-04 16:54:58 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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 __COCOA_SELECTOR_PROTOCOL_H__
|
|
|
|
#define __COCOA_SELECTOR_PROTOCOL_H__
|
|
|
|
|
|
|
|
#include "ccTypes.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCObject.h"
|
2011-05-31 14:04:14 +08:00
|
|
|
#include <string>
|
|
|
|
|
2011-06-01 17:51:08 +08:00
|
|
|
#ifdef ENABLE_LUA
|
2011-06-01 20:29:05 +08:00
|
|
|
#include "../lua_support/CCLuaSrcipt.h"
|
2011-05-31 14:04:14 +08:00
|
|
|
#endif
|
|
|
|
|
2010-08-04 16:54:58 +08:00
|
|
|
|
|
|
|
namespace cocos2d {
|
|
|
|
class CCNode;
|
2011-03-07 17:11:57 +08:00
|
|
|
class CCEvent;
|
2010-08-04 16:54:58 +08:00
|
|
|
|
2011-06-01 17:51:08 +08:00
|
|
|
#ifdef ENABLE_LUA
|
2011-05-31 14:04:14 +08:00
|
|
|
enum ccScriptFuncType
|
|
|
|
{
|
|
|
|
ccSEL_Update,
|
|
|
|
ccSEL_Tick,
|
|
|
|
ccSEL_CallFunc,
|
|
|
|
ccSEL_CallFuncN,
|
|
|
|
ccSEL_CallFuncND,
|
|
|
|
ccSEL_MenuHandler,
|
|
|
|
ccSEL_EventHandler,
|
|
|
|
ccSEL_Max,
|
|
|
|
};
|
|
|
|
class CC_DLL CCScriptSelector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string m_scriptFunc[ccSEL_Max];
|
|
|
|
inline bool registerScriptSelector(const char* szType, const char* szSeletor)
|
|
|
|
{
|
|
|
|
if (szType == NULL || szSeletor == NULL || strlen(szType) == 0 || strlen(szSeletor) == 0)
|
|
|
|
{
|
|
|
|
CCLog("registerScriptSelector input parameter error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string strType[] ={"SEL_Update", "SEL_Tick", "SEL_CallFunc", "SEL_CallFuncN", \
|
|
|
|
"SEL_CallFuncND", "SEL_CallFuncO", "SEL_MenuHandler", "SEL_EventHandler"};
|
|
|
|
int nType = -1;
|
|
|
|
for (int i = 0; i < sizeof(strType); i++)
|
|
|
|
{
|
|
|
|
if (strcmp(strType[i].c_str(), szType) == 0)
|
|
|
|
{
|
|
|
|
nType = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nType == -1)
|
|
|
|
{
|
|
|
|
CCLog("registerScriptSelector function type error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_scriptFunc[nType] = szSeletor;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
2011-06-01 17:51:08 +08:00
|
|
|
#ifdef ENABLE_LUA
|
2011-05-31 14:04:14 +08:00
|
|
|
class CC_DLL SelectorProtocol:public CCScriptSelector
|
|
|
|
#else
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL SelectorProtocol
|
2011-05-31 14:04:14 +08:00
|
|
|
#endif
|
2010-08-04 16:54:58 +08:00
|
|
|
{
|
|
|
|
public:
|
2011-06-10 17:51:37 +08:00
|
|
|
virtual void update(ccTime dt) {CC_UNUSED_PARAM(dt);};
|
|
|
|
virtual void tick(ccTime dt){CC_UNUSED_PARAM(dt);};
|
2011-05-31 14:04:14 +08:00
|
|
|
SelectorProtocol(){};
|
2010-08-04 16:54:58 +08:00
|
|
|
virtual void callfunc(){};
|
2011-06-10 17:51:37 +08:00
|
|
|
virtual void callfunc(CCNode* pSender){CC_UNUSED_PARAM(pSender);};
|
|
|
|
virtual void callfunc(CCNode* pSender, void* pData){CC_UNUSED_PARAM(pSender);CC_UNUSED_PARAM(pData);};
|
|
|
|
virtual void menuHandler(CCObject* pSender){CC_UNUSED_PARAM(pSender);};
|
|
|
|
virtual void eventHandler(CCEvent* pEvent) {CC_UNUSED_PARAM(pEvent);};
|
2010-08-30 15:45:39 +08:00
|
|
|
|
|
|
|
// the child call responding retain/release function
|
|
|
|
virtual void selectorProtocolRetain(void) {};
|
|
|
|
virtual void selectorProtocolRelease(void) {};
|
2010-08-04 16:54:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class CCNode;
|
|
|
|
typedef void (SelectorProtocol::*SEL_SCHEDULE)(ccTime);
|
|
|
|
|
|
|
|
typedef void (SelectorProtocol::*SEL_CallFunc)();
|
|
|
|
typedef void (SelectorProtocol::*SEL_CallFuncN)(CCNode*);
|
|
|
|
typedef void (SelectorProtocol::*SEL_CallFuncND)(CCNode*, void*);
|
2011-03-07 17:11:57 +08:00
|
|
|
typedef void (SelectorProtocol::*SEL_CallFuncO)(CCObject*);
|
|
|
|
typedef void (SelectorProtocol::*SEL_MenuHandler)(CCObject*);
|
|
|
|
typedef void (SelectorProtocol::*SEL_EventHandler)(CCEvent*);
|
2010-08-04 16:54:58 +08:00
|
|
|
|
2010-09-03 11:09:47 +08:00
|
|
|
// #define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(*((SEL_SCHEDULE*)(&(&_SELECTOR))) )
|
|
|
|
// #define callfunc_selector(_SELECTOR) (SEL_CallFunc)(*((SEL_CallFunc*)(&(&_SELECTOR))) )
|
|
|
|
// #define callfuncN_selector(_SELECTOR) (SEL_CallFuncN)(*((SEL_CallFuncN*)(&(&_SELECTOR))) )
|
|
|
|
// #define callfuncND_selector(_SELECTOR) (SEL_CallFuncND)(*((SEL_CallFuncND*)(&(&_SELECTOR))) )
|
2010-11-17 16:31:48 +08:00
|
|
|
// #define menu_selector(_SELECTOR) (SEL_MenuHandler)(*((SEL_MenuHandler*)(&(&_SELECTOR)))
|
2010-09-03 11:09:47 +08:00
|
|
|
|
|
|
|
#define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR)
|
|
|
|
#define callfunc_selector(_SELECTOR) (SEL_CallFunc)(&_SELECTOR)
|
|
|
|
#define callfuncN_selector(_SELECTOR) (SEL_CallFuncN)(&_SELECTOR)
|
|
|
|
#define callfuncND_selector(_SELECTOR) (SEL_CallFuncND)(&_SELECTOR)
|
2010-12-23 16:47:29 +08:00
|
|
|
#define callfuncO_selector(_SELECTOR) (SEL_CallFuncO)(&_SELECTOR)
|
2010-11-17 16:31:48 +08:00
|
|
|
#define menu_selector(_SELECTOR) (SEL_MenuHandler)(&_SELECTOR)
|
2010-12-31 09:36:53 +08:00
|
|
|
#define event_selector(_SELECTOR) (SEL_EventHandler)(&_SELECTOR)
|
2011-05-31 14:04:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
inline void schedule_SCHEDULE(SelectorProtocol* pSel,SEL_SCHEDULE pfn, ccTime cc, std::string & strluafnc)
|
|
|
|
{
|
|
|
|
if (pSel && pfn)
|
|
|
|
{
|
|
|
|
(pSel->*pfn)(cc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-01 17:51:08 +08:00
|
|
|
#ifdef ENABLE_LUA
|
2011-05-31 14:04:14 +08:00
|
|
|
CCLuaScriptModule::sharedLuaScriptModule()->executeSchedule(strluafnc, cc);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void schedule_CallFunc(SelectorProtocol* pSel,SEL_CallFunc pfn, std::string & strluafnc)
|
|
|
|
{
|
|
|
|
if (pSel && pfn)
|
|
|
|
{
|
|
|
|
(pSel->*pfn)();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-01 17:51:08 +08:00
|
|
|
#ifdef ENABLE_LUA
|
2011-05-31 14:04:14 +08:00
|
|
|
CCLuaScriptModule::sharedLuaScriptModule()->executeCallFunc(strluafnc);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline void schedule_CallFuncN(SelectorProtocol* pSel,SEL_CallFuncN pfn, CCNode* pNode, std::string & strluafnc)
|
|
|
|
{
|
|
|
|
if (pSel && pfn)
|
|
|
|
{
|
|
|
|
(pSel->*pfn)(pNode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-01 17:51:08 +08:00
|
|
|
#ifdef ENABLE_LUA
|
2011-05-31 14:04:14 +08:00
|
|
|
CCLuaScriptModule::sharedLuaScriptModule()->executeCallFuncN(strluafnc, pNode);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void schedule_CallFuncND(SelectorProtocol* pSel,SEL_CallFuncND pfn, CCNode* pNode, void* pdata, std::string & strluafnc)
|
|
|
|
{
|
|
|
|
if (pSel && pfn)
|
|
|
|
{
|
|
|
|
(pSel->*pfn)(pNode, pdata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-01 17:51:08 +08:00
|
|
|
#ifdef ENABLE_LUA
|
2011-05-31 14:04:14 +08:00
|
|
|
CCLuaScriptModule::sharedLuaScriptModule()->executeCallFuncND(strluafnc, pNode, pdata);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void schedule_MenuHandler(SelectorProtocol* pSel,SEL_MenuHandler pfn, CCObject* pobj, std::string & strluafnc)
|
|
|
|
{
|
|
|
|
if (pSel && pfn)
|
|
|
|
{
|
|
|
|
(pSel->*pfn)(pobj);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-01 17:51:08 +08:00
|
|
|
#ifdef ENABLE_LUA
|
2011-05-31 14:04:14 +08:00
|
|
|
CCLuaScriptModule::sharedLuaScriptModule()->executeMenuHandler(strluafnc.c_str(), pobj);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline void schedule_CallFuncO(SelectorProtocol* pSel,SEL_MenuHandler pfn, CCObject* pobj, std::string & strluafnc)
|
|
|
|
{
|
|
|
|
schedule_MenuHandler(pSel, pfn, pobj, strluafnc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void schedule_EventHandler(SelectorProtocol* pSel,SEL_EventHandler pfn, CCEvent* pEvent, std::string & strluafnc)
|
|
|
|
{
|
|
|
|
if (pSel && pfn)
|
|
|
|
{
|
|
|
|
(pSel->*pfn)(pEvent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-01 17:51:08 +08:00
|
|
|
#ifdef ENABLE_LUA
|
2011-05-31 14:04:14 +08:00
|
|
|
CCLuaScriptModule::sharedLuaScriptModule()->executeEventHandler(strluafnc, pEvent);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-04 16:54:58 +08:00
|
|
|
}//namespace cocos2d
|
|
|
|
|
|
|
|
#endif // __COCOA_SELECTOR_PROTOCOL_H__
|