]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_mainmenu.cpp
Optional reconnect functionality
[minetest.git] / src / script / cpp_api / s_mainmenu.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "cpp_api/s_mainmenu.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23
24 void ScriptApiMainMenu::setMainMenuData(MainMenuDataForScript *data)
25 {
26         SCRIPTAPI_PRECHECKHEADER
27
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());
33         } else {
34                 lua_pushnil(L);
35         }
36         lua_settable(L, gamedata_idx);
37         setboolfield(L, gamedata_idx, "reconnect_requested",
38                 data->reconnect_requested);
39         lua_pop(L, 1);
40 }
41
42 void ScriptApiMainMenu::handleMainMenuEvent(std::string text)
43 {
44         SCRIPTAPI_PRECHECKHEADER
45
46         // Get handler function
47         lua_getglobal(L, "core");
48         lua_getfield(L, -1, "event_handler");
49         lua_remove(L, -2); // Remove core
50         if (lua_isnil(L, -1)) {
51                 lua_pop(L, 1); // Pop event_handler
52                 return;
53         }
54         luaL_checktype(L, -1, LUA_TFUNCTION);
55
56         // Call it
57         lua_pushstring(L, text.c_str());
58         if (lua_pcall(L, 1, 0, m_errorhandler))
59                 scriptError();
60 }
61
62 void ScriptApiMainMenu::handleMainMenuButtons(const StringMap &fields)
63 {
64         SCRIPTAPI_PRECHECKHEADER
65
66         // Get handler function
67         lua_getglobal(L, "core");
68         lua_getfield(L, -1, "button_handler");
69         lua_remove(L, -2); // Remove core
70         if (lua_isnil(L, -1)) {
71                 lua_pop(L, 1); // Pop button handler
72                 return;
73         }
74         luaL_checktype(L, -1, LUA_TFUNCTION);
75
76         // Convert fields to a Lua table
77         lua_newtable(L);
78         StringMap::const_iterator it;
79         for (it = fields.begin(); it != fields.end(); ++it) {
80                 const std::string &name = it->first;
81                 const std::string &value = it->second;
82                 lua_pushstring(L, name.c_str());
83                 lua_pushlstring(L, value.c_str(), value.size());
84                 lua_settable(L, -3);
85         }
86
87         // Call it
88         if (lua_pcall(L, 1, 0, m_errorhandler))
89                 scriptError();
90 }
91