Merge branch 'add-textinput-hook' into 'next'

Add TextInput hook

See merge request STJr/SRB2!2514
This commit is contained in:
Hanicef 2025-03-22 11:32:50 +00:00
commit a99a06b71f
9 changed files with 63 additions and 3 deletions

View file

@ -954,6 +954,9 @@ boolean CON_Responder(event_t *ev)
if ((key == gamecontrol[GC_CONSOLE][0] || key == gamecontrol[GC_CONSOLE][1]) && !shiftdown)
{
if (con_destlines == 0 && I_GetTextInputMode())
return false; // some other component is holding keyboard input, don't hijack it!
I_SetTextInputMode(con_destlines == 0); // inverse, since this is changed next tic.
consoletoggle = true;
return true;

View file

@ -2270,6 +2270,11 @@ boolean G_LuaResponder(event_t *ev)
cancelled = LUA_HookKey(ev, HOOK(KeyUp));
LUA_InvalidateUserdata(ev);
}
else if (ev->type == ev_text)
{
cancelled = LUA_HookText(ev, HOOK(TextInput));
LUA_InvalidateUserdata(ev);
}
return cancelled;
}

View file

@ -238,6 +238,7 @@ static const struct {
{META_LUABANKS, "luabanks[]"},
{META_KEYEVENT, "keyevent_t"},
{META_TEXTEVENT, "textevent_t"},
{META_MOUSE, "mouse_t"},
{NULL, NULL}
};

View file

@ -75,6 +75,7 @@ automatically.
X (AddonLoaded),\
X (KeyDown),\
X (KeyUp),\
X (TextInput),\
#define STRING_HOOK_LIST(X) \
X (BotAI),/* B_BuildTailsTiccmd by skin name */\
@ -135,6 +136,7 @@ void LUA_HookBool(boolean value, int hook);
int LUA_HookPlayer(player_t *, int hook);
int LUA_HookTiccmd(player_t *, ticcmd_t *, int hook);
int LUA_HookKey(event_t *event, int hook); // Hooks for key events
int LUA_HookText(event_t *event, int hook); // Hooks for text events
void LUA_HookPreThinkFrame(void);
void LUA_HookThinkFrame(void);

View file

@ -690,6 +690,17 @@ int LUA_HookKey(event_t *event, int hook_type)
return hook.status;
}
int LUA_HookText(event_t *event, int hook_type)
{
Hook_State hook;
if (prepare_hook(&hook, false, hook_type))
{
LUA_PushUserdata(gL, event, META_TEXTEVENT);
call_hooks(&hook, 1, res_true);
}
return hook.status;
}
void LUA_HookHUD(int hook_type, huddrawlist_h list)
{
Hook_State hook;

View file

@ -129,6 +129,18 @@ static int lib_getCursorPosition(lua_State *L)
return 2;
}
static int lib_setTextInputMode(lua_State *L)
{
I_SetTextInputMode(luaL_checkboolean(L, 1));
return 0;
}
static int lib_getTextInputMode(lua_State *L)
{
lua_pushinteger(L, I_GetTextInputMode());
return 1;
}
static luaL_Reg lib[] = {
{"gameControlDown", lib_gameControlDown},
{"gameControl2Down", lib_gameControl2Down},
@ -143,6 +155,8 @@ static luaL_Reg lib[] = {
{"getMouseGrab", lib_getMouseGrab},
{"setMouseGrab", lib_setMouseGrab},
{"getCursorPosition", lib_getCursorPosition},
{"setTextInputMode", lib_setTextInputMode},
{"getTextInputMode", lib_getTextInputMode},
{NULL, NULL}
};
@ -220,6 +234,28 @@ static int lib_lenGameKeyDown(lua_State *L)
return 1;
}
////////////////
// TEXT EVENT //
////////////////
static int textevent_get(lua_State *L)
{
event_t *event = *((event_t **)luaL_checkudata(L, 1, META_TEXTEVENT));
const char *field = luaL_checkstring(L, 2);
I_Assert(event != NULL);
if (fastcmp(field,"text"))
{
char s[2] = { event->key, 0 };
lua_pushstring(L, s);
}
else
return luaL_error(L, "textevent_t has no field named %s", field);
return 1;
}
///////////////
// KEY EVENT //
///////////////
@ -285,6 +321,7 @@ static int mouse_num(lua_State *L)
int LUA_InputLib(lua_State *L)
{
LUA_RegisterUserdataMetatable(L, META_TEXTEVENT, textevent_get, NULL, NULL);
LUA_RegisterUserdataMetatable(L, META_KEYEVENT, keyevent_get, NULL, NULL);
LUA_RegisterUserdataMetatable(L, META_MOUSE, mouse_get, NULL, mouse_num);

View file

@ -93,6 +93,7 @@ extern boolean ignoregameinputs;
#define META_LUABANKS "LUABANKS[]*"
#define META_TEXTEVENT "TEXTEVENT_T*"
#define META_KEYEVENT "KEYEVENT_T*"
#define META_MOUSE "MOUSE_T*"

View file

@ -186,9 +186,6 @@ int main(int argc, char **argv)
#endif
#endif
// disable text input right off the bat, since we don't need it at the start.
I_SetTextInputMode(false);
#ifdef LOGMESSAGES
if (!M_CheckParm("-nolog"))
InitLogging();

View file

@ -1857,6 +1857,9 @@ void I_StartupGraphics(void)
if (mousegrabok && !disable_mouse)
SDLdoGrabMouse();
// disable text input right off the bat, since we don't need it at the start.
I_SetTextInputMode(false);
graphics_started = true;
}