Fonts configurable in signs, posters and steles

This commit is contained in:
Pierre-Yves Rollo 2018-11-23 11:34:55 +01:00
parent 59ae9fa4d1
commit 367e5026dd
4 changed files with 93 additions and 55 deletions

@ -101,16 +101,18 @@ local function show_font_formspec(playername)
end end
table.sort(fonts) table.sort(fonts)
local fs = 'size[4,'..(#fonts + 0.8)..']' local fs = string.format(
..default.gui_bg..default.gui_bg_img..default.gui_slots "size[4,%s]%s%s%sbutton_exit[0,%s;4,1;cancel;Cancel]",
..'button_exit[0,'..(#fonts)..';4,1;cancel;Cancel]' #fonts + 0.8, default.gui_bg, default.gui_bg_img, default.gui_slots,
#fonts)
for line = 1, #fonts do for line = 1, #fonts do
local font = font_api.get_font(fonts[line]) local font = font_api.get_font(fonts[line])
fs = fs..'image[0.1,'..(line-0.9)..';4.5,0.8;' local texture = font:make_text_texture(font.name, font:get_height()*5,
..font:make_text_texture(font.name, font:get_height()*5,
font:get_height()*1.2, 1, "center", "top", "#fff") font:get_height()*1.2, 1, "center", "top", "#fff")
..']button_exit[0,'..(line-1)..';4,1;font_'..font.name..';]' fs = string.format(
"%simage[0.1,%s;4.5,0.8;%s]button_exit[0,%s;4,1;font_%s;]",
fs, line-0.9, texture, line-1, font.name)
end end
minetest.show_formspec(context.playername, modname..':font_list', fs) minetest.show_formspec(context.playername, modname..':font_list', fs)
end end
@ -127,7 +129,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local playername = player:get_player_name() local playername = player:get_player_name()
local context = get_context(playername) local context = get_context(playername)
if minetest.is_protected(context.pos, playername) then if not context.pos
or minetest.is_protected(context.pos, playername) then
return true return true
end end
@ -139,16 +142,26 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
display_api.update_entities(context.pos) display_api.update_entities(context.pos)
end end
end end
-- Using after to avoid the "double close" bug
minetest.after(0, show_node_formspec, playername, context.pos) if context.callback and type(context.callback) == "function" then
-- Using after to avoid the "double close" bug
minetest.after(0, context.callback, playername, context.pos)
else
-- Using after to avoid the "double close" bug
minetest.after(0, show_node_formspec, playername, context.pos)
end
end end
return true return true
end) end)
function font_api.show_font_list_from_pos(player, pos) -- @param player Player viewing the form
-- @param pos Node pos
-- @param callback function(playername, pos) to be called on form close
function font_api.show_font_list(player, pos, callback)
if minetest.is_player(player) then if minetest.is_player(player) then
local context = get_context(player:get_player_name()) local context = get_context(player:get_player_name())
context.pos = pos context.pos = pos
context.callback = callback
show_font_formspec(player:get_player_name()) show_font_formspec(player:get_player_name())
end end
end end

@ -23,51 +23,58 @@ local F = function(...) return minetest.formspec_escape(S(...)) end
-- Poster specific formspec -- Poster specific formspec
local function display_poster(pos, node, player) local function display_poster(pos, node, player)
local formspec
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local def = minetest.registered_nodes[node.name].display_entities["signs:display_text"] local def = minetest.registered_nodes[node.name].display_entities["signs:display_text"]
local font = font_api.get_font(meta:get_string("font") or def.font_name) local font = font_api.get_font(meta:get_string("font") or def.font_name)
local fs
local fname = string.format("%s@%s:display",
node.name, minetest.pos_to_string(pos))
-- Title texture -- Title texture
local titletexture = font:make_text_texture( local titletexture = font:make_text_texture(
meta:get_string("display_text"), font:get_height()*8.4, font:get_height(), 1, "center") meta:get_string("display_text"), font:get_height()*8.4,
font:get_height(), 1, "center")
formspec = fs = string.format([=[
"size[7,9]".. size[7,9]bgcolor[#0000]
"background[0,0;7,9;signs_poster_formspec.png]".. background[0,0;7,9;signs_poster_formspec.png]
"image[0,-0.2;8.4,2;"..titletexture.."]".. image[0,-0.2;8.4,2;%s]
"textarea[0.3,1.5;7,8;;"..minetest.colorize("#111", minetest.formspec_escape(meta:get_string("text")))..";]".. textarea[0.3,1.5;7,8;;%s;]]=],
"bgcolor[#0000]" titletexture,
minetest.colorize("#111",
minetest.formspec_escape(meta:get_string("text"))))
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
formspec = formspec.. fs = string.format("%sbutton_exit[2.5,8;2,1;ok;%s]", fs, F("Close"))
"button_exit[2.5,8;2,1;ok;"..F("Close").."]"
else else
formspec = formspec.. fs = string.format(
"button[1,8;2,1;edit;"..F("Edit").."]".. "%sbutton[1,8;2,1;edit;%s]button_exit[4,8;2,1;ok;%s]",
"button_exit[4,8;2,1;ok;"..F("Close").."]" fs, F("Edit"), F("Close"))
end end
minetest.show_formspec(player:get_player_name(), minetest.show_formspec(player:get_player_name(), fname, fs)
node.name.."@"..minetest.pos_to_string(pos)..":display",
formspec)
end end
local function edit_poster(pos, node, player) local function edit_poster(pos, node, player)
local formspec
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local fs
local fname = string.format("%s@%s:edit",
node.name, minetest.pos_to_string(pos))
if not minetest.is_protected(pos, player:get_player_name()) then if not minetest.is_protected(pos, player:get_player_name()) then
formspec = fs = string.format([=[
"size[6.5,7.5]".. size[6.5,7.5]%s%s%s
default.gui_bg .. default.gui_bg_img .. default.gui_slots .. field[0.5,0.7;6,1;display_text;%s;%s]
"field[0.5,0.7;6,1;display_text;"..F("Title")..";".. textarea[0.5,1.7;6,6;text;%s;%s]
minetest.formspec_escape(meta:get_string("display_text")).."]".. button[1.25,7;2,1;font;%s]
"textarea[0.5,1.7;6,6;text;"..F("Text")..";".. button_exit[3.25,7;2,1;write;%s]]=],
minetest.formspec_escape(meta:get_string("text")).."]".. default.gui_bg, default.gui_bg_img, default.gui_slots, F("Title"),
"button_exit[2.25,7;2,1;write;"..F("Write").."]" minetest.formspec_escape(meta:get_string("display_text")),
minetest.show_formspec(player:get_player_name(), F("Text"), minetest.formspec_escape(meta:get_string("text")),
node.name.."@"..minetest.pos_to_string(pos)..":edit", F("Title font"), F("Write"))
formspec) minetest.show_formspec(player:get_player_name(), fname, fs)
end end
end end
@ -80,15 +87,29 @@ local function on_receive_fields_poster(pos, formname, fields, player)
if formname == node.name.."@"..minetest.pos_to_string(pos)..":display" if formname == node.name.."@"..minetest.pos_to_string(pos)..":display"
and fields.edit then and fields.edit then
edit_poster(pos, node, player) edit_poster(pos, node, player)
return true
end end
if formname == node.name.."@"..minetest.pos_to_string(pos)..":edit" if formname == node.name.."@"..minetest.pos_to_string(pos)..":edit"
and (fields.write or fields.key_enter) then then
meta:set_string("display_text", fields.display_text) if (fields.write or fields.font or fields.key_enter) then
meta:set_string("text", fields.text) meta:set_string("display_text", fields.display_text)
meta:set_string("infotext", "\""..fields.display_text meta:set_string("text", fields.text)
.."\"\n"..S("(right-click to read more text)")) meta:set_string("infotext", "\""..fields.display_text
display_api.update_entities(pos) .."\"\n"..S("(right-click to read more text)"))
display_poster(pos, node, player) display_api.update_entities(pos)
end
if (fields.write or fields.key_enter) then
display_poster(pos, node, player)
elseif (fields.font) then
font_api.show_font_list(player, pos, function (playername, pos)
local player = minetest.get_player_by_name(playername)
local node = minetest.get_node(pos)
if player and node then
edit_poster(pos, node, player)
end
end)
end
return true
end end
end end
end end

@ -81,7 +81,7 @@ function signs_api.on_receive_fields(pos, formname, fields, player)
end end
if fields and (fields.font) then if fields and (fields.font) then
signs_api.set_display_text(pos, fields.display_text) signs_api.set_display_text(pos, fields.display_text)
font_api.show_font_list_from_pos(player, pos) font_api.show_font_list(player, pos)
end end
end end
end end

@ -63,12 +63,14 @@ for i, material in ipairs(steles.materials) do
end, end,
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("formspec", "size[6,4]" meta:set_string("formspec", string.format([=[
..default.gui_bg .. default.gui_bg_img .. default.gui_slots size[6,4]%s%s%s
.."textarea[0.5,0.7;5.5,2;display_text;" textarea[0.5,0.7;5.5,2;display_text;%s;${display_text}]
..F("Displayed text (3 lines max)") button[1,3;2,1;font;%s]
..";${display_text}]" button_exit[3,3;2,1;ok;%s]]=],
.."button_exit[2,3;2,1;ok;"..F("Write").."]") default.gui_bg, default.gui_bg_img, default.gui_slots,
F("Displayed text (3 lines max)"),
F("Font"), F("Write")))
display_api.on_construct(pos) display_api.on_construct(pos)
end, end,
on_destruct = display_api.on_destruct, on_destruct = display_api.on_destruct,
@ -76,15 +78,17 @@ for i, material in ipairs(steles.materials) do
on_receive_fields = function(pos, formname, fields, player) on_receive_fields = function(pos, formname, fields, player)
if not minetest.is_protected(pos, player:get_player_name()) then if not minetest.is_protected(pos, player:get_player_name()) then
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if fields and fields.ok then if fields.ok or fields.font then
meta:set_string("display_text", fields.display_text) meta:set_string("display_text", fields.display_text)
meta:set_string("infotext", "\""..fields.display_text.."\"") meta:set_string("infotext", "\""..fields.display_text.."\"")
display_api.update_entities(pos) display_api.update_entities(pos)
end end
if fields.font then
font_api.show_font_list(player, pos)
end
end end
end, end,
on_punch = display_api.update_entities, on_punch = display_api.update_entities,
}) })
end end
end end