Move input-related Lua variables into the library

This commit is contained in:
LJ Sonic 2023-10-27 16:29:47 +02:00
parent a68440c4db
commit 23268bf3e5
2 changed files with 49 additions and 13 deletions

View file

@ -145,6 +145,30 @@ static luaL_Reg lib[] = {
{NULL, NULL}
};
///////////////
// VARIABLES //
///////////////
static int lib_get(lua_State *L)
{
const char *field = luaL_checkstring(L, 2);
if (fastcmp(field, "mouse"))
{
LUA_PushUserdata(L, &mouse, META_MOUSE);
return 1;
}
else if (fastcmp(field, "mouse2"))
{
LUA_PushUserdata(L, &mouse2, META_MOUSE);
return 1;
}
else
{
return 0;
}
}
///////////////////
// gamekeydown[] //
///////////////////
@ -239,19 +263,6 @@ static int mouse_num(lua_State *L)
int LUA_InputLib(lua_State *L)
{
lua_newuserdata(L, 0);
lua_createtable(L, 0, 2);
lua_pushcfunction(L, lib_getGameKeyDown);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lib_setGameKeyDown);
lua_setfield(L, -2, "__newindex");
lua_pushcfunction(L, lib_lenGameKeyDown);
lua_setfield(L, -2, "__len");
lua_setmetatable(L, -2);
lua_setglobal(L, "gamekeydown");
luaL_newmetatable(L, META_KEYEVENT);
lua_pushcfunction(L, keyevent_get);
lua_setfield(L, -2, "__index");
@ -265,6 +276,29 @@ int LUA_InputLib(lua_State *L)
lua_setfield(L, -2, "__len");
lua_pop(L, 1);
// Register the library, then add __index and __newindex
// metamethods to it to allow global variables
luaL_register(L, "input", lib);
lua_createtable(L, 0, 2);
lua_pushcfunction(L, lib_get);
lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2);
lua_newuserdata(L, 0);
lua_createtable(L, 0, 2);
lua_pushcfunction(L, lib_getGameKeyDown);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lib_setGameKeyDown);
lua_setfield(L, -2, "__newindex");
lua_pushcfunction(L, lib_lenGameKeyDown);
lua_setfield(L, -2, "__len");
lua_setmetatable(L, -2);
lua_pushvalue(L, -1); // TODO: 2.3: Delete (gamekeydown moved to input library)
lua_setglobal(L, "gamekeydown"); // Delete too
lua_setfield(L, -2, "gamekeydown");
lua_pop(L, 1);
return 0;
}

View file

@ -415,9 +415,11 @@ int LUA_PushGlobals(lua_State *L, const char *word)
} else if (fastcmp(word, "stagefailed")) {
lua_pushboolean(L, stagefailed);
return 1;
// TODO: 2.3: Deprecated (moved to the input library)
} else if (fastcmp(word, "mouse")) {
LUA_PushUserdata(L, &mouse, META_MOUSE);
return 1;
// TODO: 2.3: Deprecated (moved to the input library)
} else if (fastcmp(word, "mouse2")) {
LUA_PushUserdata(L, &mouse2, META_MOUSE);
return 1;