By Guest

Unnamed Paste

Auto Detect 13.46 KiB 22 21 days ago
----------------------BUTTTON 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, fell free to include--------------- ---in your projects! discord: 9551#0001-------------- ---you dont have to but giving credits is nice :)------ ------------------------------------------------------- ------------------------------------------------------- local arg = {...} if arg[1] == "list" then local oldcol = term.getTextColor() term.setTextColor(colors.green) print( "to get documentation of the functions type: button docs <function>\n\nlist of functions:\n\nbutton()\nswitch()\nswitchn()\ncounter()\nslider()\nmenu()\nbar()\nAPI()\nbundle()\nsignal()\ntimeswitch()\nclick()" ) term.setTextColor(oldcol) end if arg[1] == "docs" then if arg[2] == "button" then print() end if arg[2] == "switch" then print() end if arg[2] == "switchn" then print() end if arg[2] == "counter" then print() end if arg[2] == "slider" then print() end if arg[2] == "menu" then print() end if arg[2] == "bar" then print() end if arg[2] == "API" then print() end if arg[2] == "bundle" then print() end if arg[2] == "signal" then print() end if arg[2] == "timeswitch" then print() end if arg[2] == "click" then print() end end out = 0 --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("monitor_touch")} end out = 0 for x = 0, height - 1 do for i = 0, length - 1 do if (ins[3] == cord1 + i) and (ins[4] == cord2 + x) then out = 1 return true end end end if out ~= 1 then return false end end local function touch() local dats = {os.pullEvent("monitor_touch")} return dats end ------------------------------------------------------------------------------------------------- local function button(monitor, ins, cord1, cord2, text) m = peripheral.wrap(monitor) x = API(ins, cord1, cord2, string.len(text), 1) m.setCursorPos(cord1, cord2) m.write(text) return x end local function counter(monitor, ins, cc, cord1, cord2, cv, max, min, col) local m = peripheral.wrap(monitor) if data == nil then data = {} for is = 0, 1000 do data[is] = 0 end end m.setCursorPos(cord1, cord2) m.write("\24" .. " " .. data[cc]) m.setCursorPos(cord1, cord2 + 1) m.write("\25") if API(ins, cord1, cord2, 1, 1) == true then if data[cc] < max then data[cc] = data[cc] + cv m.setCursorPos(cord1, cord2) m.setTextColor(colors.green) m.write("\24" .. " " .. data[cc] .. " ") m.setCursorPos(cord1, cord2 + 1) m.setTextColor(colors.red) m.write("\25") m.setTextColor(colors[col]) return data[cc] end end if API(ins, cord1, cord2 + 1, 1, 1) == true then if data[cc] > min then data[cc] = data[cc] - cv m.setCursorPos(cord1, cord2) m.setTextColor(colors.green) m.write("\24" .. " " .. data[cc] .. " ") m.setCursorPos(cord1, cord2 + 1) m.setTextColor(colors.red) m.write("\25") m.setTextColor(colors[col]) return data[cc] end end end local function fill(monitor, pos1, pos2, length, height) local m = peripheral.wrap(monitor) for x = 0, height - 1 do m.setCursorPos(pos1, pos2 + x) m.write(string.rep(" ", length)) end end local function switch(monitor, cc, ins, pos1, pos2, col1, col2, col3, text) local re = string.len(text) local m = peripheral.wrap(monitor) 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 = m.getTextColor() local oldcol2 = m.getBackgroundColor() if data1[cc] == true then m.setBackgroundColor(colors[col2]) else m.setBackgroundColor(colors[col1]) end m.setCursorPos(pos1, pos2) m.write(text) m.setTextColor(oldcol1) m.setBackgroundColor(oldcol2) return (data1[cc]) end local function switchn(monitor, cc, ins, pos1, pos2, col1, col2, col3, text, text2) 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 print(re) local m = peripheral.wrap(monitor) 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 print() ff() end local oldcol1 = m.getTextColor() local oldcol2 = m.getBackgroundColor() if data2[cc] == true then m.setCursorPos(pos1, pos2) if string.len(text) ~= string.len(text2) then m.write(string.rep(" ", re)) end m.setBackgroundColor(colors[col2]) m.setCursorPos(pos1, pos2) m.setTextColor(colors[col3]) m.write(text2) else m.setCursorPos(pos1, pos2) if string.len(text) ~= string.len(text2) then m.write(string.rep(" ", re)) end m.setBackgroundColor(colors[col1]) m.setCursorPos(pos1, pos2) m.setTextColor(colors[col3]) m.write(text) end m.setTextColor(oldcol1) m.setBackgroundColor(oldcol2) return (data2[cc]) end -- bundle cable APIs for using main API with bundled cables local function bundle(side, color, state) if (type(side) == "string") and (type(color) == "string") and (type(state) == "boolean") then if state == true then rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side), colors[color])) elseif state == false then rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side), colors[color])) end else error("please use like this:\nbundle(side:string,color:string,state:boolean)") end end local function signal(side, ins, col) if ins == true then bundle(side, col, true) elseif ins == false then bundle(side, col, false) end end ------------------------------------------------------------------------------------------------- local function slider(monitor, ins, cc, pos1, pos2, length, color1, textCol) m = peripheral.wrap(monitor) local oldcol1 = m.getBackgroundColor() local oldcol2 = m.getTextColor() m.setBackgroundColor(colors[color1]) m.setTextColor(colors[textCol]) m.setCursorPos(pos1, pos2) for i = 0, length do m.write("-") m.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 m.setCursorPos(cp, pos2) data3[cc] = cp m.write("|") else m.setCursorPos(data3[cc], pos2) m.write("|") end m.setBackgroundColor(oldcol1) m.setTextColor(oldcol2) return (data3[cc] - pos1) end local function menu(monitor, ins, cc, x, y, textcol, switchcol, text) local m = peripheral.wrap(monitor) local oldcol1 = m.getTextColor() local oldcol2 = m.getBackgroundColor() local l = string.len(text) 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 m.setBackgroundColor(oldcol2) m.setCursorPos(data5[(i)], data6[(i)]) m.setTextColor(colors[textcol]) m.write(data4[(i)]) end end local i = 0 end menus() m.setCursorPos(data5[cc], data6[cc]) m.setBackgroundColor(colors[switchcol]) m.setTextColor(colors[textcol]) m.write(text) m.setTextColor(oldcol1) m.setBackgroundColor(oldcol2) menuout = text return menuout end end local function timeswitch(monitor, ins, cc, pos1, pos2, change, start, col1, col2, col3, repeats) local m = peripheral.wrap(monitor) local oldcol1 = m.getTextColor() local oldcol2 = m.getBackgroundColor() m.setBackgroundColor(colors[col2]) m.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 m.setCursorPos(pos1 + 6, pos2) m.write(data8[cc]) end m.setCursorPos(pos1, pos2) m.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 m.setBackgroundColor(colors[col3]) data8[cc] = data8[cc] - change m.setCursorPos(pos1 + 6, pos2) sleep(1) m.write(data8[cc]) until data8[cc] == 0 end end if repeats == true then data8[cc] = start end m.setBackgroundColor(colors[col2]) m.setCursorPos(pos1 + 6, pos2) m.write(data8[cc]) m.setTextColor(oldcol1) m.setBackgroundColor(oldcol2) end local function bar(monitor, pos1, pos2, length, height, ins, max, color1, color2, color3, printval, hor, text) local m = peripheral.wrap(monitor) oldcol = m.getBackgroundColor() oldcol1 = m.getTextColor() m.setTextColor(colors[color3]) local function reprint() m.setBackgroundColor(colors[color1]) fill(monitor, pos1 - 1, pos2 - height, length, height * 2) m.setBackgroundColor(oldcol) xm = m.getBackgroundColor() xb = m.getTextColor() m.setTextColor(xm) m.setBackgroundColor(xb) m.setCursorPos(pos1 - 1, pos2 - height) m.write(string.rep("\143", length + 1)) m.setTextColor(xb) m.setBackgroundColor(xm) m.setCursorPos(pos1 - 1, pos2 + height) m.write(string.rep("\131", length + 1)) for i = 0, (height * 2) - 2 do m.setCursorPos(pos1 - 1, (pos2 + i) - (height) + 1) m.write("\x95") m.setCursorPos((pos1 - 2) + (length + 1), (pos2 + i) - (height) + 1) m.write("\x95") end end reprint() local drawLength = ins / max * length local drawHeights = ins / max * height local drawHeight = math.ceil(drawHeights) local moveval = (drawHeight * 2) - 2 local z = pos2 + height m.setBackgroundColor(colors[color2]) if (hor == false) or (hor == nil) then fill(monitor, pos1, (pos2 - height) + 1, drawLength - 1, (height * 2) - 1) else fill(monitor, pos1, (z - 1) - moveval, length - 2, moveval + 1) end if printval == true then m.setCursorPos(pos1, pos2) m.setTextColor(colors[color3]) if hor == true then if ins >= max / 2 then m.setBackgroundColor(colors[color2]) else m.setBackgroundColor(colors[color1]) end elseif hor == false then m.setCursorPos(pos1, pos2) m.setTextColor(colors[color3]) if hor == true then if ins >= 1 then m.setBackgroundColor(colors[color2]) else m.setBackgroundColor(colors[color1]) end end end m.write(ins .. "/" .. max .. " " .. text) m.setBackgroundColor(oldcol) m.setTextColor(oldcol1) end m.setTextColor(oldcol1) m.setBackgroundColor(oldcol) end return { bundle = bundle, API = API, counter = counter, switch = switch, button = button, switchn = switchn, signal = signal, slider = slider, timeswitch = timeswitch, menu = menu, bar = bar, fill = fill, touch = touch }