axmol/extensions/scripting/lua-bindings/manual/base/LuaScriptHandlerMgr.cpp

382 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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.
****************************************************************************/
2022-07-12 21:31:54 +08:00
#include "scripting/lua-bindings/manual/base/LuaScriptHandlerMgr.h"
2019-11-23 20:27:39 +08:00
#include <map>
#include <string>
#include "scripting/lua-bindings/manual/tolua_fix.h"
#include "scripting/lua-bindings/manual/CCLuaStack.h"
#include "scripting/lua-bindings/manual/CCLuaValue.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
USING_NS_AX;
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
ScheduleHandlerDelegate* ScheduleHandlerDelegate::create()
{
2021-12-25 10:04:45 +08:00
ScheduleHandlerDelegate* ret = new ScheduleHandlerDelegate();
2021-12-08 00:11:53 +08:00
ret->autorelease();
return ret;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void ScheduleHandlerDelegate::scheduleFunc(float elapse) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void ScheduleHandlerDelegate::update(float elapse) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
LuaCallFunc* LuaCallFunc::create(const std::function<void(void*, Node*)>& func)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new LuaCallFunc();
2021-12-25 10:04:45 +08:00
if (ret->initWithFunction(func))
{
2019-11-23 20:27:39 +08:00
ret->autorelease();
return ret;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
return nullptr;
}
void LuaCallFunc::execute()
{
if (_functionLua)
{
2021-12-25 10:04:45 +08:00
_functionLua((void*)this, _target);
2019-11-23 20:27:39 +08:00
}
else
{
CallFuncN::execute();
}
}
2021-12-25 10:04:45 +08:00
bool LuaCallFunc::initWithFunction(const std::function<void(void*, Node*)>& func)
2019-11-23 20:27:39 +08:00
{
_functionLua = func;
return true;
}
LuaCallFunc* LuaCallFunc::clone() const
{
2021-12-25 10:04:45 +08:00
int handler =
ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::CALLFUNC);
2019-11-23 20:27:39 +08:00
if (0 == handler)
return nullptr;
2021-12-25 10:04:45 +08:00
2021-12-08 00:11:53 +08:00
auto ret = new LuaCallFunc();
2021-12-25 10:04:45 +08:00
if (_functionLua)
2019-11-23 20:27:39 +08:00
ret->initWithFunction(_functionLua);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ret->autorelease();
2021-12-25 10:04:45 +08:00
int newscriptHandler =
axis::ScriptEngineManager::getInstance()->getScriptEngine()->reallocateScriptHandler(handler);
2021-12-25 10:04:45 +08:00
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)ret, newscriptHandler,
ScriptHandlerMgr::HandlerType::CALLFUNC);
2019-11-23 20:27:39 +08:00
return ret;
}
ScriptHandlerMgr* ScriptHandlerMgr::_scriptHandlerMgr = NULL;
2021-12-25 10:04:45 +08:00
ScriptHandlerMgr::ScriptHandlerMgr() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
ScriptHandlerMgr::~ScriptHandlerMgr() {}
2019-11-23 20:27:39 +08:00
ScriptHandlerMgr* ScriptHandlerMgr::getInstance()
{
if (nullptr == _scriptHandlerMgr)
{
2021-12-08 00:11:53 +08:00
_scriptHandlerMgr = new ScriptHandlerMgr();
2019-11-23 20:27:39 +08:00
_scriptHandlerMgr->init();
}
return _scriptHandlerMgr;
}
void ScriptHandlerMgr::destroyInstance()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_scriptHandlerMgr);
2019-11-23 20:27:39 +08:00
}
void ScriptHandlerMgr::init()
{
_mapObjectHandlers.clear();
}
2021-12-25 10:04:45 +08:00
void ScriptHandlerMgr::addObjectHandler(void* object, int handler, ScriptHandlerMgr::HandlerType handlerType)
2019-11-23 20:27:39 +08:00
{
if (nullptr == object)
return;
2021-12-25 10:04:45 +08:00
// may be not need
removeObjectHandler(object, handlerType);
2019-11-23 20:27:39 +08:00
auto iter = _mapObjectHandlers.find(object);
VecHandlerPairs vecHandlers;
vecHandlers.clear();
if (_mapObjectHandlers.end() != iter)
vecHandlers = iter->second;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
HandlerPair eventHanler = std::make_pair(handlerType, handler);
vecHandlers.push_back(eventHanler);
_mapObjectHandlers[object] = vecHandlers;
}
2021-12-25 10:04:45 +08:00
void ScriptHandlerMgr::removeObjectHandler(void* object, ScriptHandlerMgr::HandlerType handlerType)
2019-11-23 20:27:39 +08:00
{
if (nullptr == object || _mapObjectHandlers.empty())
return;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
auto iterMap = _mapObjectHandlers.find(object);
if (_mapObjectHandlers.end() == iterMap)
return;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (iterMap->second.empty())
return;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
auto iterVec = iterMap->second.begin();
for (; iterVec != iterMap->second.end(); ++iterVec)
{
if (iterVec->first == handlerType)
{
LuaEngine::getInstance()->removeScriptHandler(iterVec->second);
iterMap->second.erase(iterVec);
break;
}
}
}
2021-12-25 10:04:45 +08:00
int ScriptHandlerMgr::getObjectHandler(void* object, ScriptHandlerMgr::HandlerType handlerType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (nullptr == object || _mapObjectHandlers.empty())
2019-11-23 20:27:39 +08:00
return 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
auto iter = _mapObjectHandlers.find(object);
if (_mapObjectHandlers.end() != iter)
2021-12-25 10:04:45 +08:00
for (auto& handlerPair : iter->second)
2019-11-23 20:27:39 +08:00
if (handlerPair.first == handlerType)
return handlerPair.second;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return 0;
}
void ScriptHandlerMgr::removeObjectAllHandlers(void* object)
{
if (nullptr == object || _mapObjectHandlers.empty())
return;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
auto iter = _mapObjectHandlers.find(object);
if (_mapObjectHandlers.end() != iter)
{
if (!iter->second.empty())
{
2021-12-25 10:04:45 +08:00
for (auto& handlerPair : iter->second)
2019-11-23 20:27:39 +08:00
LuaEngine::getInstance()->removeScriptHandler(handlerPair.second);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
(iter->second).clear();
}
_mapObjectHandlers.erase(iter);
}
}
ScriptHandlerMgr::HandlerType ScriptHandlerMgr::addCustomHandler(void* object, int handler)
{
assert(nullptr != object);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
auto iter = _mapObjectHandlers.find(object);
VecHandlerPairs vecHandlers;
vecHandlers.clear();
HandlerType handlerType = HandlerType::EVENT_CUSTOM_BEGAN;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_mapObjectHandlers.end() != iter)
{
vecHandlers = iter->second;
if (!vecHandlers.empty())
handlerType = static_cast<HandlerType>((int)vecHandlers.back().first + 1);
}
assert(handlerType <= HandlerType::EVENT_CUSTOM_ENDED);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
HandlerPair eventHanler = std::make_pair(handlerType, handler);
vecHandlers.push_back(eventHanler);
_mapObjectHandlers[object] = vecHandlers;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return handlerType;
}
NS_AX_END
2019-11-23 20:27:39 +08:00
static void tolua_reg_script_handler_mgr_type(lua_State* tolua_S)
{
tolua_usertype(tolua_S, "ScheduleHandlerDelegate");
tolua_usertype(tolua_S, "ScriptHandlerMgr");
2021-12-25 10:04:45 +08:00
}
2019-11-23 20:27:39 +08:00
/* method: getInstance of class ScriptHandlerMgr */
#ifndef TOLUA_DISABLE_tolua_Cocos2d_ScriptHandlerMgr_getInstance00
static int tolua_Cocos2d_ScriptHandlerMgr_getInstance00(lua_State* tolua_S)
{
2021-12-25 10:04:45 +08:00
# ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (!tolua_isusertable(tolua_S, 1, "ScriptHandlerMgr", 0, &tolua_err) || !tolua_isnoobj(tolua_S, 2, &tolua_err))
2019-11-23 20:27:39 +08:00
goto tolua_lerror;
else
2021-12-25 10:04:45 +08:00
# endif
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ScriptHandlerMgr* tolua_ret = (ScriptHandlerMgr*)ScriptHandlerMgr::getInstance();
tolua_pushusertype(tolua_S, (void*)tolua_ret, "ScriptHandlerMgr");
2019-11-23 20:27:39 +08:00
}
return 1;
2021-12-25 10:04:45 +08:00
# ifndef TOLUA_RELEASE
2019-11-23 20:27:39 +08:00
tolua_lerror:
2021-12-25 10:04:45 +08:00
tolua_error(tolua_S, "#ferror in function 'getInstance'.", &tolua_err);
2019-11-23 20:27:39 +08:00
return 0;
2021-12-25 10:04:45 +08:00
# endif
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
#endif //#ifndef TOLUA_DISABLE
2019-11-23 20:27:39 +08:00
/* method: registerScriptHandler of class ScriptHandlerMgr */
static int tolua_Cocos2d_ScriptHandlerMgr_registerScriptHandler00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
2021-12-25 10:04:45 +08:00
if (!tolua_isusertype(tolua_S, 1, "ScriptHandlerMgr", 0, &tolua_err) ||
2022-07-12 21:31:54 +08:00
!tolua_isusertype(tolua_S, 2, "ax.Ref", 0, &tolua_err) ||
2021-12-25 10:04:45 +08:00
!toluafix_isfunction(tolua_S, 3, "LUA_FUNCTION", 0, &tolua_err) || !tolua_isnumber(tolua_S, 4, 0, &tolua_err) ||
!tolua_isnoobj(tolua_S, 5, &tolua_err))
2019-11-23 20:27:39 +08:00
goto tolua_lerror;
else
#endif
{
axis::ScriptHandlerMgr* scriptHanlderMgr =
static_cast<axis::ScriptHandlerMgr*>(tolua_tousertype(tolua_S, 1, 0));
2019-11-23 20:27:39 +08:00
#ifndef TOLUA_RELEASE
if (nullptr == scriptHanlderMgr)
{
2021-12-25 10:04:45 +08:00
tolua_error(
tolua_S,
"invalid 'scriptHanlderMgr' in function 'tolua_Cocos2d_ScriptHandlerMgr_registerScriptHandler00'\n",
NULL);
2019-11-23 20:27:39 +08:00
return 0;
}
#endif
2021-12-25 10:04:45 +08:00
LUA_FUNCTION handler = toluafix_ref_function(tolua_S, 3, 0);
2019-11-23 20:27:39 +08:00
ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)(int)tolua_tonumber(tolua_S, 4, 0);
2021-12-25 10:04:45 +08:00
scriptHanlderMgr->addObjectHandler(tolua_tousertype(tolua_S, 2, 0), handler, handlerType);
2019-11-23 20:27:39 +08:00
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
2021-12-25 10:04:45 +08:00
tolua_error(tolua_S, "#ferror in function 'registerScriptHandler'.", &tolua_err);
2019-11-23 20:27:39 +08:00
return 0;
#endif
}
/* method: unregisterScriptHandler of class ScriptHandlerMgr */
static int tolua_Cocos2d_ScriptHandlerMgr_unregisterScriptHandler00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
2021-12-25 10:04:45 +08:00
if (!tolua_isusertype(tolua_S, 1, "ScriptHandlerMgr", 0, &tolua_err) ||
2022-07-12 21:31:54 +08:00
!tolua_isusertype(tolua_S, 2, "ax.Ref", 0, &tolua_err) || !tolua_isnumber(tolua_S, 3, 0, &tolua_err) ||
2021-12-25 10:04:45 +08:00
!tolua_isnoobj(tolua_S, 4, &tolua_err))
2019-11-23 20:27:39 +08:00
goto tolua_lerror;
else
#endif
{
axis::ScriptHandlerMgr* scriptHanlderMgr =
static_cast<axis::ScriptHandlerMgr*>(tolua_tousertype(tolua_S, 1, 0));
2019-11-23 20:27:39 +08:00
#ifndef TOLUA_RELEASE
if (nullptr == scriptHanlderMgr)
{
2021-12-25 10:04:45 +08:00
tolua_error(
tolua_S,
"invalid 'scriptHanlderMgr' in function 'tolua_Cocos2d_ScriptHandlerMgr_unregisterScriptHandler00'\n",
NULL);
2019-11-23 20:27:39 +08:00
return 0;
}
#endif
ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)(int)tolua_tonumber(tolua_S, 3, 0);
scriptHanlderMgr->removeObjectHandler(tolua_tousertype(tolua_S, 2, 0), handlerType);
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
2021-12-25 10:04:45 +08:00
tolua_error(tolua_S, "#ferror in function 'unregisterScriptHandler'.", &tolua_err);
2019-11-23 20:27:39 +08:00
return 0;
#endif
}
/* method: removeObjectAllHandlers of class ScriptHandlerMgr */
static int tolua_Cocos2d_ScriptHandlerMgr_removeObjectAllHandlers00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
2021-12-25 10:04:45 +08:00
if (!tolua_isusertype(tolua_S, 1, "ScriptHandlerMgr", 0, &tolua_err) ||
2022-07-12 21:31:54 +08:00
!tolua_isusertype(tolua_S, 2, "ax.Ref", 0, &tolua_err) || !tolua_isnoobj(tolua_S, 3, &tolua_err))
2019-11-23 20:27:39 +08:00
goto tolua_lerror;
else
#endif
{
axis::ScriptHandlerMgr* scriptHanlderMgr =
static_cast<axis::ScriptHandlerMgr*>(tolua_tousertype(tolua_S, 1, 0));
2019-11-23 20:27:39 +08:00
#ifndef TOLUA_RELEASE
if (nullptr == scriptHanlderMgr)
{
2021-12-25 10:04:45 +08:00
tolua_error(
tolua_S,
"invalid 'scriptHanlderMgr' in function 'tolua_Cocos2d_ScriptHandlerMgr_removeObjectAllHandlers00'\n",
NULL);
2019-11-23 20:27:39 +08:00
return 0;
}
#endif
scriptHanlderMgr->removeObjectAllHandlers(tolua_tousertype(tolua_S, 2, 0));
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
2021-12-25 10:04:45 +08:00
tolua_error(tolua_S, "#ferror in function 'removeObjectAllHandlers'.", &tolua_err);
2019-11-23 20:27:39 +08:00
return 0;
#endif
}
TOLUA_API int tolua_script_handler_mgr_open(lua_State* tolua_S)
{
tolua_open(tolua_S);
tolua_reg_script_handler_mgr_type(tolua_S);
2021-12-25 10:04:45 +08:00
tolua_module(tolua_S, NULL, 0);
2019-11-23 20:27:39 +08:00
tolua_beginmodule(tolua_S, NULL);
2021-12-25 10:04:45 +08:00
tolua_cclass(tolua_S, "ScriptHandlerMgr", "ScriptHandlerMgr", "", NULL);
tolua_beginmodule(tolua_S, "ScriptHandlerMgr");
tolua_function(tolua_S, "getInstance", tolua_Cocos2d_ScriptHandlerMgr_getInstance00);
tolua_function(tolua_S, "registerScriptHandler", tolua_Cocos2d_ScriptHandlerMgr_registerScriptHandler00);
tolua_function(tolua_S, "unregisterScriptHandler", tolua_Cocos2d_ScriptHandlerMgr_unregisterScriptHandler00);
tolua_function(tolua_S, "removeObjectAllHandlers", tolua_Cocos2d_ScriptHandlerMgr_removeObjectAllHandlers00);
tolua_endmodule(tolua_S);
2019-11-23 20:27:39 +08:00
tolua_endmodule(tolua_S);
2021-12-25 10:04:45 +08:00
return 1;
2019-11-23 20:27:39 +08:00
}