From b4553215287139b1bc228f340d09e178f1893ac8 Mon Sep 17 00:00:00 2001 From: Sokomine Date: Sat, 8 Jan 2022 22:53:47 +0100 Subject: [PATCH] preparations for using the same code for generating fs for diffrent versions --- fs_talkdialog.lua | 114 +++++++++++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 36 deletions(-) diff --git a/fs_talkdialog.lua b/fs_talkdialog.lua index 999a86e..8e16733 100644 --- a/fs_talkdialog.lua +++ b/fs_talkdialog.lua @@ -279,23 +279,40 @@ end -- show an edit option in the main menu of the NPC; -- helper function for yl_speak_up.fs_talkdialog(..) yl_speak_up.add_edit_button_fs_talkdialog = function(formspec, h, button_name, tooltip, label, - show_main_not_alternate, alternate_label, is_exit_button) + show_main_not_alternate, alternate_label, is_exit_button, + pname) -- do not show this button at all if there is no alternate text and the condition is false if(not(alternate_label) and not(show_main_not_alternate)) then return h end + local button_dimensions = "0.5,"..(h+1)..";53.8,0.9;" + local label_start_pos = "0.7" + -- older formspecs (before v4) do not offer a scroll container and have to scroll manually + if(pname) then + local max_number_of_buttons = yl_speak_up.max_number_of_buttons + local start_index = yl_speak_up.speak_to[pname].option_index + local counter = yl_speak_up.speak_to[pname].counter + yl_speak_up.speak_to[pname].counter = counter + 1 + if counter < start_index or counter >= start_index + max_number_of_buttons then + return h + end + -- there has to be more room for the up and down arrows + button_dimensions = "1.2,"..(h+1)..";52.8,0.9;" + label_start_pos = "1.4" + end h = h + 1 if(show_main_not_alternate) then if(is_exit_button) then - table.insert(formspec, "button_exit[0.5,"..h..";53.8,0.9;"..tostring(button_name)..";]") + table.insert(formspec, "button_exit["..button_dimensions..tostring(button_name)..";]") else - table.insert(formspec, "button[0.5,"..h..";53.8,0.9;"..tostring(button_name)..";]") + table.insert(formspec, "button["..button_dimensions..tostring(button_name)..";]") end table.insert(formspec, "tooltip["..tostring(button_name)..";"..tostring(tooltip).."]") - table.insert(formspec, "label[0.7,"..(h+0.45)..";"..tostring(label).."]") + table.insert(formspec, "label["..label_start_pos..","..(h+0.45)..";"..tostring(label).."]") else - table.insert(formspec, "box[0.5,"..h..";53.8,0.9;#BBBBBB]") - table.insert(formspec, "label[0.7,"..(h+0.45)..";"..tostring(alternate_label).."]") + table.insert(formspec, "box["..button_dimensions.."#BBBBBB]") + table.insert(formspec, "label["..label_start_pos..","..(h+0.45)..";".. + tostring(alternate_label).."]") end return h end @@ -447,11 +464,11 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec owner_info = "\n\n(owned by "..minetest.formspec_escape(yl_speak_up.npc_owner[ n_id ])..")" end - local fs_version = "1" - if formspec_v >= 4 then - fs_version = "3" - end - formspec = { + local fs_version = "1" + if formspec_v >= 4 then + fs_version = "3" + end + formspec = { "formspec_version["..fs_version.."]", "size[57,33]", "position[0,0.45]", @@ -495,14 +512,17 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec "image[15.5,0.5;4,4;", portrait, "]", - } - if formspec_v >= 4 then + } + + if formspec_v >= 4 or true then -- add those things that only exist in formspec_v >= 4 - table.insert(formspec, "style_type[button;bgcolor=#a37e45]") - table.insert(formspec, "style_type[button_exit;bgcolor=#a37e45]") -- Dialog - -- table.insert(formspec, "background[-1,-1;22,25;yl_speak_up_bg_dialog2.png;false]") - -- table.insert(formspec, "background[-1,23;58,10;yl_speak_up_bg_dialog2.png;false]") - -- table.insert(formspec, "style_type[button;bgcolor=#a37e45]") + if(fs_version ~= "1") then + table.insert(formspec, "style_type[button;bgcolor=#a37e45]") + table.insert(formspec, "style_type[button_exit;bgcolor=#a37e45]") -- Dialog + -- table.insert(formspec, "background[-1,-1;22,25;yl_speak_up_bg_dialog2.png;false]") + -- table.insert(formspec, "background[-1,23;58,10;yl_speak_up_bg_dialog2.png;false]") + -- table.insert(formspec, "style_type[button;bgcolor=#a37e45]") + end -- this is used to build a list of all available dialogs for a dropdown menu in edit mode -- (only relevant in edit mode) @@ -573,16 +593,34 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec -- replace $NPC_NAME$ etc. local t = minetest.formspec_escape(yl_speak_up.replace_vars_in_text( (alternate_text or active_dialog.d_text), dialog, pname)) - yl_speak_up.add_formspec_element_with_tooltip_if(formspec, + if(fs_version ~= "1") then + yl_speak_up.add_formspec_element_with_tooltip_if(formspec, "hypertext", "0.2,5;19.6,17.8", "d_text", ""..t.."\n", t:trim()..";#000000;#FFFFFF", true) + else + yl_speak_up.add_formspec_element_with_tooltip_if(formspec, + "textarea", "0.2,5;19.6,17.8", "", + ";"..t.."\n", + t:trim(), + true) + end end - table.insert(formspec, "scrollbaroptions[min=0;max=14;smallstep=1;largestep=2;arrows=show]") - table.insert(formspec, "scrollbar[0.2,24.2;0.2,7;vertical;scr0;0]") - table.insert(formspec, "scroll_container[0,24;56,7;scr0;vertical;1]") + local pname_for_old_fs = nil + if(fs_version ~= "1") then + table.insert(formspec, "scrollbaroptions[min=0;max=14;smallstep=1;largestep=2;arrows=show]") + table.insert(formspec, "scrollbar[0.2,24.2;0.2,7;vertical;scr0;0]") + table.insert(formspec, "scroll_container[0,24;56,7;scr0;vertical;1]") + else + -- old formspec versions need to remember somewhere extern how far the player scrolled + pname_for_old_fs = pname + yl_speak_up.speak_to[pname_for_old_fs].counter = 1 + table.insert(formspec, "container[0,24]") + table.insert(formspec, "button[0.1,0;1,3;button_down;^\nU\np]") + table.insert(formspec, "button[0.1,3.2;1,4.5;button_up;D\no\nw\nn\nv]") + end h = -0.8 -- allow to delete entries that have no options later on @@ -708,7 +746,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec t, (t and not(t_alt)), t_alt, - nil) + nil, pname_for_old_fs) end end end @@ -727,7 +765,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec "click here. The NPC will return items he doesn't want.", offer_item_add_text.."I want to give you something.", -- show this in edit mode and when the NPC actually accepts items - (edit_mode or dialog.n_dialogs["d_got_item"]), nil, nil) + (edit_mode or dialog.n_dialogs["d_got_item"]), nil, nil, pname_for_old_fs) end @@ -742,7 +780,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec -- the amount of allowed options/answers has been reached (anz_options < yl_speak_up.max_number_of_options_per_dialog), "Maximum number of allowed answers/options reached. No further options/answers ".. - "can be added.", nil) + "can be added.", nil, pname_for_old_fs) h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h, "delete_this_empty_dialog", @@ -753,7 +791,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec (active_dialog and active_dialog.d_text == "" and anz_options == 0), -- (but only show this option if the dialog is empty) "If you want to delete this dialog, you need to delete all options and its ".. - "text first.", nil) + "text first.", nil, pname_for_old_fs) h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h, "show_what_points_to_this_dialog", @@ -762,7 +800,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec "or effects lead the player to this dialog here.", "Show what points to this dialog.", -- there is no alternate text to show - true, nil, nil) + true, nil, nil, pname_for_old_fs) h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h, "make_first_option", @@ -772,14 +810,14 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec "Make this dialog the first one shown when starting a conversation.", (active_dialog and active_dialog.d_sort and tonumber(active_dialog.d_sort) ~= 0), -- (but only show this option if it's not already the first one) - "This dialog will be shown whenever a conversation is started.", nil) + "This dialog will be shown whenever a conversation is started.", nil,pname_for_old_fs) h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h, "show_inventory", "Access and manage the inventory of the NPC. This is used for adding trade ".. "items, getting collected payments and managing quest items.", "Show your inventory (only accessible to owner)!", - true, nil, nil) + true, nil, nil, pname_for_old_fs) -- chat option: Mute/Unmute NPC @@ -796,7 +834,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec "useful while you edit the NPC and don't want players to see ".. "unfinished entries and/or quests.", "State: Not muted. Stop talking to other players while I give you new orders.", - (luaentity and luaentity.yl_speak_up.talk), nil, nil) + (luaentity and luaentity.yl_speak_up.talk), nil, nil, pname_for_old_fs) h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h, "un_mute_npc", -- unmute the NPC @@ -805,7 +843,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec "finished editing.", "State: You are currently muted. Talk to anyone again who wants to talk to you.", -- the NPC has to be there - (luaentity and not(luaentity.yl_speak_up.talk)), nil, nil) + (luaentity and not(luaentity.yl_speak_up.talk)), nil, nil, pname_for_old_fs) h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h, @@ -814,7 +852,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec "players. You can always give him new orders by entering edit mode again.", -- chat option:"That was all. I'm finished with giving you new orders. Remember them!" "That was all. I'm finished with giving you new orders. Remember them!", - true, nil, true) -- is button_exit + true, nil, true, pname_for_old_fs) -- is button_exit -- Offer to enter edit mode if the player has the npc_talk_owner priv AND owns the npc. -- The npc_master priv allows to edit all NPC. @@ -825,7 +863,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec "answers that can be given.", -- chat option: "I am your owner. I have new orders for you. "I am your owner. I have new orders for you.", - true, nil, true) -- is button_exit + true, nil, true, pname_for_old_fs) -- is button_exit end -- add a Let's trade button to the first dialog if the NPC has trades @@ -843,16 +881,20 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec "show_trade_list", "Show a list of trades the NPC has to offer.", "Let's trade!", - (has_trades), nil, nil) + (has_trades), nil, nil, pname_for_old_fs) end h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h, "button_exit", yl_speak_up.message_button_option_exit, yl_speak_up.message_button_option_exit, - true, nil, true) -- button_exit + true, nil, true, pname_for_old_fs) -- button_exit - table.insert(formspec, "scroll_container_end[]") + if(fs_version ~= "1") then + table.insert(formspec, "scroll_container_end[]") + else + table.insert(formspec, "container_end[]") + end table.insert(formspec, "container_end[]") -- if the player has an older formspec version