1
0
mirror of https://github.com/mt-mods/homedecor_modpack.git synced 2024-12-23 00:20:17 +01:00

Optimize create_lines() by using patterns, and prevent truncation of infotext.

This commit is contained in:
kaeza 2013-10-16 14:47:16 -02:00
parent 7219f6cd49
commit 6b09ea3a6f

View File

@ -317,54 +317,13 @@ local NUMBER_OF_LINES = 4
local LINE_HEIGHT = 14 local LINE_HEIGHT = 14
local CHAR_WIDTH = 5 local CHAR_WIDTH = 5
homedecor.string_to_array = function(str)
local tab = {}
for i=1,string.len(str) do
table.insert(tab, string.sub(str, i,i))
end
return tab
end
homedecor.string_to_word_array = function(str)
local tab = {}
local current = 1
tab[1] = ""
for _,char in ipairs(homedecor.string_to_array(str)) do
if char ~= " " then
tab[current] = tab[current]..char
else
current = current+1
tab[current] = ""
end
end
return tab
end
homedecor.create_lines = function(text) homedecor.create_lines = function(text)
local line = ""
local line_num = 1
local tab = {} local tab = {}
for _,word in ipairs(homedecor.string_to_word_array(text)) do for line in text:gmatch("([^|]+)|?") do
if string.len(line)+string.len(word) < LINE_LENGTH and word ~= "|" then line = line:gsub("^%s*(.*)%s*$", "%1") -- Trim whitespace
if line ~= "" then
line = line.." "..word
else
line = word
end
else
table.insert(tab, line) table.insert(tab, line)
if word ~= "|" then if #tab >= NUMBER_OF_LINES then break end
line = word
else
line = ""
end end
line_num = line_num+1
if line_num > NUMBER_OF_LINES then
return tab
end
end
end
table.insert(tab, line)
return tab return tab
end end