From c2402ccaed539c0e1d7586c99931ba43891b60cd Mon Sep 17 00:00:00 2001 From: Sokomine Date: Mon, 12 Jul 2021 17:52:34 +0200 Subject: [PATCH] show variable usage (nicely colored) --- fs_edit_general.lua | 16 +++---- quest_api.lua | 105 ++++++++++++++++++++++++++++++++------------ 2 files changed, 85 insertions(+), 36 deletions(-) diff --git a/fs_edit_general.lua b/fs_edit_general.lua index 9267244..0c82917 100644 --- a/fs_edit_general.lua +++ b/fs_edit_general.lua @@ -975,10 +975,10 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, -- TODO: actually implement/show usage of the variable as formspec yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:"..formspec_input_to, - formspec = "size[9,2]".. - "label[0.2,0.5;This ought to show usage of a variable.]".. - -- back to the list with that one precondition or effect - "button[1.5,1.5;2,0.9;back_from_show_var_usage;Back]"}) + formspec = yl_speak_up.get_list_of_usage_of_variable( + element[ id_prefix.."variable"], pname, true, + "back_from_show_var_usage") + }) return end -- show var usuage - but this time from the edit dialog for that precondition or effect @@ -989,10 +989,10 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, -- TODO: actually implement/show usage of the variable as formspec yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:"..formspec_input_to, - formspec = "size[9,2]".. - "label[0.2,0.5;This ought to show usage of a variable.]".. - -- back to editing that particular precondition or effect - "button[1.5,1.5;2,0.9;back_from_error_msg;Back]"}) + formspec = yl_speak_up.get_list_of_usage_of_variable( + element[ id_prefix.."variable"], pname, true, + "back_from_error_msg") + }) return end end diff --git a/quest_api.lua b/quest_api.lua index 44213d7..29194bb 100644 --- a/quest_api.lua +++ b/quest_api.lua @@ -387,7 +387,7 @@ end -- find out where this variable is used in NPCs -yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_preconditions) +yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_preconditions, back_button_name) -- which NPC (might be several) is using this variable? local npc_list = yl_speak_up.get_npc_users_of_variable(var_name) -- list of all relevant preconditions, actions and effects @@ -399,23 +399,18 @@ yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_prec for d_id, d in pairs(dialog.n_dialogs) do if(d and d.d_options) then for o_id, o in pairs(d.d_options) do - -- if preconditions or effects apply: show the action as well - local show_action = false - local text = "\n".. - tostring(n_id)..":"..tostring(d_id)..":"..tostring(o_id)..":".. - "\tTo "..tostring(dialog.n_npc)..": ".. - tostring(o.o_text_when_prerequisites_met) + local p_text = "" + local r_text = "" if(o and o.o_prerequisites and check_preconditions) then for p_id, p in pairs(o.o_prerequisites) do if(p and p.p_type and p.p_type == "state" and p.p_variable and p.p_variable == var_name) then - if(text) then - table.insert(res, text) - text = nil - end - table.insert(res, "\t\tC: "..tostring(p_id).." ".. - yl_speak_up.show_precondition(p, pname)) - show_action = true + p_text = p_text..",#FFFF00,".. + minetest.formspec_escape(tostring(p_id)).. + ",#FFFF00,pre(C)ondition,#FFFF00,".. + minetest.formspec_escape(p.p_type)..",#FFFF00,".. + minetest.formspec_escape( + yl_speak_up.show_precondition(p, pname)) end end end @@ -423,29 +418,83 @@ yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_prec for r_id, r in pairs(o.o_results) do if(r and r.r_type and r.r_type == "state" and r.r_variable and r.r_variable == var_name) then - if(text) then - table.insert(res, text) - text = nil - end - table.insert(res, "\t\tE: "..tostring(r_id).." ".. - yl_speak_up.show_effect(r, pname)) - show_action = true + r_text = r_text..",#55FF55,".. + minetest.formspec_escape(tostring(r_id)).. + ",#55FF55,(Ef)fect,#55FF55,".. + minetest.formspec_escape(r.r_type)..",#55FF55,".. + minetest.formspec_escape( + yl_speak_up.show_effect(r, pname)) end end end - if(o and o.actions and show_action) then + -- if preconditions or effects apply: show the action as well + if(o and o.actions and (p_text ~= "" or r_text ~= "")) then for a_id, a in pairs(o.actions) do - table.insert(res, "\t\tA: "..tostring(a_id).." ".. - yl_speak_up.show_action(a, pname)) + -- no need to introduce an a_text; this will follow + -- directly after p_text, and p_text is finished + p_text = p_text..",FF9900,".. + minetest.formspec_escape(tostring(a_id)).. + ",#FF9900,(A)ction,#FF9900,".. + minetest.formspec_escape(a.a_type)..",#FF9900,".. + minetest.formspec_escape( + yl_speak_up.show_action(a, pname)) end end + if(p_text ~= "" or r_text ~= "") then + -- show newlines as <\n> in order to save space + local d_text = string.gsub( + dialog.n_dialogs[ d_id ].d_text or "?", + "\n", "<"..minetest.formspec_escape("\\").."n>") + -- break the text up into lines of length x + local parts = minetest.wrap_text(d_text, 80, true) + -- only show the first two lines (we don't have infinite room) + local prefix = ",#FFFFFF,,#FFFFFF,,#FFFFFF,,#BBBBFF," + if(#parts < 2) then + d_text = minetest.formspec_escape(parts[1]) + elseif(#parts == 2) then + d_text = minetest.formspec_escape(parts[1]).. + prefix..minetest.formspec_escape(parts[2]) + elseif(#parts == 3) then + d_text = minetest.formspec_escape(parts[1]).. + prefix..minetest.formspec_escape(parts[2]).. + prefix..minetest.formspec_escape(parts[3]) + else + -- indicate that there is more + d_text = minetest.formspec_escape(parts[1]).. + prefix..minetest.formspec_escape(parts[2]).. + prefix..minetest.formspec_escape(parts[3]).. + minetest.formspec_escape(" [...]") + end + table.insert(res, + "#6666FF,".. + tostring(n_id)..",#6666FF,NPC,#6666FF,named:,#6666FF,".. + minetest.formspec_escape(dialog.n_npc or "?")..",".. + "#BBBBFF,".. + tostring(d_id)..",#BBBBFF,Dialog,#BBBBFF,says:,#BBBBFF,".. + d_text..",".. + "#FFFFFF,".. + tostring(o_id)..",#FFFFFF,Option,#FFFFFF,A:,#FFFFFF,".. + minetest.formspec_escape(tostring( + o.o_text_when_prerequisites_met or "?")).. + p_text..r_text) + end end end end end end - for i, line in ipairs(res) do - --minetest.chat_send_player("singleplayer", line) - print(line) -- TODO - end + + local formspec = { + "formspec_version[3]", + "size[57,33]", + -- back to the list with that one precondition or effect + -- TODO: give this button a better description (back to where?) + "button[0.2,0.2;2,0.9;"..back_button_name..";Back]", +-- type x_id color type text + "tablecolumns[color,span=1;text;color,span=1;text;color,span=1;text;color,span=1;text]", + "table[1.2,1.2;55.0,30.0;table_of_variable_uses;" + } + -- insert blank lines between lines belonging together + table.insert(formspec, table.concat(res, ",#FFFFFF,,#FFFFFF,,#FFFFFF,,#FFFFFF,,")) + return table.concat(formspec, "\n") end