Change formspec to use textarea element.

This also removes the pipe as line separator, and uses real
newlines instead.

To be compatible with older signs, those older signs are *NOT*
updated, and still use the old line handling code. In order to
take advantage of the new UI, old signs will have to be
replaced. This is an unfortunate drawback that cannot be
fixed.

Newly-placed signs will have the new features right away.
This commit is contained in:
Diego Martinez 2014-07-16 13:59:04 -03:00
parent 6f43085597
commit 6518120958

View File

@ -250,7 +250,7 @@ local fences_with_sign = { }
-- some local helper functions -- some local helper functions
local function split_lines_and_words(text) local function split_lines_and_words_old(text)
local lines = { } local lines = { }
local line = { } local line = { }
if not text then return end if not text then return end
@ -269,6 +269,15 @@ local function split_lines_and_words(text)
return lines return lines
end end
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 })
end
return lines
end
local math_max = math.max local math_max = math.max
local function fill_line(x, y, w, c) local function fill_line(x, y, w, c)
@ -380,9 +389,10 @@ local function make_sign_texture(lines)
return table.concat(texture, "") return table.concat(texture, "")
end end
local function set_obj_text(obj, text) local function set_obj_text(obj, text, new)
local split = new and split_lines_and_words or split_lines_and_words_old
obj:set_properties({ obj:set_properties({
textures={make_sign_texture(split_lines_and_words(text))}, textures={make_sign_texture(split(text))},
visual_size = TEXT_SCALE, visual_size = TEXT_SCALE,
}) })
end end
@ -391,9 +401,10 @@ signs_lib.construct_sign = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string( meta:set_string(
"formspec", "formspec",
"field[text;;${text}]".. "size[6,4]"..
"size[4,2]".. "textarea[0,0;6.5,3;text;;${text}]"..
"background[-1.25,-0.5;6.5,3;bg_signs_lib.jpg]") "button_exit[2,3;2,1;ok;Write]"..
"background[-0.5,-0.5;7,5;bg_signs_lib.jpg]")
meta:set_string("infotext", "") meta:set_string("infotext", "")
end end
@ -418,9 +429,14 @@ end
signs_lib.update_sign = function(pos, fields) signs_lib.update_sign = function(pos, fields)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local new
if fields then if fields then
meta:set_string("infotext", make_infotext(fields.text).." ") meta:set_string("infotext", make_infotext(fields.text).." ")
meta:set_string("text", fields.text) meta:set_string("text", fields.text)
meta:set_int("__signslib_new_format", 1)
new = true
else
new = (meta:get_int("__signslib_new_format") ~= 0)
end end
local text = meta:get_string("text") local text = meta:get_string("text")
if text == nil then return end if text == nil then return end
@ -428,11 +444,11 @@ signs_lib.update_sign = function(pos, fields)
for _, v in ipairs(objects) do for _, v in ipairs(objects) do
local e = v:get_luaentity() local e = v:get_luaentity()
if e and e.name == "signs:text" then if e and e.name == "signs:text" then
set_obj_text(v, text) set_obj_text(v, text, new)
return return
end end
end end
-- if there is no entity -- if there is no entity
local sign_info local sign_info
if minetest.get_node(pos).name == "signs:sign_yard" then if minetest.get_node(pos).name == "signs:sign_yard" then
@ -533,19 +549,17 @@ function signs_lib.determine_sign_type(itemstack, placer, pointed_thing)
end end
function signs_lib.receive_fields(pos, formname, fields, sender) function signs_lib.receive_fields(pos, formname, fields, sender)
if fields and fields.text then
minetest.log("action", S("%s wrote \"%s\" to sign at %s"):format(
(sender:get_player_name() or ""),
fields.text,
minetest.pos_to_string(pos)
))
end
if minetest.is_protected(pos, sender:get_player_name()) then if minetest.is_protected(pos, sender:get_player_name()) then
minetest.record_protection_violation(pos, minetest.record_protection_violation(pos,
sender:get_player_name()) sender:get_player_name())
return return
end end
if fields and fields.text then if fields and fields.text and fields.ok then
minetest.log("action", S("%s wrote \"%s\" to sign at %s"):format(
(sender:get_player_name() or ""),
fields.text,
minetest.pos_to_string(pos)
))
signs_lib.update_sign(pos, fields) signs_lib.update_sign(pos, fields)
end end
end end
@ -654,14 +668,14 @@ minetest.register_node(":signs:sign_post", {
drawtype = "nodebox", drawtype = "nodebox",
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = { fixed = {
{ -0.125, -0.5, -0.125, 0.125, 0.5, 0.125 }, { -0.125, -0.5, -0.125, 0.125, 0.5, 0.125 },
{ -0.45, -0.15, -0.225, 0.45, 0.45, -0.125 }, { -0.45, -0.15, -0.225, 0.45, 0.45, -0.125 },
} }
}, },
selection_box = { selection_box = {
type = "fixed", type = "fixed",
fixed = { fixed = {
{ -0.125, -0.5, -0.125, 0.125, 0.5, 0.125 }, { -0.125, -0.5, -0.125, 0.125, 0.5, 0.125 },
{ -0.45, -0.15, -0.225, 0.45, 0.45, -0.125 }, { -0.45, -0.15, -0.225, 0.45, 0.45, -0.125 },
} }
@ -728,7 +742,7 @@ function signs_lib.register_fence_with_sign(fencename, fencewithsignname)
return return
end end
if minetest.is_protected(pointed_thing.above, playername) then if minetest.is_protected(pointed_thing.above, playername) then
minetest.record_protection_violation(pointed_thing.above, playername) minetest.record_protection_violation(pointed_thing.above, playername)
return return
end end