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,7 +444,7 @@ 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
@ -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