Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Edited by arshia111
- -- Thanks to GufNZ for helping me out
- local mon = peripheral.wrap("top")
- mon.setTextScale(1)
- mon.setTextColor(colors.white)
- local button={}
- backgroundColor = colors.blue
- mon.setBackgroundColor(backgroundColor)
- -- If you want the default
- -- "green when on, red when off, white text" button
- -- just set tcolor, bcolorOn and bcolorOff to nil
- function setTable(name, func, xmin, xmax, ymin, ymax, tcolor, bcolorOn, bcolorOff)
- 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]["tcolor"] = tcolor
- button[name]["bcolorOn"] = bcolorOn
- button[name]["bcolorOff"] = bcolorOff
- end
- function clearTable()
- button = {}
- mon.clear()
- end
- function funcName()
- print("You clicked buttonText")
- end
- function fillTable()
- setTable("ButtonText", funcName, 5, 25, 4, 8)
- 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(backgroundColor)
- end
- function screen()
- local currColor
- for name,data in pairs(button) do
- mon.setTextColor(data["tcolor"] or colors.white)
- local on = data["active"]
- if on == false then
- currColor = data["bcolorOff"] or colors.red
- elseif on == true then
- currColor = data["bcolorOn"] or colors.lime
- end
- fill(name, currColor, data)
- end
- end
- function toggleButton(name)
- button[name]["active"] = not button[name]["active"]
- screen()
- end
- function flashButton(name, time)
- toggleButton(name)
- sleep(time)
- toggleButton(name)
- end
- function 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
- --data["active"] = not data["active"]
- --print(name)
- end
- end
- end
- return false
- end
- function centerText(text, y)
- w, h = mon.getSize()
- if string.len(text) > w then
- print("The text to be centered at line "..y.." is too long!")
- end
- if y > h then
- print("The text on line "..y.." is too low!")
- end
- mon.setCursorPos((w-string.len(text))/2+1, y)
- 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