created function yl_speak_up.get_fs_manage_general for scrolling through variables, quest and quest steps (in the future)

This commit is contained in:
Sokomine 2022-11-13 21:58:11 +01:00
parent 71f9e5e540
commit f982b44c97
2 changed files with 157 additions and 111 deletions

View File

@ -41,3 +41,72 @@ yl_speak_up.create_dropdown_playerlist = function(player, pname,
end
return text
end
-- inserts buttons into formspec which allow to select previous/next entry, to go back,
-- create new entries, delete entries and select entries from a dropdown menu;
-- returns the currently selected entry or nil (=create new entry)
-- Note: Designed for a formspec of size "size[18,12]"
yl_speak_up.get_fs_manage_general = function(player, param,
formspec, list_of_entries,
text_add_new, tooltip_add_new,
what_is_the_list_about,
tooltip_add_entry_general, tooltip_del_entry_general)
local selected = nil
local pname = player:get_player_name()
-- the yl_speak_up.create_dropdown_playerlist function needs a table - not a list
local table_of_entries = {}
for i, k in ipairs(list_of_entries) do
table_of_entries[ k ] = true
end
-- "Add variable:" is currently selected
if(not(yl_speak_up.speak_to[pname].tmp_index_general)
or yl_speak_up.speak_to[pname].tmp_index_general == 1
or not(list_of_entries[ yl_speak_up.speak_to[pname].tmp_index_general - 1])) then
yl_speak_up.speak_to[pname].tmp_index_general = 1
table.insert(formspec, "button[12.2,2.15;2.5,0.6;add_list_entry;")
table.insert(formspec, minetest.formspec_escape(text_add_new))
table.insert(formspec, "]")
table.insert(formspec, "tooltip[add_list_entry;")
table.insert(formspec, minetest.formspec_escape(tooltip_add_new))
table.insert(formspec, "]")
else
-- index 1 is "Add variable:"
selected = list_of_entries[ yl_speak_up.speak_to[pname].tmp_index_general - 1]
end
if(yl_speak_up.speak_to[pname].tmp_index_general > 1) then
table.insert(formspec, "button[4.0,0.2;2.0,0.6;prev;< Prev]"..
"button[4.0,11.0;2.0,0.6;prev;< Prev]")
end
if(yl_speak_up.speak_to[pname].tmp_index_general <= #list_of_entries) then
table.insert(formspec, "button[12.0,0.2;2.0,0.6;next;Next >]"..
"button[12.0,11.0;2.0,0.6;next;Next >]")
end
table.insert(formspec, "button[0.0,0.2;2.0,0.6;back;Back]"..
"button[8.0,11.0;2.0,0.6;back;Back]")
local what = minetest.formspec_escape(what_is_the_list_about)
table.insert(formspec, "label[7.0,0.4;* Manage your ")
table.insert(formspec, what)
table.insert(formspec, "s *]")
table.insert(formspec, "label[0.2,2.45;Your ")
table.insert(formspec, what)
table.insert(formspec, ":]")
-- offer a dropdown list and a text input field for new varialbe names for adding
table.insert(formspec, yl_speak_up.create_dropdown_playerlist(
player, pname,
table_of_entries,
yl_speak_up.speak_to[pname].tmp_index_general,
2.6, 2.15, 1.0, 0.6,
"list_of_entries",
what,
"Delete selected "..what,
"add_entry_general",
minetest.formspec_escape(tooltip_add_entry_general),
"del_entry_general",
minetest.formspec_escape(tooltip_del_entry_general)
))
-- either nil or the text of the selected entry
return selected
end

View File

@ -8,41 +8,41 @@ yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
-- leave this formspec
if(fields and (fields.quit or fields.exit or fields.back)) then
local last_fs = yl_speak_up.speak_to[pname][ "working_at" ]
yl_speak_up.speak_to[pname].tmp_index_variable = nil
yl_speak_up.speak_to[pname].tmp_index_general = nil
yl_speak_up.show_fs(player, last_fs)
return
-- add a new variable?
elseif(fields and fields.add_variable) then
elseif(fields and fields.add_list_entry) then
local error_msg = ""
if(not(fields.add_variable_name) or fields.add_variable_name == ""
or fields.add_variable_name:trim() == "") then
if(not(fields.add_entry_general) or fields.add_entry_general == ""
or fields.add_entry_general:trim() == "") then
error_msg = "Please enter the name of your variable!"
-- limit names to something more sensible
elseif(string.len(fields.add_variable_name) > 30) then
elseif(string.len(fields.add_entry_general) > 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
elseif(string.len(fields.add_entry_general: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)
fields.add_entry_general = fields.add_entry_general:trim()
local res = yl_speak_up.add_quest_variable(pname, fields.add_entry_general)
-- not really an error msg here - but fascilitates output
error_msg = "A new variable named\n \""..
minetest.formspec_escape(fields.add_variable_name)..
minetest.formspec_escape(fields.add_entry_general)..
"\"\nhas been created."
if(not(res)) then
error_msg = "Failed to create variable named\n \""..
minetest.formspec_escape(fields.add_variable_name).."\"."
minetest.formspec_escape(fields.add_entry_general).."\"."
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)
local index = table.indexof(var_list, fields.add_entry_general)
if(index and index > -1) then
yl_speak_up.speak_to[pname].tmp_index_variable = index + 1
yl_speak_up.speak_to[pname].tmp_index_general = index + 1
end
end
end
@ -57,7 +57,7 @@ yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = yl_speak_up.fs_get_list_of_usage_of_variable(
fields.list_var_names, pname, true,
fields.list_of_entries, pname, true,
"back_from_msg",
"Back to manage variables",
-- not an internal variable
@ -69,13 +69,13 @@ yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
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)
local index = yl_speak_up.speak_to[pname].tmp_index_variable
local index = yl_speak_up.speak_to[pname].tmp_index_general
if(not(index)) then
yl_speak_up.speak_to[pname].tmp_index_variable = 1
yl_speak_up.speak_to[pname].tmp_index_general = 1
elseif(fields["prev"] and index > 1) then
yl_speak_up.speak_to[pname].tmp_index_variable = index - 1
yl_speak_up.speak_to[pname].tmp_index_general = index - 1
elseif(fields["next"] and index <= #var_list) then
yl_speak_up.speak_to[pname].tmp_index_variable = index + 1
yl_speak_up.speak_to[pname].tmp_index_general = index + 1
end
yl_speak_up.show_fs(player, "manage_variables", fields.stored_value_for_player)
return
@ -87,16 +87,16 @@ yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
yl_speak_up.strip_pname_from_varlist(var_list, pname)
table.sort(var_list)
-- a var name was selected in the dropdown list
if(fields and fields.list_var_names and fields.list_var_names ~= "") then
local index = table.indexof(var_list, fields.list_var_names)
if(fields.list_var_names == "Add variable:") then
if(fields and fields.list_of_entries and fields.list_of_entries ~= "") then
local index = table.indexof(var_list, fields.list_of_entries)
if(fields.list_of_entries == "Add variable:") then
index = 0
end
if(index and index > -1) then
yl_speak_up.speak_to[pname].tmp_index_variable = index + 1
yl_speak_up.speak_to[pname].tmp_index_general = index + 1
end
end
local var_name = var_list[ yl_speak_up.speak_to[pname].tmp_index_variable - 1]
local var_name = var_list[ yl_speak_up.speak_to[pname].tmp_index_general - 1]
if(not(var_name)) then
var_name = ""
end
@ -208,7 +208,7 @@ yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
-- delete (empty) variable
elseif(fields
and ((fields.delete_variable and fields.delete_variable ~= "")
and ((fields.del_entry_general and fields.del_entry_general ~= "")
or (fields.delete_unused_variable and fields.delete_unused_variable ~= ""))
and var_name) then
local text = yl_speak_up.del_quest_variable(pname, var_name, nil)
@ -338,7 +338,7 @@ yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
return
-- a var name was selected in the dropdown list
elseif(fields and fields.list_var_names and fields.list_var_names ~= "") then
elseif(fields and fields.list_of_entries and fields.list_of_entries ~= "") then
-- show the same formspec again, with a diffrent variable selected
yl_speak_up.show_fs(player, "manage_variables")
return
@ -358,43 +358,43 @@ yl_speak_up.get_fs_manage_variables = function(player, param)
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
or not(var_list[ yl_speak_up.speak_to[pname].tmp_index_variable - 1])) then
yl_speak_up.speak_to[pname].tmp_index_variable = 1
additional_buttons = "button[12.2,2.15;2.5,0.6;add_variable;Create variable]"..
"tooltip[add_variable;Create a new varialbe with the name\n"..
"you entered in the field to the left.]"
else
local formspec = {}
table.insert(formspec, "size[18,12]"..
"label[0.2,1.2;Note: Each variable will store a diffrent value for each "..
"player who interacts with the NPC.\n"..
"You can grant read and write access to other players for your "..
"variables so that they can also use them as well.]")
local selected = yl_speak_up.get_fs_manage_general(player, param,
formspec, var_list,
"Create variable",
"Create a new varialbe with the name\n"..
"you entered in the field to the left.",
"variable",
"Enter the name of the new variable you\n"..
"want to create.",
"If you click here, the selected variable\n"..
"will be deleted.")
if(selected and selected ~= "") then
local k = selected
-- index 1 is "Add variable:"
local k = var_list[ yl_speak_up.speak_to[pname].tmp_index_variable - 1]
local pl_with_read_access = yl_speak_up.get_access_list_for_var(k, pname, "read_access")
local pl_with_write_access = yl_speak_up.get_access_list_for_var(k, pname, "write_access")
local add_read_button = ""
local add_write_button = ""
if(not(yl_speak_up.speak_to[pname].tmp_index_var_read_access)
or yl_speak_up.speak_to[pname].tmp_index_var_read_access == 1) then
yl_speak_up.speak_to[pname].tmp_index_var_read_access = 1
add_read_button = "button[14.6,2.95;1.0,0.6;add_read_access;Add]"..
"tooltip[add_read_access;Grant the player whose name you entered\n"..
table.insert(formspec, "button[14.6,2.95;1.0,0.6;add_read_access;Add]"..
"tooltip[add_read_access;Grant the player whose name you entered\n"..
"you entered in the field to the left read access\n"..
"to your variable.]"
"to your variable.]")
end
if(not(yl_speak_up.speak_to[pname].tmp_index_var_write_access)
or yl_speak_up.speak_to[pname].tmp_index_var_write_access == 1) then
yl_speak_up.speak_to[pname].tmp_index_var_write_access = 1
add_write_button = "button[14.6,3.95;1.0,0.6;add_write_access;Add]"..
table.insert(formspec, "button[14.6,3.95;1.0,0.6;add_write_access;Add]"..
"tooltip[add_write_access;Grant the player whose name you entered\n"..
"you entered in the field to the left *write* access\n"..
"to your variable.]"
"to your variable.]")
end
local list_of_npc_users = "- none -"
local list_of_node_pos_users = "- none -"
@ -418,43 +418,48 @@ yl_speak_up.get_fs_manage_variables = function(player, param)
node_pos_users, ", "))
end
end
local debug_button = "button[10.0,10.05;4.0,0.6;list_debug_mode;What am I debugging?]"..
table.insert(formspec, "button[10.0,10.05;4.0,0.6;list_debug_mode;What am I debugging?]"..
"tooltip[list_debug_mode;"..
"Show for which variables you currently have "..
"\nactivated the debug mode.]"
"\nactivated the debug mode.]")
local debuggers = yl_speak_up.get_variable_metadata(k_long, "debug")
local i = table.indexof(debuggers, pname)
if(i and i > 0) then
debug_button = "button[5.0,10.05;4.0,0.6;disable_debug_mode;Deactivate debug mode]"..
table.insert(formspec,
"button[5.0,10.05;4.0,0.6;disable_debug_mode;Deactivate debug mode]"..
"tooltip[disable_debug_mode;"..
"You will no longer receive a chat message "..
"\nwhen this value changes for a player.]"..debug_button
"\nwhen this value changes for a player.]")
else
debug_button = "button[5.0,10.05;4.0,0.6;enable_debug_mode;Activate debug mode]"..
table.insert(formspec,
"button[5.0,10.05;4.0,0.6;enable_debug_mode;Activate debug mode]"..
"tooltip[enable_debug_mode;"..
"You will receive a chat message whenever the value "..
"\nof this variable changes for one player. The debug\n"..
"messages will be sent even after relogin.]"..debug_button
"messages will be sent even after relogin.]")
end
-- checking/changing debug value for one specific player
if(not(param)) then
param = ""
end
debug_button = debug_button..
table.insert(formspec,
"label[0.2,8.05;Show stored value for player:]"..
"field[4.9,7.75;4.0,0.6;stored_value_for_player;;"..
minetest.formspec_escape(param).."]"..
"field[4.9,7.75;4.0,0.6;stored_value_for_player;;")
table.insert(formspec, minetest.formspec_escape(param))
table.insert(formspec, "]")
table.insert(formspec,
"button[9.0,7.75;4.5,0.6;show_stored_value_for_player;Show value for this player]"..
"tooltip[stored_value_for_player;Enter the name of the player for which you\n"..
"want to check (or change) the stored value.]"..
"tooltip[show_stored_value_for_player;Click here to read and the current value"..
"\nstored for this player.]"
"\nstored for this player.]")
if(param and param ~= "") then
local v = yl_speak_up.get_quest_variable_value(param, k_long) or ""
debug_button = debug_button..
table.insert(formspec,
"label[0.2,9.05;Found stored value:]"..
"field[4.9,8.75;4.0,0.6;current_value_for_player;;"..
minetest.formspec_escape(v).."]"..
"field[4.9,8.75;4.0,0.6;current_value_for_player;;")
table.insert(formspec, minetest.formspec_escape(v))
table.insert(formspec, "]"..
"tooltip[current_value_for_player;You can see and change the current "..
"value here.]"..
"button[9.0,8.75;4.5,0.6;store_new_value_for_player;"..
@ -467,13 +472,13 @@ yl_speak_up.get_fs_manage_variables = function(player, param)
"Remove this entry]"..
"tooltip[unset_value_for_player;Click here to delete the entry for this "..
"player.\nSetting the entry to an empty string would not be "..
"the same!]"
"the same!]")
end
additional_buttons = "button[12.2,2.15;3.0,0.6;show_var_usage;Where is it used?]"..
table.insert(formspec, "button[12.2,2.15;3.0,0.6;show_var_usage;Where is it used?]"..
"tooltip[show_var_usage;Show which NPC use this variable in which context.]"..
-- offer a dropdown list and a text input field for new varialbe names for adding
"label[0.2,3.25;Players with read access to this variable:]"..
yl_speak_up.create_dropdown_playerlist(player, pname,
"label[0.2,3.25;Players with read access to this variable:]")
table.insert(formspec, yl_speak_up.create_dropdown_playerlist(player, pname,
pl_with_read_access,
yl_speak_up.speak_to[pname].tmp_index_var_read_access,
6.9, 2.95, 0.0, 0.6, "list_var_read_access", "player",
@ -484,10 +489,9 @@ yl_speak_up.get_fs_manage_variables = function(player, param)
"revoke_player_var_read_access",
"If you click here, the selected player\n"..
"will no longer be able to add new\n"..
"pre(C)onditions which read your variable."
)..add_read_button..
"label[0.2,4.25;Players with *write* access to this variable:]"..
yl_speak_up.create_dropdown_playerlist(player, pname,
"pre(C)onditions which read your variable."))
table.insert(formspec, "label[0.2,4.25;Players with *write* access to this variable:]")
table.insert(formspec, yl_speak_up.create_dropdown_playerlist(player, pname,
pl_with_write_access,
yl_speak_up.speak_to[pname].tmp_index_var_write_access,
6.9, 3.95, 0.0, 0.6,
@ -498,56 +502,29 @@ yl_speak_up.get_fs_manage_variables = function(player, param)
"revoke_player_var_write_access",
"If you click here, the selected player\n"..
"will no longer be able to *write* new\n"..
"values into this variable."
)..add_write_button..
"label[0.2,5.05;Type of variable: "..
-- show variable type
"values into this variable."))
table.insert(formspec, "label[0.2,5.05;Type of variable: ")
table.insert(formspec, -- show variable type
minetest.colorize("#FFFF00",
(yl_speak_up.get_variable_metadata(k_long, "var_type")
or "String/text or numerical value, depending on how you use it"))..
".]"..
"label[0.2,6.05;This variable is used by the following "..
or "String/text or numerical value, depending on how you use it")))
table.insert(formspec, ".]")
table.insert(formspec, "label[0.2,6.05;This variable is used by the following ")
table.insert(formspec,
minetest.colorize("#FFFF00", tostring(c1)).." NPC:\n\t"..
-- those are only n_id - no need to formspec_escape that
minetest.colorize("#FFFF00", list_of_npc_users)..".]"..
"label[0.2,7.05;This variable is used by the following "..
minetest.colorize("#FFFF00", list_of_npc_users))
table.insert(formspec, ".]")
table.insert(formspec, "label[0.2,7.05;This variable is used by the following ")
table.insert(formspec,
minetest.colorize("#FFFF00", tostring(c2)).." node positions:\n\t"..
-- those are only pos_to_string(..) - no need to formspec_escape that
minetest.colorize("#FFFF00", list_of_node_pos_users)..".]"..
minetest.colorize("#FFFF00", list_of_node_pos_users))
table.insert(formspec, ".]")
table.insert(formspec,
"button[0.2,10.05;4.0,0.6;show_stored_values_for_var;Show all stored values]"..
"tooltip[show_stored_values_for_var;A diffrent value can be stored for each "..
"player.\nShow these values in a table.]"..
debug_button
"player.\nShow these values in a table.]")
end
if(yl_speak_up.speak_to[pname].tmp_index_variable > 1) then
additional_buttons = additional_buttons..
"button[4.0,0.2;2.0,0.6;prev;< Prev]"..
"button[4.0,11.0;2.0,0.6;prev;< Prev]"
end
if(yl_speak_up.speak_to[pname].tmp_index_variable <= #var_list) then
additional_buttons = additional_buttons..
"button[12.0,0.2;2.0,0.6;next;Next >]"..
"button[12.0,11.0;2.0,0.6;next;Next >]"
end
return "size[18,12]"..
"label[7.0,0.4;* Manage your variables *]"..
"label[0.2,1.2;Note: Each variable will store a diffrent value for each player who "..
"interacts with the NPC.\n"..
"You can grant read and write access to other players for your "..
"variables so that they can also use them as well.]"..
"label[0.2,2.45;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_vars, yl_speak_up.speak_to[pname].tmp_index_variable,
2.6, 2.15, 1.0, 0.6, "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[0.0,0.2;2.0,0.6;back;Back]"..
"button[8.0,11.0;2.0,0.6;back;Back]"
return table.concat(formspec, "")
end