made name and description editable after initial config
This commit is contained in:
		
							parent
							
								
									726a6cbcf2
								
							
						
					
					
						commit
						e8ce4b1c23
					
				@ -1051,7 +1051,7 @@ end
 | 
			
		||||
 | 
			
		||||
-- initialize the npc without having to use a staff;
 | 
			
		||||
-- returns true when initialization possible
 | 
			
		||||
local function get_fs_initial_config(player, n_id, d_id)
 | 
			
		||||
local function get_fs_initial_config(player, n_id, d_id, is_initial_config)
 | 
			
		||||
	local pname = player:get_player_name()
 | 
			
		||||
 | 
			
		||||
	-- is the player allowed to edit this npc?
 | 
			
		||||
@ -1061,15 +1061,25 @@ local function get_fs_initial_config(player, n_id, d_id)
 | 
			
		||||
		return get_error_message()
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	local tmp_name = n_id
 | 
			
		||||
	local tmp_descr = "A new NPC without description"
 | 
			
		||||
	local tmp_text = "Please provide your new NPC with a name and description!"
 | 
			
		||||
	-- use existing name and description as presets when just editing
 | 
			
		||||
	if(not(is_initial_config)) then
 | 
			
		||||
		local dialog = yl_speak_up.speak_to[pname].dialog
 | 
			
		||||
		tmp_name = dialog.n_npc
 | 
			
		||||
		tmp_descr = dialog.n_description
 | 
			
		||||
		tmp_text = "You can change the name and description of your NPC."
 | 
			
		||||
	end
 | 
			
		||||
	local formspec = "size[10,4]"..
 | 
			
		||||
		"label[0.2,0.2;Please provide your new NPC with a name and description!]"..
 | 
			
		||||
		"label[0.2,0.2;"..tmp_text.."]"..
 | 
			
		||||
		-- name of the npc
 | 
			
		||||
		"label[0.2,1.05;Name:]"..
 | 
			
		||||
		"field[2.0,1.2;4,0.9;n_npc;;"..minetest.formspec_escape(n_id).."]"..
 | 
			
		||||
		"field[2.0,1.2;4,0.9;n_npc;;"..minetest.formspec_escape(tmp_name).."]"..
 | 
			
		||||
		"tooltip[n_npc;n_npc: The name of the NPC;#FFFFFF;#000000]"..
 | 
			
		||||
		-- description of the npc
 | 
			
		||||
		"label[0.2,2.05;Description:]"..
 | 
			
		||||
		"field[2.0,2.2;8,0.9;n_description;;A new NPC without description]"..
 | 
			
		||||
		"field[2.0,2.2;8,0.9;n_description;;"..minetest.formspec_escape(tmp_descr).."]"..
 | 
			
		||||
		"tooltip[n_description;n_description: A description for the NPC;#FFFFFF;#000000]"..
 | 
			
		||||
		-- save and exit buttons
 | 
			
		||||
		"button_exit[3.2,3.2;2,0.9;save_initial_config;Save]"..
 | 
			
		||||
@ -1134,7 +1144,8 @@ local function get_fs_talkdialog(player, n_id, d_id)
 | 
			
		||||
                    " talked to unconfigured NPC with ID n_" ..
 | 
			
		||||
                        n_id .. ", position of user was " .. minetest.pos_to_string(player:get_pos(), 0)
 | 
			
		||||
        )
 | 
			
		||||
	return get_fs_initial_config(player, n_id, d_id)
 | 
			
		||||
	-- this is the initial config
 | 
			
		||||
	return get_fs_initial_config(player, n_id, d_id, true)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    if c_d_id == nil then return get_error_message() end
 | 
			
		||||
@ -1227,7 +1238,6 @@ local function get_fs_talkdialog(player, n_id, d_id)
 | 
			
		||||
	local d_id_to_dropdown_index = {}
 | 
			
		||||
 | 
			
		||||
	-- display the window with the text the NPC is saying
 | 
			
		||||
	-- TODO: make name and description likewise editable?
 | 
			
		||||
	-- TODO: make name of dialog and sort option editable?
 | 
			
		||||
	if(edit_mode) then
 | 
			
		||||
 | 
			
		||||
@ -1267,6 +1277,9 @@ local function get_fs_talkdialog(player, n_id, d_id)
 | 
			
		||||
            table.insert(formspec, "button[13.9,4.0;1,0.9;show_new_dialog;+]")
 | 
			
		||||
            table.insert(formspec, "tooltip[show_new_dialog;Create a new dialog.]")
 | 
			
		||||
 | 
			
		||||
            -- allow to edit name and description of the NPC
 | 
			
		||||
            table.insert(formspec, "button[13.4,0.3;2,0.9;button_edit_name_and_description;Edit]")
 | 
			
		||||
	    table.insert(formspec, "tooltip[button_edit_name_and_description;Edit name and description of your NPC.]")
 | 
			
		||||
 | 
			
		||||
            table.insert(formspec, "textarea[0.2,5;19.6,17.8;d_text;;")
 | 
			
		||||
            table.insert(formspec, minetest.formspec_escape(active_dialog.d_text))
 | 
			
		||||
@ -2424,22 +2437,37 @@ minetest.register_on_player_receive_fields(
 | 
			
		||||
			minetest.chat_send_player(pname, "The name and description of your NPC cannot be longer than 40 characters.")
 | 
			
		||||
			return
 | 
			
		||||
		end
 | 
			
		||||
		local dialog = yl_speak_up.speak_to[pname].dialog
 | 
			
		||||
		local d_id = yl_speak_up.speak_to[pname].d_id
 | 
			
		||||
		local count = 0
 | 
			
		||||
		for k,v in pairs(dialog) do
 | 
			
		||||
			count = count + 1
 | 
			
		||||
		end
 | 
			
		||||
		-- give the NPC its first dialog
 | 
			
		||||
		local f = {}
 | 
			
		||||
		-- create a new dialog
 | 
			
		||||
		f.d_id = yl_speak_up.text_new_dialog_id
 | 
			
		||||
		-- ...with this text
 | 
			
		||||
		f.d_text = "Hi. I am "..tostring(fields.n_npc)..". I don't know much yet.\n"..
 | 
			
		||||
			"Hopefully "..tostring(pname).." will teach me to talk soon."
 | 
			
		||||
		-- it is the first, initial dialog
 | 
			
		||||
		f.d_sort = "0"
 | 
			
		||||
		f.n_npc = fields.n_npc
 | 
			
		||||
		f.n_description = fields.n_description
 | 
			
		||||
		f.npc_owner = yl_speak_up.npc_owner[ n_id ]
 | 
			
		||||
		-- create and save the first dialog for this npc
 | 
			
		||||
		local dialog = fields_to_dialog(pname, f)
 | 
			
		||||
		save_dialog(n_id, dialog)
 | 
			
		||||
		yl_speak_up.speak_to[pname].dialog = dialog
 | 
			
		||||
		if(not(dialog) or count==0) then
 | 
			
		||||
			local f = {}
 | 
			
		||||
			-- create a new dialog
 | 
			
		||||
			f.d_id = yl_speak_up.text_new_dialog_id
 | 
			
		||||
			-- ...with this text
 | 
			
		||||
			f.d_text = "Hi. I am "..tostring(fields.n_npc)..". I don't know much yet.\n"..
 | 
			
		||||
				"Hopefully "..tostring(pname).." will teach me to talk soon."
 | 
			
		||||
			-- it is the first, initial dialog
 | 
			
		||||
			f.d_sort = "0"
 | 
			
		||||
			f.n_npc = fields.n_npc
 | 
			
		||||
			f.n_description = fields.n_description
 | 
			
		||||
			f.npc_owner = yl_speak_up.npc_owner[ n_id ]
 | 
			
		||||
			-- create and save the first dialog for this npc
 | 
			
		||||
			local dialog = fields_to_dialog(pname, f)
 | 
			
		||||
			save_dialog(n_id, dialog)
 | 
			
		||||
			yl_speak_up.speak_to[pname].dialog = dialog
 | 
			
		||||
		-- just change name and description
 | 
			
		||||
		else
 | 
			
		||||
			dialog = yl_speak_up.speak_to[pname].dialog
 | 
			
		||||
			dialog.n_npc = fields.n_npc
 | 
			
		||||
			dialog.n_description = fields.n_description
 | 
			
		||||
			save_dialog(n_id, dialog)
 | 
			
		||||
		end
 | 
			
		||||
		dialog = yl_speak_up.speak_to[pname].dialog
 | 
			
		||||
 | 
			
		||||
		-- show nametag etc.
 | 
			
		||||
		if yl_speak_up.speak_to[pname].obj then
 | 
			
		||||
@ -2456,7 +2484,7 @@ minetest.register_on_player_receive_fields(
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		-- actually start a chat with our new npc
 | 
			
		||||
		minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, n_id, nil))
 | 
			
		||||
		minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_talkdialog(player, n_id, d_id))
 | 
			
		||||
		return
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
@ -2464,10 +2492,16 @@ minetest.register_on_player_receive_fields(
 | 
			
		||||
	local edit_mode = (yl_speak_up.edit_mode[pname] == yl_speak_up.speak_to[pname].n_id)
 | 
			
		||||
 | 
			
		||||
	-- if in edit mode: detect if something was changed;
 | 
			
		||||
	if(edit_mode) then
 | 
			
		||||
	if(edit_mode or fields.button_edit_name_and_description) then
 | 
			
		||||
	    yl_speak_up.edit_mode_apply_changes(pname, fields)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
	-- the player wants to change name and description; show the formspec
 | 
			
		||||
	if(edit_mode and fields.button_edit_name_and_description) then
 | 
			
		||||
		-- this is not the initial config - but the same formspec can be used
 | 
			
		||||
		minetest.show_formspec(pname, "yl_speak_up:talk", get_fs_initial_config(player, n_id, yl_speak_up.speak_to[pname].d_id, false))
 | 
			
		||||
		return
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	-- start edit mode (requires npc_talk_owner)
 | 
			
		||||
	if fields.button_start_edit_mode then
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user