options can now be moved up or down much easier

This commit is contained in:
Sokomine 2022-10-26 14:01:25 +02:00
parent ab0e9faf14
commit aedf4b6200
2 changed files with 60 additions and 5 deletions

View File

@ -164,6 +164,14 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields)
if(o_id and fields["delete_option_"..o_id]) then
fields["del_option"] = true
fields.o_id = o_id
-- ..or move an option up by one in the list
elseif(o_id and fields["option_move_up_"..o_id]) then
fields["option_move_up"] = true
fields.o_id = o_id
-- ..or move an option down by one in the list
elseif(o_id and fields["option_move_down_"..o_id]) then
fields["option_move_down"] = true
fields.o_id = o_id
end
end
end
@ -190,6 +198,34 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields)
return result
end
-- move an option up by one
local d_options = dialog.n_dialogs[d_id].d_options
if(fields[ "option_move_up"] and fields.o_id and d_options[fields.o_id]) then
local sorted_o_list = yl_speak_up.get_sorted_options(d_options, "o_sort")
local idx = table.indexof(sorted_o_list, fields.o_id)
if(idx > 1) then
-- swap the two positions
tmp = dialog.n_dialogs[d_id].d_options[fields.o_id].o_sort
dialog.n_dialogs[d_id].d_options[fields.o_id].o_sort =
dialog.n_dialogs[d_id].d_options[sorted_o_list[idx - 1]].o_sort
dialog.n_dialogs[d_id].d_options[sorted_o_list[idx - 1]].o_sort = tmp
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Dialog "..d_id..": Option "..tostring(fields.o_id).." was moved up by one.")
end
-- ..or move the option down by one
elseif(fields[ "option_move_down"] and fields.o_id and d_options[fields.o_id]) then
local sorted_o_list = yl_speak_up.get_sorted_options(d_options, "o_sort")
local idx = table.indexof(sorted_o_list, fields.o_id)
if(idx > 0 and idx < #sorted_o_list) then
tmp = dialog.n_dialogs[d_id].d_options[fields.o_id].o_sort
dialog.n_dialogs[d_id].d_options[fields.o_id].o_sort =
dialog.n_dialogs[d_id].d_options[sorted_o_list[idx + 1]].o_sort
dialog.n_dialogs[d_id].d_options[sorted_o_list[idx + 1]].o_sort = tmp
table.insert(yl_speak_up.npc_was_changed[ n_id ],
"Dialog "..d_id..": Option "..tostring(fields.o_id).." was moved down by one.")
end
end
-- ignore entries to o_sort if they are not a number
if(fields[ "edit_option_o_sort"]
and tonumber(fields[ "edit_option_o_sort"])

View File

@ -482,7 +482,8 @@ end
-- 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)
dialog, active_dialog, dialog_list, d_id_to_dropdown_index,
current_index, anz_options)
local offset = 0.0
local field_length = 44.4
if(pname_for_old_fs) then
@ -571,17 +572,34 @@ yl_speak_up.get_fs_talkdialog_line_in_edit_mode = function(
-- show the actual text for the option
yl_speak_up.add_formspec_element_with_tooltip_if(formspec,
"field", tostring(9.9+offset)..","..h..";"..
tostring(field_length-1.2)..",0.9",
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(9.9+offset+field_length-1.1)..","..h..";1.0,0.9", "delete_option_"..oid,
"button", tostring(9.9+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(9.9+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(9.9+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
@ -867,7 +885,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec
anz_options = anz_options + 1
end
for _, s_o_id in ipairs(sorted_o_list) do
for i, s_o_id in ipairs(sorted_o_list) do
local sb_v = active_dialog.d_options[s_o_id]
local oid = minetest.formspec_escape(sb_v.o_id)
local res = {}
@ -875,7 +893,8 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec
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)
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(