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::setMainMenuErrorMessage(std::string errormessage)
26 SCRIPTAPI_PRECHECKHEADER
28 lua_getglobal(L, "gamedata");
29 int gamedata_idx = lua_gettop(L);
30 lua_pushstring(L, "errormessage");
31 lua_pushstring(L, errormessage.c_str());
32 lua_settable(L, gamedata_idx);
36 void ScriptApiMainMenu::handleMainMenuEvent(std::string text)
38 SCRIPTAPI_PRECHECKHEADER
40 // Get handler function
41 lua_getglobal(L, "engine");
42 lua_getfield(L, -1, "event_handler");
45 luaL_checktype(L, -1, LUA_TFUNCTION);
48 lua_pushstring(L, text.c_str());
49 if(lua_pcall(L, 1, 0, 0))
50 scriptError("error running function engine.event_handler: %s\n",
54 void ScriptApiMainMenu::handleMainMenuButtons(std::map<std::string, std::string> fields)
56 SCRIPTAPI_PRECHECKHEADER
58 // Get handler function
59 lua_getglobal(L, "engine");
60 lua_getfield(L, -1, "button_handler");
63 luaL_checktype(L, -1, LUA_TFUNCTION);
65 // Convert fields to lua table
67 for(std::map<std::string, std::string>::const_iterator
68 i = fields.begin(); i != fields.end(); i++){
69 const std::string &name = i->first;
70 const std::string &value = i->second;
71 lua_pushstring(L, name.c_str());
72 lua_pushlstring(L, value.c_str(), value.size());
77 if(lua_pcall(L, 1, 0, 0))
78 scriptError("error running function engine.button_handler: %s\n",