diff options
author | v-rob <robinsonvincent89@gmail.com> | 2020-07-10 03:11:26 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-10 12:11:26 +0200 |
commit | b1ff04e06da531dc52f6ee91e2967b39743033e6 (patch) | |
tree | 3d1af58d8c8003c5099122d0021b46d87c076c6f | |
parent | 2384c10e104fd0c945f3677130e7d25c0a841482 (diff) | |
download | minetest-b1ff04e06da531dc52f6ee91e2967b39743033e6.tar.gz minetest-b1ff04e06da531dc52f6ee91e2967b39743033e6.tar.bz2 minetest-b1ff04e06da531dc52f6ee91e2967b39743033e6.zip |
Formspec: Make dropdowns optionally return event based on index, not value (#9496)
-rw-r--r-- | doc/lua_api.txt | 15 | ||||
-rw-r--r-- | src/gui/guiFormSpecMenu.cpp | 39 | ||||
-rw-r--r-- | src/gui/guiFormSpecMenu.h | 1 | ||||
-rw-r--r-- | src/network/networkprotocol.h | 4 |
4 files changed, 39 insertions, 20 deletions
diff --git a/doc/lua_api.txt b/doc/lua_api.txt index f2a83eca5..2ac4d6766 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2443,7 +2443,7 @@ Elements * `color` is color specified as a `ColorString`. If the alpha component is left blank, the box will be semitransparent. -### `dropdown[<X>,<Y>;<W>;<name>;<item 1>,<item 2>, ...,<item n>;<selected idx>]` +### `dropdown[<X>,<Y>;<W>;<name>;<item 1>,<item 2>, ...,<item n>;<selected idx>;<index event>]` * Show a dropdown field * **Important note**: There are two different operation modes: @@ -2454,8 +2454,12 @@ Elements * Fieldname data is transferred to Lua * Items to be shown in dropdown * Index of currently selected dropdown item +* `index event` (optional, allowed parameter since formspec version 4): Specifies the + event field value for selected items. + * `true`: Selected item index + * `false` (default): Selected item value -### `dropdown[<X>,<Y>;<W>,<H>;<name>;<item 1>,<item 2>, ...,<item n>;<selected idx>]` +### `dropdown[<X>,<Y>;<W>,<H>;<name>;<item 1>,<item 2>, ...,<item n>;<selected idx>;<index event>]` * Show a dropdown field * **Important note**: This syntax for dropdowns can only be used with the @@ -2468,6 +2472,10 @@ Elements * Fieldname data is transferred to Lua * Items to be shown in dropdown * Index of currently selected dropdown item +* `index event` (optional, allowed parameter since formspec version 4): Specifies the + event field value for selected items. + * `true`: Selected item index + * `false` (default): Selected item value ### `checkbox[<X>,<Y>;<name>;<label>;<selected>]` @@ -4491,7 +4499,8 @@ Call these functions only at load time! * `button` and variants: If pressed, contains the user-facing button text as value. If not pressed, is `nil` * `field`, `textarea` and variants: Text in the field - * `dropdown`: Text of selected item + * `dropdown`: Either the index or value, depending on the `index event` + dropdown argument. * `tabheader`: Tab index, starting with `"1"` (only if tab changed) * `checkbox`: `"true"` if checked, `"false"` if unchecked * `textlist`: See `minetest.explode_textlist_event` diff --git a/src/gui/guiFormSpecMenu.cpp b/src/gui/guiFormSpecMenu.cpp index 37edf3c4b..54fc94993 100644 --- a/src/gui/guiFormSpecMenu.cpp +++ b/src/gui/guiFormSpecMenu.cpp @@ -1318,19 +1318,20 @@ void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl; } - void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element) { - std::vector<std::string> parts = split(element,';'); + std::vector<std::string> parts = split(element, ';'); - if ((parts.size() == 5) || - ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION))) + if (parts.size() == 5 || parts.size() == 6 || + (parts.size() > 6 && m_formspec_version > FORMSPEC_API_VERSION)) { - std::vector<std::string> v_pos = split(parts[0],','); + std::vector<std::string> v_pos = split(parts[0], ','); std::string name = parts[2]; - std::vector<std::string> items = split(parts[3],','); - std::string str_initial_selection; - str_initial_selection = parts[4]; + std::vector<std::string> items = split(parts[3], ','); + std::string str_initial_selection = parts[4]; + + if (parts.size() >= 6 && is_yes(parts[5])) + m_dropdown_index_event[name] = true; MY_CHECKPOS("dropdown",0); @@ -1397,8 +1398,8 @@ void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element return; } - errorstream << "Invalid dropdown element(" << parts.size() << "): '" - << element << "'" << std::endl; + errorstream << "Invalid dropdown element(" << parts.size() << "): '" << element + << "'" << std::endl; } void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element) @@ -1414,8 +1415,8 @@ void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element { std::vector<std::string> parts = split(element,';'); - if ((parts.size() == 4) || - ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION))) + if (parts.size() == 4 || + (parts.size() > 4 && m_formspec_version > FORMSPEC_API_VERSION)) { std::vector<std::string> v_pos = split(parts[0],','); std::vector<std::string> v_geom = split(parts[1],','); @@ -2940,6 +2941,8 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize) theme_by_name.clear(); theme_by_type.clear(); m_clickthrough_elements.clear(); + field_close_on_enter.clear(); + m_dropdown_index_event.clear(); m_bgnonfullscreen = true; m_bgfullscreen = false; @@ -3727,10 +3730,14 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no) } s32 selected = e->getSelected(); if (selected >= 0) { - std::vector<std::string> *dropdown_values = - getDropDownValues(s.fname); - if (dropdown_values && selected < (s32)dropdown_values->size()) { - fields[name] = (*dropdown_values)[selected]; + if (m_dropdown_index_event.find(s.fname) != + m_dropdown_index_event.end()) { + fields[name] = std::to_string(selected + 1); + } else { + std::vector<std::string> *dropdown_values = + getDropDownValues(s.fname); + if (dropdown_values && selected < (s32)dropdown_values->size()) + fields[name] = (*dropdown_values)[selected]; } } } else if (s.ftype == f_TabHeader) { diff --git a/src/gui/guiFormSpecMenu.h b/src/gui/guiFormSpecMenu.h index 28088416d..a91afd2f7 100644 --- a/src/gui/guiFormSpecMenu.h +++ b/src/gui/guiFormSpecMenu.h @@ -303,6 +303,7 @@ protected: std::vector<ListRingSpec> m_inventory_rings; std::vector<gui::IGUIElement *> m_backgrounds; std::unordered_map<std::string, bool> field_close_on_enter; + std::unordered_map<std::string, bool> m_dropdown_index_event; std::vector<FieldSpec> m_fields; std::vector<std::pair<FieldSpec, GUITable *>> m_tables; std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes; diff --git a/src/network/networkprotocol.h b/src/network/networkprotocol.h index fd683eac9..28abf02c0 100644 --- a/src/network/networkprotocol.h +++ b/src/network/networkprotocol.h @@ -238,8 +238,10 @@ with this program; if not, write to the Free Software Foundation, Inc., bgcolor[]: use 3 parameters (bgcolor, formspec (now an enum), fbgcolor) box[] and image[] elements enable clipping by default new element: scroll_container[] + FORMSPEC VERSION 4: + Allow dropdown indexing events */ -#define FORMSPEC_API_VERSION 3 +#define FORMSPEC_API_VERSION 4 #define TEXTURENAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-" |