From f3970f641eb60bf92492b6715084273cc1bceb74 Mon Sep 17 00:00:00 2001 From: Thomas--S Date: Thu, 1 Feb 2018 16:54:55 +0100 Subject: Seperate signs API from signs definitions Change modnames from *_lib to *_api --- signs_api/init.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 signs_api/init.lua (limited to 'signs_api/init.lua') diff --git a/signs_api/init.lua b/signs_api/init.lua new file mode 100644 index 0000000..c13a253 --- /dev/null +++ b/signs_api/init.lua @@ -0,0 +1,33 @@ +--[[ + 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 . +--]] + +signs_api = {} +signs_api.name = minetest.get_current_modname() +signs_api.path = minetest.get_modpath(signs_api.name) + +-- Load support for intllib. +local S, NS = dofile(signs_api.path.."/intllib.lua") +signs_api.intllib = S + +dofile(signs_api.path.."/common.lua") + + + + -- cgit v1.2.3 From 8c7557e45d4744fe35ad058950062cf771640126 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 13 Jul 2018 20:41:53 +0200 Subject: Rework all nodes displaying text according to new font_api --- signs_api/init.lua | 194 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 193 insertions(+), 1 deletion(-) (limited to 'signs_api/init.lua') diff --git a/signs_api/init.lua b/signs_api/init.lua index c13a253..e8a2b2b 100644 --- a/signs_api/init.lua +++ b/signs_api/init.lua @@ -25,8 +25,200 @@ signs_api.path = minetest.get_modpath(signs_api.name) -- Load support for intllib. local S, NS = dofile(signs_api.path.."/intllib.lua") signs_api.intllib = S +local F = function(...) return minetest.formspec_escape(S(...)) end -dofile(signs_api.path.."/common.lua") +local function update_font_index_meta(meta) + local font = meta:get_string("font") + local count = 0 + for key, def in pairs(font_api.registered_fonts) do + count = count + 1 + if font == key then + meta:set_string("font_idx", count) + end + end +end + +function signs_api.set_display_text(pos,text,font) + local meta = minetest.get_meta(pos) + meta:set_string("display_text", text) + meta:set_string("infotext", "\""..text.."\"") + meta:set_string("font", font) + update_font_index_meta(meta) + display_api.update_entities(pos) +end + +function signs_api.set_formspec(pos) + local meta = minetest.get_meta(pos) + local ndef = minetest.registered_nodes[minetest.get_node(pos).name] + if ndef and ndef.display_entities + and ndef.display_entities["signs:display_text"] then + local maxlines = ndef.display_entities["signs:display_text"].maxlines + local formspec, formheight + + if maxlines == 1 then + formspec = + "field[0.5,0.7;5.5,1;display_text;"..F("Text").. + ";${display_text}]" + formheight = 2 + else + local extralabel = "" + if maxlines then + extralabel = F(" (first %s lines only)"):format(maxlines) + end + + formspec = + "textarea[0.5,0.7;5.5,2;display_text;"..F("Text").."".. + extralabel..";${display_text}]" + formheight = 3 + end + + formspec = formspec.."button_exit[2,"..formheight..";2,1;ok;".. + F("Write").."]" + formheight = formheight + 1 + formspec = "size[6,"..formheight.."]"..default.gui_bg.. + default.gui_bg_img..default.gui_slots..formspec + + meta:set_string("formspec", formspec) + end +end + +function signs_api.on_receive_fields(pos, formname, fields, player) + if not minetest.is_protected(pos, player:get_player_name()) then + if fields and (fields.ok or fields.key_enter) then + signs_api.set_display_text(pos, fields.display_text, fields.font) + end + end +end + +-- On place callback for direction signs +-- (chooses which sign according to look direction) +function signs_api.on_place_direction(itemstack, placer, pointed_thing) + local name = itemstack:get_name() + 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 + + 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 + + -- Only for direction signs + if ndef.signs_other_dir then + if test[ndir] > 0 then + itemstack:set_name(ndef.signs_other_dir) + end + itemstack = minetest.item_place(itemstack, placer, pointed_thing, ndir) + itemstack:set_name(name) + + return itemstack + else + return minetest.item_place(itemstack, placer, pointed_thing, ndir) + end +end + +-- Handles screwdriver rotation. Direction is affected for direction signs +function signs_api.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_api.update_entities(pos) + end + else + display_api.on_rotate(pos, node, user, mode, new_param2) + end + return false; +end + +function signs_api.register_sign(mod, name, model) + -- Default fields + local fields = { + sunlight_propagates = true, + paramtype = "light", + paramtype2 = "facedir", + drawtype = "nodebox", + node_box = { + type = "fixed", + fixed = {-model.width/2, -model.height/2, 0.5, + model.width/2, model.height/2, 0.5 - model.depth}, + }, + groups = {choppy=2, dig_immediate=2, not_blocking_trains = 1, display_modpack_node = 1}, + sounds = default.node_sound_defaults(), + display_entities = { + ["signs:display_text"] = { + on_display_update = font_api.on_display_update, + depth = 0.5 - display_api.entity_spacing - model.depth, + size = { x = model.width, y = model.height }, + aspect_ratio = 1/2, + maxlines = 1, + }, + + }, + on_place = display_api.on_place, + on_construct = function(pos) + local ndef = minetest.registered_nodes[minetest.get_node(pos).name] + local meta = minetest.get_meta(pos) + meta:set_string("font", ndef.display_entities.font_name or + font_api.get_default_font_name()) + update_font_index_meta(meta) + signs_api.set_formspec(pos) + display_api.on_construct(pos) + end, + on_destruct = display_api.on_destruct, + on_rotate = signs_api.on_rotate, + on_receive_fields = signs_api.on_receive_fields, + on_punch = function(pos, node, player, pointed_thing) + display_api.update_entities(pos) + end, + } + + -- Node fields override + for key, value in pairs(model.node_fields) do + 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 + fields.display_entities["signs:display_text"][key] = value + end + + minetest.register_node(mod..":"..name, fields) +end + +-- Text entity for all signs +display_api.register_display_entity("signs:display_text") -- cgit v1.2.3 From c19348d305945f70af25257ad70e980687ffbae8 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Mon, 16 Jul 2018 10:18:37 +0200 Subject: Cleanup some unused code --- signs_api/init.lua | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'signs_api/init.lua') diff --git a/signs_api/init.lua b/signs_api/init.lua index e8a2b2b..1a4357e 100644 --- a/signs_api/init.lua +++ b/signs_api/init.lua @@ -27,23 +27,15 @@ local S, NS = dofile(signs_api.path.."/intllib.lua") signs_api.intllib = S local F = function(...) return minetest.formspec_escape(S(...)) end -local function update_font_index_meta(meta) - local font = meta:get_string("font") - local count = 0 - for key, def in pairs(font_api.registered_fonts) do - count = count + 1 - if font == key then - meta:set_string("font_idx", count) - end - end -end - -function signs_api.set_display_text(pos,text,font) +function signs_api.set_display_text(pos, text, font) local meta = minetest.get_meta(pos) meta:set_string("display_text", text) - meta:set_string("infotext", "\""..text.."\"") + if text and text ~= "" then + meta:set_string("infotext", "\""..text.."\"") + else + meta:set_string("infotext", "") + end meta:set_string("font", font) - update_font_index_meta(meta) display_api.update_entities(pos) end @@ -184,7 +176,6 @@ function signs_api.register_sign(mod, name, model) local meta = minetest.get_meta(pos) meta:set_string("font", ndef.display_entities.font_name or font_api.get_default_font_name()) - update_font_index_meta(meta) signs_api.set_formspec(pos) display_api.on_construct(pos) end, -- cgit v1.2.3 From 44b911be9e57dccea606da2b6907135af75d5033 Mon Sep 17 00:00:00 2001 From: 12Me21 <12Me21.MC@gmail.com> Date: Wed, 26 Sep 2018 13:41:30 -0400 Subject: Improve on_rotate functions (#21) Fixed bugs and improve on_rotate and support color- paramtype2s --- signs_api/init.lua | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'signs_api/init.lua') diff --git a/signs_api/init.lua b/signs_api/init.lua index 1a4357e..ec3b0e9 100644 --- a/signs_api/init.lua +++ b/signs_api/init.lua @@ -132,18 +132,19 @@ function signs_api.on_place_direction(itemstack, placer, pointed_thing) end -- Handles screwdriver rotation. Direction is affected for direction signs +-- If rotation mode is 2 and sign is directional, swap direction. +-- Otherwise use display_api's on_rotate function. function signs_api.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}) + if ndef.signs_other_dir then + minetest.swap_node(pos, {name = ndef.signs_other_dir, + param1 = node.param1, param2 = node.param2}) display_api.update_entities(pos) - end - else - display_api.on_rotate(pos, node, user, mode, new_param2) + return true + end end - return false; + return display_api.on_rotate(pos, node, user, mode, new_param2) end function signs_api.register_sign(mod, name, model) -- cgit v1.2.3 From 641dddfd5100eafc3d8571576c0ae679c2877ea3 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sun, 18 Nov 2018 15:29:59 +0100 Subject: Code simplification and securisation --- signs_api/init.lua | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) (limited to 'signs_api/init.lua') diff --git a/signs_api/init.lua b/signs_api/init.lua index ec3b0e9..7e81e94 100644 --- a/signs_api/init.lua +++ b/signs_api/init.lua @@ -35,49 +35,62 @@ function signs_api.set_display_text(pos, text, font) else meta:set_string("infotext", "") end - meta:set_string("font", font) + if font then + meta:set_string("font", font) + end display_api.update_entities(pos) end function signs_api.set_formspec(pos) local meta = minetest.get_meta(pos) local ndef = minetest.registered_nodes[minetest.get_node(pos).name] - if ndef and ndef.display_entities + if ndef and ndef.display_entities and ndef.display_entities["signs:display_text"] then local maxlines = ndef.display_entities["signs:display_text"].maxlines - local formspec, formheight + local fs, x, y if maxlines == 1 then - formspec = - "field[0.5,0.7;5.5,1;display_text;"..F("Text").. - ";${display_text}]" - formheight = 2 + fs = "field[0.5,0.7;5.5,1;display_text;"..F("Text").. + ";${display_text}]" + y = 1.2 else local extralabel = "" if maxlines then extralabel = F(" (first %s lines only)"):format(maxlines) end - formspec = - "textarea[0.5,0.7;5.5,2;display_text;"..F("Text").."".. + fs = "textarea[0.5,0.7;5.5,2;display_text;"..F("Text").."".. extralabel..";${display_text}]" - formheight = 3 + y = 2.4 end - formspec = formspec.."button_exit[2,"..formheight..";2,1;ok;".. + x = 0.2 + fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_font.png;font;]" + x = x + 0.4 + fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_left.png;left;]" + x = x + 0.4 + fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_center.png;center;]" + x = x + 0.4 + fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_right.png;right;]" + y = y + 0.7 + fs = fs.."button_exit[2,"..y..";2,1;ok;".. F("Write").."]" - formheight = formheight + 1 - formspec = "size[6,"..formheight.."]"..default.gui_bg.. - default.gui_bg_img..default.gui_slots..formspec + y = y + 0.8 + fs = "size[6,"..y.."]"..default.gui_bg.. + default.gui_bg_img..default.gui_slots..fs - meta:set_string("formspec", formspec) + meta:set_string("formspec", fs) end end function signs_api.on_receive_fields(pos, formname, fields, player) if not minetest.is_protected(pos, player:get_player_name()) then if fields and (fields.ok or fields.key_enter) then - signs_api.set_display_text(pos, fields.display_text, fields.font) + signs_api.set_display_text(pos, fields.display_text) + end + if fields and (fields.font) then + signs_api.set_display_text(pos, fields.display_text) + font_api.show_font_list_from_pos(player, pos) end end end @@ -183,7 +196,8 @@ function signs_api.register_sign(mod, name, model) on_destruct = display_api.on_destruct, on_rotate = signs_api.on_rotate, on_receive_fields = signs_api.on_receive_fields, - on_punch = function(pos, node, player, pointed_thing) + on_punch = function(pos, node, player, pointed_thing) + signs_api.set_formspec(pos) display_api.update_entities(pos) end, } @@ -211,7 +225,3 @@ end -- Text entity for all signs display_api.register_display_entity("signs:display_text") - - - - -- cgit v1.2.3 From 59ae9fa4d153d9f1a554357febecd7f69c2642c9 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sun, 18 Nov 2018 15:44:55 +0100 Subject: Simplified signs formspec for release --- signs_api/init.lua | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'signs_api/init.lua') diff --git a/signs_api/init.lua b/signs_api/init.lua index 7e81e94..4f21495 100644 --- a/signs_api/init.lua +++ b/signs_api/init.lua @@ -47,7 +47,7 @@ function signs_api.set_formspec(pos) if ndef and ndef.display_entities and ndef.display_entities["signs:display_text"] then local maxlines = ndef.display_entities["signs:display_text"].maxlines - local fs, x, y + local fs, y if maxlines == 1 then fs = "field[0.5,0.7;5.5,1;display_text;"..F("Text").. @@ -64,17 +64,8 @@ function signs_api.set_formspec(pos) y = 2.4 end - x = 0.2 - fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_font.png;font;]" - x = x + 0.4 - fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_left.png;left;]" - x = x + 0.4 - fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_center.png;center;]" - x = x + 0.4 - fs = fs.."image_button["..x..","..y..";0.5,0.5;font_api_right.png;right;]" - y = y + 0.7 - fs = fs.."button_exit[2,"..y..";2,1;ok;".. - F("Write").."]" + fs = fs.."button[1,"..y..";2,1;font;"..F("Font").."]" + fs = fs.."button_exit[3,"..y..";2,1;ok;"..F("Write").."]" y = y + 0.8 fs = "size[6,"..y.."]"..default.gui_bg.. default.gui_bg_img..default.gui_slots..fs -- cgit v1.2.3 From 367e5026ddef3e6a0220f37a2f4aba64ec88f16b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 23 Nov 2018 11:34:55 +0100 Subject: Fonts configurable in signs, posters and steles --- signs_api/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'signs_api/init.lua') diff --git a/signs_api/init.lua b/signs_api/init.lua index 4f21495..9fcaa10 100644 --- a/signs_api/init.lua +++ b/signs_api/init.lua @@ -81,7 +81,7 @@ function signs_api.on_receive_fields(pos, formname, fields, player) end if fields and (fields.font) then 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 -- cgit v1.2.3