Skip to content

Commit b1ff04e

Browse files
authoredJul 10, 2020
Formspec: Make dropdowns optionally return event based on index, not value (#9496)
1 parent 2384c10 commit b1ff04e

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed
 

Diff for: ‎doc/lua_api.txt

+12-3
Original file line numberDiff line numberDiff line change
@@ -2443,7 +2443,7 @@ Elements
24432443
* `color` is color specified as a `ColorString`.
24442444
If the alpha component is left blank, the box will be semitransparent.
24452445

2446-
### `dropdown[<X>,<Y>;<W>;<name>;<item 1>,<item 2>, ...,<item n>;<selected idx>]`
2446+
### `dropdown[<X>,<Y>;<W>;<name>;<item 1>,<item 2>, ...,<item n>;<selected idx>;<index event>]`
24472447

24482448
* Show a dropdown field
24492449
* **Important note**: There are two different operation modes:
@@ -2454,8 +2454,12 @@ Elements
24542454
* Fieldname data is transferred to Lua
24552455
* Items to be shown in dropdown
24562456
* Index of currently selected dropdown item
2457+
* `index event` (optional, allowed parameter since formspec version 4): Specifies the
2458+
event field value for selected items.
2459+
* `true`: Selected item index
2460+
* `false` (default): Selected item value
24572461

2458-
### `dropdown[<X>,<Y>;<W>,<H>;<name>;<item 1>,<item 2>, ...,<item n>;<selected idx>]`
2462+
### `dropdown[<X>,<Y>;<W>,<H>;<name>;<item 1>,<item 2>, ...,<item n>;<selected idx>;<index event>]`
24592463

24602464
* Show a dropdown field
24612465
* **Important note**: This syntax for dropdowns can only be used with the
@@ -2468,6 +2472,10 @@ Elements
24682472
* Fieldname data is transferred to Lua
24692473
* Items to be shown in dropdown
24702474
* Index of currently selected dropdown item
2475+
* `index event` (optional, allowed parameter since formspec version 4): Specifies the
2476+
event field value for selected items.
2477+
* `true`: Selected item index
2478+
* `false` (default): Selected item value
24712479

24722480
### `checkbox[<X>,<Y>;<name>;<label>;<selected>]`
24732481

@@ -4491,7 +4499,8 @@ Call these functions only at load time!
44914499
* `button` and variants: If pressed, contains the user-facing button
44924500
text as value. If not pressed, is `nil`
44934501
* `field`, `textarea` and variants: Text in the field
4494-
* `dropdown`: Text of selected item
4502+
* `dropdown`: Either the index or value, depending on the `index event`
4503+
dropdown argument.
44954504
* `tabheader`: Tab index, starting with `"1"` (only if tab changed)
44964505
* `checkbox`: `"true"` if checked, `"false"` if unchecked
44974506
* `textlist`: See `minetest.explode_textlist_event`

Diff for: ‎src/gui/guiFormSpecMenu.cpp

+23-16
Original file line numberDiff line numberDiff line change
@@ -1318,19 +1318,20 @@ void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element
13181318
errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
13191319
}
13201320

1321-
13221321
void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
13231322
{
1324-
std::vector<std::string> parts = split(element,';');
1323+
std::vector<std::string> parts = split(element, ';');
13251324

1326-
if ((parts.size() == 5) ||
1327-
((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1325+
if (parts.size() == 5 || parts.size() == 6 ||
1326+
(parts.size() > 6 && m_formspec_version > FORMSPEC_API_VERSION))
13281327
{
1329-
std::vector<std::string> v_pos = split(parts[0],',');
1328+
std::vector<std::string> v_pos = split(parts[0], ',');
13301329
std::string name = parts[2];
1331-
std::vector<std::string> items = split(parts[3],',');
1332-
std::string str_initial_selection;
1333-
str_initial_selection = parts[4];
1330+
std::vector<std::string> items = split(parts[3], ',');
1331+
std::string str_initial_selection = parts[4];
1332+
1333+
if (parts.size() >= 6 && is_yes(parts[5]))
1334+
m_dropdown_index_event[name] = true;
13341335

13351336
MY_CHECKPOS("dropdown",0);
13361337

@@ -1397,8 +1398,8 @@ void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element
13971398

13981399
return;
13991400
}
1400-
errorstream << "Invalid dropdown element(" << parts.size() << "): '"
1401-
<< element << "'" << std::endl;
1401+
errorstream << "Invalid dropdown element(" << parts.size() << "): '" << element
1402+
<< "'" << std::endl;
14021403
}
14031404

14041405
void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
@@ -1414,8 +1415,8 @@ void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element
14141415
{
14151416
std::vector<std::string> parts = split(element,';');
14161417

1417-
if ((parts.size() == 4) ||
1418-
((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
1418+
if (parts.size() == 4 ||
1419+
(parts.size() > 4 && m_formspec_version > FORMSPEC_API_VERSION))
14191420
{
14201421
std::vector<std::string> v_pos = split(parts[0],',');
14211422
std::vector<std::string> v_geom = split(parts[1],',');
@@ -2940,6 +2941,8 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
29402941
theme_by_name.clear();
29412942
theme_by_type.clear();
29422943
m_clickthrough_elements.clear();
2944+
field_close_on_enter.clear();
2945+
m_dropdown_index_event.clear();
29432946

29442947
m_bgnonfullscreen = true;
29452948
m_bgfullscreen = false;
@@ -3727,10 +3730,14 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
37273730
}
37283731
s32 selected = e->getSelected();
37293732
if (selected >= 0) {
3730-
std::vector<std::string> *dropdown_values =
3731-
getDropDownValues(s.fname);
3732-
if (dropdown_values && selected < (s32)dropdown_values->size()) {
3733-
fields[name] = (*dropdown_values)[selected];
3733+
if (m_dropdown_index_event.find(s.fname) !=
3734+
m_dropdown_index_event.end()) {
3735+
fields[name] = std::to_string(selected + 1);
3736+
} else {
3737+
std::vector<std::string> *dropdown_values =
3738+
getDropDownValues(s.fname);
3739+
if (dropdown_values && selected < (s32)dropdown_values->size())
3740+
fields[name] = (*dropdown_values)[selected];
37343741
}
37353742
}
37363743
} else if (s.ftype == f_TabHeader) {

Diff for: ‎src/gui/guiFormSpecMenu.h

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ class GUIFormSpecMenu : public GUIModalMenu
303303
std::vector<ListRingSpec> m_inventory_rings;
304304
std::vector<gui::IGUIElement *> m_backgrounds;
305305
std::unordered_map<std::string, bool> field_close_on_enter;
306+
std::unordered_map<std::string, bool> m_dropdown_index_event;
306307
std::vector<FieldSpec> m_fields;
307308
std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
308309
std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;

Diff for: ‎src/network/networkprotocol.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
238238
bgcolor[]: use 3 parameters (bgcolor, formspec (now an enum), fbgcolor)
239239
box[] and image[] elements enable clipping by default
240240
new element: scroll_container[]
241+
FORMSPEC VERSION 4:
242+
Allow dropdown indexing events
241243
*/
242-
#define FORMSPEC_API_VERSION 3
244+
#define FORMSPEC_API_VERSION 4
243245

244246
#define TEXTURENAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-"
245247

0 commit comments

Comments
 (0)
Please sign in to comment.