]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_mainmenu.cpp
Improve the path select GUI (#5852)
[dragonfireclient.git] / src / script / lua_api / l_mainmenu.cpp
index f905080128ce9cfc07bd97155ef681d056185f40..3ed2ba0e0ee181b0a18790b5f19cc69924061539 100644 (file)
@@ -24,23 +24,21 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "guiEngine.h"
 #include "guiMainMenu.h"
 #include "guiKeyChangeMenu.h"
-#include "guiFileSelectMenu.h"
+#include "guiPathSelectMenu.h"
 #include "subgame.h"
 #include "version.h"
 #include "porting.h"
 #include "filesys.h"
 #include "convert_json.h"
 #include "serverlist.h"
-#include "emerge.h"
-#include "sound.h"
+#include "mapgen.h"
 #include "settings.h"
-#include "main.h" // for g_settings
-#include "log.h"
 #include "EDriverTypes.h"
 
 #include <IFileArchive.h>
 #include <IFileSystem.h>
 
+
 /******************************************************************************/
 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
 {
@@ -115,15 +113,19 @@ int ModApiMainMenu::l_start(lua_State *L)
 
        bool valid = false;
 
+       MainMenuData *data = engine->m_data;
 
-       engine->m_data->selected_world          = getIntegerData(L, "selected_world",valid) -1;
-       engine->m_data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
-       engine->m_data->name                            = getTextData(L,"playername");
-       engine->m_data->password                        = getTextData(L,"password");
-       engine->m_data->address                         = getTextData(L,"address");
-       engine->m_data->port                            = getTextData(L,"port");
-       engine->m_data->serverdescription       = getTextData(L,"serverdescription");
-       engine->m_data->servername                      = getTextData(L,"servername");
+       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;
@@ -211,7 +213,7 @@ int ModApiMainMenu::l_get_table_index(lua_State *L)
        GUIEngine* engine = getGuiEngine(L);
        sanity_check(engine != NULL);
 
-       std::wstring tablename(narrow_to_wide(luaL_checkstring(L, 1)));
+       std::string tablename(luaL_checkstring(L, 1));
        GUITable *table = engine->m_menu->getTable(tablename);
        s32 selection = table ? table->getSelected() : 0;
 
@@ -296,7 +298,7 @@ int ModApiMainMenu::l_get_games(lua_State *L)
                int table2 = lua_gettop(L);
                int internal_index=1;
                for (std::set<std::string>::iterator iter = games[i].addon_mods_paths.begin();
-                               iter != games[i].addon_mods_paths.end(); iter++) {
+                               iter != games[i].addon_mods_paths.end(); ++iter) {
                        lua_pushnumber(L,internal_index);
                        lua_pushstring(L,(*iter).c_str());
                        lua_settable(L, table2);
@@ -574,6 +576,13 @@ int ModApiMainMenu::l_get_favorites(lua_State *L)
                        lua_settable(L, top_lvl2);
                }
 
+               if (servers[i].isMember("ping")) {
+                       float ping = servers[i]["ping"].asFloat();
+                       lua_pushstring(L, "ping");
+                       lua_pushnumber(L, ping);
+                       lua_settable(L, top_lvl2);
+               }
+
                lua_settable(L, top);
                index++;
        }
@@ -703,16 +712,13 @@ int ModApiMainMenu::l_set_topleft_text(lua_State *L)
 /******************************************************************************/
 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
 {
-       lua_newtable(L);
+       std::vector<const char *> names;
+       Mapgen::getMapgenNames(&names, lua_toboolean(L, 1));
 
-       std::list<const char *> names;
-       EmergeManager::getMapgenNames(names);
-
-       int i = 1;
-       for (std::list<const char *>::const_iterator
-                       it = names.begin(); it != names.end(); ++it) {
-               lua_pushstring(L, *it);
-               lua_rawseti(L, -2, i++);
+       lua_newtable(L);
+       for (size_t i = 0; i != names.size(); i++) {
+               lua_pushstring(L, names[i]);
+               lua_rawseti(L, -2, i + 1);
        }
 
        return 1;
@@ -722,86 +728,73 @@ int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
 /******************************************************************************/
 int ModApiMainMenu::l_get_modpath(lua_State *L)
 {
-       std::string modpath
-                       = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
+       std::string modpath = fs::RemoveRelativePathComponents(
+               porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
        lua_pushstring(L, modpath.c_str());
        return 1;
 }
 
 /******************************************************************************/
-int ModApiMainMenu::l_get_gamepath(lua_State *L)
+int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
 {
-       std::string gamepath
-                       = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
-       lua_pushstring(L, gamepath.c_str());
+       std::string modpath = fs::RemoveRelativePathComponents(
+               porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
+       lua_pushstring(L, modpath.c_str());
        return 1;
 }
 
 /******************************************************************************/
-int ModApiMainMenu::l_get_texturepath(lua_State *L)
+int ModApiMainMenu::l_get_gamepath(lua_State *L)
 {
-       std::string gamepath
-                       = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures");
+       std::string gamepath = fs::RemoveRelativePathComponents(
+               porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
        lua_pushstring(L, gamepath.c_str());
        return 1;
 }
 
-int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
+/******************************************************************************/
+int ModApiMainMenu::l_get_texturepath(lua_State *L)
 {
-       std::string gamepath
-                       = fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "textures");
+       std::string gamepath = fs::RemoveRelativePathComponents(
+               porting::path_user + DIR_DELIM + "textures");
        lua_pushstring(L, gamepath.c_str());
        return 1;
 }
 
-/******************************************************************************/
-int ModApiMainMenu::l_get_dirlist(lua_State *L)
+int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
 {
-       const char *path        = luaL_checkstring(L, 1);
-       bool dironly            = lua_toboolean(L, 2);
-
-       std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
-
-       unsigned int index = 1;
-       lua_newtable(L);
-       int table = lua_gettop(L);
-
-       for (unsigned int i=0;i< dirlist.size(); i++) {
-               if ((dirlist[i].dir) || (dironly == false)) {
-                       lua_pushnumber(L,index);
-                       lua_pushstring(L,dirlist[i].name.c_str());
-                       lua_settable(L, table);
-                       index++;
-               }
-       }
-
+       std::string gamepath = fs::RemoveRelativePathComponents(
+               porting::path_share + DIR_DELIM + "textures");
+       lua_pushstring(L, gamepath.c_str());
        return 1;
 }
 
 /******************************************************************************/
 int ModApiMainMenu::l_create_dir(lua_State *L) {
-       const char *path        = luaL_checkstring(L, 1);
+       const char *path = luaL_checkstring(L, 1);
 
        if (ModApiMainMenu::isMinetestPath(path)) {
-               lua_pushboolean(L,fs::CreateAllDirs(path));
+               lua_pushboolean(L, fs::CreateAllDirs(path));
                return 1;
        }
-       lua_pushboolean(L,false);
+
+       lua_pushboolean(L, false);
        return 1;
 }
 
 /******************************************************************************/
 int ModApiMainMenu::l_delete_dir(lua_State *L)
 {
-       const char *path        = luaL_checkstring(L, 1);
+       const char *path = luaL_checkstring(L, 1);
 
        std::string absolute_path = fs::RemoveRelativePathComponents(path);
 
        if (ModApiMainMenu::isMinetestPath(absolute_path)) {
-               lua_pushboolean(L,fs::RecursiveDelete(absolute_path));
+               lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
                return 1;
        }
-       lua_pushboolean(L,false);
+
+       lua_pushboolean(L, false);
        return 1;
 }
 
@@ -840,7 +833,7 @@ int ModApiMainMenu::l_copy_dir(lua_State *L)
 int ModApiMainMenu::l_extract_zip(lua_State *L)
 {
        GUIEngine* engine = getGuiEngine(L);
-       sanity_check(engine != NULL);(engine != 0);
+       sanity_check(engine);
 
        const char *zipfile     = luaL_checkstring(L, 1);
        const char *destination = luaL_checkstring(L, 2);
@@ -957,13 +950,14 @@ bool ModApiMainMenu::isMinetestPath(std::string path)
 }
 
 /******************************************************************************/
-int ModApiMainMenu::l_show_file_open_dialog(lua_State *L)
+int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
 {
        GUIEngine* engine = getGuiEngine(L);
        sanity_check(engine != NULL);
 
        const char *formname= luaL_checkstring(L, 1);
        const char *title       = luaL_checkstring(L, 2);
+       bool is_file_select = lua_toboolean(L, 3);
 
        GUIFileSelectMenu* fileOpenMenu =
                new GUIFileSelectMenu(engine->m_device->getGUIEnvironment(),
@@ -971,46 +965,13 @@ int ModApiMainMenu::l_show_file_open_dialog(lua_State *L)
                                                                -1,
                                                                engine->m_menumanager,
                                                                title,
-                                                               formname);
+                                                               formname,
+                                                               is_file_select);
        fileOpenMenu->setTextDest(engine->m_buttonhandler);
        fileOpenMenu->drop();
        return 0;
 }
 
-/******************************************************************************/
-int ModApiMainMenu::l_get_version(lua_State *L)
-{
-       lua_pushstring(L, g_version_string);
-       return 1;
-}
-
-/******************************************************************************/
-int ModApiMainMenu::l_sound_play(lua_State *L)
-{
-       GUIEngine* engine = getGuiEngine(L);
-
-       SimpleSoundSpec spec;
-       read_soundspec(L, 1, spec);
-       bool looped = lua_toboolean(L, 2);
-
-       u32 handle = engine->playSound(spec, looped);
-
-       lua_pushinteger(L, handle);
-
-       return 1;
-}
-
-/******************************************************************************/
-int ModApiMainMenu::l_sound_stop(lua_State *L)
-{
-       GUIEngine* engine = getGuiEngine(L);
-
-       u32 handle = luaL_checkinteger(L, 1);
-       engine->stopSound(handle);
-
-       return 1;
-}
-
 /******************************************************************************/
 int ModApiMainMenu::l_download_file(lua_State *L)
 {
@@ -1056,11 +1017,33 @@ int ModApiMainMenu::l_get_video_drivers(lua_State *L)
        return 1;
 }
 
+/******************************************************************************/
+int ModApiMainMenu::l_get_video_modes(lua_State *L)
+{
+       std::vector<core::vector3d<u32> > videomodes
+               = porting::getSupportedVideoModes();
+
+       lua_newtable(L);
+       for (u32 i = 0; i != videomodes.size(); i++) {
+               lua_newtable(L);
+               lua_pushnumber(L, videomodes[i].X);
+               lua_setfield(L, -2, "w");
+               lua_pushnumber(L, videomodes[i].Y);
+               lua_setfield(L, -2, "h");
+               lua_pushnumber(L, videomodes[i].Z);
+               lua_setfield(L, -2, "depth");
+
+               lua_rawseti(L, -2, i + 1);
+       }
+
+       return 1;
+}
+
 /******************************************************************************/
 int ModApiMainMenu::l_gettext(lua_State *L)
 {
-       std::wstring wtext = wstrgettext((std::string) luaL_checkstring(L, 1));
-       lua_pushstring(L, wide_to_narrow(wtext).c_str());
+       std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
+       lua_pushstring(L, text.c_str());
 
        return 1;
 }
@@ -1095,7 +1078,9 @@ int ModApiMainMenu::l_get_screen_info(lua_State *L)
 /******************************************************************************/
 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
 {
-       lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
+       u16 proto_version_min = g_settings->getFlag("send_pre_v25_init") ?
+               CLIENT_PROTOCOL_VERSION_MIN_LEGACY : CLIENT_PROTOCOL_VERSION_MIN;
+       lua_pushinteger(L, proto_version_min);
        return 1;
 }
 
@@ -1146,24 +1131,22 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
        API_FCT(set_topleft_text);
        API_FCT(get_mapgen_names);
        API_FCT(get_modpath);
+       API_FCT(get_clientmodpath);
        API_FCT(get_gamepath);
        API_FCT(get_texturepath);
        API_FCT(get_texturepath_share);
-       API_FCT(get_dirlist);
        API_FCT(create_dir);
        API_FCT(delete_dir);
        API_FCT(copy_dir);
        API_FCT(extract_zip);
        API_FCT(get_mainmenu_path);
-       API_FCT(show_file_open_dialog);
-       API_FCT(get_version);
+       API_FCT(show_path_select_dialog);
        API_FCT(download_file);
        API_FCT(get_modstore_details);
        API_FCT(get_modstore_list);
-       API_FCT(sound_play);
-       API_FCT(sound_stop);
        API_FCT(gettext);
        API_FCT(get_video_drivers);
+       API_FCT(get_video_modes);
        API_FCT(get_screen_info);
        API_FCT(get_min_supp_proto);
        API_FCT(get_max_supp_proto);
@@ -1171,25 +1154,24 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
 }
 
 /******************************************************************************/
-void ModApiMainMenu::InitializeAsync(AsyncEngine& engine)
+void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
 {
 
-       ASYNC_API_FCT(get_worlds);
-       ASYNC_API_FCT(get_games);
-       ASYNC_API_FCT(get_favorites);
-       ASYNC_API_FCT(get_mapgen_names);
-       ASYNC_API_FCT(get_modpath);
-       ASYNC_API_FCT(get_gamepath);
-       ASYNC_API_FCT(get_texturepath);
-       ASYNC_API_FCT(get_texturepath_share);
-       ASYNC_API_FCT(get_dirlist);
-       ASYNC_API_FCT(create_dir);
-       ASYNC_API_FCT(delete_dir);
-       ASYNC_API_FCT(copy_dir);
-       //ASYNC_API_FCT(extract_zip); //TODO remove dependency to GuiEngine
-       ASYNC_API_FCT(get_version);
-       ASYNC_API_FCT(download_file);
-       ASYNC_API_FCT(get_modstore_details);
-       ASYNC_API_FCT(get_modstore_list);
-       //ASYNC_API_FCT(gettext); (gettext lib isn't threadsafe)
+       API_FCT(get_worlds);
+       API_FCT(get_games);
+       API_FCT(get_favorites);
+       API_FCT(get_mapgen_names);
+       API_FCT(get_modpath);
+       API_FCT(get_clientmodpath);
+       API_FCT(get_gamepath);
+       API_FCT(get_texturepath);
+       API_FCT(get_texturepath_share);
+       API_FCT(create_dir);
+       API_FCT(delete_dir);
+       API_FCT(copy_dir);
+       //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
+       API_FCT(download_file);
+       API_FCT(get_modstore_details);
+       API_FCT(get_modstore_list);
+       //API_FCT(gettext); (gettext lib isn't threadsafe)
 }