mirror of
https://github.com/APercy/airutils.git
synced 2025-03-21 18:41:21 +00:00
improve character counting for prefix
This commit is contained in:
parent
7fa37d98e9
commit
2da331dd2a
3 changed files with 72 additions and 10 deletions
2
init.lua
2
init.lua
|
@ -648,7 +648,7 @@ if airutils._use_signs_api then
|
||||||
if entity then
|
if entity then
|
||||||
if entity.owner == name or minetest.check_player_privs(name, {protection_bypass=true}) then
|
if entity.owner == name or minetest.check_player_privs(name, {protection_bypass=true}) then
|
||||||
if param then
|
if param then
|
||||||
entity._ship_name = string.sub(param, 1,20)
|
entity._ship_name = string.sub(param, 1, 40)
|
||||||
else
|
else
|
||||||
entity._ship_name = ""
|
entity._ship_name = ""
|
||||||
end
|
end
|
||||||
|
|
|
@ -680,12 +680,6 @@ local function _paint(self, l_textures, colstr, paint_list, mask_associations)
|
||||||
l_textures[_] = "("..l_textures[_]..")^("..texture_name.."^[mask:"..mask_texture..")" --add the mask
|
l_textures[_] = "("..l_textures[_]..")^("..texture_name.."^[mask:"..mask_texture..")" --add the mask
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if airutils._use_signs_api then
|
|
||||||
indx = texture:find('airutils_name_canvas.png')
|
|
||||||
if indx then
|
|
||||||
l_textures[_] = "airutils_name_canvas.png^"..airutils.convert_text_to_texture(self._ship_name, self._name_color or 0, self._name_hor_aligment or 0.8)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return l_textures
|
return l_textures
|
||||||
|
@ -730,6 +724,17 @@ function airutils.param_paint(self, colstr, colstr_2)
|
||||||
self._color = colstr
|
self._color = colstr
|
||||||
self._color_2 = colstr_2
|
self._color_2 = colstr_2
|
||||||
local l_textures = self.initial_properties.textures
|
local l_textures = self.initial_properties.textures
|
||||||
|
|
||||||
|
--to reduce cpu processing, put the prefix here
|
||||||
|
if airutils._use_signs_api then
|
||||||
|
for _, texture in ipairs(l_textures) do
|
||||||
|
local indx = texture:find('airutils_name_canvas.png')
|
||||||
|
if indx then
|
||||||
|
l_textures[_] = "airutils_name_canvas.png^"..airutils.convert_text_to_texture(self._ship_name, self._name_color or 0, self._name_hor_aligment or 3.0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
l_textures = _paint(self, l_textures, colstr) --paint the main plane
|
l_textures = _paint(self, l_textures, colstr) --paint the main plane
|
||||||
l_textures = _paint(self, l_textures, colstr_2, self._painting_texture_2) --paint the main plane
|
l_textures = _paint(self, l_textures, colstr_2, self._painting_texture_2) --paint the main plane
|
||||||
self.object:set_properties({textures=l_textures})
|
self.object:set_properties({textures=l_textures})
|
||||||
|
|
63
text.lua
63
text.lua
|
@ -14,6 +14,7 @@ local TP = signs_lib.path .. "/textures"
|
||||||
local CHAR_FILE = "%s_%02x.png"
|
local CHAR_FILE = "%s_%02x.png"
|
||||||
-- Fonts path
|
-- Fonts path
|
||||||
local CHAR_PATH = TP .. "/" .. CHAR_FILE
|
local CHAR_PATH = TP .. "/" .. CHAR_FILE
|
||||||
|
local max_lenght = 20
|
||||||
|
|
||||||
-- Initialize character texture cache
|
-- Initialize character texture cache
|
||||||
local ctexcache = {}
|
local ctexcache = {}
|
||||||
|
@ -27,6 +28,60 @@ local function fill_line(x, y, w, c, font_size, colorbgw)
|
||||||
return table.concat(tex)
|
return table.concat(tex)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function clamp_characters(text, max_lenght)
|
||||||
|
text = text or ""
|
||||||
|
max_lenght = max_lenght or 20
|
||||||
|
local control_chars = {"##","#0","#1","#2","#3","#4","#5","#6","#7","#8","#9","#a","#b","#c","#d","#e","#f"}
|
||||||
|
local control_order = {}
|
||||||
|
local new_string = text
|
||||||
|
|
||||||
|
--first creates a memory of each control code
|
||||||
|
for i = 1, #new_string do
|
||||||
|
local c = new_string:sub(i,i+1)
|
||||||
|
for i, item in pairs(control_chars) do
|
||||||
|
if c == item then
|
||||||
|
table.insert(control_order, item)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--create control spaces (the order was saved in "control_order"
|
||||||
|
local control_char = "\001"
|
||||||
|
for i, item in pairs(control_chars) do
|
||||||
|
new_string = string.gsub(new_string, item, control_char)
|
||||||
|
end
|
||||||
|
|
||||||
|
--now make another string counting it outside and breaking when reachs 20
|
||||||
|
local count = 0
|
||||||
|
local curr_index = 0
|
||||||
|
local c = ""
|
||||||
|
for i = 1, #new_string, 1 do
|
||||||
|
c = string.sub(new_string,i,i)
|
||||||
|
if not (c == control_char) then
|
||||||
|
count = count + 1
|
||||||
|
end
|
||||||
|
curr_index = i
|
||||||
|
if count == max_lenght then break end
|
||||||
|
end
|
||||||
|
local cutstring = string.sub(new_string,1,curr_index)
|
||||||
|
|
||||||
|
--now reconstruct the string
|
||||||
|
local outputstring = ""
|
||||||
|
local control_order_curr_intex = 0
|
||||||
|
for i = 1, #cutstring, 1 do
|
||||||
|
c = string.sub(cutstring,i,i)
|
||||||
|
if not (c == control_char) then
|
||||||
|
outputstring = outputstring .. (c or "")
|
||||||
|
else
|
||||||
|
control_order_curr_intex = control_order_curr_intex + 1
|
||||||
|
outputstring = outputstring .. (control_order[control_order_curr_intex] or "")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return outputstring, count
|
||||||
|
end
|
||||||
|
|
||||||
-- check if a file does exist
|
-- check if a file does exist
|
||||||
-- to avoid reopening file after checking again
|
-- to avoid reopening file after checking again
|
||||||
-- pass TRUE as second argument
|
-- pass TRUE as second argument
|
||||||
|
@ -116,7 +171,7 @@ local function make_text_texture(text, default_color, line_width, line_height, c
|
||||||
else
|
else
|
||||||
maxw = math.max(width, maxw)
|
maxw = math.max(width, maxw)
|
||||||
end
|
end
|
||||||
local max_input_chars = 20
|
local max_input_chars = max_lenght
|
||||||
if #chars < max_input_chars then
|
if #chars < max_input_chars then
|
||||||
table.insert(chars, {
|
table.insert(chars, {
|
||||||
off = ch_offs,
|
off = ch_offs,
|
||||||
|
@ -186,9 +241,11 @@ function airutils.convert_text_to_texture(text, default_color, horizontal_aligme
|
||||||
local colorbgw
|
local colorbgw
|
||||||
local chars_per_line
|
local chars_per_line
|
||||||
local widemult
|
local widemult
|
||||||
text = string.sub(text,1,20)
|
local count = 0
|
||||||
|
--text = string.sub(text,1,max_lenght)
|
||||||
|
text, count = clamp_characters(text, max_lenght)
|
||||||
|
|
||||||
if string.len(text) <= 10 then
|
if count <= 10 then
|
||||||
widemult = 0.75
|
widemult = 0.75
|
||||||
font_size = 32
|
font_size = 32
|
||||||
chars_per_line = 10
|
chars_per_line = 10
|
||||||
|
|
Loading…
Add table
Reference in a new issue