Hi, i would like to know how we can do a text that when we touch it execute some code. Thanks for your help!
main.lua
local component = require("component")
local event = require("event")
local computer = require("computer")
local gpu = component.gpu
local term = require("term")
local version = "1.0"
local h, w = gpu.getResolution()
API = require("buttonAPI")
local function base()
gpu.setBackground(0x0000cc)
gpu.fill(1,1,h,1," ")
gpu.set((h / 2) - 5,1,"TopiGui v"..version)
gpu.setBackground(0xcccccc)
gpu.fill(1,2,h,w, " ")
while true do
gpu.setBackground(0x0000cc)
memoryPercentage = computer.freeMemory() * 100 / computer.totalMemory()
energyPercentage = computer.energy() * 100 / computer.maxEnergy()
gpu.set(h / 1.25,1,"RAM: "..string.format(" %2.0f", memoryPercentage).." % Free")
gpu.set(h / 1.1,1,"Energy: "..string.format(" %2.0f", energyPercentage).." % ")
os.sleep(1)
end
end
function API.fillTable()
API.setTable("Exit", exit, 10,20,3,5)
API.screen()
end
function getClick()
local _, _, x, y = event.pull(1,touch)
if x == nil or y == nil then
local h, w = gpu.getResolution()
gpu.set(h, w, ".")
gpu.set(h, w, " ")
else
API.checkxy(x,y)
end
end
function tor()
computer.shutdown()
end
base()
API.fillTable()
while true do
getClick()
end
buttonAPI.lua
local API = {}
local button={}
local component = require("component")
local colors = require("colors")
local term = require("term")
local mon = component.gpu
local w, h = mon.getResolution()
local Green = 0x00AA00
local Red = 0xAA0000
local Black = 0x000000
buttonStatus = nil
function API.clear()
mon.setBackground(Black)
mon.fill(1, 1, w, h, " ")
end
function API.clearTable()
button = {}
API.clear()
end
function API.setTable(name, func, xmin, xmax, ymin, ymax)
button[name] = {}
button[name]["func"] = func
button[name]["active"] = false
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
end
function API.fill(text, color, bData)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] + bData["xmin"] - string.len(text)) /2)+1
local oldColor = mon.setBackground(color)
mon.fill(bData["xmin"], bData["ymin"], (bData["xmax"]-bData["xmin"]+1), (bData["ymax"]-bData["ymin"]+1), " ")
mon.set(xspot, yspot, text)
mon.setBackground(oldColor)
end
function API.screen()
local currColor
for name,data in pairs(button) do
local on = data["active"]
if on == true then currColor = Green else currColor = Red end
API.fill(name, currColor, data)
end
end
function API.toggleButton(name)
button[name]["active"] = not button[name]["active"]
buttonStatus = button[name]["active"]
API.screen()
end
function API.flash(name,length)
API.toggleButton(name)
API.screen()
os.sleep(length)
API.toggleButton(name)
API.screen()
end
function API.checkxy(x, y)
for name, data in pairs(button) do
if y>=data["ymin"] and y <= data["ymax"] then
if x>=data["xmin"] and x<= data["xmax"] then
data["func"]()
return true
end
end
end
return false
end
function API.heading(text)
w, h = mon.getResolution()
term.setCursor((w-string.len(text))/2+1, 1)
term.write(text)
end
function API.label(w, h, text)
term.setCursor(w, h)
term.write(text)
end
return API