Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local API = {}
- local button = {}
- local component = require("component")
- local gpu = component.gpu
- local event = require("event")
- function API.clearTable()
- button = {}
- end
- function API.setTable(name, func, xmin, ymin, xmax, ymax, text, colors) -- color is an object { on : 0x000000, off 0xAAAAAA}
- button[name] = {}
- button[name]["text"] = text
- button[name]["name"] = 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
- button[name]["colors"] = colors
- end
- function API.fill(bData)
- local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
- local xspot = math.floor((bData["xmin"] + bData["xmax"]) /2) - math.floor((string.len(bData["text"])/2))
- local oldColor = gpu.getBackground()
- local curColor = bData["colors"].on
- local curText = " "
- if type(bData["text"]) == "table" then
- curColor = bData["text"].on
- if bData["active"] then
- curColor = bData["text"].off
- end
- else
- curText = bData["text"]
- end
- if bData["active"] then
- curColor = bData["colors"].off
- end
- gpu.setBackground(curColor)
- gpu.fill(bData["xmin"], bData["ymin"], bData["xmax"] - bData["xmin"] + 1, bData["ymax"] - bData["ymin"] + 1, " ")
- gpu.set(xspot, yspot, curText)
- gpu.setBackground(oldColor)
- end
- function API.screen()
- for name,data in pairs(button) do
- API.fill(data)
- end
- end
- function API.toggleButton(bData)
- bData["active"] = not bData["active"]
- 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"]()
- toggleButton(data)
- return true
- end
- end
- end
- return false
- end
- return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement