Skip to content

Commit 91bf635

Browse files
committed
Add allow_close[] element to formspecs
1 parent 47c75b3 commit 91bf635

20 files changed

+80
-43
lines changed

builtin/common/settings/dlg_change_mapgen_flags.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ local function get_formspec(dialogdata)
177177

178178
return (
179179
"size[" .. width .. "," .. height + 0.25 .. ",true]" ..
180+
"allow_close[false]" ..
180181
create_textfield(description_box, setting_name, comment_text) ..
181182
formspec ..
182183
"button[" .. width / 2 - 2.5 .. "," .. height - 0.4 .. ";2.5,1;btn_done;" ..

builtin/common/settings/dlg_settings.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ local function get_formspec(dialogdata)
541541
"formspec_version[6]",
542542
"size[", tostring(tabsize.width), ",", tostring(tabsize.height + extra_h), "]",
543543
core.settings:get_bool("touch_gui") and "padding[0.01,0.01]" or "",
544+
"allow_close[" .. tostring(dialogdata.allow_close) .. "]",
544545
"bgcolor[#0000]",
545546

546547
-- HACK: this is needed to allow resubmitting the same formspec
@@ -809,6 +810,7 @@ if INIT == "mainmenu" then
809810
local dlg = dialog_create("dlg_settings", get_formspec, buttonhandler, eventhandler)
810811

811812
dlg.data.page_id = update_filtered_pages("")
813+
dlg.data.allow_close = false
812814

813815
return dlg
814816
end
@@ -834,6 +836,7 @@ else
834836
dialog = {}
835837
dialog.data = {}
836838
dialog.data.page_id = update_filtered_pages("")
839+
dialog.data.allow_close = true
837840
dialog.delete = function()
838841
dialog = nil
839842
-- only needed for the "fields.back" case, in the "fields.quit"

builtin/fstk/tabview.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ local function get_formspec(self)
8585
if tab.formspec_version then
8686
prepend = ("formspec_version[%d]"):format(tab.formspec_version) .. prepend
8787
end
88+
89+
prepend = prepend .. "allow_close[false]"
90+
8891
end
8992

9093
local end_button_size = 0.75

builtin/fstk/ui.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function ui.get_message_formspec(title, message, btn_id)
5757
return table.concat({
5858
"size[14,8]",
5959
"real_coordinates[true]",
60+
"allow_close[false]",
6061
"set_focus[", btn_id, ";true]",
6162
"box[0.5,1.2;13,5;#000]",
6263
("textarea[0.5,1.2;13,5;;%s;%s]"):format(title, message),
@@ -81,6 +82,7 @@ function ui.update()
8182
formspec = {
8283
"size[14,8]",
8384
"real_coordinates[true]",
85+
"allow_close[false]",
8486
"set_focus[btn_reconnect_yes;true]",
8587
"box[0.5,1.2;13,5;#000]",
8688
("textarea[0.5,1.2;13,5;;%s;%s]"):format(

builtin/mainmenu/common.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ end
285285

286286
function confirmation_formspec(message, confirm_id, confirm_label, cancel_id, cancel_label)
287287
return "size[10,2.5,true]" ..
288+
"allow_close[false]" ..
288289
"label[0.5,0.5;" .. message .. "]" ..
289290
"style[" .. confirm_id .. ";bgcolor=red]" ..
290291
"button[0.5,1.5;2.5,0.5;" .. confirm_id .. ";" .. confirm_label .. "]" ..

builtin/mainmenu/content/dlg_contentdb.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ local function get_info_formspec(size, padding, text)
133133
"formspec_version[6]",
134134
"size[", size.x, ",", size.y, "]",
135135
"padding[0,0]",
136+
"allow_close[false]",
136137
"bgcolor[;true]",
137138

138139
"label[", padding.x + 3.625, ",4.35;", text, "]",
@@ -232,6 +233,7 @@ local function get_formspec(dlgdata)
232233
"formspec_version[7]",
233234
"size[", size.x, ",", size.y, "]",
234235
"padding[0,0]",
236+
"allow_close[false]",
235237
"bgcolor[;true]",
236238

237239
"container[", window_padding.x, ",", window_padding.y, "]",

builtin/mainmenu/content/dlg_install.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ local function get_loading_formspec()
2929
"formspec_version[3]",
3030
"size[", w, ",9.05]",
3131
TOUCH_GUI and "padding[0.01,0.01]" or "position[0.5,0.55]",
32+
"allow_close[false]",
3233
"label[3,4.525;", fgettext("Loading..."), "]",
3334
}
3435
return table.concat(formspec)
@@ -122,6 +123,7 @@ local function get_formspec(data)
122123
"formspec_version[3]",
123124
"size[", w, ",9.05]",
124125
TOUCH_GUI and "padding[0.01,0.01]" or "position[0.5,0.55]",
126+
"allow_close[false]",
125127
"style[title;border=false]",
126128
"box[0,0;", w, ",0.8;#3333]",
127129
"button[0,0;", w, ",0.8;title;", fgettext("Install $1", package.title) , "]",

builtin/mainmenu/content/dlg_package.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ local function get_info_formspec(size, padding, text)
2121
"formspec_version[6]",
2222
"size[", size.x, ",", size.y, "]",
2323
"padding[0,0]",
24+
"allow_close[false]",
2425
"bgcolor[;true]",
2526

2627
"label[4,4.35;", text, "]",
@@ -83,6 +84,7 @@ local function get_formspec(data)
8384
"formspec_version[7]",
8485
"size[", size.x, ",", size.y, "]",
8586
"padding[0,0]",
87+
"allow_close[false]",
8688
"bgcolor[;true]",
8789

8890
"container[", window_padding.x, ",", window_padding.y, "]",

builtin/mainmenu/dlg_clients_list.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ local function clients_list_formspec(dialogdata)
2020
"formspec_version[8]",
2121
"size[6,9.5]",
2222
TOUCH_GUI and "padding[0.01,0.01]" or "",
23+
"allow_close[false]",
2324
"hypertext[0,0;6,1.5;;<global margin=5 halign=center valign=middle>",
2425
fgettext("Players connected to\n$1",
2526
"<b>" .. core.hypertext_escape(servername) .. "</b>") .. "]",

builtin/mainmenu/dlg_config_world.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ local function get_formspec(data)
125125

126126
local retval =
127127
"size[11.5,7.5,true]" ..
128+
"allow_close[false]" ..
128129
"label[0.5,0;" .. fgettext("World:") .. "]" ..
129130
"label[1.75,0;" .. core.formspec_escape(data.worldspec.name) .. "]"
130131

builtin/mainmenu/dlg_create_world.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ local function create_world_formspec(dialogdata)
282282

283283
local retval =
284284
"size[12.25,7.4,true]" ..
285+
"allow_close[false]" ..
285286

286287
-- Left side
287288
"container[0,0]"..

builtin/mainmenu/dlg_register.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ local function register_formspec(dialogdata)
2727
local retval = {
2828
"formspec_version[4]",
2929
"size[8,", tostring(buttons_y + 1.175), "]",
30+
"allow_close[false]",
3031
"set_focus[", (dialogdata.name ~= "" and "password" or "name"), "]",
3132
"label[0.375,0.8;", title, "]",
3233
"field[0.375,1.575;7.25,0.8;name;", core.formspec_escape(fgettext("Name")), ";",

builtin/mainmenu/dlg_reinstall_mtg.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ local function get_formspec(dialogdata)
7676
return table.concat({
7777
"formspec_version[6]",
7878
"size[12.8,7]",
79+
"allow_close[false]",
7980
"hypertext[0.375,0.375;12.05,5.2;text;", core.formspec_escape(markup), "]",
8081
"container[0.375,5.825]",
8182
"style[dismiss;bgcolor=red]",

builtin/mainmenu/dlg_rename_modpack.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
local function rename_modpack_formspec(dialogdata)
2121
local retval =
2222
"size[11.5,4.5,true]" ..
23+
"allow_close[false]" ..
2324
"button[3.25,3.5;2.5,0.5;dlg_rename_modpack_confirm;"..
2425
fgettext("Accept") .. "]" ..
2526
"button[5.75,3.5;2.5,0.5;dlg_rename_modpack_cancel;"..

builtin/mainmenu/dlg_server_list_mods.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ local function get_formspec(dialogdata)
6161
"formspec_version[8]",
6262
"size[8,9.5]",
6363
TOUCH_GUI and "padding[0.01,0.01]" or "",
64+
"allow_close[false]",
6465
"hypertext[0,0;8,1.5;;<global margin=5 halign=center valign=middle>", heading, "]",
6566
"tablecolumns[", group_by_prefix and
6667
(expand_all and "indent;text" or "tree;text") or "text", "]",

doc/lua_api.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2866,6 +2866,13 @@ Elements
28662866
* For information on converting forms to the new coordinate system, see `Migrating
28672867
to Real Coordinates`.
28682868

2869+
### `allow_close[<bool>]`
2870+
2871+
* When set to false, the formspec will not close when the user tries to close
2872+
the formspec with the escape key or similar. Default true.
2873+
* The formspec can still be closed as normal with `button_exit[]` and
2874+
`core.close_formspec()` regardless of this setting.
2875+
28692876
### `container[<X>,<Y>]`
28702877

28712878
* Start of a container block, moves all physical elements in the container by
@@ -6166,8 +6173,10 @@ Call these functions only at load time!
61666173
* `table`: See `core.explode_table_event`
61676174
* `scrollbar`: See `core.explode_scrollbar_event`
61686175
* Special case: `["quit"]="true"` is sent when the user actively
6169-
closed the form by mouse click, keypress or through a button_exit[]
6176+
closed the form by mouse click, keypress or through a `button_exit[]`
61706177
element.
6178+
* Special case: `["try_quit"]="true"` is sent when the user tries to
6179+
close the formspec, but the the formspec used `allow_close[false]`.
61716180
* Special case: `["key_enter"]="true"` is sent when the user pressed
61726181
the Enter key and the focus was either nowhere (causing the formspec
61736182
to be closed) or on a button. If the focus was on a text field,

games/devtest/mods/testformspec/formspec.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,11 @@ local pages = {
339339
[[
340340
formspec_version[3]
341341
size[12,13]
342+
allow_close[false]
342343
image_button[0,0;1,1;logo.png;rc_image_button_1x1;1x1]
343-
image_button[1,0;2,2;logo.png;rc_image_button_2x2;2x2]
344+
image_button_exit[1,0;2,2;logo.png;rc_image_button_2x2;2x2 exit]
344345
button[0,2;1,1;rc_button_1x1;1x1]
345-
button[1,2;2,2;rc_button_2x2;2x2]
346+
button_exit[1,2;2,2;rc_button_2x2;2x2 exit]
346347
item_image[0,4;1,1;air]
347348
item_image[1,4;2,2;air]
348349
item_image_button[0,6;1,1;testformspec:node;rc_item_image_button_1x1;1x1]
@@ -575,6 +576,10 @@ core.register_on_player_receive_fields(function(player, formname, fields)
575576
if fields.submit_window then
576577
show_test_formspec(player:get_player_name())
577578
end
579+
580+
if fields.try_quit then
581+
core.chat_send_player(player:get_player_name(), "Quit attempt received")
582+
end
578583
end)
579584

580585
core.register_chatcommand("test_formspec", {

src/gui/guiEngine.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ GUIEngine::GUIEngine(JoystickController *joystick,
168168
"",
169169
false);
170170

171-
m_menu->allowClose(false);
172171
m_menu->lockSize(true,v2u32(800,600));
173172

174173
// Initialize scripting

src/gui/guiFormSpecMenu.cpp

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
105105
current_keys_pending.key_down = false;
106106
current_keys_pending.key_up = false;
107107
current_keys_pending.key_enter = false;
108-
current_keys_pending.key_escape = false;
109108

110109
m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
111110
m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
@@ -2833,6 +2832,11 @@ void GUIFormSpecMenu::parseModel(parserData *data, const std::string &element)
28332832
m_fields.push_back(spec);
28342833
}
28352834

2835+
void GUIFormSpecMenu::parseAllowClose(parserData *data, const std::string &element)
2836+
{
2837+
m_allowclose = is_yes(element);
2838+
}
2839+
28362840
void GUIFormSpecMenu::removeAll()
28372841
{
28382842
// Remove children
@@ -2901,6 +2905,7 @@ const std::unordered_map<std::string, std::function<void(GUIFormSpecMenu*, GUIFo
29012905
{"scroll_container_end", &GUIFormSpecMenu::parseScrollContainerEnd},
29022906
{"set_focus", &GUIFormSpecMenu::parseSetFocus},
29032907
{"model", &GUIFormSpecMenu::parseModel},
2908+
{"allow_close", &GUIFormSpecMenu::parseAllowClose},
29042909
};
29052910

29062911

@@ -3003,6 +3008,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
30033008
field_close_on_enter.clear();
30043009
m_dropdown_index_event.clear();
30053010

3011+
m_allowclose = true;
30063012
m_bgnonfullscreen = true;
30073013
m_bgfullscreen = false;
30083014

@@ -3788,12 +3794,12 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode)
37883794

37893795
if (quitmode == quit_mode_accept) {
37903796
fields["quit"] = "true";
3791-
}
3792-
3793-
if (quitmode == quit_mode_cancel) {
3797+
} else if (quitmode == quit_mode_cancel) {
37943798
fields["quit"] = "true";
37953799
m_text_dst->gotText(fields);
37963800
return;
3801+
} else if (quitmode == quit_mode_try) {
3802+
fields["try_quit"] = "true";
37973803
}
37983804

37993805
if (current_keys_pending.key_down) {
@@ -3816,11 +3822,6 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode)
38163822
current_field_enter_pending.clear();
38173823
}
38183824

3819-
if (current_keys_pending.key_escape) {
3820-
fields["key_escape"] = "true";
3821-
current_keys_pending.key_escape = false;
3822-
}
3823-
38243825
for (const GUIFormSpecMenu::FieldSpec &s : m_fields) {
38253826
if (s.send) {
38263827
std::string name = s.fname;
@@ -3997,6 +3998,8 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
39973998
if (m_allowclose) {
39983999
acceptInput(quit_mode_accept);
39994000
quitMenu();
4001+
} else {
4002+
acceptInput(quit_mode_try);
40004003
}
40014004
}
40024005
}
@@ -4013,6 +4016,7 @@ void GUIFormSpecMenu::tryClose()
40134016
acceptInput(quit_mode_cancel);
40144017
quitMenu();
40154018
} else {
4019+
acceptInput(quit_mode_try);
40164020
m_text_dst->gotText(L"MenuQuit");
40174021
}
40184022
}
@@ -4059,9 +4063,13 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
40594063
FATAL_ERROR("Reached a source line that can't ever been reached");
40604064
break;
40614065
}
4062-
if (current_keys_pending.key_enter && m_allowclose) {
4063-
acceptInput(quit_mode_accept);
4064-
quitMenu();
4066+
if (current_keys_pending.key_enter) {
4067+
if (m_allowclose) {
4068+
acceptInput(quit_mode_accept);
4069+
quitMenu();
4070+
} else {
4071+
acceptInput(quit_mode_try);
4072+
}
40654073
} else {
40664074
acceptInput();
40674075
}
@@ -4806,14 +4814,10 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
48064814
s32 caller_id = event.GUIEvent.Caller->getID();
48074815

48084816
if (caller_id == 257) {
4809-
if (m_allowclose) {
4810-
acceptInput(quit_mode_accept);
4811-
quitMenu();
4812-
} else {
4813-
acceptInput();
4814-
m_text_dst->gotText(L"ExitButton");
4815-
}
4816-
// quitMenu deallocates menu
4817+
acceptInput(quit_mode_accept);
4818+
quitMenu();
4819+
4820+
m_text_dst->gotText(L"ExitButton");
48174821
return true;
48184822
}
48194823

@@ -4842,12 +4846,10 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
48424846
}
48434847

48444848
if (s.is_exit) {
4845-
if (m_allowclose) {
4846-
acceptInput(quit_mode_accept);
4847-
quitMenu();
4848-
} else {
4849-
m_text_dst->gotText(L"ExitButton");
4850-
}
4849+
acceptInput(quit_mode_accept);
4850+
quitMenu();
4851+
4852+
m_text_dst->gotText(L"ExitButton");
48514853
return true;
48524854
}
48534855

@@ -4910,15 +4912,18 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
49104912
}
49114913
}
49124914

4913-
if (m_allowclose && close_on_enter) {
4914-
current_keys_pending.key_enter = true;
4915-
acceptInput(quit_mode_accept);
4916-
quitMenu();
4915+
current_keys_pending.key_enter = true;
4916+
4917+
if (close_on_enter) {
4918+
if (m_allowclose) {
4919+
acceptInput(quit_mode_accept);
4920+
quitMenu();
4921+
} else {
4922+
acceptInput(quit_mode_try);
4923+
}
49174924
} else {
4918-
current_keys_pending.key_enter = true;
49194925
acceptInput();
49204926
}
4921-
// quitMenu deallocates menu
49224927
return true;
49234928
}
49244929
}

0 commit comments

Comments
 (0)