Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitorState
- local mon = term
- local button = {}
- local color = {
- normale = {
- text = colors.white,
- background = colors.blue
- },
- clicked = {
- text = colors.blue,
- background = colors.white
- }
- }
- function setMonitor(state)
- monitorState = state
- if monitorState then
- for i,name in pairs(peripheral.getNames()) do
- for j,method in pairs(peripheral.getMethods(name)) do
- if (method == 'isColor') then
- mon = peripheral.wrap(name)
- mon.clear()
- mon.setTextScale(1)
- local x, y = mon.getSize()
- if x < 40 or y < 27 then
- mon.setTextScale(0.5)
- else
- mon.setTextScale(1)
- end
- end
- end
- end
- else
- mon = term
- end
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- end
- function setColors(c1, c2, c3, c4)
- color = {
- normale = {
- text = c2,
- background = c1
- },
- clicked = {
- text = c4,
- background = c3
- }
- }
- end
- function setTitle(text)
- w, h = mon.getSize()
- mon.setCursorPos((w-string.len(text))/2+1, 1)
- mon.write(text)
- end
- function setClearLabel(w, h, text)
- mon.setCursorPos(w, h)
- mon.clearLine()
- mon.write(text)
- end
- function setLabel(w, h, text)
- mon.setCursorPos(w, h)
- mon.write(text)
- end
- function setColorLabel(w, h, c1, c2, text)
- mon.setBackgroundColor(c1)
- mon.setTextColor(c2)
- mon.setCursorPos(w, h)
- mon.write(text)
- end
- function clearButtons()
- button = {}
- end
- function setButton(name, func, xmin, xmax, ymin, ymax, state, extra)
- button[name] = {}
- button[name]["func"] = func
- button[name]["extra"] = extra
- button[name]["active"] = false
- button[name]["show"] = state
- button[name]["xmin"] = xmin
- button[name]["ymin"] = ymin
- button[name]["xmax"] = xmax
- button[name]["ymax"] = ymax
- end
- function setButtonShow(name, data)
- button[name]["show"] = data
- end
- function Clear()
- mon.clear()
- end
- function onScreen()
- local background
- local text
- for name,data in pairs(button) do
- if data["show"] then
- if mon.isColor then
- if (data["active"]) then
- background = color.clicked.background
- text = color.clicked.text
- else
- background = color.normale.background
- text = color.normale.text
- end
- else
- if (data["active"]) then
- background = colors.white
- text = colors.black
- else
- background = colors.black
- text = colors.white
- end
- end
- mon.setBackgroundColor(background)
- mon.setTextColor(text)
- local yspot = math.floor((data["ymin"] + data["ymax"]) /2)
- local xspot = math.floor((data["xmax"] - data["xmin"] - string.len(name)) /2) +1
- for j = data["ymin"], data["ymax"] do
- mon.setCursorPos(data["xmin"]-2, j)
- if j == yspot then
- mon.write(name)
- else
- for i = data["xmin"], data["xmax"] do
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- end
- end
- end
- function toggleButton(name)
- button[name]["active"] = not button[name]["active"]
- onScreen()
- end
- function flashButton(name)
- toggleButton(name)
- onScreen()
- sleep(0.15)
- toggleButton(name)
- onScreen()
- end
- function buttonPressed(x, y)
- for name, data in pairs(button) do
- if data["show"] then
- if y>=data["ymin"] and y <= data["ymax"] then
- if x>=data["xmin"] and x<= data["xmax"] then
- flashButton(name)
- if data["extra"] then
- data["func"](data["extra"])
- else
- data["func"]()
- end
- return true
- end
- end
- end
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment