1
0
mirror of https://github.com/pyrollo/display_modpack.git synced 2025-06-28 22:26:41 +02:00

Added facedir node support to display_lib, created font_lib, updated mods accordingly

This commit is contained in:
Pierre-Yves Rollo
2015-11-28 20:44:04 +01:00
parent 9b4513bf9d
commit 1b39bf7ae3
134 changed files with 426 additions and 390 deletions

View File

@ -2,29 +2,19 @@
This mod provides various signs with text display. Text is locked if area is protected.
**Dependancies**: display_lib, default
**Dependancies**: default, display\_lib, font\_lib
**License**: WTFPL
(Font taken from VanessaE's homedecor/signs_lib)
## Recipes
**Blue Street Sign**
**Sign** (overrides default sign)
B W S
S S S
- - -
W W W
W W W
- S -
B = Blue Dye, W = White Dye, S = Steel Ingot
**Green Street Sign**
G W S
S S S
- - -
G = Green Dye, W = White Dye, S = Steel Ingot
W = Wooden Plank, S = Stick
**Poster**
@ -44,11 +34,3 @@ Poster displays only title, much more text can be read by right-clicking on it.
W = Wooden Plank (group)
**Black direction sign**
B W S
S S -
- - -
B = Black Dye, W = White Dye, S = Steel Ingot

View File

@ -1,120 +1,18 @@
local font = {}
signs.font_height = 10
-- Get png width, suposing png width is less than 256 (it is the case for all font textures)
local function get_png_width(filename)
local file=assert(io.open(filename,"rb"))
-- All font png are smaller than 256x256 --> read only last byte
file:seek("set",19)
local w = file:read(1)
file:close()
return w:byte()
end
-- Computes line width for a given font height and text
function signs.get_line_width(text)
local char
local width = 0
for p=1,#text
do
char = text:sub(p,p):byte()
if font[char] then
width = width + font[char].width
end
end
return width
end
--- Builds texture part for a text line
-- @param text Text to be rendered
-- @param x Starting x position in texture
-- @param width Width of the texture (extra text is not rendered)
-- @param y Vertical position of the line in texture
-- @return Texture string
function signs.make_line_texture(text, x, width, y)
local char
local texture = ""
for p=1,#text
do
char = text:sub(p,p):byte()
if font[char] then
-- Add image only if it is visible (at least partly)
if x + font[char].width >= 0 and x <= width then
texture = texture..string.format(":%d,%d=%s", x, y, font[char].filename)
end
x = x + font[char].width
end
end
return texture
end
local function split_lines(text, maxlines)
local splits = text:split("\n")
if maxlines then
local lines = {}
for num = 1,maxlines do
lines[num] = splits[num]
end
return lines
else
return splits
end
end
function signs.on_display_update(pos, objref)
local meta = minetest.get_meta(pos)
local text = meta:get_string("display_text")
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
if ndef and ndef.sign_model then
local model = signs.sign_models[ndef.sign_model]
local lines = split_lines(text, model.maxlines)
local texturew = model.width/model.xscale
local textureh = model.height/model.yscale
local texture = ""
local y
if model.valing == "top" then
y = signs.font_height / 2
else
y = (textureh - signs.font_height * #lines) / 2 + 1
end
for _, line in pairs(lines) do
texture = texture..signs.make_line_texture(line,
(texturew - signs.get_line_width(line)) / 2,
texturew, y)
y = y + signs.font_height
end
local texture = string.format("[combine:%dx%d", texturew, textureh)..texture
if model.color then texture = texture.."^[colorize:"..model.color end
objref:set_properties({ textures={texture}, visual_size = {x=model.width, y=model.height}})
end
end
function signs.set_formspec(pos)
local meta = minetest.get_meta(pos)
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
if ndef and ndef.sign_model then
local model = signs.sign_models[ndef.sign_model]
if ndef and ndef.display_entities and ndef.display_entities["signs:text"] then
local maxlines = ndef.display_entities["signs:text"].maxlines
local formspec
if model.maxlines == 1 then
if maxlines == 1 then
formspec = "size[6,3]"..
"field[0.5,0.7;5.5,1;display_text;Displayed text;${display_text}]"..
"button_exit[2,2;2,1;ok;Write]"
else
local extralabel = ""
if model.maxlines then
extralabel = " (first "..model.maxlines.." lines only)"
if maxlines then
extralabel = " (first "..maxlines.." lines only)"
end
formspec = "size[6,4]"..
@ -192,14 +90,6 @@ function signs.on_rotate_direction(pos, node, user, mode, new_param2)
end
end
-- Populate fonts table
local w, filename
for charnum=32,126 do
filename = string.format("signs_%02x.png", charnum)
w = get_png_width(signs.path.."/textures/"..filename)
font[charnum] = {filename=filename, width=w}
end
-- Generic callback for show_formspec displayed formspecs
minetest.register_on_player_receive_fields(function(player, formname, fields)
local found, _, mod, node_name, pos = formname:find("([%w_]+):([%w_]+)@(.+)")
@ -214,3 +104,56 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
end
end)
function signs.register_sign(mod, name, model)
-- Default fields
local fields = {
sunlight_propagates = true,
paramtype = "light",
paramtype2 = "wallmounted",
drawtype = "nodebox",
node_box = {
type = "wallmounted",
wall_side = {-0.5, -model.height/2, -model.width/2,
-0.5 + model.depth, model.height/2, model.width/2},
wall_bottom = {-model.width/2, -0.5, -model.height/2,
model.width/2, -0.5 + model.depth, model.height/2},
wall_top = {-model.width/2, 0.5, -model.height/2,
model.width/2, 0.5 - model.depth, model.height/2},
},
groups = {choppy=2,dig_immediate=2,attached_node=1},
sounds = default.node_sound_defaults(),
display_entities = {
["signs:text"] = {
on_display_update = font_lib.on_display_update,
depth = 0.499 - model.depth,
size = { x = 1, y = 1 },
resolution = { x = 64, y = 64 },
maxlines = 1,
},
},
on_place = display_lib.on_place,
on_construct = function(pos)
signs.set_formspec(pos)
display_lib.on_construct(pos)
end,
on_destruct = display_lib.on_destruct,
on_rotate = display_lib.on_rotate,
on_receive_fields = signs.on_receive_fields,
}
-- Node fields override
for key, value in pairs(model.node_fields) do
fields[key] = value
end
if not fields.wield_image then fields.wield_image = fields.inventory_image end
-- Entity fields override
for key, value in pairs(model.entity_fields) do
fields.display_entities["signs:text"][key] = value
end
minetest.register_node(mod..":"..name, fields)
end

View File

@ -1,21 +1,3 @@
minetest.register_craft({
output = 'signs:blue_street',
recipe = {
{'dye:blue', 'dye:white', 'default:steel_ingot'},
{'default:steel_ingot', 'default:steel_ingot', 'default:steel_ingot'},
{'', '', ''},
}
})
minetest.register_craft({
output = 'signs:green_street',
recipe = {
{'dye:green', 'dye:white', 'default:steel_ingot'},
{'default:steel_ingot', 'default:steel_ingot', 'default:steel_ingot'},
{'', '', ''},
}
})
minetest.register_craft({
output = 'signs:wooden_right',
recipe = {
@ -25,15 +7,6 @@ minetest.register_craft({
}
})
minetest.register_craft({
output = 'signs:black_right',
recipe = {
{'dye:black', 'dye:white', 'default:steel_ingot'},
{'default:steel_ingot', 'default:steel_ingot', ''},
{'', '', ''},
}
})
minetest.register_craft({
output = 'signs:poster',
recipe = {

View File

@ -1,3 +1,3 @@
default
display_lib
font_lib

View File

@ -1,63 +0,0 @@
local font = {}
signs.font_height = 10
-- Get png width, suposing png width is less than 256 (it is the case for all font textures)
local function get_png_width(filename)
local file=assert(io.open(filename,"rb"))
-- All font png are smaller than 256x256 --> read only last byte
file:seek("set",19)
local w = file:read(1)
file:close()
return w:byte()
end
-- Computes line width for a given font height and text
function signs.get_line_width(text)
local char
local width = 0
for p=1,#text
do
char = text:sub(p,p):byte()
if font[char] then
width = width + font[char].width
end
end
return width
end
--- Builds texture part for a text line
-- @param text Text to be rendered
-- @param x Starting x position in texture
-- @param width Width of the texture (extra text is not rendered)
-- @param y Vertical position of the line in texture
-- @return Texture string
function signs.make_line_texture(text, x, width, y)
local char
local texture = ""
for p=1,#text
do
char = text:sub(p,p):byte()
if font[char] then
-- Add image only if it is visible (at least partly)
if x + font[char].width >= 0 and x <= width then
texture = texture..string.format(":%d,%d=%s", x, y, font[char].filename)
end
x = x + font[char].width
end
end
return texture
end
-- Populate fonts table
local w, filename
for charnum=32,126 do
filename = string.format("signs_%02x.png", charnum)
w = get_png_width(signs.path.."/textures/"..filename)
font[charnum] = {filename=filename, width=w}
end

View File

@ -39,44 +39,21 @@ local function on_receive_fields_poster(pos, formname, fields, player)
end
end
signs.sign_models = {
blue_street={
depth=1/16,
width=14/16,
height=12/16,
color="#fff",
maxlines = 3,
xscale = 1/144,
yscale = 1/64,
fields = {
description="Blue street sign",
tiles={"signs_blue_street.png"},
inventory_image="signs_blue_street_inventory.png",
},
},
green_street={
depth=1/32,
width=1,
height=6/16,
color="#fff",
maxlines = 1,
xscale = 1/96,
yscale = 1/64,
fields = {
description="Green street sign",
tiles={"signs_green_street.png"},
inventory_image="signs_green_street_inventory.png",
},
},
-- Text entity for all signs
display_lib.register_display_entity("signs:text")
-- Sign models and registration
local models = {
wooden_right={
depth=1/16,
width=14/16,
height=7/16,
color="#000",
maxlines = 2,
xscale = 1/112,
yscale = 1/64,
fields = {
width = 14/16,
height = 7/16,
entity_fields = {
resolution = { x = 112, y = 64 },
maxlines = 2,
color="#000",
},
node_fields = {
description="Wooden direction sign",
tiles={"signs_wooden_right.png"},
inventory_image="signs_wooden_inventory.png",
@ -87,13 +64,14 @@ signs.sign_models = {
},
wooden_left={
depth=1/16,
width=14/16,
height=7/16,
color="#000",
maxlines = 2,
xscale = 1/112,
yscale = 1/64,
fields = {
width = 14/16,
height = 7/16,
entity_fields = {
resolution = { x = 112, y = 64 },
maxlines = 2,
color="#000",
},
node_fields = {
description="Wooden direction sign",
tiles={"signs_wooden_left.png"},
inventory_image="signs_wooden_inventory.png",
@ -103,50 +81,17 @@ signs.sign_models = {
on_rotate=signs.on_rotate_direction,
},
},
black_right={
depth=1/32,
width=1,
height=0.5,
color="#000",
maxlines = 1,
xscale = 1/96,
yscale = 1/64,
fields = {
description="Black direction sign",
tiles={"signs_black_right.png"},
inventory_image="signs_black_inventory.png",
on_place=signs.on_place_direction,
on_rotate=signs.on_rotate_direction,
},
},
black_left={
depth=1/32,
width=1,
height=0.5,
color="#000",
maxlines = 1,
xscale = 1/96,
yscale = 1/64,
fields = {
description="Black direction sign",
tiles={"signs_black_left.png"},
inventory_image="signs_black_inventory.png",
groups={choppy=1,oddly_breakable_by_hand=1,not_in_creative_inventory=1},
drop="signs:black_right",
on_place=signs.on_place_direction,
on_rotate=signs.on_rotate_direction,
},
},
poster={
depth=1/32,
width=26/32,
height=30/32,
color="#000",
valing="top",
maxlines = 1,
xscale = 1/144,
yscale = 1/64,
fields = {
width = 26/32,
height = 30/32,
entity_fields = {
resolution = { x = 144, y = 64 },
maxlines = 1,
color="#000",
valign="top",
},
node_fields = {
description="Poster",
tiles={"signs_poster.png"},
inventory_image="signs_poster_inventory.png",
@ -157,47 +102,27 @@ signs.sign_models = {
},
}
display_lib.register_display_entity("signs:text")
for model_name, model in pairs(signs.sign_models)
for name, model in pairs(models)
do
local fields = {
sunlight_propagates = true,
paramtype = "light",
paramtype2 = "wallmounted",
drawtype = "nodebox",
node_box = {
type = "wallmounted",
wall_side = {-0.5, -model.height/2, -model.width/2,
-0.5 + model.depth, model.height/2, model.width/2},
wall_bottom = {-model.width/2, -0.5, -model.height/2,
model.width/2, -0.5 + model.depth, model.height/2},
wall_top = {-model.width/2, 0.5, -model.height/2,
model.width/2, 0.5 - model.depth, model.height/2},
},
groups = {choppy=1,oddly_breakable_by_hand=1},
sign_model = model_name,
display_entities = {
["signs:text"] = {
depth = model.depth-0.499,
on_display_update = signs.on_display_update },
},
on_place = display_lib.on_place,
on_construct = function(pos)
signs.set_formspec(pos)
display_lib.on_construct(pos)
end,
on_destruct = display_lib.on_destruct,
on_rotate = display_lib.on_rotate,
on_receive_fields = signs.on_receive_fields,
}
for key, value in pairs(model.fields) do
fields[key] = value
end
if not fields.wield_image then fields.wield_image = fields.inventory_image end
minetest.register_node("signs:"..model_name, fields)
signs.register_sign("signs", name, model)
end
-- Override default sign
signs.register_sign(":default", "sign_wall", {
depth = 1/16,
width = 14/16,
height = 10/16,
entity_fields = {
size = { x = 12/16, y = 8/16 },
resolution = { x = 144, y = 64 },
maxlines = 3,
color="#000",
},
node_fields = {
description="Sign",
tiles={"signs_default.png"},
inventory_image="signs_default_inventory.png",
},
})

Binary file not shown.

Before

Width:  |  Height:  |  Size: 232 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 235 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 237 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 237 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 259 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 249 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 237 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 248 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 234 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 234 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 233 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 234 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 245 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 236 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 242 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 241 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 232 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 239 B

Some files were not shown because too many files have changed in this diff Show More