Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Button API for Advanced Computers
- This was originally developed for monitors
- adapted for computers
- This was originally designed by Direwolf20
- Updated by GNOOR1S/LostboyDev/Gunnar Jessee
- ]]
- term.setTextColor(colors.white)
- local button={}
- term.setBackgroundColor(colors.black)
- function clearTable()
- button = {}
- term.clear()
- end
- function setTable(name, func, xmin, ymin, xmax, ymax, col)
- 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]["color"] = col
- end
- function fill(text, color, bData)
- term.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
- term.setCursorPos(bData["xmin"], j)
- if j == yspot then
- for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
- if k == xspot then
- term.write(text)
- else
- term.write(" ")
- end
- end
- else
- for i = bData["xmin"], bData["xmax"] do
- term.write(" ")
- end
- end
- end
- term.setBackgroundColor(colors.black)
- end
- -- Refreshes screen
- function screen()
- local currColor
- for name,data in pairs(button) do
- local on = data["active"]
- if on == true then currColor = colors.lime else currColor = colors.red end
- fill(name, currColor, data)
- end
- end
- -- Toggles on and off color
- function toggleButton(name)
- button[name]["active"] = not button[name]["active"]
- screen()
- end
- -- Put into the function that is contained in your button
- function flash(name)
- toggleButton(name)
- screen()
- sleep(0.15)
- toggleButton(name)
- screen()
- 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
- end
- end
- end
- return false
- end
- -- Centers text at the top of the computer
- function heading(text)
- local w, h = term.getSize()
- term.setCursorPos((w-string.len(text))/2+1, 1)
- term.write(text)
- end
- -- Just writes text has if it were a button
- -- WIP
- function label(w, h, text)
- term.setCursorPos(w, h)
- term.write(text)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement