display_modpack/signs/common.lua

195 lines
5.9 KiB
Lua
Raw Normal View History

--[[
signs mod for Minetest - Various signs with text displayed on
(c) Pierre-Yves Rollo
This file is part of signs.
signs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
signs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with signs. If not, see <http://www.gnu.org/licenses/>.
--]]
local S = signs.intllib
local F = function(...) return minetest.formspec_escape(S(...)) end
2015-11-11 14:26:39 +01:00
function signs.set_formspec(pos)
local meta = minetest.get_meta(pos)
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
2017-11-30 21:15:06 +01:00
if ndef and ndef.display_entities and ndef.display_entities["signs:display_text"] then
local maxlines = ndef.display_entities["signs:display_text"].maxlines
2015-11-11 14:26:39 +01:00
local formspec
if maxlines == 1 then
2015-11-11 14:26:39 +01:00
formspec = "size[6,3]"..
2017-08-26 14:12:38 +02:00
"field[0.5,0.7;5.5,1;display_text;"..F("Text")..";${display_text}]"..
"button_exit[2,2;2,1;ok;"..F("Write").."]"
2015-11-11 14:26:39 +01:00
else
local extralabel = ""
if maxlines then
extralabel = F(" (first %s lines only)"):format(maxlines)
2015-11-11 14:26:39 +01:00
end
formspec = "size[6,4]"..
2017-08-26 14:12:38 +02:00
"textarea[0.5,0.7;5.5,2;display_text;"..F("Text")..""..extralabel..";${display_text}]"..
"button_exit[2,3;2,1;ok;"..F("Write").."]"
2015-11-11 14:26:39 +01:00
end
meta:set_string("formspec", formspec)
end
end
function signs.on_receive_fields(pos, formname, fields, player)
if not minetest.is_protected(pos, player:get_player_name()) then
local meta = minetest.get_meta(pos)
2017-08-26 14:12:38 +02:00
if fields and (fields.ok or fields.key_enter) then
2015-11-11 14:26:39 +01:00
meta:set_string("display_text", fields.display_text)
meta:set_string("infotext", "\""..fields.display_text.."\"")
display_lib.update_entities(pos)
end
end
end
-- On place callback for direction signs
-- (chooses which sign according to look direction)
function signs.on_place_direction(itemstack, placer, pointed_thing)
local name = itemstack:get_name()
2017-08-26 14:12:38 +02:00
local ndef = minetest.registered_nodes[name]
local bdir = {x = pointed_thing.under.x - pointed_thing.above.x,
y = pointed_thing.under.y - pointed_thing.above.y,
z = pointed_thing.under.z - pointed_thing.above.z}
local pdir = placer:get_look_dir()
local ndir, test
if ndef.paramtype2 == "facedir" then
if bdir.x == 0 and bdir.z == 0 then
-- Ceiling or floor pointed (facedir chosen from player dir)
ndir = minetest.dir_to_facedir({x=pdir.x, y=0, z=pdir.z})
else
-- Wall pointed
ndir = minetest.dir_to_facedir(bdir)
end
test = {[0]=-pdir.x, pdir.z, pdir.x, -pdir.z}
end
2015-11-11 14:26:39 +01:00
2017-08-26 14:12:38 +02:00
if ndef.paramtype2 == "wallmounted" then
ndir = minetest.dir_to_wallmounted(bdir)
if ndir == 0 or ndir == 1 then
-- Ceiling or floor
ndir = minetest.dir_to_wallmounted({x=pdir.x, y=0, z=pdir.z})
end
test = {0, pdir.z, -pdir.z, -pdir.x, pdir.x}
end
2015-11-11 14:26:39 +01:00
2017-08-26 14:12:38 +02:00
-- Only for direction signs
if ndef.signs_other_dir then
if test[ndir] > 0 then
itemstack:set_name(ndef.signs_other_dir)
2015-11-11 14:26:39 +01:00
end
2017-08-26 14:12:38 +02:00
itemstack = minetest.item_place(itemstack, placer, pointed_thing, ndir)
itemstack:set_name(name)
2015-11-11 14:26:39 +01:00
return itemstack
else
2017-08-26 14:12:38 +02:00
return minetest.item_place(itemstack, placer, pointed_thing, ndir)
2015-11-11 14:26:39 +01:00
end
end
2017-08-26 14:12:38 +02:00
-- Handles screwdriver rotation. Direction is affected for direction signs
function signs.on_rotate(pos, node, player, mode, new_param2)
if mode == 2 then
local ndef = minetest.registered_nodes[node.name]
if ndef.signs_other_dir then
minetest.swap_node(pos, {name = ndef.signs_other_dir,
param1 = node.param1, param2 = node.param2})
display_lib.update_entities(pos)
end
2017-08-26 14:12:38 +02:00
else
display_lib.on_rotate(pos, node, user, mode, new_param2)
end
return false;
2015-11-11 14:26:39 +01:00
end
2017-08-26 14:12:38 +02:00
-- Generic callback for show_formspec displayed formspecs of "sign" mod
2015-11-11 14:26:39 +01:00
minetest.register_on_player_receive_fields(function(player, formname, fields)
2015-11-11 14:32:22 +01:00
local found, _, mod, node_name, pos = formname:find("([%w_]+):([%w_]+)@(.+)")
2015-11-11 14:26:39 +01:00
if found then
if mod ~= 'signs' then return end
local ndef = minetest.registered_nodes[mod..":"..node_name]
if ndef and ndef.on_receive_fields then
ndef.on_receive_fields(minetest.string_to_pos(pos), formname, fields, player)
end
end
end)
function signs.register_sign(mod, name, model)
-- Default fields
local fields = {
sunlight_propagates = true,
paramtype = "light",
2017-08-26 14:12:38 +02:00
paramtype2 = "facedir",
drawtype = "nodebox",
node_box = {
2017-08-26 14:12:38 +02:00
type = "fixed",
fixed = {-model.width/2, -model.height/2, 0.5,
model.width/2, model.height/2, 0.5 - model.depth},
},
2017-12-08 14:04:59 +01:00
groups = {choppy=2, dig_immediate=2, not_blocking_trains = 1},
sounds = default.node_sound_defaults(),
display_entities = {
2017-11-30 21:15:06 +01:00
["signs:display_text"] = {
on_display_update = font_lib.on_display_update,
depth = 0.499 - model.depth,
2017-08-26 14:12:38 +02:00
size = { x = model.width, y = model.height },
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,
2017-08-26 14:12:38 +02:00
on_rotate = signs.on_rotate,
on_receive_fields = signs.on_receive_fields,
on_punch = function(pos, node, player, pointed_thing) display_lib.update_entities(pos) end,
}
-- Node fields override
for key, value in pairs(model.node_fields) do
2016-12-18 20:57:40 +01:00
if key == "groups" then
for key2, value2 in pairs(value) do
fields[key][key2] = value2
end
else
fields[key] = value
end
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
2017-11-30 21:15:06 +01:00
fields.display_entities["signs:display_text"][key] = value
end
minetest.register_node(mod..":"..name, fields)
end