axmol/extensions/scripting/lua-bindings/manual/controller/axlua_controller_manual.cpp

385 lines
14 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-10-11 12:15:41 +08:00
Copyright (c) 2021 Bytedance Inc.
2022-08-08 18:02:17 +08:00
https://axys1.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-08-08 18:02:17 +08:00
#include "scripting/lua-bindings/manual/controller/axlua_controller_manual.hpp"
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID || AX_TARGET_PLATFORM == AX_PLATFORM_IOS)
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +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"
2021-12-25 10:04:45 +08:00
# include "scripting/lua-bindings/manual/CCLuaValue.h"
# include "scripting/lua-bindings/manual/CCLuaEngine.h"
# include "base/CCGameController.h"
2019-11-23 20:27:39 +08:00
2022-08-08 18:02:17 +08:00
static void cloneEventListenerControllerHandler(const ax::EventListenerController* src,
ax::EventListenerController* dst,
2021-12-25 10:04:45 +08:00
ScriptHandlerMgr::HandlerType type)
2019-11-23 20:27:39 +08:00
{
if (nullptr == src || nullptr == dst)
return;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
LUA_FUNCTION handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)src, type);
if (0 != handler)
{
int newscriptHandler = ScriptEngineManager::getInstance()->getScriptEngine()->reallocateScriptHandler(handler);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)dst, newscriptHandler, type);
switch (type)
{
2021-12-25 10:04:45 +08:00
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_CONNECTED:
{
2022-08-08 18:02:17 +08:00
dst->onConnected = [=](ax::Controller* controller, Event* event) {
2021-12-25 10:04:45 +08:00
};
}
break;
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_DISCONNECTED:
{
2022-08-08 18:02:17 +08:00
dst->onDisconnected = [=](ax::Controller* controller, Event* event) {
2021-12-25 10:04:45 +08:00
};
}
break;
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_KEYDOWN:
{
2022-08-08 18:02:17 +08:00
dst->onKeyDown = [=](ax::Controller* controller, int keyCode, Event* event) {
2021-12-25 10:04:45 +08:00
};
}
break;
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_KEYUP:
{
2022-08-08 18:02:17 +08:00
dst->onKeyUp = [=](ax::Controller* controller, int keyCode, Event* event) {
2021-12-25 10:04:45 +08:00
};
}
break;
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_KEYREPEAT:
{
2022-08-08 18:02:17 +08:00
dst->onKeyRepeat = [=](ax::Controller* controller, int keyCode, Event* event) {};
2021-12-25 10:04:45 +08:00
}
break;
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_AXIS:
{
2022-08-08 18:02:17 +08:00
dst->onAxisEvent = [=](ax::Controller* controller, int keyCode, Event* event) {};
2021-12-25 10:04:45 +08:00
}
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
}
2022-08-08 18:02:17 +08:00
static int toaxlua_EventListenerController_clone(lua_State* tolua_S)
2019-11-23 20:27:39 +08:00
{
if (nullptr == tolua_S)
return 0;
2021-12-25 10:04:45 +08:00
int argc = 0;
2022-08-08 18:02:17 +08:00
ax::EventListenerController* self = nullptr;
# if _AX_DEBUG >= 1
2019-11-23 20:27:39 +08:00
tolua_Error tolua_err;
2022-07-12 21:31:54 +08:00
if (!tolua_isusertype(tolua_S, 1, "ax.EventListenerController", 0, &tolua_err))
2021-12-25 10:04:45 +08:00
goto tolua_lerror;
# endif
2022-08-08 18:02:17 +08:00
self = static_cast<ax::EventListenerController*>(tolua_tousertype(tolua_S, 1, 0));
# if _AX_DEBUG >= 1
2021-12-25 10:04:45 +08:00
if (nullptr == self)
{
2022-08-08 18:02:17 +08:00
tolua_error(tolua_S, "invalid 'self' in function 'toaxlua_EventListenerController_clone'\n", nullptr);
2021-12-25 10:04:45 +08:00
return 0;
}
# endif
2019-11-23 20:27:39 +08:00
argc = lua_gettop(tolua_S) - 1;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (argc == 0)
{
2022-08-08 18:02:17 +08:00
ax::EventListenerController* tolua_ret = ax::EventListenerController::create();
2021-12-25 10:04:45 +08:00
if (nullptr == tolua_ret)
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
cloneEventListenerControllerHandler(self, tolua_ret, ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_CONNECTED);
2021-12-25 10:04:45 +08:00
cloneEventListenerControllerHandler(self, tolua_ret,
ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_DISCONNECTED);
2019-11-23 20:27:39 +08:00
cloneEventListenerControllerHandler(self, tolua_ret, ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_KEYDOWN);
cloneEventListenerControllerHandler(self, tolua_ret, ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_KEYUP);
cloneEventListenerControllerHandler(self, tolua_ret, ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_KEYREPEAT);
cloneEventListenerControllerHandler(self, tolua_ret, ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_AXIS);
2021-12-25 10:04:45 +08:00
int ID = (int)tolua_ret->_ID;
2021-10-11 12:15:41 +08:00
int* luaID = &tolua_ret->_luaID;
2022-07-12 21:31:54 +08:00
toluafix_pushusertype_ccobject(tolua_S, ID, luaID, (void*)tolua_ret, "ax.EventListenerController");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return 1;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AXLOG("'clone' has wrong number of arguments: %d, was expecting %d\n", argc, 0);
2019-11-23 20:27:39 +08:00
return 0;
2021-12-25 10:04:45 +08:00
2022-08-08 18:02:17 +08:00
# if _AX_DEBUG >= 1
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 'clone'.", &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
}
2022-08-08 18:02:17 +08:00
static int toaxlua_EventListenerController_registerScriptHandler(lua_State* tolua_S)
2019-11-23 20:27:39 +08:00
{
if (nullptr == tolua_S)
return 0;
2021-12-25 10:04:45 +08:00
int argc = 0;
2022-08-08 18:02:17 +08:00
ax::EventListenerController* self = nullptr;
# if _AX_DEBUG >= 1
2019-11-23 20:27:39 +08:00
tolua_Error tolua_err;
2022-07-12 21:31:54 +08:00
if (!tolua_isusertype(tolua_S, 1, "ax.EventListenerController", 0, &tolua_err))
2021-12-25 10:04:45 +08:00
goto tolua_lerror;
# endif
2022-08-08 18:02:17 +08:00
self = static_cast<ax::EventListenerController*>(tolua_tousertype(tolua_S, 1, 0));
# if _AX_DEBUG >= 1
2021-12-25 10:04:45 +08:00
if (nullptr == self)
{
tolua_error(tolua_S,
2022-08-08 18:02:17 +08:00
"invalid 'self' in function 'toaxlua_EventListenerController_registerScriptHandler'\n",
2021-12-25 10:04:45 +08:00
nullptr);
return 0;
}
# endif
2019-11-23 20:27:39 +08:00
argc = lua_gettop(tolua_S) - 1;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (argc == 2)
{
2022-08-08 18:02:17 +08:00
# if _AX_DEBUG >= 1
2021-12-25 10:04:45 +08:00
if (!toluafix_isfunction(tolua_S, 2, "LUA_FUNCTION", 0, &tolua_err) ||
2019-11-23 20:27:39 +08:00
!tolua_isnumber(tolua_S, 3, 0, &tolua_err))
{
goto tolua_lerror;
}
2021-12-25 10:04:45 +08:00
# endif
LUA_FUNCTION handler = toluafix_ref_function(tolua_S, 2, 0);
ScriptHandlerMgr::HandlerType type =
static_cast<ScriptHandlerMgr::HandlerType>((int)tolua_tonumber(tolua_S, 3, 0));
2021-11-15 19:05:08 +08:00
auto stack = LuaEngine::getInstance()->getLuaStack();
auto Ls = stack->getLuaState();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
switch (type)
{
2021-12-25 10:04:45 +08:00
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_CONNECTED:
{
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type);
2022-08-08 18:02:17 +08:00
self->onConnected = [=](ax::Controller* controller, Event* event) {
2022-07-12 21:31:54 +08:00
tolua_pushusertype(Ls, (void*)controller, "ax.Controller");
2021-12-25 10:04:45 +08:00
int ID = (event) ? (int)event->_ID : -1;
int* luaID = (event) ? &event->_luaID : NULL;
2022-07-12 21:31:54 +08:00
toluafix_pushusertype_ccobject(Ls, ID, luaID, (void*)event, "ax.event");
2021-12-25 10:04:45 +08:00
stack->executeFunctionByHandler(handler, 2);
};
}
break;
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_DISCONNECTED:
{
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type);
2022-08-08 18:02:17 +08:00
self->onDisconnected = [=](ax::Controller* controller, Event* event) {
2022-07-12 21:31:54 +08:00
tolua_pushusertype(Ls, (void*)controller, "ax.Controller");
2021-12-25 10:04:45 +08:00
int ID = (event) ? (int)event->_ID : -1;
int* luaID = (event) ? &event->_luaID : NULL;
2022-07-12 21:31:54 +08:00
toluafix_pushusertype_ccobject(Ls, ID, luaID, (void*)event, "ax.event");
2021-12-25 10:04:45 +08:00
stack->executeFunctionByHandler(handler, 2);
};
}
break;
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_KEYDOWN:
{
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type);
2022-08-08 18:02:17 +08:00
self->onKeyDown = [=](ax::Controller* controller, int keyCode, Event* event) {
2022-07-12 21:31:54 +08:00
tolua_pushusertype(Ls, (void*)controller, "ax.Controller");
2021-12-25 10:04:45 +08:00
tolua_pushnumber(Ls, (lua_Number)keyCode);
int ID = (event) ? (int)event->_ID : -1;
int* luaID = (event) ? &event->_luaID : NULL;
2022-07-12 21:31:54 +08:00
toluafix_pushusertype_ccobject(Ls, ID, luaID, (void*)event, "ax.event");
2021-12-25 10:04:45 +08:00
stack->executeFunctionByHandler(handler, 3);
};
}
break;
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_KEYUP:
{
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type);
2022-08-08 18:02:17 +08:00
self->onKeyUp = [=](ax::Controller* controller, int keyCode, Event* event) {
2022-07-12 21:31:54 +08:00
tolua_pushusertype(Ls, (void*)controller, "ax.Controller");
2021-12-25 10:04:45 +08:00
tolua_pushnumber(Ls, (lua_Number)keyCode);
int ID = (event) ? (int)event->_ID : -1;
int* luaID = (event) ? &event->_luaID : NULL;
2022-07-12 21:31:54 +08:00
toluafix_pushusertype_ccobject(Ls, ID, luaID, (void*)event, "ax.event");
2021-12-25 10:04:45 +08:00
stack->executeFunctionByHandler(handler, 3);
};
}
break;
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_KEYREPEAT:
{
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type);
2022-08-08 18:02:17 +08:00
self->onKeyRepeat = [=](ax::Controller* controller, int keyCode, Event* event) {
2022-07-12 21:31:54 +08:00
tolua_pushusertype(Ls, (void*)controller, "ax.Controller");
2021-12-25 10:04:45 +08:00
tolua_pushnumber(Ls, (lua_Number)keyCode);
int ID = (event) ? (int)event->_ID : -1;
int* luaID = (event) ? &event->_luaID : NULL;
2022-07-12 21:31:54 +08:00
toluafix_pushusertype_ccobject(Ls, ID, luaID, (void*)event, "ax.event");
2021-12-25 10:04:45 +08:00
stack->executeFunctionByHandler(handler, 3);
};
}
break;
case ScriptHandlerMgr::HandlerType::EVENT_CONTROLLER_AXIS:
{
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type);
2022-08-08 18:02:17 +08:00
self->onAxisEvent = [=](ax::Controller* controller, int keyCode, Event* event) {
2022-07-12 21:31:54 +08:00
tolua_pushusertype(Ls, (void*)controller, "ax.Controller");
2021-12-25 10:04:45 +08:00
tolua_pushnumber(Ls, (lua_Number)keyCode);
int ID = (event) ? (int)event->_ID : -1;
int* luaID = (event) ? &event->_luaID : NULL;
2022-07-12 21:31:54 +08:00
toluafix_pushusertype_ccobject(Ls, ID, luaID, (void*)event, "ax.event");
2021-12-25 10:04:45 +08:00
stack->executeFunctionByHandler(handler, 3);
};
}
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
return 0;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AXLOG("'registerScriptHandler' has wrong number of arguments: %d, was expecting %d\n", argc, 2);
2019-11-23 20:27:39 +08:00
return 0;
2021-12-25 10:04:45 +08:00
2022-08-08 18:02:17 +08:00
# if _AX_DEBUG >= 1
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 'registerScriptHandler'.", &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
}
static void extendEventListenerController(lua_State* L)
{
2022-07-12 21:31:54 +08:00
lua_pushstring(L, "ax.EventListenerController");
2019-11-23 20:27:39 +08:00
lua_rawget(L, LUA_REGISTRYINDEX);
2021-12-25 10:04:45 +08:00
if (lua_istable(L, -1))
2019-11-23 20:27:39 +08:00
{
2022-08-08 18:02:17 +08:00
tolua_function(L, "clone", toaxlua_EventListenerController_clone);
tolua_function(L, "registerScriptHandler", toaxlua_EventListenerController_registerScriptHandler);
2019-11-23 20:27:39 +08:00
}
lua_pop(L, 1);
}
2022-08-08 18:02:17 +08:00
static int toaxlua_Controller_getKeyStatus(lua_State* tolua_S)
2019-11-23 20:27:39 +08:00
{
if (nullptr == tolua_S)
return 0;
2021-12-25 10:04:45 +08:00
int argc = 0;
2022-08-08 18:02:17 +08:00
ax::Controller* self = nullptr;
# if _AX_DEBUG >= 1
2019-11-23 20:27:39 +08:00
tolua_Error tolua_err;
2022-07-12 21:31:54 +08:00
if (!tolua_isusertype(tolua_S, 1, "ax.Controller", 0, &tolua_err))
2021-12-25 10:04:45 +08:00
goto tolua_lerror;
# endif
2022-08-08 18:02:17 +08:00
self = static_cast<ax::Controller*>(tolua_tousertype(tolua_S, 1, 0));
# if _AX_DEBUG >= 1
2021-12-25 10:04:45 +08:00
if (nullptr == self)
{
2022-08-08 18:02:17 +08:00
tolua_error(tolua_S, "invalid 'self' in function 'toaxlua_Controller_getKeyStatus'\n", nullptr);
2021-12-25 10:04:45 +08:00
return 0;
}
# endif
2019-11-23 20:27:39 +08:00
argc = lua_gettop(tolua_S) - 1;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (argc == 1)
{
2022-08-08 18:02:17 +08:00
# if _AX_DEBUG >= 1
2019-11-23 20:27:39 +08:00
if (!tolua_isnumber(tolua_S, 2, 0, &tolua_err))
goto tolua_lerror;
2021-12-25 10:04:45 +08:00
# endif
int keyCode = (int)tolua_tonumber(tolua_S, 2, 0);
2022-08-08 18:02:17 +08:00
ax::Controller::KeyStatus keyStatus = self->getKeyStatus(keyCode);
2021-12-25 10:04:45 +08:00
lua_newtable(tolua_S); /* L: table */
lua_pushstring(tolua_S, "isPressed"); /* L: table key */
lua_pushboolean(tolua_S, (int)keyStatus.isPressed); /* L: table key value*/
lua_rawset(tolua_S, -3); /* table[key] = value, L: table */
lua_pushstring(tolua_S, "value"); /* L: table key */
lua_pushnumber(tolua_S, (lua_Number)keyStatus.value); /* L: table key value*/
lua_rawset(tolua_S, -3); /* table[key] = value, L: table */
lua_pushstring(tolua_S, "isAnalog"); /* L: table key */
lua_pushnumber(tolua_S, (int)keyStatus.isAnalog); /* L: table key value*/
lua_rawset(tolua_S, -3); /* table[key] = value, L: table */
2019-11-23 20:27:39 +08:00
return 1;
}
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AXLOG("'clone' has wrong number of arguments: %d, was expecting %d\n", argc, 0);
2019-11-23 20:27:39 +08:00
return 0;
2021-12-25 10:04:45 +08:00
2022-08-08 18:02:17 +08:00
# if _AX_DEBUG >= 1
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 'getKeyStatus'.", &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
}
static void extendController(lua_State* L)
{
2022-07-12 21:31:54 +08:00
lua_pushstring(L, "ax.Controller");
2019-11-23 20:27:39 +08:00
lua_rawget(L, LUA_REGISTRYINDEX);
2021-12-25 10:04:45 +08:00
if (lua_istable(L, -1))
2019-11-23 20:27:39 +08:00
{
2022-08-08 18:02:17 +08:00
tolua_function(L, "getKeyStatus", toaxlua_Controller_getKeyStatus);
2019-11-23 20:27:39 +08:00
}
lua_pop(L, 1);
}
int register_all_ax_controller_manual(lua_State* L)
2019-11-23 20:27:39 +08:00
{
if (nullptr == L)
return 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
extendEventListenerController(L);
extendController(L);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return 0;
}
2022-07-16 10:43:05 +08:00
#endif //#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID || AX_TARGET_PLATFORM == AX_PLATFORM_IOS)