1
0
mirror of https://github.com/pyrollo/display_modpack.git synced 2025-06-28 22:26:41 +02:00

3 Commits

Author SHA1 Message Date
07eb4737fa Added font formspec textures 2018-11-18 15:30:37 +01:00
641dddfd51 Code simplification and securisation 2018-11-18 15:29:59 +01:00
b8357a505c New font selection formspec 2018-11-10 22:35:47 +01:00
7 changed files with 189 additions and 25 deletions

154
font_api/fontform.lua Normal file
View File

@ -0,0 +1,154 @@
--[[
font_api mod for Minetest - Library to add font display capability
to display_api mod.
(c) Pierre-Yves Rollo
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
--]]
local modname = minetest.get_current_modname()
local contexts = {}
minetest.register_on_leaveplayer(function(player)
if minetest.is_player(player) then
contexts[player:get_player_name()] = nil
end
end)
local function get_context(playername)
if not contexts[playername] then
contexts[playername] = { playername = playername }
end
return contexts[playername]
end
-- Show node formspec functions
local function show_node_formspec(playername, pos)
local meta = minetest.get_meta(pos)
-- Decontextualize formspec
local fs = meta:get_string('formspec')
if not fs then
return
end
-- Change context and currrent_name references to nodemeta references
-- Change context and currrent_name references to nodemeta references
local nodemeta = string.format("nodemeta:%i,%i,%i", pos.x, pos.y ,pos.z)
fs = fs:gsub("current_name", nodemeta)
fs = fs:gsub("context", nodemeta)
-- Change all ${} to their corresponding metadata values
local s, e
repeat
s, e = fs:find('%${.*}')
if s and e then
fs = fs:sub(1, s-1)..
minetest.formspec_escape(meta:get_string(fs:sub(s+2,e-1)))..
fs:sub(e+1)
end
until s == nil
local context = get_context(playername)
context.node_pos = pos
-- Find node on_receive_fields
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
if ndef and ndef.on_receive_fields then
context.on_receive_fields = ndef.on_receive_fields
end
-- Show formspec
minetest.show_formspec(playername, modname..':context_formspec', fs)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= modname..':context_formspec' then
return
end
if not minetest.is_player(player) then
return true
end
local context = get_context(player:get_player_name())
if context.on_receive_fields then
context.on_receive_fields(context.pos, '', fields, player)
end
return true
end)
-- Specific functions
local function show_font_formspec(playername)
local context = get_context(playername)
local fonts = {}
for name, _ in pairs(font_api.registered_fonts) do
fonts[#fonts+1] = name
end
table.sort(fonts)
local fs = 'size[4,'..(#fonts + 0.8)..']'
..default.gui_bg..default.gui_bg_img..default.gui_slots
..'button_exit[0,'..(#fonts)..';4,1;cancel;Cancel]'
for line = 1, #fonts do
local font = font_api.get_font(fonts[line])
fs = fs..'image[0.1,'..(line-0.9)..';4.5,0.8;'
..font:make_text_texture(font.name, font:get_height()*5,
font:get_height()*1.2, 1, "center", "top", "#fff")
..']button_exit[0,'..(line-1)..';4,1;font_'..font.name..';]'
end
minetest.show_formspec(context.playername, modname..':font_list', fs)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= modname..':font_list' then
return
end
if not minetest.is_player(player) then
return true
end
local playername = player:get_player_name()
local context = get_context(playername)
if minetest.is_protected(context.pos, playername) then
return true
end
if fields.quit == 'true' then
for name, _ in pairs(font_api.registered_fonts) do
if fields['font_'..name] then
local meta = minetest.get_meta(context.pos)
meta:set_string("font", name)
display_api.update_entities(context.pos)
end
end
-- Using after to avoid the "double close" bug
minetest.after(0, show_node_formspec, playername, context.pos)
end
return true
end)
function font_api.show_font_list_from_pos(player, pos)
if minetest.is_player(player) then
local context = get_context(player:get_player_name())
context.pos = pos
show_font_formspec(player:get_player_name())
end
end

View File

@ -29,9 +29,10 @@ font_api.path = minetest.get_modpath(font_api.name)
dofile(font_api.path.."/font.lua")
dofile(font_api.path.."/registry.lua")
dofile(font_api.path.."/fontform.lua")
--- Standard on_display_update entity callback.
-- Node should have a corresponding display_entity with size, resolution and
-- Node should have a corresponding display_entity with size, resolution and
-- maxlines fields and optionally halign, valign and color fields
-- @param pos Node position
-- @param objref Object reference of entity
@ -49,8 +50,8 @@ function font_api.on_display_update(pos, objref)
local maxlines = def.maxlines or 1 -- TODO:How to do w/o maxlines ?
objref:set_properties({
textures={font:make_text_texture(text,
objref:set_properties({
textures={font:make_text_texture(text,
font:get_height(maxlines) * def.size.x / def.size.y
/ (def.aspect_ratio or 1),
font:get_height(maxlines),
@ -62,4 +63,3 @@ end
-- Compatibility
font_lib = font_api

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 238 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 222 B

View File

@ -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")