1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Mainmenu: Move core.on_before_close to s_mainmenu like other callbacks, and doc

This commit is contained in:
Desour 2025-04-20 21:57:38 +02:00 committed by sfan5
parent 2c83c67b7d
commit 4a8f84b259
6 changed files with 31 additions and 20 deletions

View file

@ -34,7 +34,7 @@ void ScriptApiMainMenu::handleMainMenuEvent(const std::string &text)
lua_getfield(L, -1, "event_handler");
lua_remove(L, -2); // Remove core
if (lua_isnil(L, -1)) {
lua_pop(L, 1); // Pop event_handler
lua_pop(L, 2); // Pop event_handler, error handler
return;
}
luaL_checktype(L, -1, LUA_TFUNCTION);
@ -56,7 +56,7 @@ void ScriptApiMainMenu::handleMainMenuButtons(const StringMap &fields)
lua_getfield(L, -1, "button_handler");
lua_remove(L, -2); // Remove core
if (lua_isnil(L, -1)) {
lua_pop(L, 1); // Pop button handler
lua_pop(L, 2); // Pop button handler, error handler
return;
}
luaL_checktype(L, -1, LUA_TFUNCTION);
@ -76,3 +76,25 @@ void ScriptApiMainMenu::handleMainMenuButtons(const StringMap &fields)
PCALL_RES(lua_pcall(L, 1, 0, error_handler));
lua_pop(L, 1); // Pop error handler
}
void ScriptApiMainMenu::beforeClose()
{
SCRIPTAPI_PRECHECKHEADER
int error_handler = PUSH_ERROR_HANDLER(L);
// Get handler function
lua_getglobal(L, "core");
lua_getfield(L, -1, "on_before_close");
lua_remove(L, -2); // Remove core
if (lua_isnil(L, -1)) {
lua_pop(L, 2); // Pop callback, error handler
return;
}
luaL_checktype(L, -1, LUA_TFUNCTION);
// Call it
PCALL_RES(lua_pcall(L, 0, 0, error_handler));
lua_pop(L, 1); // Pop error handler
}

View file

@ -27,4 +27,9 @@ public:
* @param fields data in field format
*/
void handleMainMenuButtons(const StringMap &fields);
/**
* Called before the menu is closed, either to exit or to join a game
*/
void beforeClose();
};