s_mainmenu.cpp 2.6 KB

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