Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------term---MENU/PANEL API----------------------
- --------------------by elektrobom1!--------------------
- ---Copyright (c) 2021-2022 9551------------9551#0001---
- ---using this code in your project is fine!------------
- ---as long as you dont claim you made it---------------
- ---im cool with it, feel free to include---------------
- ---in your projects! discord: 9551#0001--------------
- ---you dont have to but giving credits is nice :)------
- -------------------------------------------------------
- --this is term version of 9551s button API this is highly beta!
- -------------------------------------------------------
- --*! pastebin get LTDZZZEJ button
- --*functions usages
- --*API(ins, cord1, cord2, length, height)
- --*timetouch(timeout)
- --*touch()
- --*button(lr,ins, cord1, cord2, text)
- --*counter(lr, ins, cc, cord1, cord2, cv, max, min, col)
- --*fill(pos1, pos2, length, height)
- --*switch(lr, ccins, pos1, pos2, col1, col2, col3, text)
- --*switchn(lr, cc, ins, pos1, pos2, col1, col2, col3, text, text2)
- --*bundle(side, color, state)
- --*signal(side, ins, col)
- --*sliderVer/Hor(lr, ins, cc, pos1, pos2, length, color1, textCol)
- --*local function menu(lr, ins, cc, x, y, textcol, switchcol, text, returns1, more, returns2) (returns menuout)
- --*bar(pos1, pos2, length, height, ins, max, color1, color2, color3, printval, hor, text, format, rect, thicc)
- --*timeswitch(lr, ins, cc, pos1, pos2, change, start, col1, col2, col3, repeats)
- --*render(text,x,y,textcol,backCol)
- --*menudata()
- --*frame(pos1, pos2, length, height, color3, color1, thicc)
- --*db allows you to get stored data from all functions that use stored data!
- --*how to use db: in your function for example switch you do b.switch("db",<data>) data is the storing position you want to get data of
- --base API this API is made for detecting clicks in an area
- local function API(ins, cord1, cord2, length, height)
- if ins == true then
- ins = {os.pullEvent("mouse_click")}
- end
- if ins[3] >= cord1 and ins[3] <= cord1 + length-1 then
- if ins[4] >= cord2 and ins[4] <= cord2 + height-1 then
- return true
- else
- return false
- end
- end
- end
- local function touch()
- local dats = {os.pullEvent("mouse_click")}
- return dats
- end
- local function timetouch(timeout)
- local timer = os.startTimer(timeout)
- while true do
- local event = {os.pullEvent()}
- if (event[1] == "timer") and (event[2] == timer) then
- return {"timeout", "tout", 1000, 1000}
- elseif (event[1] == "mouse_click") then
- return {event[1], event[2], event[3], event[4]}
- end
- end
- end
- -------------------------------------------------------------------------------------------------
- local function button(lr, ins, cord1, cord2, text)
- if ins[2] == lr or ins[2] == "tout" then
- if ins ~= nil then
- x = API(ins, cord1, cord2, string.len(text), 1)
- term.setCursorPos(cord1, cord2)
- term.write(text)
- return x
- end
- end
- end
- local function counter(lr, ins, cc, cord1, cord2, cv, max, min, col)
- if lr == "db" then
- if data == nil then
- return nil
- else
- return data[ins]
- end
- end
- if lr == "setdb" then
- if data == nil then
- return "no data to edit"
- else
- data[ins] = cc
- return("value changed too "..type(cc))
- end
- end
- if ins[2] == lr or ins[2] == "tout" then
- if ins ~= nil then
- if data == nil then
- data = {}
- for is = 0, 1000 do
- data[is] = 0
- end
- end
- term.setCursorPos(cord1, cord2)
- term.write("\24" .. " " .. data[cc])
- term.setCursorPos(cord1, cord2 + 1)
- term.write("\25")
- if API(ins, cord1, cord2, 1, 1) == true then
- if data[cc] < max then
- data[cc] = data[cc] + cv
- term.setCursorPos(cord1, cord2)
- term.setTextColor(colors.green)
- term.write("\24" .. " " .. data[cc] .. " ")
- term.setCursorPos(cord1, cord2 + 1)
- term.setTextColor(colors.red)
- term.write("\25")
- term.setTextColor(colors[col])
- return data[cc]
- end
- end
- end
- if API(ins, cord1, cord2 + 1, 1, 1) == true then
- if data[cc] > min then
- data[cc] = data[cc] - cv
- term.setCursorPos(cord1, cord2)
- term.setTextColor(colors.green)
- term.write("\24" .. " " .. data[cc] .. " ")
- term.setCursorPos(cord1, cord2 + 1)
- term.setTextColor(colors.red)
- term.write("\25")
- term.setTextColor(colors[col])
- return data[cc]
- end
- end
- end
- end
- local function fill(pos1, pos2, length, height)
- for x = 0, height - 1 do
- term.setCursorPos(pos1, pos2 + x)
- term.write(string.rep(" ", length))
- end
- end
- local function switch(lr, cc, ins, pos1, pos2, col1, col2, col3, text)
- if lr == "db" then
- if data1 == nil then
- return nil
- else
- return data1[cc]
- end
- end
- if lr == "setdb" then
- if data1 == nil then
- return "no data to edit"
- else
- data1[cc] = ins
- return("value changed too "..type(ins))
- end
- end
- if ins[2] == lr or ins[2] == "tout" then
- if ins ~= nil then
- local re = string.len(text)
- if data1 == nil then
- data1 = {}
- for is = 0, 1000 do
- data1[is] = false
- end
- end
- local function ff()
- data1[cc] = not data1[cc]
- end
- if API(ins, pos1, pos2, string.len(text), 1) == true then
- ff()
- end
- local oldcol1 = term.getTextColor()
- local oldcol2 = term.getBackgroundColor()
- if data1[cc] == true then
- term.setBackgroundColor(colors[col2])
- else
- term.setBackgroundColor(colors[col1])
- end
- term.setCursorPos(pos1, pos2)
- term.setTextColor(colors[col3])
- term.write(text)
- term.setTextColor(oldcol1)
- term.setBackgroundColor(oldcol2)
- return (data1[cc])
- end
- end
- end
- local function switchn(lr, cc, ins, pos1, pos2, col1, col2, col3, text, text2)
- if lr == "db" then
- if data2 == nil then
- return nil
- else
- return data2[cc]
- end
- end
- if lr == "setdb" then
- if data2 == nil then
- return "no data to edit"
- else
- data2[cc] = ins
- return("value changed too "..type(ins))
- end
- end
- if ins[2] == lr or ins[2] == "tout" then
- if ins ~= nil then
- local re = string.len(text)
- if string.len(text) ~= string.len(text2) then
- if string.len(text) > string.len(text2) then
- re = string.len(text)
- else
- re = string.len(text2)
- end
- end
- if data2 == nil then
- data2 = {}
- for is = 0, 1000 do
- data2[is] = false
- end
- end
- local function ff()
- data2[cc] = not data2[cc]
- end
- if API(ins, pos1, pos2, re, 1) == true then
- ff()
- end
- local oldcol1 = term.getTextColor()
- local oldcol2 = term.getBackgroundColor()
- if data2[cc] == true then
- term.setCursorPos(pos1, pos2)
- if string.len(text) ~= string.len(text2) then
- term.write(string.rep(" ", re))
- end
- term.setBackgroundColor(colors[col2])
- term.setCursorPos(pos1, pos2)
- term.setTextColor(colors[col3])
- term.write(text2)
- else
- term.setCursorPos(pos1, pos2)
- if string.len(text) ~= string.len(text2) then
- term.write(string.rep(" ", re))
- end
- term.setBackgroundColor(colors[col1])
- term.setCursorPos(pos1, pos2)
- term.setTextColor(colors[col3])
- term.write(text)
- end
- term.setTextColor(oldcol1)
- term.setBackgroundColor(oldcol2)
- return (data2[cc])
- end
- end
- end
- -- bundle cable APIs for using main API with bundled cables
- local function bundle(side, color, state)
- if (type(side) == "string") and (type(color) == "number") and (type(state) == "boolean") then
- if state == true then
- rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side), color))
- elseif state == false then
- rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side), color))
- end
- else
- error("please use like this:\nbundle(side:string,colors.(color),state:boolean)")
- end
- end
- local function signal(side, ins, col, func)
- if ins == "clear" then
- rs.setBundledOutput(side, 0)
- else
- if func == true then
- if ins == "on" then
- ins = true
- end
- if ins == "nil" then
- ins = false
- end
- end
- if ins ~= nil then
- if ins == true then
- bundle(side, col, true)
- elseif ins == false then
- bundle(side, col, false)
- end
- end
- end
- end
- -------------------------------------------------------------------------------------------------
- local function sliderHor(lr, ins, cc, pos1, pos2, length, color1, textCol)
- if lr == "db" then
- if data3 == nil then
- return nil
- else
- return data3[ins]
- end
- end
- if lr == "setdb" then
- if data3 == nil then
- return "no data to edit"
- else
- data3[ins] = cc
- return("value changed too "..type(cc))
- end
- end
- if ins[2] == lr or ins[2] == "tout" then
- if ins ~= nil then
- local oldcol1 = term.getBackgroundColor()
- local oldcol2 = term.getTextColor()
- term.setBackgroundColor(colors[color1])
- term.setTextColor(colors[textCol])
- term.setCursorPos(pos1, pos2)
- for i = 0, length do
- term.write("-")
- term.setCursorPos(pos1 + i, pos2)
- end
- if data3 == nil then
- data3 = {}
- for is = 0, 1000 do
- data3[is] = 0
- end
- end
- local cp = (ins[3])
- if (ins[4] == pos2) and (ins[3] >= pos1) and (ins[3] <= (pos1 + length) - 1) then
- term.setCursorPos(cp, pos2)
- data3[cc] = cp
- term.write("|")
- else
- term.setCursorPos(data3[cc], pos2)
- term.write("|")
- end
- term.setBackgroundColor(oldcol1)
- term.setTextColor(oldcol2)
- if data3[cc] - pos1 >= 0 then
- return (data3[cc] - pos1)
- elseif data3[cc] - pos1 < 0 then
- return 0
- end
- end
- end
- end
- local function sliderVer(lr, ins, cc, pos1, pos2, length, color1, textCol)
- if lr == "db" then
- if data10 == nil then
- return nil
- else
- return data10[ins]
- end
- end
- if lr == "setdb" then
- if data10 == nil then
- return "no data to edit"
- else
- data10[ins] = cc
- return("value changed too "..type(cc))
- end
- end
- if ins[2] == lr or ins[2] == "tout" then
- if ins ~= nil then
- local oldcol1 = term.getBackgroundColor()
- local oldcol2 = term.getTextColor()
- term.setBackgroundColor(colors[color1])
- term.setTextColor(colors[textCol])
- term.setCursorPos(pos1, pos2)
- for i = 0, length do
- term.write("\124")
- term.setCursorPos(pos1, pos2 - i)
- end
- if data10 == nil then
- data10 = {}
- for is = 0, 1000 do
- data10[is] = 0
- end
- end
- local cp = ins[4]
- if (ins[3] == pos1) and (ins[4] <= pos2) and (ins[4] >= (pos2 - length) + 1) then
- term.setCursorPos(pos1, cp)
- data10[cc] = cp
- term.write("\xad")
- else
- term.setCursorPos(pos1, data10[cc])
- term.write("\xad")
- end
- term.setBackgroundColor(oldcol1)
- term.setTextColor(oldcol2)
- if data10[cc] - pos1 >= 0 then
- return (data10[cc] - pos1)
- elseif data10[cc] - pos1 < 0 then
- return 0
- end
- end
- end
- end
- local function render(text, x, y, textcol, backCol)
- local oldcol1 = term.getBackgroundColor()
- local oldcol2 = term.getTextColor()
- local cur = {term.getCursorPos()}
- term.setTextColor(colors[textcol])
- if type(backCol) == "string" then
- term.setBackgroundColor(colors[backCol])
- elseif type(backCol) == "number" then
- term.setBackgroundColor(backCol)
- end
- term.setCursorPos(x, y)
- term.write(text)
- term.setBackgroundColor(oldcol1)
- term.setTextColor(oldcol2)
- term.setCursorPos(cur[1], cur[2])
- end
- local function menu(lr, ins, cc, x, y, textcol, switchcol, text, returns1, more, returns2)
- if ins[2] == lr or ins[2] == "tout" then
- if thisIsUseless == nil then
- for i = 0, 1000 do
- thisIsUseless = {}
- thisIsUseless[i] = false
- end
- end
- if not thisIsUseless[cc] then
- render(text, x, y, textcol, oldcol2)
- end
- if ins ~= nil then
- local oldcol1 = term.getTextColor()
- local oldcol2 = term.getBackgroundColor()
- local l = string.len(text)
- if ins[1] ~= "timeout" then
- if data4 == nil then
- data4 = {}
- for is = 0, 1000 do
- data4[is] = false
- end
- end
- if data5 == nil then
- data5 = {}
- for is = 0, 1000 do
- data5[is] = false
- end
- end
- if data6 == nil then
- data6 = {}
- for is = 0, 1000 do
- data6[is] = false
- end
- end
- if API(ins, x, y, l, 1) == true then
- data4[cc] = text
- data5[cc] = x
- data6[cc] = y
- local function menus()
- for i = 1, 500 do
- if data4[i] ~= false then
- term.setBackgroundColor(oldcol2)
- term.setCursorPos(data5[i], data6[i])
- term.setTextColor(colors[textcol])
- term.write(data4[i])
- end
- end
- local i = 0
- end
- menus()
- term.setCursorPos(data5[cc], data6[cc])
- term.setBackgroundColor(colors[switchcol])
- term.setTextColor(colors[textcol])
- term.write(text)
- term.setTextColor(oldcol1)
- term.setBackgroundColor(oldcol2)
- menuout = text
- if returns1 == nil then
- return menuout
- else
- if (more == nil) or (more == false) then
- menuout = returns1
- return menuout
- else
- menuout = {
- returns1,
- returns2
- }
- if menuout == nil then
- return 0
- end
- return menuout
- end
- end
- end
- end
- end
- end
- thisIsUseless[cc] = true
- if more == true then
- if menuout == nil then
- menuout = {returns1, "nil"}
- end
- end
- end
- local function menudata()
- if menuout ~= nil then
- return menuout
- else
- return "no output"
- end
- end
- local function timeswitch(lr, ins, cc, pos1, pos2, change, start, col1, col2, col3, repeats)
- if lr == "db" then
- if data7 == nil then
- return nil
- else
- return data7[ins]
- end
- end
- if lr == "setdb" then
- if data7 == nil then
- return "no data to edit"
- else
- data7[ins] = cc
- return("value changed too "..type(cc))
- end
- end
- if ins[2] == lr or ins[2] == "tout" then
- if ins ~= nil then
- local oldcol1 = term.getTextColor()
- local oldcol2 = term.getBackgroundColor()
- term.setBackgroundColor(colors[col2])
- term.setTextColor(colors[col1])
- if data7 == nil then
- data7 = {}
- for is = 0, 1000 do
- data7[is] = false
- end
- end
- if data8 == nil then
- data8 = {}
- for is = 0, 1000 do
- data8[is] = false
- end
- end
- if data8[cc] == false then
- data8[cc] = start
- term.setCursorPos(pos1 + 6, pos2)
- term.write(data8[cc])
- end
- term.setCursorPos(pos1, pos2)
- term.write("start")
- if API(ins, pos1, pos2, 4, 1) == true then
- data7[cc] = true
- end
- if data7[cc] == true then
- if data8[cc] > 0 then
- repeat
- timeOut = data8[cc]
- term.setBackgroundColor(colors[col3])
- data8[cc] = data8[cc] - change
- term.setCursorPos(pos1 + 6, pos2)
- sleep(1)
- term.write(data8[cc])
- until data8[cc] == 0
- return "ended"
- end
- end
- if repeats == true then
- data8[cc] = start
- end
- term.setBackgroundColor(colors[col2])
- term.setCursorPos(pos1 + 6, pos2)
- term.write(data8[cc])
- term.setTextColor(oldcol1)
- term.setBackgroundColor(oldcol2)
- end
- end
- end
- local function bar(
- pos1,
- pos2,
- length,
- height,
- ins,
- max,
- color1,
- color2,
- color3,
- printval,
- hor,
- text,
- format,
- rect,
- thicc)
- if (ins == nil) or (ins < 0) then
- ins = 0
- end
- if format == nil then
- local format = false
- end
- if ins ~= nil then
- oldcol = term.getBackgroundColor()
- oldcol1 = term.getTextColor()
- term.setTextColor(colors[color3])
- local function reprint()
- term.setBackgroundColor(colors[color1])
- fill(pos1 - 1, pos2 - height, length, height * 2)
- term.setBackgroundColor(oldcol)
- xm = term.getBackgroundColor()
- xb = term.getTextColor()
- term.setTextColor(xm)
- term.setBackgroundColor(xb)
- term.setCursorPos(pos1 - 1, pos2 - height)
- if thicc then
- term.setBackgroundColor(colors[color3])
- end
- if thicc then
- term.write(string.rep("\x83", length + 1)) --\143
- term.setTextColor(xb)
- term.setBackgroundColor(xm)
- else
- term.write("\159" .. string.rep("\143", length - 1)) --\x83
- term.setTextColor(xb)
- term.setBackgroundColor(xm)
- term.write("\144")
- end
- if thicc then
- term.setBackgroundColor(colors[color3])
- end
- term.setCursorPos(pos1 - 1, pos2 + height)
- if thicc then
- term.write(string.rep("\x8c", length + 1)) --\131
- else
- term.write("\130" .. string.rep("\131", length - 1) .. "\129") --\x8c
- end
- for i = 0, (height * 2) - 2 do
- if not thicc then
- term.setTextColor(xm)
- term.setBackgroundColor(xb)
- end
- term.setCursorPos(pos1 - 1, (pos2 + i) - (height) + 1)
- if thicc then
- term.setBackgroundColor(colors[color3])
- end
- term.write("\x95")
- term.setCursorPos((pos1 - 2) + (length + 1), (pos2 + i) - (height) + 1)
- if not thicc then
- term.setTextColor(xb)
- term.setBackgroundColor(xm)
- end
- term.write("\x95")
- end
- end
- if rect ~= false then
- reprint()
- else
- fill(pos1 - 1, pos2 - height, length, height * 2)
- end
- local drawLength = ins / max * length
- local drawHeights = ins / max * height
- local drawHeight = math.ceil(drawHeights)
- local moveval = (drawHeight * 2) - 2
- local z = pos2 + height
- term.setBackgroundColor(colors[color2])
- if (hor == false) or (hor == nil) then
- fill(pos1, (pos2 - height) + 1, drawLength - 1, (height * 2) - 1)
- else
- fill(pos1, (z - 1) - moveval, length - 1, moveval + 1)
- end
- if printval == true then
- term.setCursorPos(pos1, pos2)
- term.setTextColor(colors[color3])
- if hor == true then
- if format then
- if ins >= max / 2 then
- term.setBackgroundColor(colors[color2])
- else
- term.setBackgroundColor(colors[color1])
- end
- else
- if ins >= (max / 2) - (max / height) then
- term.setBackgroundColor(colors[color2])
- else
- term.setBackgroundColor(colors[color1])
- end
- end
- elseif hor == false then
- term.setCursorPos(pos1, pos2)
- term.setTextColor(colors[color3])
- if hor == true then
- if ins >= 1 then
- term.setBackgroundColor(colors[color2])
- else
- term.setBackgroundColor(colors[color1])
- end
- end
- end
- if format then
- term.write(ins .. "/" .. max)
- term.setCursorPos(pos1, pos2 + 1)
- term.write(text)
- else
- term.write(ins .. "/" .. max .. " " .. text)
- end
- term.setBackgroundColor(oldcol)
- term.setTextColor(oldcol1)
- end
- term.setTextColor(oldcol1)
- term.setBackgroundColor(oldcol)
- end
- end
- local function frame(pos1, pos2, length, height, color3, color1, thicc)
- local oldcol = term.getBackgroundColor()
- local oldcol1 = term.getTextColor()
- term.setBackgroundColor(colors[color1])
- fill(pos1 - 1, pos2 - height, length, height * 2)
- term.setBackgroundColor(oldcol)
- xm = term.getBackgroundColor()
- xb = term.getTextColor()
- term.setTextColor(xm)
- term.setBackgroundColor(xb)
- term.setCursorPos(pos1 - 1, pos2 - height)
- if thicc then
- term.setBackgroundColor(colors[color3])
- term.setTextColor(oldcol)
- term.write(string.rep("\x83", length + 1)) --\143
- term.setTextColor(xb)
- term.setBackgroundColor(xm)
- else
- term.setTextColor(oldcol)
- term.setBackgroundColor(colors[color3])
- term.write("\159" .. string.rep("\143", length - 1)) --\x83
- term.setTextColor(colors[color3])
- term.setBackgroundColor(oldcol)
- term.write("\144")
- end
- term.setCursorPos(pos1 - 1, pos2 + height)
- if thicc then
- term.setBackgroundColor(oldcol)
- term.setTextColor(colors[color3])
- term.write(string.rep("\x8f", length + 1)) --\131
- term.setBackgroundColor(colors[color1])
- term.setTextColor(colors[color3])
- else
- term.write("\130" .. string.rep("\131", length - 1) .. "\129") --\x8c
- end
- for i = 0, (height * 2) - 2 do
- if not thicc then
- term.setTextColor(xm)
- term.setBackgroundColor(xb)
- end
- term.setCursorPos(pos1 - 1, (pos2 + i) - (height) + 1)
- if thicc then
- term.setBackgroundColor(colors[color3])
- end
- term.setBackgroundColor(colors[color3])
- term.write("\x95")
- term.setCursorPos((pos1 - 2) + (length + 1), (pos2 + i) - (height) + 1)
- if not thicc then
- term.setTextColor(xb)
- term.setBackgroundColor(xm)
- end
- term.setTextColor(colors[color3])
- term.write("\x95")
- end
- term.setBackgroundColor(oldcol)
- term.setTextColor(oldcol1)
- end
- return {
- bundle = bundle,
- API = API,
- counter = counter,
- switch = switch,
- button = button,
- switchn = switchn,
- signal = signal,
- sliderVer = sliderVer,
- timeswitch = timeswitch,
- menu = menu,
- bar = bar,
- fill = fill,
- touch = touch,
- timetouch = timetouch,
- sliderHor = sliderHor,
- render = render,
- menudata = menudata,
- frame = frame
- }
Add Comment
Please, Sign In to add comment