moved yl_speak_up.get_fs_talkdialog_line_in_edit_mode to edit_mode.lua and shortened to get_fs_talkdialog_line

This commit is contained in:
Sokomine 2024-02-02 19:27:16 +01:00
parent 24a6edcd53
commit 3dab37dae7
2 changed files with 161 additions and 150 deletions

View File

@ -190,3 +190,154 @@ yl_speak_up.get_fs_talkdialog_main_text = function(pname, formspec, h, dialog, d
return {h = h, formspec = formspec, d_id_to_dropdown_index = d_id_to_dropdown_index,
dialog_list = dialog_list}
end
-- helper function for yl_speak_up.get_fs_talkdialog:
-- prints one entry (option/answer) in normal and *edit_mode*
local old_talkdialog_line = yl_speak_up.get_fs_talkdialog_line
yl_speak_up.get_fs_talkdialog_line = function(
formspec, h, pname_for_old_fs, oid, sb_v,
dialog, allowed, pname,
-- these additional parameters are needed *here*, in edit_mode:
active_dialog, dialog_list, d_id_to_dropdown_index,
current_index, anz_options)
if(not(yl_speak_up.in_edit_mode(pname))) then
-- in normal mode:
return old_talkdialog_line(formspec, h, pname_for_old_fs, oid, sb_v,
dialog, allowed, pname,
active_dialog, dialog_list, d_id_to_dropdown_index,
current_index, anz_options)
end
-- in edit mode:
local offset = 0.0
local field_length = 43.8
if(pname_for_old_fs) then
offset = 0.7
field_length = 41.8
end
h = h + 1
-- 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", tostring(2.9+offset).."," .. 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
-- has this option more results/effects than just switching to another dialog?
local has_other_results = false
if(results ~= nil) then
for k, v in pairs(results) do
if v.r_type == "dialog"
and (dialog.n_dialogs[v.r_value] ~= nil
or v.r_value == "d_end"
or v.r_value == "d_trade"
or v.r_value == "d_got_item") then
-- there may be more than one in the data structure
target_dialog = v.r_value
elseif v.r_type ~= "dialog" then
has_other_results = true
end
end
end
-- add a button "-> d_<nr>" that leads to the target dialog (if one is set)
-- selecting an option this way MUST NOT execute the pre(C)onditions or (Ef)fects!
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", tostring(9.6+offset)..","..h..";1,0.9", "button_" .. oid,
"->",
"Go to target dialog "..minetest.formspec_escape(target_dialog or "")..
" that will be shown when this option ("..oid..") is selected.",
(target_dialog))
-- allow to set a new target dialog
table.insert(formspec, "dropdown["..tostring(5.0+offset)..","..h..";4.7,1;d_id_"..
oid..";"..
dialog_list..";"..
(d_id_to_dropdown_index[(target_dialog or "?")] or "0")..",]")
-- add a tooltip "Change target dialog"
table.insert(formspec, "tooltip[5.0,"..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
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", tostring(0.5+offset)..","..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", tostring(1.6+offset)..","..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))
local d_option = active_dialog.d_options[sb_v.o_id]
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", tostring(2.25+offset)..","..h..";0.6,0.9", "quests_"..oid,
"Q",
"This option sets a (Q)est step if possible.\n"..
"A special precondition is evaluated automaticly\n"..
"to check if the quest step can be set.",
(d_option and d_option.quest_id and d_option.quest_step))
-- 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", tostring(1.05+offset)..","..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)))
if(sb_v.o_autoanswer) then
table.insert(formspec,
"label["..tostring(10.5+offset+0.2)..","..tostring(h+0.5)..";"..
minetest.formspec_escape("[Automaticly selected if preconditions are met] "..
tostring(sb_v.o_text_when_prerequisites_met))..
"]")
elseif(active_dialog.o_random) then
table.insert(formspec,
"label["..tostring(10.5+offset+0.2)..","..tostring(h+0.5)..";"..
minetest.formspec_escape("[One of these options is randomly selected] "..
tostring(sb_v.o_text_when_prerequisites_met))..
"]")
else
-- show the actual text for the option
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"field", tostring(10.5+offset)..","..h..";"..
tostring(field_length-2.3)..",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)
end
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", tostring(10.5+offset+field_length-2.2)..","..h..";1.0,0.9", "delete_option_"..oid,
"Del",
"Delete this option/answer.",
true)
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
-- "image_button", tostring(9.9+offset+field_length-0.5)..","..h..";0.5,0.9"..
-- ";gui_furnace_arrow_bg.png^[transformR180",
"button", tostring(10.5+offset+field_length-1.1)..","..h..";0.5,0.9",
"option_move_down_"..oid,
"v",
"Move this option/answer one down.",
(current_index < anz_options))
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
-- "image_button", tostring(9.9+offset+field_length-1.0)..","..h..";0.5,0.9"..
-- ";gui_furnace_arrow_bg.png",
"button", tostring(10.5+offset+field_length-0.5)..","..h..";0.5,0.9",
"option_move_up_"..oid,
"^",
"Move this option/answer one up.",
(current_index > 1))
return {h = h, formspec = formspec}
end

View File

@ -329,9 +329,13 @@ end
-- helper function for yl_speak_up.get_fs_talkdialog:
-- prints one entry (option/answer) in normal mode - not in edit_mode
yl_speak_up.get_fs_talkdialog_line_in_normal_mode = function(
yl_speak_up.get_fs_talkdialog_line = function(
formspec, h, pname_for_old_fs, oid, sb_v,
dialog, allowed, pname)
dialog, allowed, pname,
-- these additional parameters are needed in edit_mode and not used here
active_dialog, dialog_list, d_id_to_dropdown_index,
current_index, anz_options)
local t = "- no text given -"
local t_alt = nil
-- the preconditions are fulfilled; showe the option
@ -378,144 +382,6 @@ yl_speak_up.get_fs_talkdialog_line_in_normal_mode = function(
end
-- helper function for yl_speak_up.get_fs_talkdialog:
-- prints one entry (option/answer) in *edit_mode*
yl_speak_up.get_fs_talkdialog_line_in_edit_mode = function(
formspec, h, pname_for_old_fs, oid, sb_v,
dialog, active_dialog, dialog_list, d_id_to_dropdown_index,
current_index, anz_options)
local offset = 0.0
local field_length = 43.8
if(pname_for_old_fs) then
offset = 0.7
field_length = 41.8
end
h = h + 1
-- 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", tostring(2.9+offset).."," .. 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
-- has this option more results/effects than just switching to another dialog?
local has_other_results = false
if(results ~= nil) then
for k, v in pairs(results) do
if v.r_type == "dialog"
and (dialog.n_dialogs[v.r_value] ~= nil
or v.r_value == "d_end"
or v.r_value == "d_trade"
or v.r_value == "d_got_item") then
-- there may be more than one in the data structure
target_dialog = v.r_value
elseif v.r_type ~= "dialog" then
has_other_results = true
end
end
end
-- add a button "-> d_<nr>" that leads to the target dialog (if one is set)
-- selecting an option this way MUST NOT execute the pre(C)onditions or (Ef)fects!
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", tostring(9.6+offset)..","..h..";1,0.9", "button_" .. oid,
"->",
"Go to target dialog "..minetest.formspec_escape(target_dialog or "")..
" that will be shown when this option ("..oid..") is selected.",
(target_dialog))
-- allow to set a new target dialog
table.insert(formspec, "dropdown["..tostring(5.0+offset)..","..h..";4.7,1;d_id_"..
oid..";"..
dialog_list..";"..
(d_id_to_dropdown_index[(target_dialog or "?")] or "0")..",]")
-- add a tooltip "Change target dialog"
table.insert(formspec, "tooltip[5.0,"..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
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", tostring(0.5+offset)..","..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", tostring(1.6+offset)..","..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))
local d_option = active_dialog.d_options[sb_v.o_id]
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", tostring(2.25+offset)..","..h..";0.6,0.9", "quests_"..oid,
"Q",
"This option sets a (Q)est step if possible.\n"..
"A special precondition is evaluated automaticly\n"..
"to check if the quest step can be set.",
(d_option and d_option.quest_id and d_option.quest_step))
-- 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", tostring(1.05+offset)..","..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)))
if(sb_v.o_autoanswer) then
table.insert(formspec,
"label["..tostring(10.5+offset+0.2)..","..tostring(h+0.5)..";"..
minetest.formspec_escape("[Automaticly selected if preconditions are met] "..
tostring(sb_v.o_text_when_prerequisites_met))..
"]")
elseif(active_dialog.o_random) then
table.insert(formspec,
"label["..tostring(10.5+offset+0.2)..","..tostring(h+0.5)..";"..
minetest.formspec_escape("[One of these options is randomly selected] "..
tostring(sb_v.o_text_when_prerequisites_met))..
"]")
else
-- show the actual text for the option
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"field", tostring(10.5+offset)..","..h..";"..
tostring(field_length-2.3)..",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)
end
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"button", tostring(10.5+offset+field_length-2.2)..","..h..";1.0,0.9", "delete_option_"..oid,
"Del",
"Delete this option/answer.",
true)
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
-- "image_button", tostring(9.9+offset+field_length-0.5)..","..h..";0.5,0.9"..
-- ";gui_furnace_arrow_bg.png^[transformR180",
"button", tostring(10.5+offset+field_length-1.1)..","..h..";0.5,0.9",
"option_move_down_"..oid,
"v",
"Move this option/answer one down.",
(current_index < anz_options))
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
-- "image_button", tostring(9.9+offset+field_length-1.0)..","..h..";0.5,0.9"..
-- ";gui_furnace_arrow_bg.png",
"button", tostring(10.5+offset+field_length-0.5)..","..h..";0.5,0.9",
"option_move_up_"..oid,
"^",
"Move this option/answer one up.",
(current_index > 1))
return {h = h, formspec = formspec}
end
-- helper function for yl_speak_up.get_fs_talkdialog:
-- if the player can edit the NPC,
-- either add a button for entering edit mode
@ -831,18 +697,12 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec
local sb_v = active_dialog.d_options[s_o_id]
local oid = minetest.formspec_escape(sb_v.o_id)
local res = {}
-- in edit_mode: show all options
if(edit_mode) then
res = yl_speak_up.get_fs_talkdialog_line_in_edit_mode(
formspec, h, pname_for_old_fs, oid, sb_v,
dialog, active_dialog, dialog_list, d_id_to_dropdown_index,
i, #sorted_o_list)
-- normal mode: show an option if the prerequirements (if any are defined) are met
elseif(not(edit_mode)) then
res = yl_speak_up.get_fs_talkdialog_line_in_normal_mode(
res = yl_speak_up.get_fs_talkdialog_line(
formspec, h, pname_for_old_fs, oid, sb_v,
dialog, allowed, pname)
end
dialog, allowed, pname,
-- these additional parameters are needed in edit_mode
active_dialog, dialog_list, d_id_to_dropdown_index, i, #sorted_o_list)
formspec = res.formspec
h = res.h
end