3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "cpp_api/s_mainmenu.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
24 void ScriptApiMainMenu::setMainMenuData(MainMenuDataForScript *data)
26 SCRIPTAPI_PRECHECKHEADER
28 lua_getglobal(L, "gamedata");
29 int gamedata_idx = lua_gettop(L);
30 lua_pushstring(L, "errormessage");
31 if (!data->errormessage.empty()) {
32 lua_pushstring(L, data->errormessage.c_str());
36 lua_settable(L, gamedata_idx);
37 setboolfield(L, gamedata_idx, "reconnect_requested", data->reconnect_requested);
41 void ScriptApiMainMenu::handleMainMenuEvent(std::string text)
43 SCRIPTAPI_PRECHECKHEADER
45 int error_handler = PUSH_ERROR_HANDLER(L);
47 // Get handler function
48 lua_getglobal(L, "core");
49 lua_getfield(L, -1, "event_handler");
50 lua_remove(L, -2); // Remove core
51 if (lua_isnil(L, -1)) {
52 lua_pop(L, 1); // Pop event_handler
55 luaL_checktype(L, -1, LUA_TFUNCTION);
58 lua_pushstring(L, text.c_str());
59 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
60 lua_pop(L, 1); // Pop error handler
63 void ScriptApiMainMenu::handleMainMenuButtons(const StringMap &fields)
65 SCRIPTAPI_PRECHECKHEADER
67 int error_handler = PUSH_ERROR_HANDLER(L);
69 // Get handler function
70 lua_getglobal(L, "core");
71 lua_getfield(L, -1, "button_handler");
72 lua_remove(L, -2); // Remove core
73 if (lua_isnil(L, -1)) {
74 lua_pop(L, 1); // Pop button handler
77 luaL_checktype(L, -1, LUA_TFUNCTION);
79 // Convert fields to a Lua table
81 StringMap::const_iterator it;
82 for (it = fields.begin(); it != fields.end(); ++it) {
83 const std::string &name = it->first;
84 const std::string &value = it->second;
85 lua_pushstring(L, name.c_str());
86 lua_pushlstring(L, value.c_str(), value.size());
91 PCALL_RES(lua_pcall(L, 1, 0, error_handler));
92 lua_pop(L, 1); // Pop error handler