/*
Minetest
Copyright (C) 2013 sapier
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "lua_api/l_mainmenu.h"
#include "lua_api/l_internal.h"
#include "common/c_content.h"
#include "cpp_api/s_async.h"
#include "guiEngine.h"
#include "guiMainMenu.h"
#include "guiKeyChangeMenu.h"
#include "guiFileSelectMenu.h"
#include "subgame.h"
#include "version.h"
#include "porting.h"
#include "filesys.h"
#include "convert_json.h"
#include "serverlist.h"
#include "mapgen.h"
#include "sound.h"
#include "settings.h"
#include "log.h"
#include "EDriverTypes.h"
#include <IFileArchive.h>
#include <IFileSystem.h>
/******************************************************************************/
std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
{
lua_getglobal(L, "gamedata");
lua_getfield(L, -1, name.c_str());
if(lua_isnil(L, -1))
return "";
return luaL_checkstring(L, -1);
}
/******************************************************************************/
int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
{
lua_getglobal(L, "gamedata");
lua_getfield(L, -1, name.c_str());
if(lua_isnil(L, -1)) {
valid = false;
return -1;
}
valid = true;
return luaL_checkinteger(L, -1);
}
/******************************************************************************/
int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
{
lua_getglobal(L, "gamedata");
lua_getfield(L, -1, name.c_str());
if(lua_isnil(L, -1)) {
valid = false;
return false;
}
valid = true;
return lua_toboolean(L, -1);
}
/******************************************************************************/
int ModApiMainMenu::l_update_formspec(lua_State *L)
{
GUIEngine* engine = getGuiEngine(L);
sanity_check(engine != NULL);
if (engine->m_startgame)
return 0;
//read formspec
std::string formspec(luaL_checkstring(L, 1));
if (engine->m_formspecgui != 0) {
engine->m_formspecgui->setForm(formspec);
}
return 0;
}
/******************************************************************************/
int ModApiMainMenu::l_start(lua_State *L)
{
GUIEngine* engine = getGuiEngine(L);
sanity_check(engine != NULL);
//update c++ gamedata from lua table
bool valid = false;
MainMenuData *data = engine->m_data;
data->selected_world = getIntegerData(L, "selected_world",valid) -1;
data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
data->do_reconnect = getBoolData(L, "do_reconnect", valid);
if (!data->do_reconnect) {
data->name = getTextData(L,"playername");
data->password = getTextData(L,"password");
data->address = getTextData(L,"address");
data->port = getTextData(L,"port");
}
data->serverdescription = getTextData(L,"serverdescription");
data->servername = getTextData(L,"servername");
//close menu next time
engine->m_startgame = true;
return 0;
}
/******************************************************************************/
int ModApiMainMenu::l_close(lua_State *L)
{
GUIEngine* engine = getGuiEngine(L);
sanity_check(engine != NULL);
engine->m_kill = true;
return 0;
}
/******************************************************************************/
int ModApiMainMenu::l_set_background(lua_State *L)
{
GUIEngine* engine = getGuiEngine(L);
sanity_check(engine != NULL);
std::string backgroundlevel(luaL_checkstring(L, 1));
std::string texturename(luaL_checkstring(L, 2));
bool tile_image = false;
bool retval = false;
unsigned int minsize = 16;
if (!lua_isnone(L, 3)) {
tile_image = lua_toboolean(L, 3);
}
if (!lua_isnone(L, 4)) {
minsize = lua_tonumber(L, 4);
}
if (backgroundlevel == "background") {
retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
tile_image, minsize);
}
if (backgroundlevel == "overlay") {
retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
tile_image, minsize);
}
if (backgroundlevel == "header") {
retval |= engine->setTexture(TEX_LAYER_HEADER, texturename,
tile_image, minsize);
}
if (backgroundlevel == "footer") {
retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
tile_image, minsize);
}
lua_pushboolean(L,retval);
return 1;
}
/******************************************************************************/
int ModApiMainMenu::l_set_clouds(lua_State *L)
{
GUIEngine* engine = getGuiEngine(L);
sanity_check(engine != NULL);
bool value = lua_toboolean(L,1);
engine->m_clouds_enabled = value;
return 0;
}
/******************************************************************************/
int ModApiMainMenu::l_get_textlist_index(lua_State *L)
{
// get_table_index accepts both tables and textlists
return l_get_table_index(L);
}
/******************************************************************************/
int ModApiMainMenu::l_get_table_index(lua_State *L)
{
GUIEngine* engine = getGuiEngine(L);
sanity_check(engine != NULL);
|