2013-07-10 15:04:00 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
#include "tolua_fix.h"
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include "LuaScriptHandlerMgr.h"
|
|
|
|
#include "cocos2d.h"
|
2013-10-17 15:04:30 +08:00
|
|
|
#include "extensions/cocos-ext.h"
|
2013-07-10 15:04:00 +08:00
|
|
|
#include "CCLuaStack.h"
|
|
|
|
#include "CCLuaValue.h"
|
|
|
|
#include "CCLuaEngine.h"
|
2013-07-17 10:15:25 +08:00
|
|
|
#include "Lua_web_socket.h"
|
|
|
|
#include "LuaOpengl.h"
|
2013-07-10 15:04:00 +08:00
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
using namespace cocos2d::extension;
|
|
|
|
|
2013-07-11 13:38:25 +08:00
|
|
|
NS_CC_BEGIN
|
2013-07-10 15:04:00 +08:00
|
|
|
|
|
|
|
ScheduleHandlerDelegate* ScheduleHandlerDelegate::create()
|
|
|
|
{
|
|
|
|
ScheduleHandlerDelegate *ret = new ScheduleHandlerDelegate();
|
|
|
|
if (NULL != ret )
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleHandlerDelegate::scheduleFunc(float elapse)
|
|
|
|
{
|
2013-07-15 10:38:16 +08:00
|
|
|
|
2013-07-10 15:04:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScheduleHandlerDelegate::update(float elapse)
|
|
|
|
{
|
2013-07-15 10:38:16 +08:00
|
|
|
|
2013-07-10 15:04:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-15 10:38:16 +08:00
|
|
|
|
2013-11-22 11:51:07 +08:00
|
|
|
LuaCallFunc * LuaCallFunc::create(const std::function<void(void* ,Node*)>& func)
|
2013-07-10 15:04:00 +08:00
|
|
|
{
|
2013-11-22 11:51:07 +08:00
|
|
|
auto ret = new LuaCallFunc();
|
|
|
|
|
|
|
|
if (ret && ret->initWithFunction(func) ) {
|
2013-07-10 15:04:00 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2013-11-22 11:51:07 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaCallFunc::execute()
|
|
|
|
{
|
|
|
|
if (_functionLua)
|
|
|
|
{
|
|
|
|
_functionLua((void*)this,_target);
|
|
|
|
}
|
2013-07-10 15:04:00 +08:00
|
|
|
else
|
|
|
|
{
|
2013-11-22 11:51:07 +08:00
|
|
|
CallFuncN::execute();
|
2013-07-10 15:04:00 +08:00
|
|
|
}
|
|
|
|
}
|
2013-11-22 11:51:07 +08:00
|
|
|
|
|
|
|
bool LuaCallFunc::initWithFunction(const std::function<void (void*, Node*)> &func)
|
2013-07-15 10:38:16 +08:00
|
|
|
{
|
2013-11-22 11:51:07 +08:00
|
|
|
_functionLua = func;
|
|
|
|
return true;
|
2013-07-15 10:38:16 +08:00
|
|
|
}
|
2013-07-10 15:04:00 +08:00
|
|
|
|
2013-07-15 10:38:16 +08:00
|
|
|
LuaCallFunc* LuaCallFunc::clone() const
|
2013-07-10 15:04:00 +08:00
|
|
|
{
|
2013-09-04 22:17:26 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::CALLFUNC);
|
2013-07-15 10:38:16 +08:00
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return NULL;
|
2013-07-10 15:04:00 +08:00
|
|
|
|
2013-07-15 10:38:16 +08:00
|
|
|
auto ret = new LuaCallFunc();
|
2013-07-10 15:04:00 +08:00
|
|
|
|
2013-11-22 11:51:07 +08:00
|
|
|
if( _functionLua )
|
|
|
|
{
|
|
|
|
ret->initWithFunction(_functionLua);
|
|
|
|
}
|
2013-07-15 10:38:16 +08:00
|
|
|
|
|
|
|
ret->autorelease();
|
2013-11-22 11:51:07 +08:00
|
|
|
|
|
|
|
int newscriptHandler = cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->reallocateScriptHandler(handler);
|
2013-07-15 10:38:16 +08:00
|
|
|
|
2013-11-22 11:51:07 +08:00
|
|
|
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)ret, newscriptHandler, ScriptHandlerMgr::HandlerType::CALLFUNC);
|
|
|
|
|
2013-07-15 10:38:16 +08:00
|
|
|
return ret;
|
2013-07-10 15:04:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ScriptHandlerMgr* ScriptHandlerMgr::_scriptHandlerMgr = NULL;
|
|
|
|
|
|
|
|
ScriptHandlerMgr::ScriptHandlerMgr()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
ScriptHandlerMgr::~ScriptHandlerMgr()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_scriptHandlerMgr);
|
|
|
|
}
|
|
|
|
ScriptHandlerMgr* ScriptHandlerMgr::getInstance()
|
|
|
|
{
|
|
|
|
if (NULL == _scriptHandlerMgr)
|
|
|
|
{
|
|
|
|
_scriptHandlerMgr = new ScriptHandlerMgr();
|
|
|
|
_scriptHandlerMgr->init();
|
|
|
|
}
|
|
|
|
return _scriptHandlerMgr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptHandlerMgr::init()
|
|
|
|
{
|
|
|
|
_mapObjectHandlers.clear();
|
|
|
|
}
|
|
|
|
|
2013-09-04 22:17:26 +08:00
|
|
|
void ScriptHandlerMgr::addObjectHandler(void* object,int handler,ScriptHandlerMgr::HandlerType handlerType)
|
2013-07-10 15:04:00 +08:00
|
|
|
{
|
|
|
|
if (NULL == object)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//may be not need
|
2013-09-04 22:17:26 +08:00
|
|
|
removeObjectHandler(object,handlerType);
|
2013-07-10 15:04:00 +08:00
|
|
|
|
|
|
|
auto iter = _mapObjectHandlers.find(object);
|
2013-09-04 22:17:26 +08:00
|
|
|
VecHandlerPairs vecHandlers;
|
2013-07-10 15:04:00 +08:00
|
|
|
vecHandlers.clear();
|
|
|
|
if (_mapObjectHandlers.end() != iter)
|
|
|
|
{
|
|
|
|
vecHandlers = iter->second;
|
|
|
|
}
|
|
|
|
|
2013-09-04 22:17:26 +08:00
|
|
|
HandlerPair eventHanler = std::make_pair(handlerType, handler);
|
2013-07-10 15:04:00 +08:00
|
|
|
vecHandlers.push_back(eventHanler);
|
|
|
|
_mapObjectHandlers[object] = vecHandlers;
|
|
|
|
}
|
2013-09-04 22:17:26 +08:00
|
|
|
void ScriptHandlerMgr::removeObjectHandler(void* object,ScriptHandlerMgr::HandlerType handlerType)
|
2013-07-10 15:04:00 +08:00
|
|
|
{
|
|
|
|
if (NULL == object || _mapObjectHandlers.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto iterMap = _mapObjectHandlers.find(object);
|
|
|
|
if (_mapObjectHandlers.end() == iterMap)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (iterMap->second.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto iterVec = iterMap->second.begin();
|
|
|
|
bool exist = false;
|
|
|
|
for (; iterVec != iterMap->second.end(); iterVec++)
|
|
|
|
{
|
2013-09-04 22:17:26 +08:00
|
|
|
if (iterVec->first == handlerType)
|
2013-07-10 15:04:00 +08:00
|
|
|
{
|
|
|
|
exist = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exist)
|
|
|
|
{
|
|
|
|
iterMap->second.erase(iterVec);
|
|
|
|
}
|
|
|
|
|
2013-07-15 10:38:16 +08:00
|
|
|
}
|
2013-09-04 22:17:26 +08:00
|
|
|
int ScriptHandlerMgr::getObjectHandler(void* object,ScriptHandlerMgr::HandlerType handlerType)
|
2013-07-10 15:04:00 +08:00
|
|
|
{
|
|
|
|
if (NULL == object || _mapObjectHandlers.empty() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
auto iter = _mapObjectHandlers.find(object);
|
|
|
|
|
|
|
|
if (_mapObjectHandlers.end() != iter)
|
|
|
|
{
|
|
|
|
auto iterVec = (iter->second).begin();
|
|
|
|
for (; iterVec != (iter->second).end(); iterVec++)
|
|
|
|
{
|
2013-09-04 22:17:26 +08:00
|
|
|
if (iterVec->first == handlerType)
|
2013-07-10 15:04:00 +08:00
|
|
|
{
|
|
|
|
return iterVec->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-15 10:38:16 +08:00
|
|
|
void ScriptHandlerMgr::removeObjectAllHandlers(void* object)
|
2013-07-11 13:38:25 +08:00
|
|
|
{
|
|
|
|
if (NULL == object || _mapObjectHandlers.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto iter = _mapObjectHandlers.find(object);
|
|
|
|
|
|
|
|
if (_mapObjectHandlers.end() != iter)
|
|
|
|
{
|
|
|
|
(iter->second).clear();
|
|
|
|
_mapObjectHandlers.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
2013-07-10 15:04:00 +08:00
|
|
|
|
2013-07-17 16:40:31 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
2013-07-17 10:15:25 +08:00
|
|
|
int tolua_Cocos2d_WebSocket_registerScriptHandler00(lua_State* tolua_S)
|
|
|
|
{
|
|
|
|
#ifndef TOLUA_RELEASE
|
|
|
|
tolua_Error tolua_err;
|
|
|
|
if (
|
|
|
|
!tolua_isusertype(tolua_S,1,"WebSocket",0,&tolua_err) ||
|
|
|
|
!toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err) ||
|
|
|
|
!tolua_isnumber(tolua_S,3,0,&tolua_err) ||
|
|
|
|
!tolua_isnoobj(tolua_S,4,&tolua_err)
|
|
|
|
)
|
|
|
|
goto tolua_lerror;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0);
|
|
|
|
if (NULL != self ) {
|
|
|
|
int handler = ( toluafix_ref_function(tolua_S,2,0));
|
2013-09-04 22:17:26 +08:00
|
|
|
ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)((int)tolua_tonumber(tolua_S,3,0) + (int)ScriptHandlerMgr::HandlerType::WEBSOCKET_OPEN);
|
2013-07-17 10:15:25 +08:00
|
|
|
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, handlerType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#ifndef TOLUA_RELEASE
|
|
|
|
tolua_lerror:
|
|
|
|
tolua_error(tolua_S,"#ferror in function 'registerScriptHandler'.",&tolua_err);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int tolua_Cocos2d_WebSocket_unregisterScriptHandler00(lua_State* tolua_S)
|
|
|
|
{
|
|
|
|
#ifndef TOLUA_RELEASE
|
|
|
|
tolua_Error tolua_err;
|
|
|
|
if (
|
|
|
|
!tolua_isusertype(tolua_S,1,"WebSocket",0,&tolua_err) ||
|
|
|
|
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
|
|
|
|
!tolua_isnoobj(tolua_S,3,&tolua_err)
|
|
|
|
)
|
|
|
|
goto tolua_lerror;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0);
|
|
|
|
if (NULL != self ) {
|
2013-09-04 22:17:26 +08:00
|
|
|
ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)((int)tolua_tonumber(tolua_S,2,0) + (int)ScriptHandlerMgr::HandlerType::WEBSOCKET_OPEN);
|
2013-07-17 10:15:25 +08:00
|
|
|
|
|
|
|
ScriptHandlerMgr::getInstance()->removeObjectHandler((void*)self, handlerType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#ifndef TOLUA_RELEASE
|
|
|
|
tolua_lerror:
|
|
|
|
tolua_error(tolua_S,"#ferror in function 'unregisterScriptHandler'.",&tolua_err);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
2013-07-17 16:40:31 +08:00
|
|
|
#endif
|
2013-07-17 10:15:25 +08:00
|
|
|
|
|
|
|
int tolua_Cocos2d_GLNode_registerScriptDrawHandler00(lua_State* tolua_S)
|
|
|
|
{
|
|
|
|
#ifndef TOLUA_RELEASE
|
|
|
|
tolua_Error tolua_err;
|
|
|
|
if (!tolua_isusertype(tolua_S,1,"GLNode",0,&tolua_err) ||
|
|
|
|
(tolua_isvaluenil(tolua_S,2,&tolua_err) || !toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err)) ||
|
|
|
|
!tolua_isnoobj(tolua_S,3,&tolua_err))
|
|
|
|
goto tolua_lerror;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
GLNode* glNode = (GLNode*) tolua_tousertype(tolua_S,1,0);
|
|
|
|
LUA_FUNCTION handler = ( toluafix_ref_function(tolua_S,2,0));
|
2013-09-04 22:17:26 +08:00
|
|
|
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)glNode, handler, ScriptHandlerMgr::HandlerType::GL_NODE_DRAW);
|
2013-07-17 10:15:25 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#ifndef TOLUA_RELEASE
|
|
|
|
tolua_lerror:
|
|
|
|
tolua_error(tolua_S,"#ferror in function 'registerScriptDrawHandler'.",&tolua_err);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int tolua_Cocos2d_GLNode_unregisterScriptDrawHandler00(lua_State* tolua_S)
|
|
|
|
{
|
|
|
|
#ifndef TOLUA_RELEASE
|
|
|
|
tolua_Error tolua_err;
|
|
|
|
if (!tolua_isusertype(tolua_S,1,"GLNode",0,&tolua_err) ||
|
|
|
|
!tolua_isnoobj(tolua_S,2,&tolua_err))
|
|
|
|
goto tolua_lerror;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
GLNode* glNode = (GLNode*)tolua_tousertype(tolua_S,1,0);
|
2013-09-04 22:17:26 +08:00
|
|
|
ScriptHandlerMgr::getInstance()->removeObjectHandler((void*)glNode,ScriptHandlerMgr::HandlerType::GL_NODE_DRAW);
|
2013-07-17 10:15:25 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#ifndef TOLUA_RELEASE
|
|
|
|
tolua_lerror:
|
|
|
|
tolua_error(tolua_S,"#ferror in function 'unregisterScriptDrawHandler'.",&tolua_err);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-15 10:38:16 +08:00
|
|
|
static void tolua_reg_script_handler_mgr_type(lua_State* tolua_S)
|
2013-07-10 15:04:00 +08:00
|
|
|
{
|
2013-07-15 10:38:16 +08:00
|
|
|
tolua_usertype(tolua_S, "ScheduleHandlerDelegate");
|
|
|
|
tolua_usertype(tolua_S, "ScriptHandlerMgr");
|
2013-07-10 15:04:00 +08:00
|
|
|
}
|
2013-07-15 10:38:16 +08:00
|
|
|
/* method: getInstance of class ScriptHandlerMgr */
|
|
|
|
#ifndef TOLUA_DISABLE_tolua_Cocos2d_ScriptHandlerMgr_getInstance00
|
|
|
|
static int tolua_Cocos2d_ScriptHandlerMgr_getInstance00(lua_State* tolua_S)
|
2013-07-10 15:04:00 +08:00
|
|
|
{
|
|
|
|
#ifndef TOLUA_RELEASE
|
|
|
|
tolua_Error tolua_err;
|
2013-07-15 10:38:16 +08:00
|
|
|
if (!tolua_isusertable(tolua_S,1,"ScriptHandlerMgr",0,&tolua_err) ||
|
|
|
|
!tolua_isnoobj(tolua_S,2,&tolua_err) )
|
2013-07-10 15:04:00 +08:00
|
|
|
goto tolua_lerror;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2013-07-15 10:38:16 +08:00
|
|
|
ScriptHandlerMgr* tolua_ret = (ScriptHandlerMgr*) ScriptHandlerMgr::getInstance();
|
|
|
|
tolua_pushusertype(tolua_S,(void*)tolua_ret,"ScriptHandlerMgr");
|
2013-07-10 15:04:00 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
#ifndef TOLUA_RELEASE
|
|
|
|
tolua_lerror:
|
2013-07-15 10:38:16 +08:00
|
|
|
tolua_error(tolua_S,"#ferror in function 'getInstance'.",&tolua_err);
|
2013-07-10 15:04:00 +08:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif //#ifndef TOLUA_DISABLE
|
|
|
|
|
|
|
|
|
|
|
|
TOLUA_API int tolua_script_handler_mgr_open(lua_State* tolua_S)
|
|
|
|
{
|
|
|
|
tolua_open(tolua_S);
|
|
|
|
tolua_reg_script_handler_mgr_type(tolua_S);
|
|
|
|
tolua_module(tolua_S, NULL,0);
|
|
|
|
tolua_beginmodule(tolua_S, NULL);
|
|
|
|
tolua_cclass(tolua_S,"ScriptHandlerMgr","ScriptHandlerMgr","",NULL);
|
|
|
|
tolua_beginmodule(tolua_S, "ScriptHandlerMgr");
|
2013-07-15 10:38:16 +08:00
|
|
|
tolua_function(tolua_S, "getInstance", tolua_Cocos2d_ScriptHandlerMgr_getInstance00);
|
2013-07-10 15:04:00 +08:00
|
|
|
tolua_endmodule(tolua_S);
|
|
|
|
tolua_endmodule(tolua_S);
|
|
|
|
return 1;
|
|
|
|
}
|