Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local API = {}
- local button={}
- --local l1 = h-3
- --local l2 = h-4
- --local l3 = h-5
- --local l4 = h-6
- --local l5 = h-7
- --local l6 = h-8
- --local l7 = h-9
- local component = require("component")
- local colors = require("colors")
- local term = require("term")
- local gpu = component.gpu
- local w, h = gpu.getResolution()
- local mlib = require "mathlib"
- buttonStatus = nil
- function API.write(num, color, text)
- num=tonumber(num)
- color=tonumber(color)
- if num > 0 and num < 8 then
- local val = num + 1
- gpu.setBackground(mlib["black"])
- gpu.setForeground(color)
- -- x,y=term.getCursor()
- term.setCursor(3,h-val)
- term.write(text)
- gpu.setForeground(mlib["white"])
- -- term.setCursor(x,y)
- end
- end
- function API.clearLine(line)
- local val
- local num = line
- if num and num > 0 and num < 8 then
- val = num + 2
- term.setCursor(3, h-val)
- gpu.setBackground(mlib["black"])
- for i=3,w-4 do gpu.set(i,h-val," ") end
- end
- end
- function API.clearTerm()
- -- term.setCursor(3, h-8)
- gpu.setBackground(0x000000)
- gpu.fill(3, h-8, w-4, 7, " ")
- term.setCursor(3,h-2)
- end
- function API.clearButtons()
- button = {}
- gpu.setBackground(mlib["black"])
- gpu.fill(2, 2, w-4, h-11, " ")
- end
- function API.setTable(name, func, xmin, xmax, ymin, ymax, draw, displayName, drawColor)
- if displayName == "" then displayName = name end
- drawColor = drawColor or mlib["green"]
- 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
- button[name]["draw"] = draw
- button[name]["displayName"] = displayName
- button[name]["color"] = drawColor
- 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 = gpu.setBackground(tonumber(bData["color"]))
- gpu.fill(bData["xmin"], bData["ymin"], (bData["xmax"]-bData["xmin"]+1), (bData["ymax"]-bData["ymin"]+1), " ")
- gpu.set(xspot, yspot, text)
- gpu.setBackground(oldColor)
- end
- function API.screen()
- local currColor = black
- for name,data in pairs(button) do
- local on = data["active"]
- if on == true then currColor = mlib["green"] else currColor = mlib["red"] end
- if data["draw"] == true then
- API.fill(data["displayName"], 0x00ff000, data)
- -- API.fill(name, currColor, data)
- end
- end
- end
- function API.hideButtons(index)
- index = index or "all"
- if index == "all" then
- for name,data in pairs(button) do
- data[name]["draw"] = false
- API.clearButtons()
- API.screen()
- end
- else
- indices = parse(index)
- for i in #ndices do
- button[i]["draw"] = false
- end
- API.clearButtons()
- API.screen()
- end
- end
- function parse(str)
- local words = {}
- for c in string.gmatch(str, "%w+") do
- table.insert(words, c)
- end
- return words
- 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"] and data["draw"] == true then
- data["func"]()
- return true
- end
- end
- end
- return false
- end
- function API.heading(text)
- w, h = gpu.getResolution()
- term.setCursor((w-string.len(text))/2+1, 1)
- term.write(text)
- end
- function API.label(text)
- term.setCursor((w-string.len(text))/2+3, h)
- term.write(text)
- end
- function API.cursor(x,y)
- local oldX, oldY = term.getCursor()
- term.setCursor(x,y)
- return oldX, oldY
- end
- return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement