Improve the path select GUI (#5852)
authorred-001 <red-001@outlook.ie>
Sun, 11 Jun 2017 07:43:31 +0000 (08:43 +0100)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Sun, 11 Jun 2017 07:43:31 +0000 (09:43 +0200)
- Allow lua to chose whatever directories or files can be selected
- Fix selecting directories
- Rename dialog to `guiPathSelectMenu` from `guiFileSelectMenu`
- Rename lua function for opening the menu from `show_file_open_dialog` to `show_path_select_dialog`
- Remove duplicate code and fix code style.

Related changes
- fix `clang-format` whitelist.
- Regenerate minetest.conf.example

12 files changed:
build/android/jni/Android.mk
builtin/mainmenu/dlg_settings_advanced.lua
builtin/settingtypes.txt
minetest.conf.example
src/CMakeLists.txt
src/guiFileSelectMenu.cpp [deleted file]
src/guiFileSelectMenu.h [deleted file]
src/guiPathSelectMenu.cpp [new file with mode: 0644]
src/guiPathSelectMenu.h [new file with mode: 0644]
src/script/lua_api/l_mainmenu.cpp
src/script/lua_api/l_mainmenu.h
util/travis/clang-format-whitelist.txt

index 7b741e04bc0c4fc0a04bb81aa3a20b0e70dd5a8f..8f3b342c514032501ef2c0a04a832c18ed5723fe 100644 (file)
@@ -152,7 +152,7 @@ LOCAL_SRC_FILES := \
                jni/src/gettext.cpp                       \
                jni/src/guiChatConsole.cpp                \
                jni/src/guiEngine.cpp                     \
-               jni/src/guiFileSelectMenu.cpp             \
+               jni/src/guiPathSelectMenu.cpp             \
                jni/src/guiFormSpecMenu.cpp               \
                jni/src/guiKeyChangeMenu.cpp              \
                jni/src/guiPasswordChange.cpp             \
index 206ce1620a7a78b9df51b69c4b16ea786ef0cb46..cac9f6983f5b05768e635b3d75dd5f02e6e2fc7e 100644 (file)
@@ -196,7 +196,7 @@ local function parse_setting_line(settings, line, read_all, base_level, allow_se
                return
        end
 
-       if setting_type == "path" then
+       if setting_type == "path" or setting_type == "filepath" then
                local default = remaining_line:match("^(.*)$")
 
                if not default then
@@ -206,7 +206,7 @@ local function parse_setting_line(settings, line, read_all, base_level, allow_se
                table.insert(settings, {
                        name = name,
                        readable_name = readable_name,
-                       type = "path",
+                       type = setting_type,
                        default = default,
                        comment = current_comment,
                })
@@ -504,14 +504,14 @@ local function create_change_setting_formspec(dialogdata)
                end
                formspec = formspec .. ";" .. selected_index .. "]"
 
-       elseif setting.type == "path" then
+       elseif setting.type == "path" or setting.type == "filepath" then
                local current_value = dialogdata.selected_path
                if not current_value then
                        current_value = get_current_value(setting)
                end
                formspec = formspec .. "field[0.5,4;7.5,1;te_setting_value;;"
                                .. core.formspec_escape(current_value) .. "]"
-                               .. "button[8,3.75;2,1;btn_browser_path;" .. fgettext("Browse") .. "]"
+                               .. "button[8,3.75;2,1;btn_browser_" .. setting.type .. ";" .. fgettext("Browse") .. "]"
 
        else
                -- TODO: fancy input for float, int, flags, noise_params, v3f
@@ -606,7 +606,13 @@ local function handle_change_setting_buttons(this, fields)
        end
 
        if fields["btn_browser_path"] then
-               core.show_file_open_dialog("dlg_browse_path", fgettext_ne("Select path"))
+               core.show_path_select_dialog("dlg_browse_path",
+                       fgettext_ne("Select directory"), false)
+       end
+
+       if fields["btn_browser_filepath"] then
+               core.show_path_select_dialog("dlg_browse_path",
+                       fgettext_ne("Select file"), true)
        end
 
        if fields["dlg_browse_path_accepted"] then
index 0f2715ab5e9a7ab7063c73fdc07fdba3935d7dc4..53fb238595bec975923d6c8517d81e0c23c1c534 100644 (file)
@@ -12,6 +12,7 @@
 #    - float
 #    - enum
 #    - path
+#    - filepath
 #    - key (will be ignored in GUI, since a special key change dialog exists)
 #    - flags
 #    - noise_params
@@ -31,6 +32,8 @@
 #            - default value1,value2,...
 #   * path:
 #            - default (if default is not specified then "" is set)
+#   * filepath:
+#            - default (if default is not specified then "" is set)
 #   * key:
 #            - default
 #   * flags:
@@ -642,7 +645,7 @@ tooltip_show_delay (Tooltip delay) int 400
 freetype (Freetype fonts) bool true
 
 #    Path to TrueTypeFont or bitmap.
-font_path (Font path) path fonts/liberationsans.ttf
+font_path (Font path) filepath fonts/liberationsans.ttf
 
 font_size (Font size) int 16
 
@@ -652,12 +655,12 @@ font_shadow (Font shadow) int 1
 #    Font shadow alpha (opaqueness, between 0 and 255).
 font_shadow_alpha (Font shadow alpha) int 127 0 255
 
-mono_font_path (Monospace font path) path fonts/liberationmono.ttf
+mono_font_path (Monospace font path) filepath fonts/liberationmono.ttf
 
 mono_font_size (Monospace font size) int 15
 
 #    This font will be used for certain languages.
-fallback_font_path (Fallback font) path fonts/DroidSansFallbackFull.ttf
+fallback_font_path (Fallback font) filepath fonts/DroidSansFallbackFull.ttf
 fallback_font_size (Fallback font size) int 15
 fallback_font_shadow (Fallback font shadow) int 1
 fallback_font_shadow_alpha (Fallback font shadow alpha) int 128 0 255
index 2487dad3449bf2bf58a510e65df34d52f3295e03..d3831defa7f19e88a7ba676f9f56ddd458dd9158 100644 (file)
 # freetype = true
 
 #    Path to TrueTypeFont or bitmap.
-#    type: path
+#    type: filepath
 # font_path = fonts/liberationsans.ttf
 
 #    type: int
 #    type: int min: 0 max: 255
 # font_shadow_alpha = 127
 
-#    type: path
+#    type: filepath
 # mono_font_path = fonts/liberationmono.ttf
 
 #    type: int
 # mono_font_size = 15
 
 #    This font will be used for certain languages.
-#    type: path
+#    type: filepath
 # fallback_font_path = fonts/DroidSansFallbackFull.ttf
 
 #    type: int
index 6963d0306d56adffee9f1d857511fcc45309dba0..08ea809c765c05847aee44f5f39a7b1e7a1e992a 100644 (file)
@@ -509,7 +509,7 @@ set(client_SRCS
        game.cpp
        guiChatConsole.cpp
        guiEngine.cpp
-       guiFileSelectMenu.cpp
+       guiPathSelectMenu.cpp
        guiFormSpecMenu.cpp
        guiKeyChangeMenu.cpp
        guiPasswordChange.cpp
diff --git a/src/guiFileSelectMenu.cpp b/src/guiFileSelectMenu.cpp
deleted file mode 100644 (file)
index 65a07be..0000000
+++ /dev/null
@@ -1,119 +0,0 @@
-/*
- 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 "guiFileSelectMenu.h"
-
-GUIFileSelectMenu::GUIFileSelectMenu(gui::IGUIEnvironment* env,
-               gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
-               const std::string &title, const std::string &formname) :
-       GUIModalMenu(env, parent, id, menumgr),
-       m_title(utf8_to_wide(title)),
-       m_accepted(false),
-       m_text_dst(NULL),
-       m_formname(formname)
-{
-}
-
-GUIFileSelectMenu::~GUIFileSelectMenu()
-{
-       removeChildren();
-       setlocale(LC_NUMERIC, "C");
-}
-
-void GUIFileSelectMenu::removeChildren()
-{
-       const core::list<gui::IGUIElement*> &children = getChildren();
-       core::list<gui::IGUIElement*> children_copy;
-       for (core::list<gui::IGUIElement*>::ConstIterator i = children.begin(); i
-                != children.end(); i++)
-       {
-               children_copy.push_back(*i);
-       }
-       for (core::list<gui::IGUIElement*>::Iterator i = children_copy.begin(); i
-                != children_copy.end(); i++)
-       {
-               (*i)->remove();
-       }
-}
-
-void GUIFileSelectMenu::regenerateGui(v2u32 screensize)
-{
-       removeChildren();
-       m_fileOpenDialog = 0;
-
-       core::dimension2du size(600,400);
-       core::rect < s32 > rect(0,0,screensize.X,screensize.Y);
-
-       DesiredRect = rect;
-       recalculateAbsolutePosition(false);
-
-       m_fileOpenDialog =
-                       Environment->addFileOpenDialog(m_title.c_str(),false,this,-1);
-
-       core::position2di pos = core::position2di(screensize.X/2 - size.Width/2,screensize.Y/2 -size.Height/2);
-       m_fileOpenDialog->setRelativePosition(pos);
-       m_fileOpenDialog->setMinSize(size);
-}
-
-void GUIFileSelectMenu::drawMenu()
-{
-       gui::IGUISkin* skin = Environment->getSkin();
-       if (!skin)
-               return;
-
-       gui::IGUIElement::draw();
-}
-
-void GUIFileSelectMenu::acceptInput() {
-       if ((m_text_dst != 0) && (this->m_formname != "")){
-               StringMap fields;
-
-               if (m_accepted)
-                       fields[m_formname + "_accepted"] = wide_to_utf8(m_fileOpenDialog->getFileName());
-               else
-                       fields[m_formname + "_canceled"] = m_formname;
-
-               this->m_text_dst->gotText(fields);
-       }
-}
-
-bool GUIFileSelectMenu::OnEvent(const SEvent& event)
-{
-
-       if (event.EventType == irr::EET_GUI_EVENT) {
-               switch (event.GUIEvent.EventType) {
-                       case gui::EGET_ELEMENT_CLOSED:
-                       case gui::EGET_FILE_CHOOSE_DIALOG_CANCELLED:
-                               m_accepted=false;
-                               acceptInput();
-                               quitMenu();
-                               return true;
-                       case gui::EGET_DIRECTORY_SELECTED:
-                       case gui::EGET_FILE_SELECTED:
-                               m_accepted=true;
-                               acceptInput();
-                               quitMenu();
-                               return true;
-                       default:
-                               //ignore this event
-                               break;
-               }
-       }
-       return Parent ? Parent->OnEvent(event) : false;
-}
diff --git a/src/guiFileSelectMenu.h b/src/guiFileSelectMenu.h
deleted file mode 100644 (file)
index 0348237..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- 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.
- */
-
-#ifndef GUIFILESELECTMENU_H_
-#define GUIFILESELECTMENU_H_
-
-#include <string>
-
-#include "modalMenu.h"
-#include "IGUIFileOpenDialog.h"
-#include "guiFormSpecMenu.h" //required because of TextDest only !!!
-
-class GUIFileSelectMenu : public GUIModalMenu
-{
-public:
-       GUIFileSelectMenu(gui::IGUIEnvironment *env, gui::IGUIElement *parent, s32 id,
-                       IMenuManager *menumgr, const std::string &title,
-                       const std::string &formid);
-       ~GUIFileSelectMenu();
-
-       void removeChildren();
-
-       /*
-        Remove and re-add (or reposition) stuff
-        */
-       void regenerateGui(v2u32 screensize);
-
-       void drawMenu();
-
-       bool OnEvent(const SEvent &event);
-
-       void setTextDest(TextDest *dest) { m_text_dst = dest; }
-
-private:
-       void acceptInput();
-
-       std::wstring m_title;
-       bool m_accepted;
-
-       gui::IGUIFileOpenDialog *m_fileOpenDialog;
-
-       TextDest *m_text_dst;
-
-       std::string m_formname;
-};
-
-#endif /* GUIFILESELECTMENU_H_ */
diff --git a/src/guiPathSelectMenu.cpp b/src/guiPathSelectMenu.cpp
new file mode 100644 (file)
index 0000000..d992d8c
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ 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 "guiPathSelectMenu.h"
+
+GUIFileSelectMenu::GUIFileSelectMenu(gui::IGUIEnvironment* env,
+               gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
+               const std::string &title, const std::string &formname,
+               bool is_file_select) :
+       GUIModalMenu(env, parent, id, menumgr),
+       m_title(utf8_to_wide(title)),
+       m_accepted(false),
+       m_text_dst(NULL),
+       m_formname(formname),
+       m_file_select_dialog(is_file_select)
+{
+}
+
+GUIFileSelectMenu::~GUIFileSelectMenu()
+{
+       removeChildren();
+       setlocale(LC_NUMERIC, "C");
+}
+
+void GUIFileSelectMenu::regenerateGui(v2u32 screensize)
+{
+       removeChildren();
+       m_fileOpenDialog = 0;
+
+       core::dimension2du size(600, 400);
+       core::rect<s32> rect(0, 0, screensize.X, screensize.Y);
+
+       DesiredRect = rect;
+       recalculateAbsolutePosition(false);
+
+       m_fileOpenDialog =
+                       Environment->addFileOpenDialog(m_title.c_str(), false, this, -1);
+
+       core::position2di pos = core::position2di(screensize.X / 2 - size.Width / 2,
+                       screensize.Y / 2 - size.Height / 2);
+       m_fileOpenDialog->setRelativePosition(pos);
+       m_fileOpenDialog->setMinSize(size);
+}
+
+void GUIFileSelectMenu::drawMenu()
+{
+       gui::IGUISkin *skin = Environment->getSkin();
+       if (!skin)
+               return;
+
+       gui::IGUIElement::draw();
+}
+
+void GUIFileSelectMenu::acceptInput()
+{
+       if ((m_text_dst != 0) && (this->m_formname != "")) {
+               StringMap fields;
+               if (m_accepted) {
+                       std::string path;
+                       if (!m_file_select_dialog) {
+                               core::string<fschar_t> string =
+                                               m_fileOpenDialog->getDirectoryName();
+                               path = std::string(string.c_str());
+                       } else {
+                               path = wide_to_utf8(m_fileOpenDialog->getFileName());
+                       }
+                       fields[m_formname + "_accepted"] = path;
+               } else {
+                       fields[m_formname + "_canceled"] = m_formname;
+               }
+               this->m_text_dst->gotText(fields);
+       }
+       quitMenu();
+}
+
+bool GUIFileSelectMenu::OnEvent(const SEvent &event)
+{
+       if (event.EventType == irr::EET_GUI_EVENT) {
+               switch (event.GUIEvent.EventType) {
+               case gui::EGET_ELEMENT_CLOSED:
+               case gui::EGET_FILE_CHOOSE_DIALOG_CANCELLED:
+                       m_accepted = false;
+                       acceptInput();
+                       return true;
+               case gui::EGET_DIRECTORY_SELECTED:
+                       m_accepted = !m_file_select_dialog;
+                       acceptInput();
+                       return true;
+               case gui::EGET_FILE_SELECTED:
+                       m_accepted = m_file_select_dialog;
+                       acceptInput();
+                       return true;
+               default:
+                       // ignore this event
+                       break;
+               }
+       }
+       return Parent ? Parent->OnEvent(event) : false;
+}
diff --git a/src/guiPathSelectMenu.h b/src/guiPathSelectMenu.h
new file mode 100644 (file)
index 0000000..add4e36
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ 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.
+ */
+
+#ifndef GUIFILESELECTMENU_H_
+#define GUIFILESELECTMENU_H_
+
+#include <string>
+
+#include "modalMenu.h"
+#include "IGUIFileOpenDialog.h"
+#include "guiFormSpecMenu.h" //required because of TextDest only !!!
+
+class GUIFileSelectMenu : public GUIModalMenu
+{
+public:
+       GUIFileSelectMenu(gui::IGUIEnvironment *env, gui::IGUIElement *parent, s32 id,
+                       IMenuManager *menumgr, const std::string &title,
+                       const std::string &formid, bool is_file_select);
+       ~GUIFileSelectMenu();
+
+       /*
+        Remove and re-add (or reposition) stuff
+        */
+       void regenerateGui(v2u32 screensize);
+
+       void drawMenu();
+
+       bool OnEvent(const SEvent &event);
+
+       void setTextDest(TextDest *dest) { m_text_dst = dest; }
+
+private:
+       void acceptInput();
+
+       std::wstring m_title;
+       bool m_accepted;
+
+       gui::IGUIFileOpenDialog *m_fileOpenDialog;
+
+       TextDest *m_text_dst;
+
+       std::string m_formname;
+       bool m_file_select_dialog;
+};
+
+#endif /* GUIFILESELECTMENU_H_ */
index 3889718145805ead9c8f9f9550b608dfcc472a80..3ed2ba0e0ee181b0a18790b5f19cc69924061539 100644 (file)
@@ -24,7 +24,7 @@ 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"
@@ -950,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(),
@@ -964,7 +965,8 @@ 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;
@@ -1138,7 +1140,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
        API_FCT(copy_dir);
        API_FCT(extract_zip);
        API_FCT(get_mainmenu_path);
-       API_FCT(show_file_open_dialog);
+       API_FCT(show_path_select_dialog);
        API_FCT(download_file);
        API_FCT(get_modstore_details);
        API_FCT(get_modstore_list);
index b5015add3705d50698699da85872a3a66af11f7b..1d3e931e16b53214950c1f249da2b2b1fe578820 100644 (file)
@@ -86,7 +86,7 @@ private:
 
        static int l_show_keys_menu(lua_State *L);
 
-       static int l_show_file_open_dialog(lua_State *L);
+       static int l_show_path_select_dialog(lua_State *L);
 
        static int l_set_topleft_text(lua_State *L);
 
index 932f59978dae84ce97e97543d0a6da9b68ddb6bf..66e312cdb012d7f06854410b4e7ddd6dbe298de9 100644 (file)
@@ -89,7 +89,8 @@ src/guiChatConsole.cpp
 src/guiChatConsole.h
 src/guiEngine.cpp
 src/guiEngine.h
-src/guiFileSelectMenu.cpp
+src/guiPathSelectMenu.cpp
+src/guiPathSelectMenu.h
 src/guiFormSpecMenu.cpp
 src/guiFormSpecMenu.h
 src/guiKeyChangeMenu.cpp