1
0
mirror of https://github.com/pyrollo/digiterms.git synced 2024-11-14 14:20:32 +01:00
digiterms/functions.lua

67 lines
2.0 KiB
Lua
Raw Normal View History

2018-12-02 20:39:59 +01:00
--[[
2018-12-08 16:01:34 +01:00
digiterms mod for Minetest - Digiline monitors using Display API / Font API
2018-12-02 20:39:59 +01:00
(c) Pierre-Yves Rollo
2018-12-08 16:01:34 +01:00
This file is part of digiterms.
2018-12-02 20:39:59 +01:00
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/>.
--]]
2018-12-08 16:01:34 +01:00
digiterms.columns = 30
2018-12-02 20:39:59 +01:00
local function get_lines(pos)
local lines = {}
local meta = minetest.get_meta(pos)
local text = meta:get_string("display_text")
local pos = 1
repeat
local found = text:find("\n", pos) or #text + 1
lines[#lines+1] = text:sub(pos, found - 1)
pos = found + 1
until (pos > (#text + 1))
return lines
end
local function set_lines(pos, lines)
local meta = minetest.get_meta(pos)
meta:set_string("display_text", table.concat(lines, "\n"))
end
local function push_line(lines, line, maxlines)
for index = 1, (maxlines - 1) do
lines[index] = lines[index + 1] or ""
end
lines[maxlines] = line
end
local function push_text(lines, text, maxlines, maxcolumns)
local pos = 1
repeat
local found = text:find("\n", pos) or #text + 1
local line = text:sub(pos, found - 1)
for index = 1, math.ceil(line:len() / maxcolumns) do
local part = line:sub((index-1)*maxcolumns+1, index*maxcolumns)
push_line(lines, part, maxlines)
end
pos = found + 1
until (pos > (#text + 1))
end
2018-12-08 16:01:34 +01:00
function digiterms.push_text_on_screen(pos, text)
2018-12-02 20:39:59 +01:00
local lines = get_lines(pos)
2018-12-08 16:01:34 +01:00
push_text(lines, text, 6, digiterms.columns)
2018-12-02 20:39:59 +01:00
set_lines(pos, lines)
display_api.update_entities(pos)
end