diff --git a/functions.lua b/functions.lua index 06f94fa..dc67059 100644 --- a/functions.lua +++ b/functions.lua @@ -907,6 +907,14 @@ local function get_fs_edit_option_dialog(player, n_id, d_id, o_id) end end + -- if no target dialog has been selected: default is to go to the dialog with d_sort 0 + if(not(target_dialog) or target_dialog == "" or not(dialog.n_dialogs[target_dialog])) then + for d, v in pairs(dialog.n_dialogs) do + if(v.d_sort and tonumber(v.d_sort) == 0) then + target_dialog = d + end + end + end -- this is the text the NPC will say in reaction to this answer local next_text = "" if(target_dialog and dialog.n_dialogs[target_dialog]) then @@ -940,6 +948,28 @@ local function get_fs_edit_option_dialog(player, n_id, d_id, o_id) alternate_answer_option = "2" end + -- can the button "prev(ious)" be shown? + local button_prev = "" + -- can the button "next" be shown? + local button_next = "" + for o,v in pairs(n_dialog.d_options) do + -- the button prev can appear if there is at least one option with lower o_sort + if(button_prev == "" and v.o_sort and v.o_sort ~= "" and d_option.o_sort + and tonumber(v.o_sort) < tonumber(d_option.o_sort)) then + button_prev = "".. + "button[7.4,17;2.0,0.9;edit_option_prev;Prev]".. + "tooltip[edit_option_prev;Go to previous option/answer ".. + "(according to o_sort).]" + -- the button next can appear if there is at least one option with a higher o_sort + elseif(button_next == "" and v.o_sort and v.o_sort ~= "" and d_option.o_sort + and tonumber(v.o_sort) > tonumber(d_option.o_sort)) then + button_next = "".. + "button[12.0,17;2.0,0.9;edit_option_next;Next]".. + "tooltip[edit_option_next;Go to next option/answer ".. + "(according to o_sort).]" + end + end + -- build up the formspec local formspec = "".. "formspec_version[3]".. @@ -1005,10 +1035,8 @@ local function get_fs_edit_option_dialog(player, n_id, d_id, o_id) "button[2.4,17;2.0,0.9;edit_option_add;Add]".. "tooltip[edit_option_add;Add a new option/answer to this dialog.]".. -- button: prev/next - "button[7.4,17;2.0,0.9;edit_option_prev;Prev]".. - "tooltip[edit_option_prev;Go to previous option/answer (according to o_sort).]".. - "button[12.0,17;2.0,0.9;edit_option_next;Next]".. - "tooltip[edit_option_next;Go to next option/answer (according to o_sort).]".. + button_prev.. + button_next.. -- button: go back to dialog (repeated from top of the page) "button[15.8,17;5.0,0.9;show_current_dialog;Back to dialog ".. minetest.formspec_escape(d_id).."]".. @@ -2084,6 +2112,7 @@ minetest.register_on_player_receive_fields( ) +-- process input from formspec created in get_fs_edit_option_dialog(..) minetest.register_on_player_receive_fields( function(player, formname, fields) if formname ~= "yl_speak_up:edit_option_dialog" then @@ -2098,10 +2127,16 @@ minetest.register_on_player_receive_fields( end local n_id = yl_speak_up.speak_to[pname].n_id local d_id = yl_speak_up.speak_to[pname].d_id + -- this is passed on as a hidden field + local o_id = fields.o_id + + local dialog = yl_speak_up.speak_to[pname].dialog + local n_dialog = dialog.n_dialogs[d_id] + local d_option = n_dialog.d_options[o_id] -- this meny is specific to an option for a dialog; if no dialog is selected, we really -- can't know what to do - if(not(d_id)) then + if(not(d_id) or not(d_option)) then return end @@ -2111,6 +2146,45 @@ minetest.register_on_player_receive_fields( return end + -- the player wants to see the previous option/answer + if(fields.edit_option_prev) then + local cmp = 0 + local o_found = o_id + -- lets find out if there is any option with a lower o_sort value + for o,v in pairs(n_dialog.d_options) do + -- find the largest value that is smaller than d_option.o_sort + if(v.o_sort and v.o_sort ~= "" and d_option.o_sort + and tonumber(v.o_sort) < tonumber(d_option.o_sort) + and tonumber(v.o_sort) > cmp) then + cmp = tonumber(v.o_sort) + o_found = o + end + end + -- show that dialog; fallback: show the same (o_id) again + minetest.show_formspec(pname, "yl_speak_up:edit_option_dialog", get_fs_edit_option_dialog(player, n_id, d_id, o_found)) + return + + -- the player wants to see the next option/answer + elseif(fields.edit_option_next) then + local cmp = 10000000 + local o_found = o_id + -- lets find out if there is any option with a larger o_sort value + for o,v in pairs(n_dialog.d_options) do + -- find the lowest value that is larger than d_option.o_sort + if(v.o_sort and v.o_sort ~= "" and d_option.o_sort + and tonumber(v.o_sort) > tonumber(d_option.o_sort) + and tonumber(v.o_sort) < cmp) then + cmp = tonumber(v.o_sort) + o_found = o + end + end + -- show that dialog; fallback: show the same (o_id) again + minetest.show_formspec(pname, "yl_speak_up:edit_option_dialog", get_fs_edit_option_dialog(player, n_id, d_id, o_found)) + return + end + + + --minetest.chat_send_player(pname, "Fields: "..minetest.serialize(fields)) -- if ESC is pressed or anything else unpredicted happens: go back to the main dialog edit window