Fix backwards compatibility issue introduced by close_on_enter

master
rubenwardy 2016-10-03 00:30:33 +01:00 committed by Ner'zhul
parent 0baea8c25c
commit 067766eec2
3 changed files with 48 additions and 18 deletions

View File

@ -1542,7 +1542,7 @@ examples.
* If `true` the background is clipped to formspec size * If `true` the background is clipped to formspec size
(`x` and `y` are used as offset values, `w` and `h` are ignored) (`x` and `y` are used as offset values, `w` and `h` are ignored)
#### `pwdfield[<X>,<Y>;<W>,<H>;<name>;<label>;<close_on_enter>]` #### `pwdfield[<X>,<Y>;<W>,<H>;<name>;<label>]`
* Textual password style field; will be sent to server when a button is clicked * Textual password style field; will be sent to server when a button is clicked
* When enter is pressed in field, fields.key_enter_field will be sent with the name * When enter is pressed in field, fields.key_enter_field will be sent with the name
of this field. of this field.
@ -1552,10 +1552,9 @@ examples.
* Position and size units are inventory slots * Position and size units are inventory slots
* `name` is the name of the field as returned in fields to `on_receive_fields` * `name` is the name of the field as returned in fields to `on_receive_fields`
* `label`, if not blank, will be text printed on the top left above the field * `label`, if not blank, will be text printed on the top left above the field
* `close_on_enter` (optional) is whether the form should accept and close when enter is * See field_close_on_enter to stop enter closing the formspec
pressed in this field. Defaults to true.
#### `field[<X>,<Y>;<W>,<H>;<name>;<label>;<default>;<close_on_enter>]` #### `field[<X>,<Y>;<W>,<H>;<name>;<label>;<default>]`
* Textual field; will be sent to server when a button is clicked * Textual field; will be sent to server when a button is clicked
* When enter is pressed in field, fields.key_enter_field will be sent with the name * When enter is pressed in field, fields.key_enter_field will be sent with the name
of this field. of this field.
@ -1569,18 +1568,21 @@ examples.
* `default` may contain variable references such as `${text}'` which * `default` may contain variable references such as `${text}'` which
will fill the value from the metadata value `text` will fill the value from the metadata value `text`
* **Note**: no extra text or more than a single variable is supported ATM. * **Note**: no extra text or more than a single variable is supported ATM.
* `close_on_enter` (optional) is whether the form should accept and close when enter is * See field_close_on_enter to stop enter closing the formspec
pressed in this field. Defaults to true.
#### `field[<name>;<label>;<default>;<close_on_enter>]` #### `field[<name>;<label>;<default>]`
* As above, but without position/size units * As above, but without position/size units
* When enter is pressed in field, fields.key_enter_field will be sent with the name * When enter is pressed in field, fields.key_enter_field will be sent with the name
of this field. of this field.
* Special field for creating simple forms, such as sign text input * Special field for creating simple forms, such as sign text input
* Must be used without a `size[]` element * Must be used without a `size[]` element
* A "Proceed" button will be added automatically * A "Proceed" button will be added automatically
* `close_on_enter` (optional) is whether the form should accept and close when enter is * See field_close_on_enter to stop enter closing the formspec
pressed in this field. Defaults to true.
#### `field_close_on_enter[<name>;<close_on_enter>]`
* <name> is the name of the field
* if <close_on_enter> is false, pressing enter in the field will submit the form but not close it
* defaults to true when not specified (ie: no tag for a field)
#### `textarea[<X>,<Y>;<W>,<H>;<name>;<label>;<default>]` #### `textarea[<X>,<Y>;<W>,<H>;<name>;<label>;<default>]`
* Same as fields above, but with multi-line input * Same as fields above, but with multi-line input

View File

@ -914,6 +914,16 @@ void GUIFormSpecMenu::parseDropDown(parserData* data,std::string element)
<< element << "'" << std::endl; << element << "'" << std::endl;
} }
void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data,
const std::string &element)
{
std::vector<std::string> parts = split(element,';');
if (parts.size() == 2 ||
(parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
field_close_on_enter[parts[0]] = is_yes(parts[1]);
}
}
void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element) void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
{ {
std::vector<std::string> parts = split(element,';'); std::vector<std::string> parts = split(element,';');
@ -977,8 +987,11 @@ void GUIFormSpecMenu::parsePwdField(parserData* data,std::string element)
evt.KeyInput.PressedDown = true; evt.KeyInput.PressedDown = true;
e->OnEvent(evt); e->OnEvent(evt);
if (parts.size() >= 5 && !is_yes(parts[4])) { if (parts.size() >= 5) {
spec.close_on_enter = false; // TODO: remove after 2016-11-03
warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
" instead of the 5th param" << std::endl;
field_close_on_enter[name] = is_yes(parts[4]);
} }
m_fields.push_back(spec); m_fields.push_back(spec);
@ -1062,8 +1075,11 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,
} }
} }
if (parts.size() >= 4 && !is_yes(parts[3])) { if (parts.size() >= 4) {
spec.close_on_enter = false; // TODO: remove after 2016-11-03
warningstream << "field/simple: use field_close_on_enter[name, enabled]" <<
" instead of the 4th param" << std::endl;
field_close_on_enter[name] = is_yes(parts[3]);
} }
m_fields.push_back(spec); m_fields.push_back(spec);
@ -1171,8 +1187,11 @@ void GUIFormSpecMenu::parseTextArea(parserData* data,
} }
} }
if (parts.size() >= 6 && !is_yes(parts[5])) { if (parts.size() >= 6) {
spec.close_on_enter = false; // TODO: remove after 2016-11-03
warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
" instead of the 6th param" << std::endl;
field_close_on_enter[name] = is_yes(parts[5]);
} }
m_fields.push_back(spec); m_fields.push_back(spec);
@ -1783,6 +1802,11 @@ void GUIFormSpecMenu::parseElement(parserData* data, std::string element)
return; return;
} }
if (type == "field_close_on_enter") {
parseFieldCloseOnEnter(data, description);
return;
}
if (type == "pwdfield") { if (type == "pwdfield") {
parsePwdField(data,description); parsePwdField(data,description);
return; return;
@ -3689,7 +3713,11 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
if (s.ftype == f_Unknown && if (s.ftype == f_Unknown &&
s.fid == event.GUIEvent.Caller->getID()) { s.fid == event.GUIEvent.Caller->getID()) {
current_field_enter_pending = s.fname; current_field_enter_pending = s.fname;
close_on_enter = s.close_on_enter; UNORDERED_MAP<std::string, bool>::const_iterator it =
field_close_on_enter.find(s.fname);
if (it != field_close_on_enter.end())
close_on_enter = (*it).second;
break; break;
} }
} }

View File

@ -212,7 +212,6 @@ class GUIFormSpecMenu : public GUIModalMenu
flabel(label), flabel(label),
fid(id), fid(id),
send(false), send(false),
close_on_enter(false),
ftype(f_Unknown), ftype(f_Unknown),
is_exit(false) is_exit(false)
{ {
@ -224,7 +223,6 @@ class GUIFormSpecMenu : public GUIModalMenu
std::wstring fdefault; std::wstring fdefault;
int fid; int fid;
bool send; bool send;
bool close_on_enter; // used by text fields
FormspecFieldType ftype; FormspecFieldType ftype;
bool is_exit; bool is_exit;
core::rect<s32> rect; core::rect<s32> rect;
@ -400,6 +398,7 @@ protected:
std::vector<ImageDrawSpec> m_images; std::vector<ImageDrawSpec> m_images;
std::vector<ImageDrawSpec> m_itemimages; std::vector<ImageDrawSpec> m_itemimages;
std::vector<BoxDrawSpec> m_boxes; std::vector<BoxDrawSpec> m_boxes;
UNORDERED_MAP<std::string, bool> field_close_on_enter;
std::vector<FieldSpec> m_fields; std::vector<FieldSpec> m_fields;
std::vector<StaticTextSpec> m_static_texts; std::vector<StaticTextSpec> m_static_texts;
std::vector<std::pair<FieldSpec,GUITable*> > m_tables; std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
@ -490,6 +489,7 @@ private:
void parseTable(parserData* data,std::string element); void parseTable(parserData* data,std::string element);
void parseTextList(parserData* data,std::string element); void parseTextList(parserData* data,std::string element);
void parseDropDown(parserData* data,std::string element); void parseDropDown(parserData* data,std::string element);
void parseFieldCloseOnEnter(parserData *data, const std::string &element);
void parsePwdField(parserData* data,std::string element); void parsePwdField(parserData* data,std::string element);
void parseField(parserData* data,std::string element,std::string type); void parseField(parserData* data,std::string element,std::string type);
void parseSimpleField(parserData* data,std::vector<std::string> &parts); void parseSimpleField(parserData* data,std::vector<std::string> &parts);