Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mon = nil
- local button={}
- local defaultOnCol, defaultOffCol
- function init(p)
- defaultOnCol = colors.lime
- defaultOffCol = colors.red
- mon = p
- mon.clear()
- mon.setTextColor(colors.white)
- mon.setBackgroundColor(colors.black)
- button = {}
- end
- function setTextScale(s)
- mon.setTextScale(1)
- end
- function clear()
- button = {}
- mon.clear()
- end
- function add(id, name, x, y, w, h)
- button[id] = {}
- button[id]["name"] = name
- button[id]["active"] = false
- button[id]["xmin"] = x
- button[id]["ymin"] = y
- button[id]["xmax"] = x + w
- button[id]["ymax"] = y + h
- button[id]["colOn"] = defaultOnCol
- button[id]["colOff"] = defaultOffCol
- end
- function setDefaultColors(colOn, colOff)
- defaultOnCol = colOn
- defaultOffCol = colOff
- end
- function setColors(id, colOn, colOff)
- button[id]["colOn"] = colOn
- button[id]["colOff"] = colOff
- end
- function fill(text, color, bData)
- mon.setBackgroundColor(color)
- local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
- local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
- for j = bData["ymin"], bData["ymax"] do
- mon.setCursorPos(bData["xmin"], j)
- if j == yspot then
- for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
- if k == xspot then
- mon.write(text)
- else
- mon.write(" ")
- end
- end
- else
- for i = bData["xmin"], bData["xmax"] do
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- end
- function render()
- for id,data in pairs(button) do
- local currColor = colors.black
- local on = data["active"]
- if on == true then currColor = data["colOn"] else currColor = data["colOff"] end
- fill(data["name"], currColor, data)
- end
- end
- function toggle(name)
- setOn(name, not isOn(name))
- return isOn(name)
- end
- function isOn(name)
- return button[name]["active"]
- end
- function setOn(name, on)
- button[name]["active"] = on
- render()
- end
- function flash(name)
- setOn(name, true)
- sleep(0.07)
- setOn(name, false)
- sleep(0.07)
- end
- function checkxy(x, y)
- for id, data in pairs(button) do
- if y>=data["ymin"] and y <= data["ymax"] then
- if x>=data["xmin"] and x<= data["xmax"] then
- os.queueEvent("button", id)
- return true
- end
- end
- end
- return false
- end
- function heading(text)
- w, h = mon.getSize()
- mon.setCursorPos((w-string.len(text))/2+1, 1)
- mon.write(text)
- end
- function label(w, h, text)
- mon.setCursorPos(w, h)
- mon.write(text)
- end
Advertisement
Add Comment
Please, Sign In to add comment