Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Created by DireWolf20
- Cleaned up by RandomShovel
- There may be bugs!
- ]]--
- --[[ Config ]]--
- monLocat = "top"
- --[[ Tables ]]--
- local button={}
- local buttonHist={}
- --[[ Variables ]]--
- local mon = peripheral.wrap(monLocat)
- --[[ Fix Monitor ]]--
- mon.setTextScale(1)
- mon.setTextColor(colors.white)
- mon.setBackgroundColor(colors.black)
- --[[ Functions ]]--
- function clearTable()
- button = {}
- mon.clear()
- end
- function setTable(name, func, xmin, xmax, ymin, ymax)
- 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
- end
- -- Debugging purposes
- 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) -- Averages y positions for text
- local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1 -- Averages x positions for text
- for j = bData["ymin"], bData["ymax"] do -- Fills the button
- mon.setCursorPos(bData["xmin"], j)
- if j == yspot then -- If current position is where text's Y level should be
- for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
- if k == xspot then -- If current position is where text's starting point should be
- mon.write(text)
- else -- If not at text's positions, write space, or "Button"
- mon.write(" ")
- end
- end
- else
- for i = bData["xmin"], bData["xmax"] do
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- end
- function screen()
- local currColor
- for name,data in pairs(button) do -- Gathers data from button in pairs
- local on = data["active"]
- if on == true then
- currColor = colors.lime -- If button is active, change color to lime
- else
- currColor = colors.red -- If button is inactive, change color to red
- end
- fill(name, currColor, data) -- Populate screen
- end
- end
- function toggleButton(name)
- button[name]["active"] = not button[name]["active"] -- Inverts current button
- screen()
- end
- function isActive(bName)
- local on = button[bName]["active"]
- if on == true then
- return true
- else
- return false
- end
- end
- function toggleFlash(name)
- if #buttonHist > 0 then
- if name == buttonHist[1] then
- table.remove(buttonHist, 1)
- else
- toggleButton(buttonHist[1])
- table.remove(buttonHist, 1)
- table.insert(buttonHist, name)
- end
- else
- table.insert(buttonHist, name)
- end
- toggleButton(name)
- end
- function flash(name)
- toggleButton(name)
- screen()
- sleep(0.15)
- toggleButton(name)
- screen()
- end
- function checkxy(x, y)
- for name, data in pairs(button) do -- Grabing data from button in pairs
- if y>=data["ymin"] and y <= data["ymax"] then -- If y is in button's y range
- if x>=data["xmin"] and x<= data["xmax"] then -- If x is in button's x range
- data["func"]() -- Run function
- return true
- -- Debugging
- --data["active"] = not data["active"]
- --print(name)
- end
- end
- end
- return false
- end
- function heading(text)
- w, h = mon.getSize() -- Get monitor size
- mon.setCursorPos((w-string.len(text))/2+1, 1) -- Positions header
- 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