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
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
yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, recursion_depth)
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
-- build the list of available dialogs for the dropdown list(s)
dialog_list = dialog_list..","..minetest.formspec_escape(d)
if(d == c_d_id and sorted_list[ i-1 ]) then
local prev_dialog = minetest.formspec_escape(sorted_list[i-1])
table.insert(formspec, "button[8.5,4.0;2,0.9;prev_dialog_"..prev_dialog..";<]")
table.insert(formspec, "tooltip[prev_dialog_"..prev_dialog..
";Go to previous dialog "..prev_dialog..".]")
end
if(d == c_d_id and sorted_list[ i+1 ]) then
local next_dialog = minetest.formspec_escape(sorted_list[i+1])
table.insert(formspec, "button[11,4.0;2,0.9;next_dialog_"..next_dialog..";>]")
table.insert(formspec, "tooltip[next_dialog_"..next_dialog..
";Go to next dialog "..next_dialog..".]")
if(d == c_d_id) then
local prev_dialog = tostring(minetest.formspec_escape(sorted_list[i-1]))
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "8.5,4.0;2,0.9", "prev_dialog_"..prev_dialog,
"<",
"Go to previous dialog "..prev_dialog..".",
(sorted_list[ i-1 ]))
local next_dialog = tostring(minetest.formspec_escape(sorted_list[i+1]))
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "11,4.0;2,0.9", "next_dialog_"..next_dialog,
">",
"Go to next dialog "..next_dialog..".",
(sorted_list[ i+1 ]))
end
d_id_to_dropdown_index[d] = i + 1
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, "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
table.insert(formspec, "button[13.9,4.0;1,0.9;show_new_dialog;+]")
table.insert(formspec, "tooltip[show_new_dialog;Create a new dialog.]")
-- allow to edit name and description of the NPC
table.insert(formspec, "button[13.4,0.3;2,0.9;button_edit_name_and_description;Edit]")
table.insert(formspec, "tooltip[button_edit_name_and_description;Edit name and description of your NPC.]")
table.insert(formspec, "button[15.7,0.3;2,0.9;button_save_dialog;Save]")
table.insert(formspec, "tooltip[button_save_dialog;Save this dialog.]")
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "13.9,4.0;1,0.9", "show_new_dialog",
"+",
"Create a new dialog.",
true)
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "13.4,0.3;2,0.9", "button_edit_name_and_description",
"Edit",
"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, 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.
local t = minetest.formspec_escape(yl_speak_up.replace_vars_in_text(
(alternate_text or active_dialog.d_text), dialog, pname))
table.insert(formspec, "hypertext[0.2,5;19.6,17.8;d_text;<normal>")
table.insert(formspec, t .. "\n</normal>")
table.insert(formspec, "]")
table.insert(formspec, "tooltip[d_text;")
table.insert(formspec, t:trim())
table.insert(formspec, ";#000000;#FFFFFF]")
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"hypertext", "0.2,5;19.6,17.8", "d_text",
"<normal>"..t.."\n</normal>",
t:trim()..";#000000;#FFFFFF",
true)
end
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
if edit_mode then
h = h + 1
-- 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..":]")
-- add a tooltip "Edit target dialog, pre(C)onditions and (Ef)fects for option o_<nr>"
table.insert(formspec, "tooltip[edit_option_" .. oid .. ";Edit target dialog, pre(C)onditions and (Ef)fects for option "..oid..".]")
-- add a button "o_<nr>:" that leads to an edit formspec for this option
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "2.3," .. h .. ";2,0.9", "edit_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):
local target_dialog = nil
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
if(target_dialog) then
-- 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!
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
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"
table.insert(formspec, "tooltip[4.4,"..h..";4.7,1;Change target dialog for option "..oid..".;#FFFFFF;#000000]")
-- are there any prerequirements?
local prereq = active_dialog.d_options[sb_v.o_id].o_prerequisites
if(prereq and next(prereq)) then
table.insert(formspec, "button[0.5," .. h .. ";0.5,0.9;conditions_"..oid..";C]")
-- label: "There are pre(C)onditions required for showing this option. Display them."
table.insert(formspec, "tooltip[conditions_" .. oid .. ";There are pre(C)onditions required for showing this option. Display them.]")
end
if(has_other_results) then
table.insert(formspec, "button[1.6," .. h .. ";0.6,0.9;effects_"..oid..";Ef]")
-- label: "There are further (Ef)fects (apart from switching to a new dialog)
-- set for this option. Display them."
table.insert(formspec, "tooltip[effects_" .. oid .. ";"..
"There are further (Ef)fects (apart from switching\n"..
"to a new dialog) set for this option. Display them.]")
end
local actions = active_dialog.d_options[sb_v.o_id].actions
-- are there any actions defined?
if(actions and next(actions)) then
table.insert(formspec, "button[1.1," .. h .. ";0.5,0.9;effects_"..oid..";A]")
table.insert(formspec, "tooltip[effects_" .. oid .. ";"..
"There is an (A)ction (i.e. a trade) that will happen\n"..
"when switching to a new dialog. Display effects and\n"..
"trade of this option.]")
end
-- are there any prerequirements?
local prereq = active_dialog.d_options[sb_v.o_id].o_prerequisites
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "0.5," .. h .. ";0.5,0.9", "conditions_"..oid,
"C",
"There are pre(C)onditions required for showing this option. Display them.",
(prereq and next(prereq)))
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "1.6," .. h .. ";0.6,0.9", "effects_"..oid,
"Ef",
"There are further (Ef)fects (apart from switching\n"..
"to a new dialog) set for this option. Display them.",
(has_other_results))
-- are there any actions defined?
local actions = active_dialog.d_options[sb_v.o_id].actions
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", "1.1," .. h .. ";0.5,0.9", "actions_"..oid,
"A",
"There is an (A)ction (i.e. a trade) that will happen\n"..
"when switching to a new dialog. Display actions and\n"..
"trade of this option.",
(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
else
local t = "- no text given -"