1
0
mirror of https://github.com/mt-mods/signs_lib.git synced 2025-06-28 13:56:06 +02:00

Compare commits

..

6 Commits

Author SHA1 Message Date
bbd580acb1 remove debug prints 2019-09-18 13:57:57 -04:00
2d29a539f8 fix minor global warning 2019-09-18 13:55:19 -04:00
4b2abfadce centralize entity handling
minor re-factor as needed for that
don't store entities statically, let the LBM generate them.

this works around Minetest's reload-positioning inaccuracy at very large
positive or negative X/Z coords (tested -18000/+18000 at all four corners,
both new signs and after reboot)
2019-09-18 13:51:53 -04:00
ee2dc495c3 treat streets:streetlamp_basic_top_* as a type of post
as far as sign placement is concerned.
2019-09-16 22:46:26 -04:00
f944337b1c fix non-writable signs not fitting onto a pole/fence 2019-09-15 18:37:03 -04:00
a5e711d354 Use sign formspec to switch between wide/narrow font
if available on a given sign.  The sign must have a
"foo:bar_widefont" variant, with the horizontal scaling set
appropriately (about half of the normal value for the
narrow-font sign)

Add new corresponding API to anto-generate those signs where
desired; use this feature in default wood and steel signs
2019-09-15 16:10:11 -04:00
5 changed files with 189 additions and 160 deletions

347
api.lua
View File

@ -29,6 +29,8 @@ signs_lib.standard_steel_groups.attached_node = nil
signs_lib.standard_wood_sign_sounds = table.copy(minetest.registered_items["default:sign_wall_wood"].sounds)
signs_lib.standard_steel_sign_sounds = table.copy(minetest.registered_items["default:sign_wall_steel"].sounds)
signs_lib.default_text_scale = {x=10, y=10}
signs_lib.standard_yaw = {
0,
math.pi / -2,
@ -83,7 +85,57 @@ signs_lib.rotate_walldir = {
-- Initialize character texture cache
local ctexcache = {}
signs_lib.wallmounted_rotate = function(pos, node, user, mode)
-- entity handling
minetest.register_entity("signs_lib:text", {
collisionbox = { 0, 0, 0, 0, 0, 0 },
visual = "mesh",
mesh = "signs_lib_standard_wall_sign_entity.obj",
textures = {},
static_save = false
})
function signs_lib.delete_objects(pos)
local objects = minetest.get_objects_inside_radius(pos, 0.5)
for _, v in ipairs(objects) do
v:remove()
end
end
function signs_lib.spawn_entity(pos, texture)
local node = minetest.get_node(pos)
local def = minetest.registered_items[node.name]
if not def or not def.entity_info or not def.entity_info.yaw[node.param2 + 1] then return end
local text_scale = (node and node.text_scale) or signs_lib.default_text_scale
local objects = minetest.get_objects_inside_radius(pos, 0.5)
local obj
if #objects > 0 then
obj = objects[1]
else
obj = minetest.add_entity(pos, "signs_lib:text")
end
obj:setyaw(def.entity_info.yaw[node.param2 + 1])
if not texture then
obj:set_properties({
mesh = def.entity_info.mesh,
visual_size = text_scale,
})
else
obj:set_properties({
mesh = def.entity_info.mesh,
visual_size = text_scale,
textures={texture},
})
end
end
-- rotation
function signs_lib.wallmounted_rotate(pos, node, user, mode)
if not signs_lib.can_modify(pos, user) then return false end
if mode ~= screwdriver.ROTATE_FACE or string.match(node.name, "_onpole") then
@ -93,17 +145,12 @@ signs_lib.wallmounted_rotate = function(pos, node, user, mode)
local newparam2 = signs_lib.rotate_walldir[node.param2] or 0
minetest.swap_node(pos, { name = node.name, param2 = newparam2 })
for _, v in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
local e = v:get_luaentity()
if e and e.name == "signs_lib:text" then
v:remove()
end
end
signs_lib.delete_objects(pos)
signs_lib.update_sign(pos)
return true
end
signs_lib.facedir_rotate = function(pos, node, user, mode)
function signs_lib.facedir_rotate(pos, node, user, mode)
if not signs_lib.can_modify(pos, user) then return false end
if mode ~= screwdriver.ROTATE_FACE or string.match(node.name, "_onpole") then
@ -113,18 +160,11 @@ signs_lib.facedir_rotate = function(pos, node, user, mode)
local newparam2 = signs_lib.rotate_facedir[node.param2] or 0
minetest.swap_node(pos, { name = node.name, param2 = newparam2 })
for _, v in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
local e = v:get_luaentity()
if e and e.name == "signs_lib:text" then
v:remove()
end
end
signs_lib.delete_objects(pos)
signs_lib.update_sign(pos)
return true
end
local DEFAULT_TEXT_SCALE = {x=10, y=10}
-- infinite stacks
if not minetest.settings:get_bool("creative_mode") then
@ -235,15 +275,6 @@ local fences_with_sign = { }
-- some local helper functions
local function split_lines_and_words(text)
if not text then return end
local lines = { }
for _, line in ipairs(text:split("\n")) do
table.insert(lines, line:split(" "))
end
return lines
end
local math_max = math.max
local function fill_line(x, y, w, c, font_size, colorbgw)
@ -422,26 +453,55 @@ local function make_sign_texture(lines, pos)
return table.concat(texture, "")
end
local function set_obj_text(obj, text, x, pos)
local split = split_lines_and_words
local text_ansi = Utf8ToAnsi(text)
local n = minetest.registered_nodes[minetest.get_node(pos).name]
local text_scale = (n and n.text_scale) or DEFAULT_TEXT_SCALE
local texture = make_sign_texture(split(text_ansi), pos)
obj:set_properties({
textures={texture},
visual_size = text_scale,
})
function signs_lib.split_lines_and_words(text)
if not text then return end
local lines = { }
for _, line in ipairs(text:split("\n")) do
table.insert(lines, line:split(" "))
end
return lines
end
signs_lib.construct_sign = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string(
"formspec",
"size[6,4]"..
function signs_lib.set_obj_text(pos, text)
local split = signs_lib.split_lines_and_words
local text_ansi = Utf8ToAnsi(text)
local n = minetest.registered_nodes[minetest.get_node(pos).name]
signs_lib.delete_objects(pos)
signs_lib.spawn_entity(pos, make_sign_texture(split(text_ansi), pos))
end
local function make_widefont_nodename(name)
if string.find(name, "_widefont") then return name end
if string.find(name, "_onpole") then
return string.gsub(name, "_onpole", "_widefont_onpole")
elseif string.find(name, "_hanging") then
return string.gsub(name, "_hanging", "_widefont_hanging")
else
return name.."_widefont"
end
end
function signs_lib.construct_sign(pos)
local form = "size[6,4]"..
"textarea[0,-0.3;6.5,3;text;;${text}]"..
"button_exit[2,3.4;2,1;ok;"..S("Write").."]"..
"background[-0.5,-0.5;7,5;signs_lib_sign_bg.jpg]")
"background[-0.5,-0.5;7,5;signs_lib_sign_bg.jpg]"
local node = minetest.get_node(pos)
local wname = make_widefont_nodename(node.name)
if minetest.registered_items[wname] then
local state = "off"
if string.find(node.name, "widefont") then state = "on" end
form = form.."label[1,3.4;Use wide font]"..
"image_button[1.1,3.7;1,0.6;signs_lib_switch_"..
state..".png;"..
state..";;;false;signs_lib_switch_interm.png]"..
"button_exit[3,3.4;2,1;ok;"..S("Write").."]"
else
form = form.."button_exit[2,3.4;2,1;ok;"..S("Write").."]"
end
local meta = minetest.get_meta(pos)
meta:set_string("formspec", form)
local i = meta:get_string("infotext")
if i == "" then -- it wasn't even set, so set it.
meta:set_string("infotext", "")
@ -449,18 +509,12 @@ signs_lib.construct_sign = function(pos)
end
function signs_lib.destruct_sign(pos)
local objects = minetest.get_objects_inside_radius(pos, 0.5)
for _, v in ipairs(objects) do
local e = v:get_luaentity()
if e and e.name == "signs_lib:text" then
v:remove()
end
end
signs_lib.delete_objects(pos)
end
local function make_infotext(text)
text = trim_input(text)
local lines = split_lines_and_words(text) or {}
local lines = signs_lib.split_lines_and_words(text) or {}
local lines2 = { }
for _, line in ipairs(lines) do
table.insert(lines2, (table.concat(line, " "):gsub("#[0-9a-fA-F]", ""):gsub("##", "#")))
@ -475,50 +529,45 @@ function signs_lib.update_sign(pos, fields)
text = trim_input(text)
local owner = meta:get_string("owner")
ownstr = ""
local ownstr = ""
if owner ~= "" then ownstr = S("Locked sign, owned by @1\n", owner) end
meta:set_string("text", text)
meta:set_string("infotext", ownstr..make_infotext(text).." ")
local objects = minetest.get_objects_inside_radius(pos, 0.5)
local found
for _, v in ipairs(objects) do
local e = v:get_luaentity()
if e and e.name == "signs_lib:text" then
if found then
v:remove()
else
set_obj_text(v, text, nil, pos)
found = true
end
end
end
if found then
return
end
-- if there is no entity
local signnode = minetest.get_node(pos)
local signname = signnode.name
local def = minetest.registered_items[signname]
if not def.entity_info or not def.entity_info.yaw[signnode.param2 + 1] then return end
local obj = minetest.add_entity(pos, "signs_lib:text")
obj:setyaw(def.entity_info.yaw[signnode.param2 + 1])
obj:set_properties({
mesh = def.entity_info.mesh,
})
signs_lib.set_obj_text(pos, text)
end
function signs_lib.receive_fields(pos, formname, fields, sender)
if fields and fields.text and fields.ok and signs_lib.can_modify(pos, sender) then
if not fields or not signs_lib.can_modify(pos, sender) then return end
if fields.text and fields.ok then
minetest.log("action", S("@1 wrote \"@2\" to sign at @3",
(sender:get_player_name() or ""),
fields.text:gsub('\\', '\\\\'):gsub("\n", "\\n"),
minetest.pos_to_string(pos)
))
signs_lib.update_sign(pos, fields)
elseif fields.on or fields.off then
local node = minetest.get_node(pos)
local newname
if fields.on and string.find(node.name, "widefont") then
newname = string.gsub(node.name, "_widefont", "")
elseif fields.off and not string.find(node.name, "widefont") then
newname = make_widefont_nodename(node.name)
end
if newname then
minetest.log("action", S("@1 flipped the wide-font switch to \"@2\" at @3",
(sender:get_player_name() or ""),
(fields.on and "off" or "on"),
minetest.pos_to_string(pos)
))
minetest.swap_node(pos, {name = newname, param2 = node.param2})
signs_lib.construct_sign(pos)
signs_lib.update_sign(pos, fields)
end
end
end
@ -542,30 +591,6 @@ function signs_lib.can_modify(pos, player)
return false
end
local signs_text_on_activate = function(self)
local pos = self.object:getpos()
local meta = minetest.get_meta(pos)
local signnode = minetest.get_node(pos)
local signname = signnode.name
local def = minetest.registered_items[signname]
local text = meta:get_string("text")
if text and def and def.entity_info then
text = trim_input(text)
set_obj_text(self.object, text, nil, pos)
self.object:set_properties({
mesh = def.entity_info.mesh,
})
end
end
minetest.register_entity("signs_lib:text", {
collisionbox = { 0, 0, 0, 0, 0, 0 },
visual = "mesh",
mesh = "signs_lib_standard_wall_sign_entity.obj",
textures = {},
on_activate = signs_text_on_activate,
})
-- make selection boxes
-- sizex/sizey specified in inches because that's what MUTCD uses.
@ -602,6 +627,7 @@ function signs_lib.check_for_pole(pos, pointed_thing)
or string.find(pnode.name, "default:fence_")
or string.find(pnode.name, "_post")
or string.find(pnode.name, "fencepost")
or string.find(pnode.name, "streets:streetlamp_basic_top")
or (pnode.name == "streets:bigpole" and pnode.param2 < 4)
or (pnode.name == "streets:bigpole" and pnode.param2 > 19 and pnode.param2 < 24)
)
@ -646,47 +672,7 @@ function signs_lib.register_fence_with_sign()
minetest.log("warning", "[signs_lib] ".."Attempt to call no longer used function signs_lib.register_fence_with_sign()")
end
--[[
The main sign registration function
===================================
Example minimal recommended def for writable signs:
signs_lib.register_sign("foo:my_cool_sign", {
description = "Wooden cool sign",
inventory_image = "signs_lib_sign_cool_inv.png",
tiles = {
"signs_lib_sign_cool.png",
"signs_lib_sign_cool_edges.png"
},
number_of_lines = 2,
horiz_scaling = 0.8,
vert_scaling = 1,
line_spacing = 9,
font_size = 31,
x_offset = 7,
y_offset = 4,
chars_per_line = 40,
entity_info = "standard"
})
* default def assumes a wallmounted sign with on-pole being allowed.
*For signs that can't support onpole, include in the def:
allow_onpole = false,
* "standard" entity info implies the standard wood/steel sign model, in
wallmounted mode. For facedir signs using the standard model, use:
entity_info = {
mesh = "signs_lib_standard_wall_sign_entity.obj",
yaw = signs_lib.standard_yaw
},
]]--
function signs_lib.register_sign(name, rdef)
local function register_sign(name, rdef)
local def = table.copy(rdef)
if rdef.entity_info == "standard" then
@ -698,6 +684,8 @@ function signs_lib.register_sign(name, rdef)
def.entity_info = rdef.entity_info
end
def.after_place_node = rdef.after_place_node or signs_lib.after_place_node
if rdef.entity_info then
def.on_rightclick = rdef.on_rightclick or signs_lib.construct_sign
def.on_construct = rdef.on_construct or signs_lib.construct_sign
@ -717,8 +705,6 @@ function signs_lib.register_sign(name, rdef)
def.after_place_node = function(pos, placer, itemstack, pointed_thing)
signs_lib.after_place_node(pos, placer, itemstack, pointed_thing, true)
end
else
def.after_place_node = rdef.after_place_node or signs_lib.after_place_node
end
end
@ -819,7 +805,58 @@ function signs_lib.register_sign(name, rdef)
minetest.register_node(":"..name.."_hanging", hdef)
table.insert(signs_lib.lbm_restore_nodes, name.."_hanging")
end
end
--[[
The main sign registration function
===================================
Example minimal recommended def for writable signs:
signs_lib.register_sign("foo:my_cool_sign", {
description = "Wooden cool sign",
inventory_image = "signs_lib_sign_cool_inv.png",
tiles = {
"signs_lib_sign_cool.png",
"signs_lib_sign_cool_edges.png"
},
number_of_lines = 2,
horiz_scaling = 0.8,
vert_scaling = 1,
line_spacing = 9,
font_size = 31,
x_offset = 7,
y_offset = 4,
chars_per_line = 40,
entity_info = "standard"
})
* default def assumes a wallmounted sign with on-pole being allowed.
*For signs that can't support onpole, include in the def:
allow_onpole = false,
* "standard" entity info implies the standard wood/steel sign model, in
wallmounted mode. For facedir signs using the standard model, use:
entity_info = {
mesh = "signs_lib_standard_wall_sign_entity.obj",
yaw = signs_lib.standard_yaw
},
]]--
function signs_lib.register_sign(name, rdef)
register_sign(name, rdef)
if rdef.allow_widefont then
wdef = table.copy(minetest.registered_items[name])
wdef.groups.not_in_creative_inventory = 1
wdef.horiz_scaling = wdef.horiz_scaling / 2
register_sign(name.."_widefont", wdef)
end
end
-- restore signs' text after /clearobjects and the like, the next time
@ -856,12 +893,7 @@ minetest.register_lbm({
local oldfence = signs_lib.old_fenceposts[node.name]
local newsign = signs_lib.old_fenceposts_replacement_signs[node.name]
for _, v in ipairs(minetest.get_objects_inside_radius(pos, 0.5)) do
local e = v:get_luaentity()
if e and e.name == "signs_lib:text" then
v:remove()
end
end
signs_lib.delete_objects(pos)
local oldmeta = minetest.get_meta(pos):to_table()
minetest.set_node(pos, {name = oldfence})
@ -924,12 +956,7 @@ minetest.register_chatcommand("regen_signs", {
for _, b in pairs(allsigns) do
for _, pos in ipairs(b) do
local objects = minetest.get_objects_inside_radius(pos, 0.5)
if #objects > 0 then
for _, v in ipairs(objects) do
v:remove()
end
end
signs_lib.delete_objects(pos)
local node = minetest.get_node(pos)
local def = minetest.registered_items[node.name]
if def and def.entity_info then

View File

@ -9,6 +9,7 @@ signs_lib.register_sign("default:sign_wall_wood", {
},
entity_info = "standard",
allow_hanging = true,
allow_widefont = true
})
signs_lib.register_sign("default:sign_wall_steel", {
@ -23,6 +24,7 @@ signs_lib.register_sign("default:sign_wall_steel", {
locked = true,
entity_info = "standard",
allow_hanging = true,
allow_widefont = true
})
minetest.register_alias("signs:sign_hanging", "default:sign_wall_wood_hanging")

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB