Expose keyevent_t to Lua

This commit is contained in:
LJ Sonic 2021-08-14 20:33:42 +02:00
parent 38e82e55fc
commit 5b949a6751
2 changed files with 29 additions and 0 deletions

View file

@ -172,6 +172,29 @@ static int lib_lenGameKeyDown(lua_State *L)
return 1;
}
///////////////
// KEY EVENT //
///////////////
static int keyevent_get(lua_State *L)
{
event_t *event = *((event_t **)luaL_checkudata(L, 1, META_KEYEVENT));
const char *field = luaL_checkstring(L, 2);
I_Assert(event != NULL);
if (fastcmp(field,"name"))
lua_pushstring(L, G_KeyNumToString(event->data1));
else if (fastcmp(field,"num"))
lua_pushinteger(L, event->data1);
else if (fastcmp(field,"repeated"))
lua_pushboolean(L, event->repeated);
else
return luaL_error(L, "keyevent_t has no field named %s", field);
return 1;
}
///////////
// MOUSE //
///////////
@ -227,6 +250,11 @@ int LUA_InputLib(lua_State *L)
lua_setmetatable(L, -2);
lua_setglobal(L, "gamekeydown");
luaL_newmetatable(L, META_KEYEVENT);
lua_pushcfunction(L, keyevent_get);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);
luaL_newmetatable(L, META_MOUSE);
lua_pushcfunction(L, mouse_get);
lua_setfield(L, -2, "__index");

View file

@ -88,6 +88,7 @@ extern lua_State *gL;
#define META_LUABANKS "LUABANKS[]*"
#define META_KEYEVENT "KEYEVENT_T*"
#define META_MOUSE "MOUSE_T*"
boolean luaL_checkboolean(lua_State *L, int narg);