created yl_speak_up.input_fs_manage_general and used for variable management input

This commit is contained in:
Sokomine 2022-11-14 00:06:40 +01:00
parent f982b44c97
commit 3f458bf448
2 changed files with 188 additions and 123 deletions

View File

@ -44,6 +44,140 @@ end
-- manages back, exit, prev, next, add_list_entry, del_entry_general
--
-- if a new entry is to be added, the following function that is passed as a parmeter
-- is called:
-- function_add_new_entry(pname, fields.add_entry_general)
-- expected return value: index of fields.add_entry_general in the new list
--
-- if an entry is to be deleted, the following function that is passed as a parameter
-- is called:
-- function_del_old_entry(pname, entry_name)
-- expected return value: text describing weather the removal worked or not
--
-- if any other fields are set that this function does not process, the following
-- function that is passed on as a parameter can be used:
-- function_input_check_fields(player, formname, fields, entry_name, list_of_entries)
-- expected return value: nil if the function found work; else entry_name
--
yl_speak_up.input_fs_manage_general = function(player, formname, fields,
what_is_the_list_about, min_length, max_length, function_add_new_entry,
list_of_entries, function_del_old_entry, function_input_check_fields)
local pname = player:get_player_name()
local what = minetest.formspec_escape(what_is_the_list_about or "?")
local fs_name = formname
if(formname and string.sub(formname, 0, 12) == "yl_speak_up:") then
formname = string.sub(formname, 13)
end
if(fields and fields.back_from_msg) then
yl_speak_up.show_fs(player, formname, fields.stored_value_for_player)
return
end
-- 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_general = nil
yl_speak_up.show_fs(player, last_fs)
return
-- add a new entry?
elseif(fields and fields.add_list_entry) then
local error_msg = ""
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 the "..what.." you want to create!"
-- limit names to something more sensible
elseif(string.len(fields.add_entry_general) > max_length) then
error_msg = "The name of your new "..what.." is too long.\n"..
"Only up to "..tostring(max_length).." characters are allowed."
elseif(string.len(fields.add_entry_general:trim()) < min_length) then
error_msg = "The name of your new "..what.." is too short.\n"..
"It has to be at least "..tostring(min_length).." characters long."
elseif(table.indexof(list_of_entries, fields.list_of_entries:trim()) > 0) then
error_msg = "A "..what.." with that name exists already."
else
fields.add_entry_general = fields.add_entry_general:trim()
-- this depends on what is created
local res = function_add_new_entry(pname, fields.add_entry_general)
-- not really an error msg here - but fascilitates output
error_msg = "A new "..what.." named\n \""..
minetest.formspec_escape(fields.add_entry_general)..
"\"\nhas been created."
if(not(res) or res == -1) then
error_msg = "Failed to create "..what.." named\n \""..
minetest.formspec_escape(fields.add_entry_general).."\"."
else
-- select this new entry (add 1 because the first entry of our
-- list is adding a new entry)
yl_speak_up.speak_to[pname].tmp_index_general = res + 1
end
end
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formname,
formspec = "size[6,2]"..
"label[0.2,0.0;"..error_msg.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- scroll through the variables with prev/next buttons
elseif(fields and (fields["prev"] or fields["next"])) then
local index = yl_speak_up.speak_to[pname].tmp_index_general
if(not(index)) then
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_general = index - 1
elseif(fields["next"] and index <= #list_of_entries) then
yl_speak_up.speak_to[pname].tmp_index_general = index + 1
end
yl_speak_up.show_fs(player, formname, fields.stored_value_for_player)
return
end
-- an entry was selected in the dropdown list
if(fields and fields.list_of_entries and fields.list_of_entries ~= "") then
local index = table.indexof(list_of_entries, fields.list_of_entries)
if(fields.list_of_entries == "Add "..what..":") then
index = 0
end
if(index and index > -1) then
yl_speak_up.speak_to[pname].tmp_index_general = index + 1
end
end
local entry_name = list_of_entries[ yl_speak_up.speak_to[pname].tmp_index_general - 1]
-- delete entry
if(fields and ((fields.del_entry_general and fields.del_entry_general ~= ""))
and entry_name and entry_name ~= "") then
local text = function_del_old_entry(pname, entry_name)
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:"..formname,
formspec = "size[10,2]"..
"label[0.2,0.0;Trying to delete "..what.." \""..
minetest.formspec_escape(tostring(entry_name))..
"\":\n"..text.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- maybe the custom function knows what to do with this
elseif(fields
and not(function_input_check_fields(player, formname, fields, entry_name, list_of_entries))) then
-- the function_input_check_fields managed to handle this input
return
-- an entry was selected in the dropdown list
elseif(entry_name and entry_name ~= "") then
-- show the same formspec again, with a diffrent variable selected
yl_speak_up.show_fs(player, formname)
return
end
-- try to go back to the last formspec shown before this one
if(not(yl_speak_up.speak_to[pname])) then
return
end
local last_fs = yl_speak_up.speak_to[pname][ "working_at" ]
yl_speak_up.show_fs(player, last_fs)
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)

View File

@ -1,56 +1,41 @@
yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
local pname = player:get_player_name()
if(fields and fields.back_from_msg) then
yl_speak_up.show_fs(player, "manage_variables", fields.stored_value_for_player)
return
-- helper functions for yl_speak_up.input_fs_manage_variables(..)
-- returns the index of the new variable
yl_speak_up.input_fs_manage_variables_add_new_entry = function(pname, entry_name)
local res = yl_speak_up.add_quest_variable(pname, entry_name)
if(not(res)) then
return -1
end
-- 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_general = nil
yl_speak_up.show_fs(player, last_fs)
return
-- add a new variable?
elseif(fields and fields.add_list_entry) then
local error_msg = ""
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_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_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_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_entry_general)..
"\"\nhas been created."
if(not(res)) then
error_msg = "Failed to create variable named\n \""..
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_entry_general)
if(index and index > -1) then
yl_speak_up.speak_to[pname].tmp_index_general = index + 1
end
end
end
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)
return table.indexof(var_list, entry_name)
end
-- helper functions for yl_speak_up.input_fs_manage_variables(..)
-- returns a text describing if deleting the variable worked
yl_speak_up.input_fs_manage_variables_del_old_entry = function(pname, entry_name)
-- delete (empty) variable
return yl_speak_up.del_quest_variable(pname, entry_name, nil)
end
-- helper functions for yl_speak_up.input_fs_manage_variables(..)
-- implements all the functions that are specific to managing variables and not part of
-- general item management
yl_speak_up.input_fs_manage_variables_check_fields = function(player, formname, fields, var_name, list_of_entries)
local pname = player:get_player_name()
if(not(var_name)) then
var_name = ""
end
local var_name_with_prefix = yl_speak_up.restore_complete_var_name(var_name, pname)
-- show all stored values for a variable in a table
if(fields and fields.show_stored_values_for_var and var_name) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = "size[6,2]"..
"label[0.2,0.0;"..error_msg.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
formspec = yl_speak_up.fs_show_all_var_values(player, pname, var_name)
})
return
-- show where this variable is used
elseif(fields and fields.show_var_usage and fields.show_var_usage ~= "") then
@ -64,52 +49,6 @@ yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
false)
})
return
-- scroll through the variables with prev/next buttons
elseif(fields and (fields["prev"] or fields["next"])) 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)
local index = yl_speak_up.speak_to[pname].tmp_index_general
if(not(index)) then
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_general = index - 1
elseif(fields["next"] and index <= #var_list) then
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
end
-- which variable is currently selected?
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)
-- a var name was selected in the dropdown list
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_general = index + 1
end
end
local var_name = var_list[ yl_speak_up.speak_to[pname].tmp_index_general - 1]
if(not(var_name)) then
var_name = ""
end
local var_name_with_prefix = yl_speak_up.restore_complete_var_name(var_name, pname)
-- show all stored values for a variable in a table
if(fields and fields.show_stored_values_for_var and var_name) then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = yl_speak_up.fs_show_all_var_values(player, pname, var_name)
})
return
-- enable, disable and list variables in debug mode
elseif(fields and fields.enable_debug_mode and var_name) then
yl_speak_up.set_variable_metadata(var_name, pname, "debug", pname, true)
@ -205,22 +144,6 @@ yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
").]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- delete (empty) variable
elseif(fields
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)
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = "size[10,2]"..
"label[0.2,0.0;Trying to delete variable \""..
minetest.formspec_escape(tostring(var_name))..
"\":\n"..text.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- revoke read or write access to a variable
elseif(fields
and ((fields.revoke_player_var_read_access and fields.revoke_player_var_read_access ~= "")
@ -336,19 +259,27 @@ yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
end
yl_speak_up.show_fs(player, "manage_variables")
return
end
-- this function didn't have anything to do
return "NOTHING FOUND"
end
-- a var name was selected in the dropdown list
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
end
-- try to go back to the last formspec shown before this one
if(not(yl_speak_up.speak_to[pname])) then
return
end
local last_fs = yl_speak_up.speak_to[pname][ "working_at" ]
yl_speak_up.show_fs(player, last_fs)
-- makes use of yl_speak_up.input_fs_manage_general and is thus pretty short
yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
local pname = player:get_player_name()
local list_of_entries = yl_speak_up.get_quest_variables(pname, true)
-- make names of own variables shorter
yl_speak_up.strip_pname_from_varlist(list_of_entries, pname)
table.sort(list_of_entries)
local res = yl_speak_up.input_fs_manage_general(player, formname, fields,
-- what_is_the_list_about, min_length, max_length, function_add_new_entry,
"variable", 2, 30,
yl_speak_up.input_fs_manage_variables_add_new_entry,
list_of_entries,
yl_speak_up.input_fs_manage_variables_del_old_entry,
yl_speak_up.input_fs_manage_variables_check_fields)
end