Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Original by direwolf20
- -- Edited by arshia111
- -- Thanks to GufNZ for helping me out
- -- VERSIONS
- -- v1.0, July 16, 2013 - Initial Release
- -- v1.1, April 13, 2014 - A few improvements
- -- v2.0, April 14, 2014 - Button rendering using windows
- -- v2.1, April 15, 2014 - Fix in the text color rendering
- local monSide = "back"
- local backgroundColor = colors.blue
- local textColor = colors.white
- local textScale = 1
- local mon = peripheral.wrap(monSide)
- mon.setTextScale(textScale)
- mon.setTextColor(textColor)
- mon.setBackgroundColor(backgroundColor)
- local button={}
- -- 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, arg, parent, x, y, width, height, tcolor, bcolorOn, bcolorOff)
- button[name] = {}
- button[name]["window"] = window.create(parent, x, y, width, height)
- button[name]["func"] = func
- button[name]["arg"] = arg
- button[name]["active"] = false
- button[name]["x"] = x
- button[name]["y"] = y
- button[name]["width"] = width
- button[name]["height"] = height
- button[name]["tcolor"] = tcolor
- button[name]["bcolorOn"] = bcolorOn
- button[name]["bcolorOff"] = bcolorOff
- end
- function clearTable()
- button = {}
- mon.clear()
- end
- function getActive(name)
- return button[name]["active"]
- end
- function screen()
- for name,data in pairs(button) do
- local bgColor
- if data["active"] then
- bgColor = data["bcolorOn"] or colors.lime
- else
- bgColor = data["bcolorOff"] or colors.red
- end
- local tColor = data["tcolor"] or colors.white
- local xspot = math.floor((data["width"] - string.len(name)) / 2) + 1
- local yspot = math.ceil(data["height"] / 2)
- data["window"].setBackgroundColor(bgColor)
- data["window"].clear()
- data["window"].setCursorPos(xspot, yspot)
- data["window"].setTextColor(tColor)
- data["window"].write(name)
- end
- end
- function checkxy(cx, cy)
- for name, data in pairs(button) do
- if cy >= data["y"] then
- if cy <= (data["height"] + data["y"] - 1) then
- if cx >= data["x"] then
- if cx <= (data["width"] + data["x"] - 1) then
- if data["arg"] == nil then
- data["func"]()
- else
- data["func"](data["arg"])
- end
- return true
- end
- end
- end
- end
- end
- return false
- 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 centerText(text, y)
- w, h = mon.getSize()
- if (w-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
Advertisement