Skip to content

Commit e10fee0

Browse files
committedAug 27, 2016
Allow fields to choose whether they close on enter press
1 parent 4330c63 commit e10fee0

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed
 

‎doc/lua_api.txt

+15-3
Original file line numberDiff line numberDiff line change
@@ -1503,17 +1503,23 @@ examples.
15031503
* If `true` the background is clipped to formspec size
15041504
(`x` and `y` are used as offset values, `w` and `h` are ignored)
15051505

1506-
#### `pwdfield[<X>,<Y>;<W>,<H>;<name>;<label>]`
1506+
#### `pwdfield[<X>,<Y>;<W>,<H>;<name>;<label>;<close_on_enter>]`
15071507
* Textual password style field; will be sent to server when a button is clicked
1508+
* When enter is pressed in field, fields.key_enter_field will be sent with the name
1509+
of this field.
15081510
* `x` and `y` position the field relative to the top left of the menu
15091511
* `w` and `h` are the size of the field
15101512
* Fields are a set height, but will be vertically centred on `h`
15111513
* Position and size units are inventory slots
15121514
* `name` is the name of the field as returned in fields to `on_receive_fields`
15131515
* `label`, if not blank, will be text printed on the top left above the field
1516+
* `close_on_enter` (optional) is whether the form should accept and close when enter is
1517+
pressed in this field. Defaults to true.
15141518

1515-
#### `field[<X>,<Y>;<W>,<H>;<name>;<label>;<default>]`
1519+
#### `field[<X>,<Y>;<W>,<H>;<name>;<label>;<default>;<close_on_enter>]`
15161520
* Textual field; will be sent to server when a button is clicked
1521+
* When enter is pressed in field, fields.key_enter_field will be sent with the name
1522+
of this field.
15171523
* `x` and `y` position the field relative to the top left of the menu
15181524
* `w` and `h` are the size of the field
15191525
* Fields are a set height, but will be vertically centred on `h`
@@ -1524,12 +1530,18 @@ examples.
15241530
* `default` may contain variable references such as `${text}'` which
15251531
will fill the value from the metadata value `text`
15261532
* **Note**: no extra text or more than a single variable is supported ATM.
1533+
* `close_on_enter` (optional) is whether the form should accept and close when enter is
1534+
pressed in this field. Defaults to true.
15271535

1528-
#### `field[<name>;<label>;<default>]`
1536+
#### `field[<name>;<label>;<default>;<close_on_enter>]`
15291537
* As above, but without position/size units
1538+
* When enter is pressed in field, fields.key_enter_field will be sent with the name
1539+
of this field.
15301540
* Special field for creating simple forms, such as sign text input
15311541
* Must be used without a `size[]` element
15321542
* A "Proceed" button will be added automatically
1543+
* `close_on_enter` (optional) is whether the form should accept and close when enter is
1544+
pressed in this field. Defaults to true.
15331545

15341546
#### `textarea[<X>,<Y>;<W>,<H>;<name>;<label>;<default>]`
15351547
* Same as fields above, but with multi-line input

‎src/guiFormSpecMenu.cpp

+23-5
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,8 @@ void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
894894
{
895895
std::vector<std::string> parts = split(element,';');
896896

897-
if ((parts.size() == 4) ||
898-
((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
897+
if ((parts.size() == 4) || (parts.size() == 5) ||
898+
((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
899899
{
900900
std::vector<std::string> v_pos = split(parts[0],',');
901901
std::vector<std::string> v_geom = split(parts[1],',');
@@ -952,6 +952,11 @@ void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
952952
evt.KeyInput.Shift = 0;
953953
evt.KeyInput.PressedDown = true;
954954
e->OnEvent(evt);
955+
956+
if (parts.size() >= 5 && !is_yes(parts[4])) {
957+
spec.close_on_enter = false;
958+
}
959+
955960
m_fields.push_back(spec);
956961
return;
957962
}
@@ -1033,6 +1038,10 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,
10331038
}
10341039
}
10351040

1041+
if (parts.size() >= 4 && !is_yes(parts[3])) {
1042+
spec.close_on_enter = false;
1043+
}
1044+
10361045
m_fields.push_back(spec);
10371046
}
10381047

@@ -1137,6 +1146,11 @@ void GUIFormSpecMenu::parseTextArea(parserData* data,
11371146
addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
11381147
}
11391148
}
1149+
1150+
if (parts.size() >= 6 && !is_yes(parts[5])) {
1151+
spec.close_on_enter = false;
1152+
}
1153+
11401154
m_fields.push_back(spec);
11411155
}
11421156

@@ -1150,8 +1164,8 @@ void GUIFormSpecMenu::parseField(parserData* data,std::string element,
11501164
return;
11511165
}
11521166

1153-
if ((parts.size() == 5) ||
1154-
((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
1167+
if ((parts.size() == 5) || (parts.size() == 6) ||
1168+
((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
11551169
{
11561170
parseTextArea(data,parts,type);
11571171
return;
@@ -2698,6 +2712,7 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
26982712

26992713
if (!current_field_enter_pending.empty()) {
27002714
fields["key_enter_field"] = current_field_enter_pending;
2715+
current_field_enter_pending = "";
27012716
}
27022717

27032718
if (current_keys_pending.key_escape) {
@@ -3630,15 +3645,18 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
36303645

36313646
if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
36323647
if (event.GUIEvent.Caller->getID() > 257) {
3648+
bool close_on_enter = true;
36333649
for (u32 i = 0; i < m_fields.size(); i++) {
36343650
FieldSpec &s = m_fields[i];
36353651
if (s.ftype == f_Unknown &&
36363652
s.fid == event.GUIEvent.Caller->getID()) {
36373653
current_field_enter_pending = s.fname;
3654+
close_on_enter = s.close_on_enter;
3655+
break;
36383656
}
36393657
}
36403658

3641-
if (m_allowclose) {
3659+
if (m_allowclose && close_on_enter) {
36423660
current_keys_pending.key_enter = true;
36433661
acceptInput(quit_mode_accept);
36443662
quitMenu();

‎src/guiFormSpecMenu.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -202,20 +202,22 @@ class GUIFormSpecMenu : public GUIModalMenu
202202
FieldSpec(const std::string &name, const std::wstring &label,
203203
const std::wstring &default_text, int id) :
204204
fname(name),
205-
fid(id)
205+
flabel(label),
206+
fid(id),
207+
send(false),
208+
close_on_enter(false),
209+
ftype(f_Unknown),
210+
is_exit(false)
206211
{
207212
//flabel = unescape_enriched(label);
208-
flabel = label;
209213
fdefault = unescape_enriched(default_text);
210-
send = false;
211-
ftype = f_Unknown;
212-
is_exit = false;
213214
}
214215
std::string fname;
215216
std::wstring flabel;
216217
std::wstring fdefault;
217218
int fid;
218219
bool send;
220+
bool close_on_enter; // used by text fields
219221
FormspecFieldType ftype;
220222
bool is_exit;
221223
core::rect<s32> rect;

0 commit comments

Comments
 (0)
Please sign in to comment.