Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --1.0 added centerTextXY and centerTextX for api by overwol
- --2.0 added button colors background custom for click This button or not clicked
- --2.1 recod api for optimizing api
- function centerTextXY(text,htext)
- local w,h = term.getSize()
- if htext == nil then
- htext = 0
- end
- local htext = tonumber(htext)
- term.setCursorPos(math.floor(w/2-text:len()/2+.5), math.floor(h/2+0.5+htext))
- io.write(text)
- end
- function centerTextX(text,htext)
- local w,h = term.getSize()
- term.setCursorPos(math.floor(w/2-text:len()/2+.5), htext)
- io.write(text)
- end
- function heading(text)
- w, h = term.getSize()
- term.setCursorPos((w-string.len(text))/2+1, 1)
- term.write(text)
- end
- function setTable(name, textl1, textl2, func, funcv, xmin, ymin, xmax, ymax, textColor, backgroundColorOn, backgroundColorOff, active)
- button[name] = {}
- button[name]["textl1"] = textl1
- button[name]["textl2"] = textl2
- button[name]["func"] = func
- button[name]["funcv"] = funcv
- if active == nil then
- button[name]["active"] = false
- else
- button[name]["active"] = active
- end
- button[name]["xmin"] = xmin
- button[name]["ymin"] = ymin
- button[name]["xmax"] = xmax
- button[name]["ymax"] = ymax
- button[name]["textColor"] = textColor
- button[name]["backgroundColorOn"] = backgroundColorOn
- button[name]["backgroundColorOff"] = backgroundColorOff
- end
- function screen()
- local currColor
- for name,data in pairs(button) do
- term.setTextColor(data["textColor"] or colors.white)
- local on = data["active"]
- if on == false then
- currColor = data["backgroundColorOff"] or colors.red
- elseif on == true then
- currColor = data["backgroundColorOn"] or colors.lime
- end
- fill(name, currColor, data)
- end
- end
- function fill(name, color, bData)
- term.setBackgroundColor(color)
- local text1 = bData["textl1"]
- local text2 = bData["textl2"]
- local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
- local yspot2 = math.floor((bData["ymin"] + bData["ymax"]) /2+1)
- local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text1)) /2) +1
- local xspot2 = math.floor((bData["xmax"] - bData["xmin"] - string.len(text2)) /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(text1) +1 do
- if k == xspot then
- term.write(text1)
- else
- term.write(" ")
- end
- end
- elseif j == yspot2 then
- for k = 0, bData["xmax"] - bData["xmin"] - string.len(text2) +1 do
- if k == xspot then
- term.write(text2)
- else
- term.write(" ")
- end
- end
- else
- for i = bData["xmin"], bData["xmax"] do
- term.write(" ")
- end
- end
- end
- term.setBackgroundColor(colors.black)
- end
- function toggleButton(name)
- button[name]["active"] = not button[name]["active"]
- screen()
- 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
- if y>=data["ymin"] and y <= data["ymax"] then
- if x>=data["xmin"] and x<= data["xmax"] then
- funcvar = data["funcv"]
- if funcvar == "" then
- data["func"]()
- else
- data["func"](data["funcv"])
- end
- return true
- --data["active"] = not data["active"]
- --print(name)
- end
- end
- end
- return false
- end
- function getClick()
- local event,side,x,y = os.pullEvent("monitor_touch")
- checkxy(x,y)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement