diff --git a/formspec_helpers.lua b/formspec_helpers.lua index 4d5120e..71d2ff4 100644 --- a/formspec_helpers.lua +++ b/formspec_helpers.lua @@ -33,7 +33,7 @@ yl_speak_up.create_dropdown_playerlist = function(player, pname, tostring(explain_add_player).."]" else text = text.."button["..tostring(start_x + 3.8 + stretch_x)..","..tostring(start_y).. - ";"..tostring(3.5 + stretch_x)..",0.9;".. + ";"..tostring(3.4 + stretch_x)..",0.9;".. tostring(field_name_for_deleting_player)..";".. tostring(delete_button_text).."]".. "tooltip["..tostring(field_name_for_deleting_player)..";".. diff --git a/quest_api.lua b/quest_api.lua index a77c666..330351f 100644 --- a/quest_api.lua +++ b/quest_api.lua @@ -164,33 +164,80 @@ yl_speak_up.input_fs_manage_variables = function(player, formname, fields) yl_speak_up.show_fs(player, "manage_variables") return end + -- leave this formspec + if(fields and (fields.quit or fields.exit)) then + local last_fs = yl_speak_up.speak_to[pname][ "working_at" ] + yl_speak_up.show_fs(player, last_fs) -- add a new variable? - if(fields and fields.add_variable) then + elseif(fields and fields.add_variable) then + local error_msg = "" if(not(fields.add_variable_name) or fields.add_variable_name == "" or fields.add_variable_name:trim() == "") then - yl_speak_up.show_fs(player, "msg", { - input_to = "yl_speak_up:manage_variables", - formspec = "size[6,2]".. - "label[0.2,0.5;Please enter the name of your variable!]".. - "button[1.5,1.5;2,0.9;back_from_msg;Back]"}) - return - end - -- TODO: limit names to something more sensible? - fields.add_variable_name = fields.add_variable_name:trim() - local res = yl_speak_up.add_quest_variable(pname, fields.add_variable_name) - local text = "A new variable named\n \""..tostring(fields.add_variable_name).. - "\"\nhas been created." - if(not(res)) then - text = "Failed to create variable named\n \"".. - tostring(fields.add_variable_name).."\"." + error_msg = "Please enter the name of your variable!" + -- limit names to something more sensible + elseif(string.len(fields.add_variable_name) > 30) then + error_msg = "The name of your variable name is too long.\n".. + "Only up to 30 characters are allowed." + elseif(string.len(fields.add_variable_name:trim()) < 2) then + error_msg = "The name of your variable name is too short.\n".. + "It has to be at least 2 characters long." + else + fields.add_variable_name = fields.add_variable_name:trim() + local res = yl_speak_up.add_quest_variable(pname, fields.add_variable_name) + -- not really an error msg here - but fascilitates output + error_msg = "A new variable named\n \"".. + minetest.formspec_escape(fields.add_variable_name).. + "\"\nhas been created." + if(not(res)) then + error_msg = "Failed to create variable named\n \"".. + minetest.formspec_escape(fields.add_variable_name).."\"." + else + -- slect this new variable + local var_list = yl_speak_up.get_quest_variables(pname, true) + -- make names of own variables shorter + yl_speak_up.strip_pname_from_varlist(var_list, pname) + table.sort(var_list) + local index = table.indexof(var_list, fields.add_variable_name) + if(index and index > -1) then + yl_speak_up.speak_to[pname].tmp_index_variable = index + 1 + end + end end yl_speak_up.show_fs(player, "msg", { input_to = "yl_speak_up:manage_variables", formspec = "size[6,2]".. - "label[0.2,0.0;"..minetest.formspec_escape(text).."]".. + "label[0.2,0.0;"..error_msg.."]".. "button[1.5,1.5;2,0.9;back_from_msg;Back]"}) return - + -- show where this variable is used + elseif(fields and fields.show_var_usage and fields.show_var_usage ~= "") then + yl_speak_up.show_fs(player, "msg", { + input_to = "yl_speak_up:manage_variables", + formspec = yl_speak_up.get_list_of_usage_of_variable( + fields.list_var_names, pname, true, + "back_from_msg", + "Back to manage variables", + -- not an internal variable + false) + }) + return + -- TODO: delete variable + -- a var name was selected in the dropdown list + elseif(fields and fields.list_var_names and fields.list_var_names ~= "") then + local var_list = yl_speak_up.get_quest_variables(pname, true) + -- make names of own variables shorter + yl_speak_up.strip_pname_from_varlist(var_list, pname) + table.sort(var_list) + local index = table.indexof(var_list, fields.list_var_names) + if(fields.list_var_names == "Add variable:") then + index = 0 + end + if(index and index > -1) then + yl_speak_up.speak_to[pname].tmp_index_variable = index + 1 + end + -- show the same formspec again, with a diffrent variable selected + yl_speak_up.show_fs(player, "manage_variables") + return end -- try to go back to the last formspec shown before this one if(not(yl_speak_up.speak_to[pname])) then @@ -202,13 +249,44 @@ end yl_speak_up.get_fs_manage_variables = function(player, param) - return "size[12,4]".. + local pname = player:get_player_name() + -- variables owned by the player - including those with write access + local var_list = yl_speak_up.get_quest_variables(pname, true) + -- make names of own variables shorter + yl_speak_up.strip_pname_from_varlist(var_list, pname) + -- the yl_speak_up.create_dropdown_playerlist function needs a table - not a list + local table_of_vars = {} + for i, k in ipairs(var_list) do + table_of_vars[ k ] = true + end + -- "Add variable:" is currently selected + local additional_buttons = "" + if(not(yl_speak_up.speak_to[pname].tmp_index_variable) + or yl_speak_up.speak_to[pname].tmp_index_variable == 1) then + yl_speak_up.speak_to[pname].tmp_index_variable = 1 + additional_buttons = "button[11.4,1.9;2.5,0.9;add_variable;Create variable]".. + "tooltip[add_variable;Create a new varialbe with the name\n".. + "you entered in the field to the left.]" + else + additional_buttons = "button[11.4,1.9;2.5,0.9;show_var_usage;Where is it used?]".. + "tooltip[show_var_usage;Show which NPC use this variable in which context.]" + end + return "size[14,4]".. "label[2.0,-0.2;* Manage your variables *]".. - "label[0.2,1.0;Create this new variable:]".. - "field[3.7,1.3;6.0,0.6;add_variable_name;;]".. - "button[9.4,1.0;2.5,0.6;add_variable;Create variable]".. - "tooltip[add_variable;Enter the name of your new variable.]".. - -- TODO: delete variable + "label[0.2,2.05;Your variables:]".. + -- offer a dropdown list and a text input field for new varialbe names for adding + yl_speak_up.create_dropdown_playerlist(player, pname, + --table_of_names, yl_speak_up.speak_to[pname].tmp_index, + table_of_vars, yl_speak_up.speak_to[pname].tmp_index_variable, + 2.2, 1.9, 1.0, "list_var_names", "variable", "Delete selected variable", + "add_variable_name", + "Enter the name of the new variable you\n".. + "want to create.", + "delete_variable", + "If you click here, the selected variable\n".. + "will be deleted." + ).. + additional_buttons.. "button[2.0,3.5;1.0,0.6;back;Back]" end