Re-work how formspecs are shown to remove the need for delays and to guarantee the formspec is show (#35)

* Initial rewrite

* Working

* Cleanup

* No need to ensure existance as we already check for existance
This commit is contained in:
Oversword 2022-02-02 00:13:45 +00:00 committed by GitHub
parent b4b8f12852
commit 2f09593ebf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 26 deletions

View File

@ -117,7 +117,7 @@ minetest.register_node("travelnet:elevator", {
meta:set_string("formspec", ([[
size[12,10]
field[0.3,5.6;6,0.7;station_name;%s;]
button_exit[6.3,6.2;1.7,0.7;station_set;%s]
button[6.3,6.2;1.7,0.7;station_set;%s]
]]):format(S("Name of this station:"), S("Store")))
minetest.set_node(vector.add(pos, { x=0, y=1, z=0 }), { name="travelnet:hidden_top" })
@ -165,4 +165,3 @@ minetest.register_craft({
output = "travelnet:elevator",
recipe = travelnet.elevator_recipe,
})

View File

@ -10,9 +10,9 @@ function travelnet.show_message(pos, player_name, title, message)
local formspec = ([[
size[8,3]
label[3,0;%s]
textlist[0,0.5;8,1.5;;%s;]
button_exit[3.5,2.5;1.0,0.5;back;%s]
button_exit[6.8,2.5;1.0,0.5;station_exit;%s]
textarea[0.5,0.5;7,1.5;;%s;]
button[3.5,2.5;1.0,0.5;back;%s]
button[6.8,2.5;1.0,0.5;station_exit;%s]
field[20,20;0.1,0.1;pos2str;Pos;%s]
]]):format(
minetest.formspec_escape(title or S("Error")),
@ -21,7 +21,7 @@ function travelnet.show_message(pos, player_name, title, message)
S("Exit"),
minetest.pos_to_string(pos)
)
minetest.show_formspec(player_name, travelnet_form_name, formspec)
travelnet.set_formspec(player_name, formspec)
end
-- show the player the formspec they would see when right-clicking the node;
@ -34,7 +34,7 @@ function travelnet.show_current_formspec(pos, meta, player_name)
local formspec = meta:get_string("formspec") ..
("field[20,20;0.1,0.1;pos2str;Pos;%s]"):format(minetest.pos_to_string(pos))
-- show the formspec manually
minetest.show_formspec(player_name, travelnet_form_name, formspec)
travelnet.set_formspec(player_name, formspec)
end
-- a player clicked on something in the formspec hse was manually shown
@ -82,15 +82,15 @@ function travelnet.reset_formspec(meta)
([[
size[10,6.0]
label[2.0,0.0;--> %s <--]
button_exit[8.0,0.0;2.2,0.7;station_dig;%s]
button[8.0,0.0;2.2,0.7;station_dig;%s]
field[0.3,1.2;9,0.9;station_name;%s:;]
label[0.3,1.5;%s]
field[0.3,2.8;9,0.9;station_network;%s;%s]
label[0.3,3.1;%s]
field[0.3,4.4;9,0.9;owner;%s;]
label[0.3,4.7;%s]
button_exit[3.8,5.3;1.7,0.7;station_set;%s]
button_exit[6.3,5.3;1.7,0.7;station_exit;%s]
button[3.8,5.3;1.7,0.7;station_set;%s]
button[6.3,5.3;1.7,0.7;station_exit;%s]
]]):format(
S("Configure this travelnet station"),
S("Remove station"),
@ -128,15 +128,15 @@ function travelnet.edit_formspec(pos, meta, player_name)
local formspec = ([[
size[10,6.0]
label[2.0,0.0;--> %s <--]
button_exit[8.0,0.0;2.2,0.7;station_dig;%s]
button[8.0,0.0;2.2,0.7;station_dig;%s]
field[0.3,1.2;9,0.9;station_name;%s:;%s]
label[0.3,1.5;%s]
field[0.3,2.8;9,0.9;station_network;%s;%s]
label[0.3,3.1;%s]
field[0.3,4.4;9,0.9;owner;%s;%s]
label[0.3,4.7;%s]
button_exit[3.8,5.3;1.7,0.7;station_set;%s]
button_exit[6.3,5.3;1.7,0.7;station_exit;%s]
button[3.8,5.3;1.7,0.7;station_set;%s]
button[6.3,5.3;1.7,0.7;station_exit;%s]
field[20,20;0.1,0.1;pos2str;Pos;%s]
]]):format(
S("Configure this travelnet station"),
@ -156,7 +156,7 @@ function travelnet.edit_formspec(pos, meta, player_name)
)
-- show the formspec manually
minetest.show_formspec(player_name, travelnet_form_name, formspec)
travelnet.set_formspec(player_name, formspec)
end
@ -171,10 +171,10 @@ function travelnet.edit_formspec_elevator(pos, meta, player_name)
local formspec = ([[
size[10,6.0]
label[2.0,0.0;--> %s <--]
button_exit[8.0,0.0;2.2,0.7;station_dig;%s]
button[8.0,0.0;2.2,0.7;station_dig;%s]
field[0.3,1.2;9,0.9;station_name;%s:;%s]
button_exit[3.8,5.3;1.7,0.7;station_set;%s]
button_exit[6.3,5.3;1.7,0.7;station_exit;%s]
button[3.8,5.3;1.7,0.7;station_set;%s]
button[6.3,5.3;1.7,0.7;station_exit;%s]
field[20,20;0.1,0.1;pos2str;Pos;%s]
]]):format(
S("Configure this elevator station"),
@ -187,7 +187,26 @@ function travelnet.edit_formspec_elevator(pos, meta, player_name)
)
-- show the formspec manually
minetest.show_formspec(player_name, travelnet_form_name, formspec)
travelnet.set_formspec(player_name, formspec)
end
local player_formspec_data = travelnet.player_formspec_data
function travelnet.set_formspec(player_name, formspec)
if player_formspec_data[player_name] and player_formspec_data[player_name].wait_mode then
player_formspec_data[player_name].formspec = formspec
else
minetest.show_formspec(player_name, travelnet_form_name, formspec)
end
end
function travelnet.show_formspec(player_name)
local formspec = player_formspec_data[player_name] and player_formspec_data[player_name].formspec
if formspec then
minetest.show_formspec(player_name, travelnet_form_name, formspec)
else
minetest.show_formspec(player_name, "", "")
end
player_formspec_data[player_name].formspec = nil
end
function travelnet.page_formspec(pos, player_name, page)

View File

@ -29,6 +29,7 @@ end
travelnet = {}
travelnet.player_formspec_data = {}
travelnet.targets = {}
travelnet.path = minetest.get_modpath(minetest.get_current_modname())
@ -133,3 +134,4 @@ end
-- upon server start, read the savefile
travelnet.restore_data()
travelnet.player_formspec_data = nil

View File

@ -1,7 +1,6 @@
local S = minetest.get_translator("travelnet")
function travelnet.on_receive_fields(pos, _, fields, player)
local function on_receive_fields_internal(pos, _, fields, player)
if not pos then
return
end
@ -118,7 +117,7 @@ function travelnet.on_receive_fields(pos, _, fields, player)
minetest.remove_node(pos)
else
-- edit station
minetest.after(0.2, travelnet.edit_formspec, pos, meta, name)
travelnet.edit_formspec(pos, meta, name)
end
return
end
@ -245,3 +244,12 @@ function travelnet.on_receive_fields(pos, _, fields, player)
end
end
local player_formspec_data = travelnet.player_formspec_data
function travelnet.on_receive_fields(pos, _, fields, player)
local name = player:get_player_name()
player_formspec_data[name] = {wait_mode=true}
on_receive_fields_internal(pos, _, fields, player)
travelnet.show_formspec(name)
player_formspec_data[name] = nil
end

View File

@ -221,15 +221,15 @@ function travelnet.primary_formspec(pos, puncher_name, fields, page_number)
-- check if there is an elevator door in front that needs to be opened
if k == station_name then
formspec = formspec ..
("button_exit[%f,%f;1,0.5;open_door;<>]label[%f,%f;%s]")
("button[%f,%f;1,0.5;open_door;<>]label[%f,%f;%s]")
:format(x, y + 2.5, x + 0.9, y + 2.35, k)
elseif is_elevator then
formspec = formspec ..
("button_exit[%f,%f;1,0.5;target;%s]label[%f,%f;%s]")
("button[%f,%f;1,0.5;target;%s]label[%f,%f;%s]")
:format(x, y + 2.5, minetest.formspec_escape(tostring(network[k].nr)), x + 0.9, y + 2.35, k)
else
formspec = formspec ..
("button_exit[%f,%f;4,0.5;target;%s]")
("button[%f,%f;4,0.5;target;%s]")
:format(x, y + 2.5, minetest.formspec_escape(k))
end
@ -238,8 +238,8 @@ function travelnet.primary_formspec(pos, puncher_name, fields, page_number)
formspec = formspec .. ([[
label[8.0,1.6;%s]
button_exit[11.3,0.0;1.0,0.5;station_exit;%s]
button_exit[10.0,0.5;2.2,0.7;station_edit;%s]
button[11.3,0.0;1.0,0.5;station_exit;%s]
button[10.0,0.5;2.2,0.7;station_edit;%s]
button[9.6,1.6;1.4,0.5;move_up;%s]
button[10.9,1.6;1.4,0.5;move_down;%s]
]]):format(