added yl_speak_up.add_formspec_element_with_tooltip_if to clean up formspec building in fs_talkdialog.lua

This commit is contained in:
Sokomine 2022-01-08 18:38:51 +01:00
parent 3bde8d18c4
commit e3eda65c00

View File

@ -299,6 +299,19 @@ yl_speak_up.add_edit_button_fs_talkdialog = function(formspec, h, button_name, t
return h return h
end end
-- show a formspec element in the main menu of the NPC (with tooltip);
-- helper function for yl_speak_up.fs_talkdialog(..)
yl_speak_up.add_formspec_element_with_tooltip_if = function(formspec, element_type, position, element_name,
element_text, tooltip, condition)
if(not(condition)) then
return
end
table.insert(formspec, element_type.."["..position..";"..element_name..";"..element_text.."]")
table.insert(formspec, "tooltip["..tostring(element_name)..";"..tostring(tooltip).."]")
end
-- recursion_depth is increased each time autoanswer is automaticly selected -- recursion_depth is increased each time autoanswer is automaticly selected
yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, recursion_depth) yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, recursion_depth)
local pname = player:get_player_name() local pname = player:get_player_name()
@ -506,17 +519,19 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec
for i, d in ipairs(sorted_list) do for i, d in ipairs(sorted_list) do
-- build the list of available dialogs for the dropdown list(s) -- build the list of available dialogs for the dropdown list(s)
dialog_list = dialog_list..","..minetest.formspec_escape(d) dialog_list = dialog_list..","..minetest.formspec_escape(d)
if(d == c_d_id and sorted_list[ i-1 ]) then if(d == c_d_id) then
local prev_dialog = minetest.formspec_escape(sorted_list[i-1]) local prev_dialog = tostring(minetest.formspec_escape(sorted_list[i-1]))
table.insert(formspec, "button[8.5,4.0;2,0.9;prev_dialog_"..prev_dialog..";<]") yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
table.insert(formspec, "tooltip[prev_dialog_"..prev_dialog.. "button", "8.5,4.0;2,0.9", "prev_dialog_"..prev_dialog,
";Go to previous dialog "..prev_dialog..".]") "<",
end "Go to previous dialog "..prev_dialog..".",
if(d == c_d_id and sorted_list[ i+1 ]) then (sorted_list[ i-1 ]))
local next_dialog = minetest.formspec_escape(sorted_list[i+1]) local next_dialog = tostring(minetest.formspec_escape(sorted_list[i+1]))
table.insert(formspec, "button[11,4.0;2,0.9;next_dialog_"..next_dialog..";>]") yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
table.insert(formspec, "tooltip[next_dialog_"..next_dialog.. "button", "11,4.0;2,0.9", "next_dialog_"..next_dialog,
";Go to next dialog "..next_dialog..".]") ">",
"Go to next dialog "..next_dialog..".",
(sorted_list[ i+1 ]))
end end
d_id_to_dropdown_index[d] = i + 1 d_id_to_dropdown_index[d] = i + 1
end end
@ -525,15 +540,21 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec
table.insert(formspec, "dropdown[3.0,4.0;5,1;d_id;"..dialog_list..";"..d_id_to_dropdown_index[c_d_id]..",]") table.insert(formspec, "dropdown[3.0,4.0;5,1;d_id;"..dialog_list..";"..d_id_to_dropdown_index[c_d_id]..",]")
table.insert(formspec, "tooltip[3.0,4.0;5,1;Select the dialog you want to edit. Currently, dialog "..c_d_id.." is beeing displayed.;#FFFFFF;#000000]") table.insert(formspec, "tooltip[3.0,4.0;5,1;Select the dialog you want to edit. Currently, dialog "..c_d_id.." is beeing displayed.;#FFFFFF;#000000]")
-- add a "+" button for creating a new dialog yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
table.insert(formspec, "button[13.9,4.0;1,0.9;show_new_dialog;+]") "button", "13.9,4.0;1,0.9", "show_new_dialog",
table.insert(formspec, "tooltip[show_new_dialog;Create a new dialog.]") "+",
"Create a new dialog.",
-- allow to edit name and description of the NPC true)
table.insert(formspec, "button[13.4,0.3;2,0.9;button_edit_name_and_description;Edit]") yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
table.insert(formspec, "tooltip[button_edit_name_and_description;Edit name and description of your NPC.]") "button", "13.4,0.3;2,0.9", "button_edit_name_and_description",
table.insert(formspec, "button[15.7,0.3;2,0.9;button_save_dialog;Save]") "Edit",
table.insert(formspec, "tooltip[button_save_dialog;Save this dialog.]") "Edit name and description of your NPC.",
true)
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "15.7,0.3;2,0.9", "button_save_dialog",
"Save",
"Save this dialog.",
true)
table.insert(formspec, "textarea[0.2,5;19.6,17.8;d_text;;") table.insert(formspec, "textarea[0.2,5;19.6,17.8;d_text;;")
table.insert(formspec, minetest.formspec_escape(active_dialog.d_text)) table.insert(formspec, minetest.formspec_escape(active_dialog.d_text))
@ -545,12 +566,11 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec
-- replace $NPC_NAME$ etc. -- replace $NPC_NAME$ etc.
local t = minetest.formspec_escape(yl_speak_up.replace_vars_in_text( local t = minetest.formspec_escape(yl_speak_up.replace_vars_in_text(
(alternate_text or active_dialog.d_text), dialog, pname)) (alternate_text or active_dialog.d_text), dialog, pname))
table.insert(formspec, "hypertext[0.2,5;19.6,17.8;d_text;<normal>") yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
table.insert(formspec, t .. "\n</normal>") "hypertext", "0.2,5;19.6,17.8", "d_text",
table.insert(formspec, "]") "<normal>"..t.."\n</normal>",
table.insert(formspec, "tooltip[d_text;") t:trim()..";#000000;#FFFFFF",
table.insert(formspec, t:trim()) true)
table.insert(formspec, ";#000000;#FFFFFF]")
end end
table.insert(formspec, "scrollbaroptions[min=0;max=14;smallstep=1;largestep=2;arrows=show]") table.insert(formspec, "scrollbaroptions[min=0;max=14;smallstep=1;largestep=2;arrows=show]")
@ -573,11 +593,12 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec
-- in edit_mode: show all options -- in edit_mode: show all options
if edit_mode then if edit_mode then
h = h + 1 h = h + 1
-- add a button "o_<nr>:" that leads to an edit formspec for this option -- add a button "o_<nr>:" that leads to an edit formspec for this option
table.insert(formspec, "button[2.3," .. h .. ";2,0.9;edit_option_" .. oid .. ";"..oid..":]") yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
-- add a tooltip "Edit target dialog, pre(C)onditions and (Ef)fects for option o_<nr>" "button", "2.3," .. h .. ";2,0.9", "edit_option_" .. oid,
table.insert(formspec, "tooltip[edit_option_" .. oid .. ";Edit target dialog, pre(C)onditions and (Ef)fects for option "..oid..".]") oid,
"Edit target dialog, pre(C)onditions and (Ef)fects for option "..oid..".",
true)
-- find the right target dialog for this option (if it exists): -- find the right target dialog for this option (if it exists):
local target_dialog = nil local target_dialog = nil
local results = active_dialog.d_options[sb_v.o_id].o_results local results = active_dialog.d_options[sb_v.o_id].o_results
@ -593,50 +614,51 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec
end end
end end
end end
if(target_dialog) then
-- add a button "-> d_<nr>" that leads to the target dialog (if one is set) -- add a button "-> d_<nr>" that leads to the target dialog (if one is set)
table.insert(formspec, "button[9.0," .. h .. ";1,0.9;button_" .. oid .. ";->]")
-- add a tooltip "Go to target dialog d_<nr>"
table.insert(formspec, "tooltip[button_" .. oid .. ";Go to target dialog "..minetest.formspec_escape(target_dialog).." that will be shown when this option ("..oid..") is selected.]")
-- selecting an option this way MUST NOT execute the pre(C)onditions or (Ef)fects! -- selecting an option this way MUST NOT execute the pre(C)onditions or (Ef)fects!
end yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "9.0," .. h .. ";1,0.9", "button_" .. oid,
"->",
"Go to target dialog "..minetest.formspec_escape(target_dialog)..
" that will be shown when this option ("..oid..") is selected.",
(target_dialog))
-- allow to set a new target dialog -- allow to set a new target dialog
table.insert(formspec, "dropdown[4.4,"..h..";4.7,1;d_id_"..oid..";"..dialog_list..";"..minetest.formspec_escape(d_id_to_dropdown_index[target_dialog] or "0")..",]") table.insert(formspec, "dropdown[4.4,"..h..";4.7,1;d_id_"..oid..";"..dialog_list..";"..minetest.formspec_escape(d_id_to_dropdown_index[target_dialog] or "0")..",]")
-- add a tooltip "Change target dialog" -- add a tooltip "Change target dialog"
table.insert(formspec, "tooltip[4.4,"..h..";4.7,1;Change target dialog for option "..oid..".;#FFFFFF;#000000]") table.insert(formspec, "tooltip[4.4,"..h..";4.7,1;Change target dialog for option "..oid..".;#FFFFFF;#000000]")
-- are there any prerequirements? -- are there any prerequirements?
local prereq = active_dialog.d_options[sb_v.o_id].o_prerequisites local prereq = active_dialog.d_options[sb_v.o_id].o_prerequisites
if(prereq and next(prereq)) then yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
table.insert(formspec, "button[0.5," .. h .. ";0.5,0.9;conditions_"..oid..";C]") "button", "0.5," .. h .. ";0.5,0.9", "conditions_"..oid,
-- label: "There are pre(C)onditions required for showing this option. Display them." "C",
table.insert(formspec, "tooltip[conditions_" .. oid .. ";There are pre(C)onditions required for showing this option. Display them.]") "There are pre(C)onditions required for showing this option. Display them.",
end (prereq and next(prereq)))
if(has_other_results) then
table.insert(formspec, "button[1.6," .. h .. ";0.6,0.9;effects_"..oid..";Ef]") yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
-- label: "There are further (Ef)fects (apart from switching to a new dialog) "button", "1.6," .. h .. ";0.6,0.9", "effects_"..oid,
-- set for this option. Display them." "Ef",
table.insert(formspec, "tooltip[effects_" .. oid .. ";".. "There are further (Ef)fects (apart from switching\n"..
"There are further (Ef)fects (apart from switching\n".. "to a new dialog) set for this option. Display them.",
"to a new dialog) set for this option. Display them.]") (has_other_results))
end
local actions = active_dialog.d_options[sb_v.o_id].actions -- are there any actions defined?
-- are there any actions defined? local actions = active_dialog.d_options[sb_v.o_id].actions
if(actions and next(actions)) then yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
table.insert(formspec, "button[1.1," .. h .. ";0.5,0.9;effects_"..oid..";A]") "button", "1.1," .. h .. ";0.5,0.9", "actions_"..oid,
table.insert(formspec, "tooltip[effects_" .. oid .. ";".. "A",
"There is an (A)ction (i.e. a trade) that will happen\n".. "There is an (A)ction (i.e. a trade) that will happen\n"..
"when switching to a new dialog. Display effects and\n".. "when switching to a new dialog. Display actions and\n"..
"trade of this option.]") "trade of this option.",
end (actions and next(actions)))
-- show the actual text for the option
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"field", "9.9," .. h .. ";44.4,0.9", "text_option_" .. oid,
";"..minetest.formspec_escape(sb_v.o_text_when_prerequisites_met),
"Edit the text that is displayed on button "..oid..".",
true)
-- show the actual text for the option
table.insert(formspec, "field[9.9," .. h .. ";44.4,0.9;text_option_" .. oid .. ";;"..
minetest.formspec_escape(sb_v.o_text_when_prerequisites_met).."]")
-- add a tooltip "Edit the text that is displayed on button o_<nr>."
table.insert(formspec, "tooltip[text_option_" .. oid .. ";Edit the text that is displayed on button "..oid..".]")
--
-- normal mode: show an option if the prerequirements (if any are defined) are met -- normal mode: show an option if the prerequirements (if any are defined) are met
else else
local t = "- no text given -" local t = "- no text given -"