name support for configure tool

This commit is contained in:
BuckarooBanzay 2024-02-25 19:12:02 +01:00
parent c89ab557a0
commit a88026d137
4 changed files with 45 additions and 14 deletions

View File

@ -16,7 +16,7 @@ local function get_outer_corners(pos1, pos2)
}
end
function pick_and_place.configure(pos1, pos2)
function pick_and_place.configure(pos1, pos2, name)
pos1, pos2 = pick_and_place.sort_pos(pos1, pos2)
for _, cpos in ipairs(get_outer_corners(pos1, pos2)) do
@ -31,6 +31,8 @@ function pick_and_place.configure(pos1, pos2)
meta:set_string("pos1", minetest.pos_to_string(rel_pos1))
meta:set_string("pos2", minetest.pos_to_string(rel_pos2))
meta:set_string("name", name)
meta:set_string("infotext", name)
end
end
end

View File

@ -1,7 +1,11 @@
local FORMSPEC_NAME = "pick_and_place:configure"
-- playername -> pos (if pos1 selected)
local pos1 = {}
-- playername -> pos (if pos2 selected)
local pos2 = {}
minetest.register_tool("pick_and_place:configure", {
description = "Placement configuration tool",
inventory_image = "pick_and_place.png^[colorize:#ffffff",
@ -13,9 +17,15 @@ minetest.register_tool("pick_and_place:configure", {
if pos1[playername] then
-- second position selected
-- configure and unmark
pick_and_place.configure(pos1[playername], pointed_pos)
pos1[playername] = nil
pos2[playername] = pointed_pos
-- show name input
minetest.show_formspec(playername, FORMSPEC_NAME, [[
size[10,1]
real_coordinates[true]
field[0.1,0.1;7,0.8;name;Name;]
button_exit[7.1,0.1;2.5,0.8;save;Save]
]])
else
-- first position selected
pos1[playername] = pointed_pos
@ -29,16 +39,31 @@ minetest.register_tool("pick_and_place:configure", {
local playername = player:get_player_name()
local pointed_pos = pick_and_place.get_pointed_position(player)
if pos1[playername] then
-- first position already selected
pick_and_place.show_preview(playername, "pick_and_place.png", "#ffffff", pointed_pos, pos1[playername])
else
-- nothing selected yet
pick_and_place.show_preview(playername, "pick_and_place.png", "#ffffff", pointed_pos)
end
-- update preview
pick_and_place.show_preview(playername, "pick_and_place.png", "#ffffff", pointed_pos, pos1[playername])
end,
on_deselect = function(_, player)
local playername = player:get_player_name()
pick_and_place.clear_preview(playername)
end
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= FORMSPEC_NAME then
return false
end
if not fields.save and not fields.key_enter_field then
return false
end
local playername = player:get_player_name()
if not pos1[playername] or not pos2[playername] then
return false
end
-- configure and unmark
pick_and_place.configure(pos1[playername], pos2[playername], fields.name)
pos1[playername] = nil
pos2[playername] = nil
end)

View File

@ -1,4 +1,4 @@
function pick_and_place.create_tool(pos1, pos2)
function pick_and_place.create_tool(pos1, pos2, name)
local size = vector.add(vector.subtract(pos2, pos1), 1)
local tool = ItemStack("pick_and_place:place 1")
@ -9,7 +9,10 @@ function pick_and_place.create_tool(pos1, pos2)
local schematic = pick_and_place.serialize(pos1, pos2)
tool_meta:set_string("schematic", schematic)
local desc = string.format("Placement tool (%d bytes, size: %s)", #schematic, minetest.pos_to_string(size))
local desc = string.format(
"Placement tool '%s' (%d bytes, size: %s)",
name or "", #schematic, minetest.pos_to_string(size)
)
tool_meta:set_string("description", desc)
return tool

View File

@ -9,12 +9,13 @@ local function on_rightclick(pos, _, _, itemstack)
-- relative positions
local rel_pos1 = minetest.string_to_pos(meta:get_string("pos1"))
local rel_pos2 = minetest.string_to_pos(meta:get_string("pos2"))
local name = meta:get_string("name")
-- absolute positions
local pos1 = vector.add(pos, rel_pos1)
local pos2 = vector.add(pos, rel_pos2)
return pick_and_place.create_tool(pos1, pos2)
return pick_and_place.create_tool(pos1, pos2, name)
end