Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cmp = require("component")
- local os = require("os")
- local table = require("table")
- local gpu = cmp.gpu
- local term = require("term")
- local event = require("event")
- local uc = require("unicode")
- local ret = {}
- local widgets = {}
- function ret.getWidgets()
- return widgets
- end
- function ret.drawProgressBar(x, y, width, current, max, color1, color2)
- term.setCursor(x, y)
- local prc = current * 100 / max
- local prc_w = prc * width / 100
- local inText = current.."/"..max
- ret.drawSpaces(x, y, color1, prc_w)
- ret.drawSpaces(x + prc_w, y, color2, width-prc_w-1)
- end
- function ret.addButton(x, y, title, color, color_active, isSwitch, callback)
- local lastID = #widgets+1
- table.insert(widgets, {})
- widgets[lastID]["x"] = x
- widgets[lastID]["y"] = y
- widgets[lastID]["title"] = title
- widgets[lastID]["color"] = color
- widgets[lastID]["color_active"] = color_active
- widgets[lastID]["callback"] = callback
- widgets[lastID]["isSwitch"] = isSwitch
- widgets[lastID]["state"] = "idle"
- widgets[lastID]["type"] = "button"
- return lastID
- end
- function ret.addLabel(x, y, width, colorFG, colorBG, title)
- local lastID = #widgets+1
- table.insert(widgets, {})
- widgets[lastID]["type"] = "label"
- widgets[lastID]["x"] = x
- widgets[lastID]["y"] = y
- widgets[lastID]["width"] = width
- widgets[lastID]["colorFG"] = colorFG
- widgets[lastID]["colorBG"] = colorBG
- widgets[lastID]["title"] = title
- return lastID
- end
- function ret.drawSpaces(x, y, color, num)
- local oldBG = gpu.setBackground(color)
- term.setCursor(x, y)
- for i = 1, num do term.write(" ") end
- gpu.setBackground(oldBG)
- end
- function ret.printAt(x, y, colorFG, colorBG, text)
- local oldBG = gpu.setBackground(colorBG)
- local oldFG = gpu.setForeground(colorFG)
- term.setCursor(x, y)
- term.write(text)
- gpu.setBackground(oldBG)
- gpu.setForeground(oldFG)
- end
- function drawButton(x, y, title, color)
- local oldBG = gpu.setBackground(color)
- ret.drawSpaces(x, y, color, uc.len(title)+4)
- term.setCursor(x, y+1)
- term.write(" "..title.." ")
- ret.drawSpaces(x, y+2, color, uc.len(title)+4)
- gpu.setBackground(oldBG)
- end
- function drawLabel(key)
- local excess = key["width"] - uc.len(key["title"])
- ret.drawSpaces(key["x"]+uc.len(key["title"]), key["y"], key["colorBG"], excess)
- ret.printAt(key["x"], key["y"], key["colorFG"], key["colorBG"], key["title"])
- end
- function drawButtons(key)
- if key["type"] == "button" then
- if key["state"] == "idle" then
- drawButton(key["x"], key["y"], key["title"], key["color"])
- else
- drawButton(key["x"], key["y"], key["title"], key["color_active"])
- end
- if key["isSwitch"] == false then
- if key["state"] == "active" then
- key["state"] = "idle"
- end
- end
- end
- end
- function ret.drawUI()
- for elem, key in pairs(widgets) do
- if key["type"] == "button" then drawButtons(key) end
- if key["type"] == "label" then drawLabel(key) end
- end
- end
- function ret.callbackTouch(addr, x, y, btn, player)
- for elem, key in pairs(widgets) do
- if x >= key["x"] and x <= key["x"]+uc.len(key["title"])+4 then
- if y >= key["y"] and y <= key["y"]+2 then
- if key["isSwitch"] then
- if key["state"] == "idle" then
- widgets[elem]["state"] = "active"
- else
- widgets[elem]["state"] = "idle"
- end
- else
- widgets[elem]["state"] = "active"
- end
- pcall(key["callback"], widgets[elem]["state"])
- end
- end
- end
- end
- --event.listen("touch", callbackTouch)
- return ret;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement