yl_speak_up/formspec_helpers.lua

44 lines
2.0 KiB
Lua

-- helper function:
-- create a formspec dropdown list with player names (first entry: Add player) and
-- an option to delete players from that list
-- Note: With the what_is_the_list_about-parameter, it is possible to handle i.e. variables as well
yl_speak_up.create_dropdown_playerlist = function(player, pname,
table_of_names, index_selected,
start_x, start_y, stretch_x, dropdown_name, what_is_the_list_about, delete_button_text,
field_name_for_adding_player, explain_add_player,
field_name_for_deleting_player, explain_delete_player)
local text = "dropdown["..tostring(start_x)..","..tostring(start_y)..";"..
tostring(3.8 + stretch_x)..";"..
tostring(dropdown_name)..";Add "..tostring(what_is_the_list_about)..":"
-- table_of_names is a table with the playernames as keys
-- we want to work with indices later on; in order to be able to do that reliably, we
-- need a defined order of names
local tmp_list = yl_speak_up.sort_keys(table_of_names, true)
for i, p in ipairs(tmp_list) do
text = text..","..minetest.formspec_escape(p)
end
-- has an entry been selected?
if(not(index_selected) or index_selected < 0 or index_selected > #tmp_list+1) then
index_selected = 1
end
text = text..";"..tostring(index_selected)..";]"
if(index_selected == 1) then
-- first index "Add player" selected? Then offer a field for entering the name
text = text.."field["..tostring(start_x + 4.0 + stretch_x)..","..tostring(start_y)..
";"..tostring(3.5 + stretch_x)..",0.9;"..
tostring(field_name_for_adding_player)..";;]"..
"tooltip["..tostring(field_name_for_adding_player)..";"..
tostring(explain_add_player).."]"
else
text = text.."button["..tostring(start_x + 3.8 + stretch_x)..","..tostring(start_y)..
";"..tostring(3.4 + stretch_x)..",0.9;"..
tostring(field_name_for_deleting_player)..";"..
tostring(delete_button_text).."]"..
"tooltip["..tostring(field_name_for_deleting_player)..";"..
tostring(explain_delete_player).."]"
end
return text
end