axmol/extensions/scripting/lua-bindings/manual/spine/lua_axis_spine_manual.cpp

510 lines
18 KiB
C++
Raw Normal View History

2014-03-10 14:04:58 +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
2014-03-10 14:04:58 +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
2014-03-10 14:04:58 +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
2014-03-10 14:04:58 +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/spine/lua_axis_spine_manual.hpp"
#include "scripting/lua-bindings/auto/lua_axis_spine_auto.hpp"
2016-04-18 15:09:21 +08:00
#include "scripting/lua-bindings/manual/tolua_fix.h"
#include "scripting/lua-bindings/manual/LuaBasicConversions.h"
2022-07-12 21:31:54 +08:00
#include "scripting/lua-bindings/manual/base/LuaScriptHandlerMgr.h"
#include "scripting/lua-bindings/manual/CCLuaValue.h"
2020-08-05 17:04:57 +08:00
#include "spine/spine.h"
#include "spine/spine-cocos2dx.h"
#include "scripting/lua-bindings/manual/spine/LuaSkeletonAnimation.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
2014-03-10 14:04:58 +08:00
using namespace spine;
2022-07-12 21:31:54 +08:00
static int lua_axis_CCSkeletonAnimation_createWithFile(lua_State* L)
2014-03-10 14:04:58 +08:00
{
if (nullptr == L)
2021-12-25 10:04:45 +08:00
return 0;
2014-03-10 14:04:58 +08:00
int argc = 0;
2021-12-25 10:04:45 +08:00
2014-03-10 14:04:58 +08:00
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
2021-12-25 10:04:45 +08:00
if (!tolua_isusertable(L, 1, "sp.SkeletonAnimation", 0, &tolua_err))
goto tolua_lerror;
2014-03-10 14:04:58 +08:00
#endif
2021-12-25 10:04:45 +08:00
2014-03-10 14:04:58 +08:00
argc = lua_gettop(L) - 1;
2021-12-25 10:04:45 +08:00
2014-03-10 14:04:58 +08:00
if (2 == argc)
{
#if COCOS2D_DEBUG >= 1
2021-12-25 10:04:45 +08:00
if (!tolua_isstring(L, 2, 0, &tolua_err) || !tolua_isstring(L, 3, 0, &tolua_err))
2014-03-10 14:04:58 +08:00
{
goto tolua_lerror;
}
#endif
const char* skeletonDataFile = tolua_tostring(L, 2, "");
2021-12-25 10:04:45 +08:00
const char* atlasFile = tolua_tostring(L, 3, "");
2014-03-10 14:04:58 +08:00
auto tolua_ret = LuaSkeletonAnimation::createWithFile(skeletonDataFile, atlasFile);
2021-12-25 10:04:45 +08:00
int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1;
2014-03-10 14:04:58 +08:00
int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL;
2021-12-25 10:04:45 +08:00
toluafix_pushusertype_ccobject(L, nID, pLuaID, (void*)tolua_ret, "sp.SkeletonAnimation");
2014-03-10 14:04:58 +08:00
return 1;
2021-12-25 10:04:45 +08:00
}
else if (3 == argc)
2014-03-10 14:04:58 +08:00
{
#if COCOS2D_DEBUG >= 1
2021-12-25 10:04:45 +08:00
if (!tolua_isstring(L, 2, 0, &tolua_err) || !tolua_isstring(L, 3, 0, &tolua_err) ||
!tolua_isnumber(L, 4, 0, &tolua_err))
2014-03-10 14:04:58 +08:00
{
goto tolua_lerror;
}
#endif
const char* skeletonDataFile = tolua_tostring(L, 2, "");
2021-12-25 10:04:45 +08:00
const char* atlasFile = tolua_tostring(L, 3, "");
LUA_NUMBER scale = tolua_tonumber(L, 4, 1);
2014-03-10 14:04:58 +08:00
auto tolua_ret = LuaSkeletonAnimation::createWithFile(skeletonDataFile, atlasFile, scale);
2021-12-25 10:04:45 +08:00
int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1;
2014-03-10 14:04:58 +08:00
int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL;
2021-12-25 10:04:45 +08:00
toluafix_pushusertype_ccobject(L, nID, pLuaID, (void*)tolua_ret, "sp.SkeletonAnimation");
2014-03-10 14:04:58 +08:00
return 1;
}
2021-12-25 10:04:45 +08:00
luaL_error(L,
"'createWithFile' function of SkeletonAnimation has wrong number of arguments: %d, was expecting %d\n",
argc, 2);
2014-03-10 14:04:58 +08:00
#if COCOS2D_DEBUG >= 1
tolua_lerror:
2021-12-25 10:04:45 +08:00
tolua_error(L, "#ferror in function 'createWithFile'.", &tolua_err);
2014-03-10 14:04:58 +08:00
#endif
return 0;
2014-03-10 14:04:58 +08:00
}
2021-12-25 10:04:45 +08:00
int executeSpineEvent(LuaSkeletonAnimation* skeletonAnimation,
int handler,
spine::EventType eventType,
spine::TrackEntry* entry,
spine::Event* event = nullptr)
{
if (nullptr == skeletonAnimation || 0 == handler)
return 0;
2021-12-25 10:04:45 +08:00
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
if (nullptr == stack)
return 0;
2021-12-25 10:04:45 +08:00
lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
if (nullptr == L)
return 0;
2021-12-25 10:04:45 +08:00
int ret = 0;
2021-12-25 10:04:45 +08:00
2020-08-05 17:04:57 +08:00
std::string animationName = (entry && entry->getAnimation()) ? entry->getAnimation()->getName().buffer() : "";
std::string eventTypeName = "";
2021-12-25 10:04:45 +08:00
switch (eventType)
{
case spine::EventType::EventType_Start:
{
eventTypeName = "start";
}
break;
case spine::EventType::EventType_Interrupt:
{
eventTypeName = "interrupt";
}
break;
case spine::EventType::EventType_End:
{
eventTypeName = "end";
}
break;
case spine::EventType::EventType_Dispose:
{
eventTypeName = "dispose";
}
break;
case spine::EventType::EventType_Complete:
{
eventTypeName = "complete";
}
break;
case spine::EventType::EventType_Event:
{
eventTypeName = "event";
}
break;
default:
break;
}
2021-12-25 10:04:45 +08:00
LuaValueDict spineEvent;
spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("type", LuaValue::stringValue(eventTypeName)));
2021-12-25 10:04:45 +08:00
spineEvent.insert(spineEvent.end(),
LuaValueDict::value_type("trackIndex", LuaValue::intValue(entry->getTrackIndex())));
spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("animation", LuaValue::stringValue(animationName)));
2021-12-25 10:04:45 +08:00
spineEvent.insert(spineEvent.end(),
LuaValueDict::value_type("loopCount", LuaValue::intValue(std::floor(entry->getTrackTime() /
entry->getAnimationEnd()))));
if (nullptr != event)
{
LuaValueDict eventData;
2021-12-25 10:04:45 +08:00
eventData.insert(eventData.end(),
LuaValueDict::value_type("name", LuaValue::stringValue(event->getData().getName().buffer())));
eventData.insert(eventData.end(),
LuaValueDict::value_type("intValue", LuaValue::intValue(event->getData().getIntValue())));
eventData.insert(eventData.end(), LuaValueDict::value_type(
"floatValue", LuaValue::floatValue(event->getData().getFloatValue())));
eventData.insert(
eventData.end(),
LuaValueDict::value_type("stringValue", LuaValue::stringValue(event->getData().getStringValue().buffer())));
spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("eventData", LuaValue::dictValue(eventData)));
}
2021-12-25 10:04:45 +08:00
stack->pushLuaValueDict(spineEvent);
ret = stack->executeFunctionByHandler(handler, 1);
return ret;
}
2014-03-10 14:04:58 +08:00
int tolua_Cocos2d_CCSkeletonAnimation_registerSpineEventHandler00(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, "sp.SkeletonAnimation", 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))
2014-03-10 14:04:58 +08:00
goto tolua_lerror;
else
#endif
{
2021-12-25 10:04:45 +08:00
LuaSkeletonAnimation* self = (LuaSkeletonAnimation*)tolua_tousertype(tolua_S, 1, 0);
if (NULL != self)
{
int handler = (toluafix_ref_function(tolua_S, 2, 0));
2020-08-05 17:04:57 +08:00
spine::EventType eventType = static_cast<spine::EventType>((int)tolua_tonumber(tolua_S, 3, 0));
2021-12-25 10:04:45 +08:00
switch (eventType)
{
case spine::EventType::EventType_Start:
{
self->setStartListener(
[=](spine::TrackEntry* entry) { executeSpineEvent(self, handler, eventType, entry); });
ScriptHandlerMgr::getInstance()->addObjectHandler(
(void*)self, handler, ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_START);
}
break;
case spine::EventType::EventType_Interrupt:
{
self->setInterruptListener(
[=](spine::TrackEntry* entry) { executeSpineEvent(self, handler, eventType, entry); });
ScriptHandlerMgr::getInstance()->addObjectHandler(
(void*)self, handler, ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_INTERRUPT);
}
break;
case spine::EventType::EventType_End:
{
self->setEndListener(
[=](spine::TrackEntry* entry) { executeSpineEvent(self, handler, eventType, entry); });
ScriptHandlerMgr::getInstance()->addObjectHandler(
(void*)self, handler, ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_END);
}
break;
case spine::EventType::EventType_Dispose:
{
self->setDisposeListener(
[=](spine::TrackEntry* entry) { executeSpineEvent(self, handler, eventType, entry); });
ScriptHandlerMgr::getInstance()->addObjectHandler(
(void*)self, handler, ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_DISPOSE);
}
break;
case spine::EventType::EventType_Complete:
{
self->setCompleteListener(
[=](spine::TrackEntry* entry) { executeSpineEvent(self, handler, eventType, entry); });
ScriptHandlerMgr::getInstance()->addObjectHandler(
(void*)self, handler, ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_COMPLETE);
}
break;
case spine::EventType::EventType_Event:
{
self->setEventListener([=](spine::TrackEntry* entry, spine::Event* event) {
executeSpineEvent(self, handler, eventType, entry, event);
});
ScriptHandlerMgr::getInstance()->addObjectHandler(
(void*)self, handler, ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_EVENT);
}
break;
default:
break;
}
2014-03-10 14:04:58 +08:00
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
2021-12-25 10:04:45 +08:00
tolua_error(tolua_S, "#ferror in function 'registerSpineEventHandler'.", &tolua_err);
2014-03-10 14:04:58 +08:00
return 0;
#endif
}
int tolua_Cocos2d_CCSkeletonAnimation_unregisterSpineEventHandler00(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, "sp.SkeletonAnimation", 0, &tolua_err) ||
!tolua_isnumber(tolua_S, 2, 0, &tolua_err) || !tolua_isnoobj(tolua_S, 3, &tolua_err))
2014-03-10 14:04:58 +08:00
goto tolua_lerror;
else
#endif
{
2021-12-25 10:04:45 +08:00
LuaSkeletonAnimation* self = (LuaSkeletonAnimation*)tolua_tousertype(tolua_S, 1, 0);
if (NULL != self)
{
2020-08-05 17:04:57 +08:00
spine::EventType eventType = static_cast<spine::EventType>((int)tolua_tonumber(tolua_S, 2, 0));
ScriptHandlerMgr::HandlerType handlerType = ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_START;
2021-12-25 10:04:45 +08:00
switch (eventType)
{
case spine::EventType::EventType_Start:
handlerType = ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_START;
self->setStartListener(nullptr);
break;
case spine::EventType::EventType_Interrupt:
handlerType = ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_INTERRUPT;
break;
case spine::EventType::EventType_End:
handlerType = ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_END;
self->setEndListener(nullptr);
break;
case spine::EventType::EventType_Dispose:
handlerType = ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_DISPOSE;
break;
case spine::EventType::EventType_Complete:
handlerType = ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_COMPLETE;
self->setCompleteListener(nullptr);
break;
case spine::EventType::EventType_Event:
handlerType = ScriptHandlerMgr::HandlerType::EVENT_SPINE_ANIMATION_EVENT;
self->setEventListener(nullptr);
break;
default:
break;
}
ScriptHandlerMgr::getInstance()->removeObjectHandler((void*)self, handlerType);
2014-03-10 14:04:58 +08:00
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
2021-12-25 10:04:45 +08:00
tolua_error(tolua_S, "#ferror in function 'unregisterScriptHandler'.", &tolua_err);
2014-03-10 14:04:58 +08:00
return 0;
#endif
}
2022-07-12 21:31:54 +08:00
static int lua_axis_spine_SkeletonAnimation_addAnimation(lua_State* tolua_S)
{
2021-12-25 10:04:45 +08:00
int argc = 0;
spine::SkeletonAnimation* cobj = nullptr;
2021-12-25 10:04:45 +08:00
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
2021-12-25 10:04:45 +08:00
#if COCOS2D_DEBUG >= 1
2021-12-25 10:04:45 +08:00
if (!tolua_isusertype(tolua_S, 1, "sp.SkeletonAnimation", 0, &tolua_err))
goto tolua_lerror;
#endif
2021-12-25 10:04:45 +08:00
cobj = (spine::SkeletonAnimation*)tolua_tousertype(tolua_S, 1, 0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
2022-07-12 21:31:54 +08:00
tolua_error(tolua_S, "invalid 'cobj' in function 'lua_axis_spine_SkeletonAnimation_addAnimation'", nullptr);
return 0;
}
#endif
2021-12-25 10:04:45 +08:00
argc = lua_gettop(tolua_S) - 1;
if (argc == 3)
{
int arg0;
const char* arg1;
bool arg2;
2021-12-25 10:04:45 +08:00
ok &= luaval_to_int32(tolua_S, 2, (int*)&arg0, "sp.SkeletonAnimation:addAnimation");
std::string arg1_tmp;
ok &= luaval_to_std_string(tolua_S, 3, &arg1_tmp, "sp.SkeletonAnimation:addAnimation");
arg1 = arg1_tmp.c_str();
ok &= luaval_to_boolean(tolua_S, 4, &arg2, "sp.SkeletonAnimation:addAnimation");
if (!ok)
return 0;
cobj->addAnimation(arg0, arg1, arg2);
2021-12-25 10:04:45 +08:00
lua_settop(tolua_S, 1);
return 1;
}
if (argc == 4)
{
int arg0;
const char* arg1;
bool arg2;
double arg3;
2021-12-25 10:04:45 +08:00
ok &= luaval_to_int32(tolua_S, 2, (int*)&arg0, "sp.SkeletonAnimation:addAnimation");
std::string arg1_tmp;
ok &= luaval_to_std_string(tolua_S, 3, &arg1_tmp, "sp.SkeletonAnimation:addAnimation");
arg1 = arg1_tmp.c_str();
ok &= luaval_to_boolean(tolua_S, 4, &arg2, "sp.SkeletonAnimation:addAnimation");
ok &= luaval_to_number(tolua_S, 5, &arg3, "sp.SkeletonAnimation:addAnimation");
if (!ok)
return 0;
2021-12-25 10:04:45 +08:00
cobj->addAnimation(arg0, arg1, arg2, arg3);
lua_settop(tolua_S, 1);
return 1;
}
2021-12-25 10:04:45 +08:00
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "addAnimation", argc, 3);
return 0;
2021-12-25 10:04:45 +08:00
#if COCOS2D_DEBUG >= 1
tolua_lerror:
2022-07-12 21:31:54 +08:00
tolua_error(tolua_S, "#ferror in function 'lua_axis_spine_SkeletonAnimation_addAnimation'.", &tolua_err);
#endif
2021-12-25 10:04:45 +08:00
return 0;
}
2022-07-12 21:31:54 +08:00
static int lua_axis_spine_SkeletonAnimation_setAnimation(lua_State* tolua_S)
{
2021-12-25 10:04:45 +08:00
int argc = 0;
spine::SkeletonAnimation* cobj = nullptr;
2021-12-25 10:04:45 +08:00
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
2021-12-25 10:04:45 +08:00
#if COCOS2D_DEBUG >= 1
2021-12-25 10:04:45 +08:00
if (!tolua_isusertype(tolua_S, 1, "sp.SkeletonAnimation", 0, &tolua_err))
goto tolua_lerror;
#endif
2021-12-25 10:04:45 +08:00
cobj = (spine::SkeletonAnimation*)tolua_tousertype(tolua_S, 1, 0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
2022-07-12 21:31:54 +08:00
tolua_error(tolua_S, "invalid 'cobj' in function 'lua_axis_spine_SkeletonAnimation_setAnimation'", nullptr);
return 0;
}
#endif
2021-12-25 10:04:45 +08:00
argc = lua_gettop(tolua_S) - 1;
if (argc == 3)
{
int arg0;
const char* arg1;
bool arg2;
2021-12-25 10:04:45 +08:00
ok &= luaval_to_int32(tolua_S, 2, (int*)&arg0, "sp.SkeletonAnimation:setAnimation");
std::string arg1_tmp;
ok &= luaval_to_std_string(tolua_S, 3, &arg1_tmp, "sp.SkeletonAnimation:setAnimation");
arg1 = arg1_tmp.c_str();
ok &= luaval_to_boolean(tolua_S, 4, &arg2, "sp.SkeletonAnimation:setAnimation");
if (!ok)
return 0;
2021-12-25 10:04:45 +08:00
cobj->setAnimation(arg0, arg1, arg2);
2021-12-25 10:04:45 +08:00
lua_settop(tolua_S, 1);
return 1;
}
2021-12-25 10:04:45 +08:00
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "setAnimation", argc, 3);
return 0;
2021-12-25 10:04:45 +08:00
#if COCOS2D_DEBUG >= 1
tolua_lerror:
2022-07-12 21:31:54 +08:00
tolua_error(tolua_S, "#ferror in function 'lua_axis_spine_SkeletonAnimation_setAnimation'.", &tolua_err);
#endif
2021-12-25 10:04:45 +08:00
return 0;
}
2014-03-10 14:04:58 +08:00
static void extendCCSkeletonAnimation(lua_State* L)
{
lua_pushstring(L, "sp.SkeletonAnimation");
lua_rawget(L, LUA_REGISTRYINDEX);
2021-12-25 10:04:45 +08:00
if (lua_istable(L, -1))
2014-03-10 14:04:58 +08:00
{
2022-07-12 21:31:54 +08:00
tolua_function(L, "create", lua_axis_CCSkeletonAnimation_createWithFile);
2014-03-10 14:04:58 +08:00
tolua_function(L, "registerSpineEventHandler", tolua_Cocos2d_CCSkeletonAnimation_registerSpineEventHandler00);
2021-12-25 10:04:45 +08:00
tolua_function(L, "unregisterSpineEventHandler",
tolua_Cocos2d_CCSkeletonAnimation_unregisterSpineEventHandler00);
2022-07-12 21:31:54 +08:00
tolua_function(L, "addAnimation", lua_axis_spine_SkeletonAnimation_addAnimation);
tolua_function(L, "setAnimation", lua_axis_spine_SkeletonAnimation_setAnimation);
2014-03-10 14:04:58 +08:00
}
lua_pop(L, 1);
2021-12-25 10:04:45 +08:00
/*Because sp.SkeletonAnimation:create creat a LuaSkeletonAnimation object,so we need use LuaSkeletonAnimation
* typename for g_luaType*/
auto typeName = typeid(LuaSkeletonAnimation).name();
g_luaType[reinterpret_cast<uintptr_t>(typeName)] = "sp.SkeletonAnimation";
g_typeCast[typeName] = "sp.SkeletonAnimation";
2014-03-10 14:04:58 +08:00
}
2022-07-12 21:31:54 +08:00
int register_all_axis_spine_manual(lua_State* L)
2014-03-10 14:04:58 +08:00
{
if (nullptr == L)
return 0;
extendCCSkeletonAnimation(L);
2021-12-25 10:04:45 +08:00
2014-03-10 14:04:58 +08:00
return 0;
}
int register_spine_module(lua_State* L)
{
lua_getglobal(L, "_G");
2021-12-25 10:04:45 +08:00
if (lua_istable(L, -1)) // stack:...,_G,
{
2022-07-12 21:31:54 +08:00
register_all_axis_spine(L);
register_all_axis_spine_manual(L);
}
lua_pop(L, 1);
return 1;
}