improved sorting of dialog list by d_sort

This commit is contained in:
Sokomine 2021-05-08 17:22:12 +02:00
parent e262e3fb9a
commit b09b4e5bf5

View File

@ -1239,40 +1239,33 @@ local function get_fs_talkdialog(player, n_id, d_id)
-- display the window with the text the NPC is saying
-- TODO: make name of dialog and sort option editable?
if(edit_mode) then
-- TODO: sort by name?
-- build the list of available dialogs for the dropdown list(s)
-- if there are dialogs defined
if(dialog and dialog.n_dialogs) then
-- the first entry will be "New dialog"
local n = 1
for k, v in pairs(dialog.n_dialogs) do
dialog_list = dialog_list .. "," .. minetest.formspec_escape(v.d_id)
-- which one is the current dialog?
n = n + 1
d_id_to_dropdown_index[v.d_id] = n
if(edit_mode and dialog and dialog.n_dialogs) then
-- sort all dialogs by d_sort
local sorted_list = yl_speak_up.get_sorted_options(dialog.n_dialogs, "d_sort")
-- add buttons for previous/next dialog
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
table.insert(formspec, "button[8.5,4.0;2,0.9;prev_dialog_"..
minetest.formspec_escape(sorted_list[i-1])..";<]")
table.insert(formspec, "tooltip[prev_dialog;Go to previous dialog "..
minetest.formspec_escape(sorted_list[i-1])..".]")
end
if(d == c_d_id and sorted_list[ i+1 ]) then
table.insert(formspec, "button[11,4.0;2,0.9;next_dialog_"..
minetest.formspec_escape(sorted_list[i+1])..";>]")
-- add a tooltip
table.insert(formspec, "tooltip[next_dialog;Go to next dialog "..
minetest.formspec_escape(sorted_list[i+1])..".]")
end
d_id_to_dropdown_index[d] = i + 1
end
local i = (d_id_to_dropdown_index[c_d_id] or 0)
table.insert(formspec, "label[0.2,4.6;Dialog:]") -- "..minetest.formspec_escape(c_d_id)..":]")
table.insert(formspec, "dropdown[3.0,4.0;5,1;d_id;"..dialog_list..";"..i..",]")
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 previous/next buttons for previous/next dialog (sorted as in the dropdown menu);
-- this is just an alternate way of walking through the dialogs
for k,v in pairs(d_id_to_dropdown_index) do
local ke = minetest.formspec_escape(k)
if( v == i-1) then
table.insert(formspec, "button[8.5,4.0;2,0.9;prev_dialog_"..ke..";<]")
-- add a tooltip
table.insert(formspec, "tooltip[prev_dialog_" .. ke .. ";Go to previous dialog "..ke..".]")
elseif(v == i+1) then
table.insert(formspec, "button[11,4.0;2,0.9;next_dialog_"..ke..";>]")
-- add a tooltip
table.insert(formspec, "tooltip[next_dialog_" .. ke .. ";Go to next dialog "..ke..".]")
end
end
-- 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.]")
@ -2030,8 +2023,6 @@ yl_speak_up.save_changes_and_switch_to_other_dialog = function(player, fields, t
end
-- helper function for sorting options/answers using options[o_id].o_sort
-- (or dialogs by d_sort)
yl_speak_up.get_sorted_options = function(options, sort_by)
@ -2041,9 +2032,20 @@ yl_speak_up.get_sorted_options = function(options, sort_by)
end
table.sort(sorted_list,
function(a,b)
return tonumber(options[a][sort_by])
and tonumber(options[b][sort_by])
and tonumber(options[a][sort_by]) < tonumber(options[b][sort_by])
-- sadly not all entries are numeric
if(tonumber(options[a][sort_by]) and tonumber(options[b][sort_by])) then
return (tonumber(options[a][sort_by]) < tonumber(options[b][sort_by]))
-- numbers have a higher priority
elseif(tonumber(options[a][sort_by])) then
return true
elseif(tonumber(options[b][sort_by])) then
return false
-- if the value is the same: sort by index
elseif(options[a][sort_by] == options[b][sort_by]) then
return (a < b)
else
return (options[a][sort_by] < options[b][sort_by])
end
end
)
return sorted_list