Skip to content

Commit

Permalink
More types support.
Browse files Browse the repository at this point in the history
  • Loading branch information
nagolove committed Feb 11, 2021
1 parent d118acf commit 3ec9075
Show file tree
Hide file tree
Showing 8 changed files with 660 additions and 596 deletions.
173 changes: 95 additions & 78 deletions alignedlabels.lua
Original file line number Diff line number Diff line change
@@ -1,91 +1,108 @@
local class = require "libs.30log"
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; require("love")

local g = love.graphics
local AlignedLabels = class("AlignedLabels")

function AlignedLabels:init(font, screenwidth, color)
self:clear(font, screenwidth, color)
AlignedLabels = {}













local AlignedLabels_mt = {
__index = AlignedLabels,
}

function AlignedLabels.new(font, screenwidth, color)
local self = {}
setmetatable(self, AlignedLabels_mt)
self:clear(font, screenwidth, color)
return self
end

function AlignedLabels:clear(font, screenwidth, color)
self.screenwidth = screenwidth or self.screenwidth
self.font = font or self.font
self.data = {}
self.colors = {}
self.default_color = color or {1, 1, 1, 1}
self.maxlen = 0
self.screenwidth = screenwidth or self.screenwidth
self.font = font or self.font
self.data = {}
self.colors = {}
self.default_color = color or { 1, 1, 1, 1 }
self.maxlen = 0
end

-- ... - list of pairs of color and text
-- AlignedLabels:add("helllo", {200, 100, 10}, "wwww", {0, 0, 100})
-- плохо, что функция не проверяет параметры на количество и тип



function AlignedLabels:add(...)
--assert(type(text) == "string")
local args = {...}
local nargs = select("#", ...)
--print("AlignedLabels:add() args = " .. inspect(args))
if nargs > 2 then
local colored_text_data = {}
local colors = {}
local text_len = 0
for i = 1, nargs, 2 do
local text = select(i, ...)
local color = select(i + 1, ...)
text_len = text_len + text:len()
colored_text_data[#colored_text_data + 1] = text
colors[#colors + 1] = color
end
self.data[#self.data + 1] = colored_text_data
--assert(check_color_t(select(i + 1, ...)))
self.colors[#self.colors + 1] = colors
if text_len > self.maxlen then
self.maxlen = text_len
end
else
self.data[#self.data + 1] = select(1, ...)
self.colors[#self.colors + 1] = select(2, ...) or self.default_color
end
end
local nargs = select("#", ...)
if nargs > 2 then
local colored_text_data = {}
local colors = {}
local text_len = 0
for i = 1, nargs, 2 do
local text = select(i, ...)
local color = select(i + 1, ...)
text_len = text_len + text:len()
colored_text_data[#colored_text_data + 1] = text
colors[#colors + 1] = color
end

function AlignedLabels:draw(x, y)
local dw = self.screenwidth / (#self.data + 1)
local i = x + dw
local f = g.getFont()
local c = {g.getColor()}
g.setFont(self.font)
for k, v in pairs(self.data) do
if type(v) == "string" then
g.setColor(self.colors[k])
g.print(v, i - self.font:getWidth(v) / 2, y)
i = i + dw
elseif type(v) == "table" then
local width = 0
for _, g in pairs(v) do
width = width + self.font:getWidth(g)
end
assert(#v == #self.colors[k])
local xpos = i - width / 2
for j, p in pairs(v) do
--print(type(self.colors[k]), inspect(self.colors[k]), k, j)
g.setColor(self.colors[k][j])
g.print(p, xpos, y)
xpos = xpos + self.font:getWidth(p)
end
i = i + dw
else
error(string.format(
"AlignedLabels:draw() : Incorrect type %s in self.data",
self.data))
end
end
g.setFont(f)
g.setColor(c)


self.data = colored_text_data



self.colors = colors

if text_len > self.maxlen then
self.maxlen = text_len
end
else
self.data[#self.data + 1] = select(1, ...)
self.colors[#self.colors + 1] = (select(2, ...)) or self.default_color
end
end

local alignedtables = {
new = function(font, screenwidth, color)
return AlignedLabels:new(font, screenwidth, color)
end
}
function AlignedLabels:draw(x, y)
local dw = self.screenwidth / (#self.data + 1)
local i = x + dw
local f = g.getFont()
local c = { g.getColor() }
g.setFont(self.font)
for k, v in pairs(self.data) do
if type(v) == "string" then
g.setColor(self.colors[k])
g.print(v, i - self.font:getWidth(v) / 2, y)
i = i + dw
elseif type(v) == "table" then
local width = 0
for _, g in ipairs(v) do
width = width + self.font:getWidth(g)
end
assert(#(v) == #self.colors[k])
local xpos = i - width / 2
for j, p in pairs(v) do

return alignedtables
g.setColor(self.colors[k][j])
g.print(p, xpos, y)
xpos = xpos + self.font:getWidth(p)
end
i = i + dw
else
error(string.format(
"AlignedLabels:draw() : Incorrect type %s in self.data",
self.data))
end
end
g.setFont(f)
g.setColor(c)
end

return AlignedLabels
95 changes: 53 additions & 42 deletions coroprocessor.lua
Original file line number Diff line number Diff line change
@@ -1,58 +1,69 @@
local resume = coroutine.resume
local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local coroutine = _tl_compat and _tl_compat.coroutine or coroutine; local pairs = _tl_compat and _tl_compat.pairs or pairs; local table = _tl_compat and _tl_compat.table or table; local resume = coroutine.resume

local CoroProcessor = {}
CoroProcessor.__index = CoroProcessor
CoroProcessor = {}









local CoroProcessor_mt = {
__index = CoroProcessor,
}

function CoroProcessor.new()
local self = setmetatable({}, CoroProcessor)
self.coros = {}
self.messages = {}
return self
local self = setmetatable({}, CoroProcessor_mt)
self.coros = {}
self.messages = {}
return self
end

function CoroProcessor:sendMessage(queuename, message)
local tbl = self.messages[queuename]
if tbl then
table.insert(tbl, message)
end
local tbl = self.messages[queuename]
if tbl then
table.insert(tbl, message)
end
end

function CoroProcessor:push(queuename, func, ...)
local q = self.coros[queuename]
if not q then
self.coros[queuename] = {}
self.messages[queuename] = {}
q = self.coros[queuename]
end
table.insert(q, coroutine.create(func))
if select("#", ...) ~= 0 then
resume(q[#q], ...)
end
local q = self.coros[queuename]
if not q then
self.coros[queuename] = {}
self.messages[queuename] = {}
q = self.coros[queuename]
end
table.insert(q, coroutine.create(func))
if select("#", ...) ~= 0 then
resume(q[#q], ...)
end
end

function CoroProcessor:update()
for k, v in pairs(self.coros) do
if #v >= 1 then
local msgs = self.messages[k]
local msg
if #msgs >= 1 then
msg = msgs[1]
table.remove(msgs, 1)
end
local ret
if msg then
ret = resume(v[1], msg)
else
ret = resume(v[1])
end
if not ret then
table.remove(v, 1)
if v[1] then
resume(v[1])
end
for k, v in pairs(self.coros) do
if #v >= 1 then
local msgs = self.messages[k]
local msg
if #msgs >= 1 then
msg = msgs[1]
table.remove(msgs, 1)
end
local ret
if msg then
ret = resume(v[1], msg)
else
ret = resume(v[1])
end
if not ret then
table.remove(v, 1)
if v[1] then
resume(v[1])
end
end
end
end
end
end
end

return CoroProcessor
Loading

0 comments on commit 3ec9075

Please sign in to comment.