Signs code clean-up

This commit is contained in:
Mikita Wiśniewski 2025-01-26 19:53:48 +07:00
parent 8ab4748c0c
commit a0ba232ca7
4 changed files with 55 additions and 56 deletions

View file

@ -54,9 +54,10 @@ separated with an empty line for readability.
The format expects 1 row with 3 columns per character:
* **Column 1:** The literal (as-is) glyph. Only [precomposed characters](https://en.wikipedia.org/wiki/Precomposed_character) are supported
* **Column 1:** The literal (as-is) glyph. Only [precomposed characters](https://en.wikipedia.org/wiki/Precomposed_character)
are supported for diacritics
* **Column 2:** Name of the texture file for this character minus the ".png"
suffix (found in the "textures/" sub-directory in root)
suffix (found in the `textures/` sub-directory in root)
* **Column 3:** Currently ignored. This is reserved for character width in
pixels in case the font will be made proportional

View file

@ -114,6 +114,7 @@ local function set_signmeta(pos, def)
if def.glow then meta:set_string("glow", def.glow) end
end
-- Text processing
local function string_to_line_array(str)
local lines = {}
local line = {}
@ -204,6 +205,7 @@ local function get_text_entity(pos, force_remove)
end
mcl_signs.get_text_entity = get_text_entity
-- Update the sign text entity (create if doesn't exist)
local function update_sign(pos)
local data = get_signdata(pos)
@ -232,6 +234,31 @@ local function update_sign(pos)
end
mcl_signs.update_sign = update_sign
core.register_lbm({
name = "mcl_signs:restore_entities",
nodenames = {"group:sign"},
label = "Restore sign text",
run_at_every_load = true,
action = update_sign,
})
-- Text entity definition
core.register_entity("mcl_signs:text", {
initial_properties = {
pointable = false,
visual = "upright_sprite",
physical = false,
collide_with_objects = false,
},
on_activate = function(self)
local pos = self.object:get_pos()
update_sign(pos)
local props = self.object:get_properties()
local t = props and props.textures
if type(t) ~= "table" or #t == 0 then self.object:remove() end
end,
})
-- Formspec
local function show_formspec(player, pos)
if not pos then return end
@ -265,6 +292,7 @@ core.register_on_player_receive_fields(function(player, formname, fields)
end
end)
-- Node definition callbacks
function sign_tpl.on_place(itemstack, placer, pointed_thing)
local under = pointed_thing.under
local node = core.get_node(under)
@ -348,6 +376,7 @@ end
-- mcl_signs.update_sign(pos)
--end
-- Wall sign definition
local sign_wall = table_merge(sign_tpl, {
mesh = "mcl_signs_signonwallmount.obj",
paramtype2 = "wallmounted",
@ -359,32 +388,6 @@ local sign_wall = table_merge(sign_tpl, {
_mcl_sign_type = "wall",
})
core.register_lbm({
nodenames = {"group:sign"},
name = "mcl_signs:restore_entities",
label = "Restore sign text",
run_at_every_load = true,
action = function(pos)
update_sign(pos)
end
})
core.register_entity("mcl_signs:text", {
initial_properties = {
pointable = false,
visual = "upright_sprite",
physical = false,
collide_with_objects = false,
},
on_activate = function(self)
local pos = self.object:get_pos()
update_sign(pos)
local props = self.object:get_properties()
local t = props and props.textures
if type(t) ~= "table" or #t == 0 then self.object:remove() end
end,
})
local function colored_texture(texture, color)
return texture.."^[multiply:"..color
end

View file

@ -1,4 +1,4 @@
mcl_signs.old_rotnames = {}
local old_rotnames = {}
-- these are the "rotation strings" of the old sign rotation scheme
local rotkeys = {
@ -44,19 +44,19 @@ local signs = {
["_cherrywood"] = "_cherry",
}
local mcl2standingsigns = {}
local mcl2rotsigns = {}
local old_standingsigns = {}
local old_rotsigns = {}
for old, new in pairs(signs) do
local newname = "mcl_signs:standing_sign"..new
mcl2standingsigns["mcl_signs:standing_sign"..old] = newname
old_standingsigns["mcl_signs:standing_sign"..old] = newname
for _, rotkey in ipairs(rotkeys) do
mcl2rotsigns["mcl_signs:standing_sign"..rotkey..old] = newname
old_rotsigns["mcl_signs:standing_sign"..rotkey..old] = newname
end
core.register_alias("mcl_signs:wall_sign"..old, "mcl_signs:wall_sign"..new)
end
function mcl_signs.upgrade_sign_meta(pos)
local function upgrade_sign_meta(pos)
local m = core.get_meta(pos)
local color = m:get_string("mcl_signs:text_color")
local glow = m:get_string("mcl_signs:glowing_sign")
@ -73,12 +73,12 @@ function mcl_signs.upgrade_sign_meta(pos)
mcl_signs.get_text_entity(pos, true) -- the 2nd "true" arg means deleting the entity for respawn
end
function mcl_signs.upgrade_sign_rot(pos, node)
local function upgrade_sign_rot(pos, node)
local numsign = false
for _,v in ipairs(rotkeys) do
if mcl2rotsigns[node.name] then
node.name = mcl2rotsigns[node.name]
if old_rotsigns[node.name] then
node.name = old_rotsigns[node.name]
node.param2 = nidp2_degrotate[v][node.param2 + 1]
numsign = true
elseif node.name:find(v) then
@ -89,8 +89,8 @@ function mcl_signs.upgrade_sign_rot(pos, node)
end
if not numsign then
if mcl2standingsigns[node.name] then
node.name = mcl2standingsigns[node.name]
if old_standingsigns[node.name] then
node.name = old_standingsigns[node.name]
end
local def = core.registered_nodes[node.name]
if def and def._mcl_sign_type == "standing" then
@ -105,7 +105,7 @@ function mcl_signs.upgrade_sign_rot(pos, node)
end
core.swap_node(pos, node)
mcl_signs.upgrade_sign_meta(pos)
upgrade_sign_meta(pos)
mcl_signs.update_sign(pos)
end
@ -114,16 +114,16 @@ core.register_lbm({
name = "mcl_signs:update_old_signs",
label = "Update old signs",
run_at_every_load = false,
action = mcl_signs.upgrade_sign_rot,
action = upgrade_sign_rot,
})
for k,_ in pairs(mcl2rotsigns) do table.insert(mcl_signs.old_rotnames, k) end
for k,_ in pairs(mcl2standingsigns) do table.insert(mcl_signs.old_rotnames, k) end
for k,_ in pairs(old_rotsigns) do table.insert(old_rotnames, k) end
for k,_ in pairs(old_standingsigns) do table.insert(old_rotnames, k) end
core.register_lbm({
nodenames = mcl_signs.old_rotnames,
nodenames = old_rotnames,
name = "mcl_signs:update_old_rotated_standing",
label = "Update old standing rotated signs",
run_at_every_load = true, --these nodes are supposed to completely be replaced
action = mcl_signs.upgrade_sign_rot
run_at_every_load = true, -- these nodes are supposed to completely be replaced
action = upgrade_sign_rot
})

View file

@ -5,26 +5,21 @@ local modname = core.get_current_modname()
local S = core.get_translator(modname)
local modpath = core.get_modpath(modname)
-- UTF-8 library from Modlib
-- UTF-8 library from modlib
local utf8 = dofile(modpath .. DIR_DELIM .. "utf8.lua")
-- Character map
-- Character map (see API.md for reference)
local charmap = {}
for line in io.lines(modpath .. "/characters.tsv") do
local split = line:split("\t")
if #split > 0 then
if #split == 3 then
local char, img, _ = split[1], split[2], split[3] -- 3rd is ignored, reserved for width
charmap[char] = img
end
end
mcl_signs.charmap = charmap
local files = {
"api",
"register",
"compat"
}
for _, file in pairs(files) do
-- Load modules and share local variables
for _, file in pairs{"api", "register", "compat"} do
loadfile(modpath .. DIR_DELIM .. file .. ".lua")(S, charmap, utf8)
end