allow to edit and store alternate texts for dialogs when comming from a particular option/failed effect

This commit is contained in:
Sokomine 2021-07-01 22:33:19 +02:00
parent ff2ba081a9
commit 3d4141e786
2 changed files with 183 additions and 6 deletions

View File

@ -640,7 +640,7 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
formspec = "size[9,2.5]"..
"label[0.2,0.5;Please insert an item first! Your NPC"..
"label[0.2,0.5;Please insert an item first! Your NPC "..
"needs\nto know what it shall give to the player.]"..
"button[1.5,2.0;2,0.9;back_from_error_msg;Back]"})
return
@ -787,6 +787,8 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
end
end
v[ "alternate_text" ] = data.alternate_text
-- only save if something was actually selected
if(v[ id_prefix.."value"]) then
if(not(dialog.n_dialogs[d_id].d_options[o_id][ element_list_name ])) then
@ -873,6 +875,72 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields,
return
end
-- edit alternate text for an action
if(fields.button_edit_action_on_failure_text_change) then
was_changed = true
local dialog = yl_speak_up.speak_to[pname].dialog
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
local failure_id = ""
if(data and data.action_failure_dialog) then
failure_id = sorted_dialog_list[ data.action_failure_dialog ]
end
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
formspec = yl_speak_up.get_fs_edit_dialog_modification(
dialog, failure_id, data.alternate_text)
})
return
-- edit alternate text for an on_failure effect
elseif(fields.button_edit_effect_on_failure_text_change) then
was_changed = true
local dialog = yl_speak_up.speak_to[pname].dialog
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formspec_input_to,
formspec = yl_speak_up.get_fs_edit_dialog_modification(
dialog, data.on_failure, data.alternate_text)
})
return
-- save the changes
elseif(fields.save_dialog_modification) then
was_changed = true
local dialog = yl_speak_up.speak_to[pname].dialog
local old_text = data.alternate_text
if(data and fields.d_text_new and fields.d_text_new ~= "$TEXT$"
and fields.d_text_new ~= data.alternate_text) then
-- store modification
data.alternate_text = fields.d_text_new
yl_speak_up.speak_to[pname][ tmp_data_cache ] = data
if(id_prefix == "r_") then
-- record the change
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Dialog "..d_id..": The text displayed for dialog "..
tostring(data.on_failure).." when selecting option "..
tostring(o_id).." in dialog "..tostring( d_id )..
" and effect "..tostring(x_id).." failed "..
" was changed from "..
"["..tostring(old_text).."] to ["..tostring(fields.d_text_new).."].")
elseif(id_prefix == "a_") then
local sorted_dialog_list = yl_speak_up.sort_keys(dialog.n_dialogs)
local failure_id = ""
if(data and data.action_failure_dialog) then
failure_id = sorted_dialog_list[ data.action_failure_dialog ]
end
-- record the change
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Dialog "..d_id..": The text displayed for dialog "..
tostring(failure_id).." when the action "..
tostring(x_id).." of option "..
tostring( o_id ).." in dialog "..tostring( d_id )..
" failed, was changed from "..
"["..tostring(old_text).."] to ["..tostring(fields.d_text_new).."].")
end
end
-- just back; nothing special (but definitely a legitimate button)
elseif(fields.back_from_edit_dialog_modification) then
was_changed = true
end
-- the player wants to change/edit a precondition or effect
if(not(fields.back)
and (fields.change_element or fields.select_what or fields.select_trade
@ -1124,6 +1192,10 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
data.lua_code = e[ id_prefix.."value" ]
end
end
if(e[ "alternate_text"]) then
data.alternate_text = e[ "alternate_text" ]
end
-- write that data back
yl_speak_up.speak_to[pname][ tmp_data_cache ] = data
end
@ -1411,7 +1483,7 @@ yl_speak_up.get_fs_edit_option_related = function(player, table_click_result,
"1.2,5.8;18.0,2.0;d_text",
", but with the following *modified* text",
":]",
"button_effect_on_failure_text_change")
"button_edit_effect_on_failure_text_change")
end
formspec = formspec..
"label[0.2,3.3;If the *previous* effect failed,]"..
@ -1655,14 +1727,14 @@ yl_speak_up.set_on_action_failure_dialog = function(pname, data, instruction)
"1.2,10.2;18.0,1.8;d_text",
"label[13.8,9.9;and show the following *modified* text:]",
"",
"button_action_on_failure_text_change")
"button_edit_action_on_failure_text_change")
end
return on_failure_dialog
end
yl_speak_up.show_colored_dialog_text = function(dialog, data, d_id, hypertext_pos,
alternate_text, postfix, button_name)
alternate_label_text, postfix, button_name)
if(not(data)) then
return ""
end
@ -1680,7 +1752,7 @@ yl_speak_up.show_colored_dialog_text = function(dialog, data, d_id, hypertext_po
text = dialog.n_dialogs[ d_id ].d_text
end
if(data and data.alternate_text and data.alternate_text ~= "") then
add_info_alternate_text = alternate_text
add_info_alternate_text = alternate_label_text
-- replace $TEXT$ with the normal dialog text and make the new text yellow
text = "<style color=#FFFF00>"..
string.gsub(
@ -1720,3 +1792,28 @@ yl_speak_up.show_colored_dialog_text = function(dialog, data, d_id, hypertext_po
-- display the edit button *inside*/on top of the hypertext field
edit_button
end
-- this allows to edit modifications of a dialog that are applied when a given option
-- is choosen - i.e. when the NPC wants to answer some questions - but those answers
-- do not warrant their own dialog
yl_speak_up.get_fs_edit_dialog_modification = function(dialog, d_id, alternate_dialog_text)
return "formspec_version[3]"..
"size[20,13]"..
-- TODO: explain what can be done here
"label[0.2,2.0;This is the normal text of dialog \""..
minetest.formspec_escape(tostring(d_id)).."\", shown for reference:]"..
yl_speak_up.show_colored_dialog_text(
dialog,
{r_id = "", r_type = "dialog"},
d_id,
"1.2,2.3;18.0,4.0;d_text_orig",
"", -- no modifications possible at this step
"",
"").. -- no edit button here as this text cannot be changed here
"label[0.2,7.0;Enter the alternate text here. $TEXT$ will be replaced with the normal "..
"dialog text above:]"..
"textarea[1.2,7.3;18.0,4.5;d_text_new;;"..
minetest.formspec_escape(alternate_dialog_text or "$TEXT$").."]"..
"button[3.0,12.0;1,0.7;back_from_edit_dialog_modification;Abort]"..
"button[6.0,12.0;1,0.7;save_dialog_modification;Save]"
end

View File

@ -57,6 +57,86 @@ yl_speak_up.input_edit_option_dialog = function(player, formname, fields)
return
end
-- want to edit the text that is shown when switching to the next dialog?
if(fields.button_edit_action_failed_dialog) then
-- the target effect is the (failed) action
local target_action = {}
local actions = d_option.actions
if(actions) then
for a_id, a in pairs(actions) do
if(a and a.a_id) then
target_action = a
end
end
end
-- remember what we're working at
yl_speak_up.speak_to[pname].edit_alternate_text_for = target_action
yl_speak_up.show_fs(player, "msg", {
input_to = formname,
formspec = yl_speak_up.get_fs_edit_dialog_modification(
dialog, target_action.a_on_failure, target_action.alternate_text)
})
return
elseif(fields.button_edit_action_success_dialog) then
-- the target effect is the "dialog" effect
local target_effect = {}
local results = d_option.o_results
if(results) then
for r_id, r in pairs(results) do
if(r and r.r_type and r.r_type == "dialog") then
target_effect = r
end
end
end
-- remember what we're working at
yl_speak_up.speak_to[pname].edit_alternate_text_for = target_effect
yl_speak_up.show_fs(player, "msg", {
input_to = formname,
formspec = yl_speak_up.get_fs_edit_dialog_modification(
dialog, target_effect.r_value, target_effect.alternate_text)
})
return
-- save the alternate action/effect text for the next dialog
elseif(fields.save_dialog_modification) then
local target_element = yl_speak_up.speak_to[pname].edit_alternate_text_for
local old_text = target_element.alternate_text
if(target_element and fields.d_text_new and fields.d_text_new ~= "$TEXT$"
and fields.d_text_new ~= target_element.alternate_text) then
target_element.alternate_text = fields.d_text_new
if(target_element and target_element.r_id) then
-- record the change
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Dialog "..d_id..": The text displayed for dialog "..
tostring(target_element.r_value).." when selecting option "..
tostring( o_id ).." in dialog "..tostring( d_id )..
" was changed from "..
"["..tostring(old_text).."] to ["..tostring(fields.d_text_new).."].")
elseif(target_element and target_element.a_id) then
-- record the change
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Dialog "..d_id..": The text displayed for dialog "..
tostring(target_element.a_on_failure).." when the action "..
tostring(target_element.a_id).." of option "..
tostring( o_id ).." in dialog "..tostring( d_id )..
" failed, was changed from "..
"["..tostring(old_text).."] to ["..tostring(fields.d_text_new).."].")
end
end
yl_speak_up.show_fs(player, "edit_option_dialog",
{n_id = n_id, d_id = d_id, o_id = o_id, caller="back-from_edit_dialog_modifications"})
return
elseif(fields.back_from_edit_dialog_modification) then
-- no longer working on an alternate text
yl_speak_up.speak_to[pname].edit_alternate_text_for = nil
yl_speak_up.show_fs(player, "edit_option_dialog",
{n_id = n_id, d_id = d_id, o_id = o_id, caller="back_from_edit_dialog_modifications"})
return
end
-- back to the main dialog window?
-- (this also happens when the last option was deleted)
if(fields.show_current_dialog or fields.quit or fields.button_exit or not(d_option) or fields.del_option) then
@ -414,7 +494,7 @@ yl_speak_up.get_fs_edit_option_dialog = function(player, n_id, d_id, o_id, calle
minetest.formspec_escape("[dialog \""..tostring(d_id).."\"]:").."]"..
yl_speak_up.show_colored_dialog_text(
dialog,
target_effect,
{r_id = "", r_type = "dialog"},
d_id,
"1.2,0.3;20.2,2.5;d_text",
"", -- no modifications possible at this step