local touchpoint = {}
local function setupLabel(buttonLen, minY, maxY, name)
local labelTable = {}
if type(name) == "table" then
for i = 1, #name do
labelTable[i] = name[i]
end
name = name.label
elseif type(name) == "string" then
local buttonText = string.sub(name, 1, buttonLen - 2)
if #buttonText < #name then
buttonText = " " .. buttonText .. " "
else
local labelLine = string.rep(" ", math.floor((buttonLen -
#buttonText) / 2)) .. buttonText
buttonText = labelLine .. string.rep(" ", buttonLen - #labelLine)
end
for i = 1, maxY - minY + 1 do
if maxY == minY or i == math.floor((maxY - minY) / 2) + 1 then
labelTable[i] = buttonText
else
labelTable[i] = string.rep(" ", buttonLen)
end
end
end
return labelTable, name
end
-- Table com funções para manipular botões
local Button = {}
function Button:draw()
print("Desenhando botões...") -- Log de depuração
local old = term.redirect(self.mon)
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
term.clear()
for name, buttonData in pairs(self.buttonList) do
if buttonData.active then
term.setBackgroundColor(buttonData.activeColor)
term.setTextColor(buttonData.activeText)
else
term.setBackgroundColor(buttonData.inactiveColor)
term.setTextColor(buttonData.inactiveText)
end
for i = buttonData.yMin, buttonData.yMax do
term.setCursorPos(buttonData.xMin, i)
term.write(buttonData.label[i - buttonData.yMin + 1])
end
end
if old then
term.redirect(old)
else
term.restore()
end
end
function Button:add(name, func, xMin, yMin, xMax, yMax, inactiveColor, activeColor,
inactiveText, activeText)
print("Adicionando botão: " .. name) -- Log de depuração
local label, name = setupLabel(xMax - xMin + 1, yMin, yMax, name)
if self.buttonList[name] then error("button already exists", 2) end
local x, y = self.mon.getSize()
if xMin < 1 or yMin < 1 or xMax > x or yMax > y then error("button out of
bounds", 2) end
self.buttonList[name] = {
func = func,
xMin = xMin,
yMin = yMin,
xMax = xMax,
yMax = yMax,
active = false,
inactiveColor = inactiveColor or colors.red,
activeColor = activeColor or colors.lime,
inactiveText = inactiveText or colors.white,
activeText = activeText or colors.white,
label = label,
}
for i = xMin, xMax do
for j = yMin, yMax do
if self.clickMap[i][j] ~= nil then
-- Undo changes
for k = xMin, xMax do
for l = yMin, yMax do
if self.clickMap[k][l] == name then
self.clickMap[k][l] = nil
end
end
end
self.buttonList[name] = nil
error("overlapping button", 2)
end
self.clickMap[i][j] = name
end
end
end
function Button:run()
print("Iniciando execução...") -- Log de depuração
while true do
self:draw()
local event = {self:handleEvents(os.pullEvent(self.side == "term" and
"mouse_click" or "monitor_touch"))}
if event[1] == "button_click" then
self.buttonList[event[2]].func()
end
end
end
function Button:handleEvents(...)
local event = {...}
if #event == 0 then event = {os.pullEvent()} end
if (self.side == "term" and event[1] == "mouse_click") or (self.side ~= "term"
and event[1] == "monitor_touch" and event[2] == self.side) then
local clicked = self.clickMap[event[3]][event[4]]
if clicked and self.buttonList[clicked] then
return "button_click", clicked
end
end
return unpack(event)
end
function touchpoint.new(monSide)
print("Criando nova instância de touchpoint...") -- Log de depuração
local buttonInstance = {
side = monSide or "term",
mon = monSide and peripheral.wrap(monSide) or term.current(),
buttonList = {},
clickMap = {},
}
local x, y = buttonInstance.mon.getSize()
for i = 1, x do
buttonInstance.clickMap[i] = {}
end
setmetatable(buttonInstance, {__index = Button})
return buttonInstance
end
return touchpoint