Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --credits: Direwolf20: for his button api that gave birth to this creation.
- function search(perToFind)
- local printMode = false
- if perToFind == "print" or perToFind==nil then
- printMode=true
- end
- if printMode == true then
- term.clear()
- term.setCursorPos(1,1)
- end
- local perip = peripheral.getNames()
- for i=1, #perip do
- if printMode == true then
- print(perip[i] .. " -- " ..peripheral.getType(perip[i]))
- else
- if perToFind==peripheral.getType(perip[i]) or perToFind==perip[i] then
- return perip[i]
- end
- end
- end
- if printMode == false then
- throw("No such peripheral: " ..perToFind)
- end
- end
- function wsearch (perToFind)
- return peripheral.wrap(search(perToFind))
- end
- function throw (text)
- printError(text)
- error()
- end
- local mon
- function getMonitor ()
- return mon
- end
- function findMonitor (monitor)
- if monitor == nil then
- monitor = "monitor"
- end
- mon = wsearch(monitor)
- end
- function setMonitor (monitor)
- mon = monitor
- end
- function prepareMonitor ()
- if mon == nil then
- findMonitor()
- end
- mon.setTextScale(1)
- mon.setTextColor(colors.white)
- mon.setBackgroundColor(colors.black)
- mon.clear()
- end
- function clearMonitor()
- mon.clear()
- end
- local buttonMenus = {}
- local groupInfo = {}
- local group
- function selectGroup (name)
- group=name
- if buttonMenus[group]== nil then
- buttonMenus[group]={}
- groupInfo[group]={}
- groupInfo[group]["num"]=0
- end
- end
- function addCustomButton ( name, func, ymin, ymax, xmin, xmax, mode, colorOFF, colorON)
- if group == nil then
- selectGroup("A")
- end
- buttonMenus[group][name]={}
- buttonMenus[group][name]["func"] = func
- buttonMenus[group][name]["active"] = false
- buttonMenus[group][name]["locked"] = false
- buttonMenus[group][name]["xmin"] = xmin
- buttonMenus[group][name]["ymin"] = ymin
- buttonMenus[group][name]["xmax"] = xmax
- buttonMenus[group][name]["ymax"] = ymax
- buttonMenus[group][name]["mode"] = mode
- buttonMenus[group][name]["custom"] = true
- if colorOFF == nil then
- buttonMenus[group][name]["colorOFF"]=colors.red
- else
- buttonMenus[group][name]["colorOFF"]=colorOFF
- end
- if colorON == nil then
- buttonMenus[group][name]["colorON"]=colors.lime
- else
- buttonMenus[group][name]["colorON"]=colorON
- end
- end
- function addCustomCornerButton(name, func, mode, sizeX, sizeY, corner, colorOFF, colorON)
- local monX,monY = mon.getSize()
- local xmin,xmax,ymin,ymax
- if string.find(corner,"r")~=nil or string.find(corner,"R")~=nil then
- xmax=monX+1
- xmin=monX-sizeX+1
- else
- xmin=1
- xmax=sizeX+1
- end
- if string.find(corner,"t")~=nil or string.find(corner,"T")~=nil then
- ymax=sizeY+1
- ymin=1
- else
- ymin=monY-sizeY+1
- ymax=monY+1
- end
- addCustomButton(name,func,ymin,ymax,xmin,xmax,mode, colorOFF, colorON)
- end
- function addBasicButton(name, func, mode, colorOFF, colorON)
- if group == nil then
- selectGroup("A")
- end
- buttonMenus[group][name]={}
- buttonMenus[group][name]["func"] = func
- buttonMenus[group][name]["active"] = false
- buttonMenus[group][name]["mode"] = mode
- buttonMenus[group][name]["custom"] = false
- buttonMenus[group][name]["locked"] = false
- groupInfo[group]["num"]=groupInfo[group]["num"] + 1
- if colorOFF == nil then
- buttonMenus[group][name]["colorOFF"]=colors.red
- else
- buttonMenus[group][name]["colorOFF"]=colorOFF
- end
- if colorON == nil then
- buttonMenus[group][name]["colorON"]=colors.lime
- else
- buttonMenus[group][name]["colorON"]=colorON
- end
- end
- local heading= ""
- function setHeading (text)
- heading=text
- end
- function printHeading()
- local head=""
- if groupInfo[group]["heading"]~= nil then
- head=groupInfo[group]["heading"]
- else
- head=heading
- end
- local w, h = mon.getSize()
- mon.setCursorPos((w-string.len(head))/2+1, 1)
- mon.write(head)
- end
- local button={}
- function setGlobalButtonVar (width, height,Xaxis)
- button["buttonX"]= width
- button["buttonY"] = height
- button["buttonsX"] = Xaxis
- end
- local monitorBorder={}
- function setGlobalMonitorBorder (mBT,mBR,mBD,mBL)
- monitorBorder["monitorBorderTop"] = mBT
- monitorBorder["monitorBorderRight"] = mBR
- monitorBorder["monitorBorderDown"] = mBD
- monitorBorder["monitorBorderLeft"] = mBL
- end
- setGlobalMonitorBorder(0,0,0,0)
- local space={}
- function setGlobalButtonBorder (sX,sY)
- space["spaceX"] = sX
- space["spaceY"] = sY
- end
- ------------------------------------------------------------------------------------------------------------------
- function setGroupMonitorBorder (mBT,mBR,mBD,mBL)
- if groupInfo[group]["MB"]==nil then
- groupInfo[group]["MB"]={}
- end
- groupInfo[group]["MB"]["monitorBorderTop"] = mBT
- groupInfo[group]["MB"]["monitorBorderRight"] = mBR
- groupInfo[group]["MB"]["monitorBorderDown"] = mBD
- groupInfo[group]["MB"]["monitorBorderLeft"] = mBL
- end
- function setGroupButtonVar (width, height,Xaxis)
- if groupInfo[group]["BV"]==nil then
- groupInfo[group]["BV"]={}
- end
- groupInfo[group]["BV"]["buttonX"]= width
- groupInfo[group]["BV"]["buttonY"]= height
- groupInfo[group]["BV"]["buttonsX"] = Xaxis
- end
- function setGroupButtonBorder (sX,sY)
- if groupInfo[group]["BB"]==nil then
- groupInfo[group]["BB"]={}
- end
- groupInfo[group]["BB"]["spaceX"] = sX
- groupInfo[group]["BB"]["spaceY"] = sY
- end
- function setGroupHeading (text)
- groupInfo[group]["heading"]=text
- end
- ----------------------------------------------------------------------------------------------------------
- function calculateButtons()
- local monX,monY = mon.getSize()
- local cButton , cVar , cMonitor
- local buttonsY
- local buttonColumn, buttonRow
- for name, groupData in pairs(buttonMenus) do
- if groupInfo[name]["BB"] ~= nil then
- cButton=groupInfo[name]["BB"]
- else
- cButton=space
- end
- if groupInfo[name]["MB"] ~= nil then
- cMonitor=groupInfo[name]["MB"]
- else
- cMonitor=monitorBorder
- end
- if groupInfo[name]["BV"] ~= nil then
- cVar=groupInfo[name]["BV"]
- else
- cVar=button
- end
- if cButton == nil then
- throw("You forgot to call 'setBasicButtonVar'")
- end
- if heading ~= "" then
- if cMonitor["monitorBorderTop"] < 2 then
- cMonitor["monitorBorderTop"]= 2
- end
- end
- if cButton["spaceX"] == nil then
- cButton["spaceX"]=math.floor ((monX - (cVar["buttonX"] * cVar["buttonsX"]) ) / (cVar["buttonsX"]+ 1 ) )
- end
- buttonsY=math.ceil(groupInfo[name]["num"] / cVar["buttonsX"])
- if cButton["spaceY"] == nil then
- cButton["spaceY"]=math.floor(( monY - (cVar["buttonY"] * buttonsY) ) / (buttonsY + 1 ) )
- end
- buttonColumn=-1
- buttonRow=0
- for bName, bData in pairs(groupData) do
- if bData["custom"] == false then
- buttonColumn=buttonColumn+1
- if buttonColumn==cVar["buttonsX"] then
- buttonColumn=0
- buttonRow=buttonRow+1
- end
- local areaNeededX = (cVar["buttonX"] * cVar["buttonsX"]) + (cButton["spaceX"] * (cVar["buttonsX"] - 1))
- local areaNeededY = (cVar["buttonY"] * buttonsY) + (cButton["spaceY"] * (buttonsY - 1))
- local arealeftX = monX - (areaNeededX + cMonitor["monitorBorderLeft"] + cMonitor["monitorBorderRight"])
- local arealeftY = monY - (areaNeededY + cMonitor["monitorBorderTop"] + cMonitor["monitorBorderDown"])
- local shiftX = math.floor(arealeftX/2)
- local shiftY = math.ceil(arealeftY/2)
- bData["xmin"] = (cMonitor["monitorBorderLeft"]+shiftX+((cButton["spaceX"] + cVar["buttonX"]) * buttonColumn)) +1
- bData["xmax"]= (cMonitor["monitorBorderLeft"]+shiftX+((cButton["spaceX"] + cVar["buttonX"]) * buttonColumn) + cVar["buttonX"] )+1
- bData["ymin"]= (cMonitor["monitorBorderTop"]+shiftY+((cButton["spaceY"] + cVar["buttonY"]) * buttonRow) ) +1
- bData["ymax"]= (cMonitor["monitorBorderTop"]+shiftY+((cButton["spaceY"] + cVar["buttonY"]) * buttonRow) + cVar["buttonY"]) +1
- if bData["xmax"]-1>(monX-cMonitor["monitorBorderRight"]) or bData["ymax"]-1>(monY-cMonitor["monitorBorderDown"]) then
- throw("Button outside border ")
- end
- if bData["xmin"]<=cMonitor["monitorBorderLeft"] or bData["ymin"]<=cMonitor["monitorBorderTop"] then
- throw("Button outside border ")
- end
- end
- end
- end
- end
- function printButton (name, bData)
- local color
- local on = bData["active"]
- if on == true then color = bData["colorON"] else color = bData["colorOFF"] end
- mon.setBackgroundColor(color)
- if bData["ymin"] == nil then
- throw ("You forgot to call calculateButtons")
- end
- local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
- local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(name)) /2) +0
- for j = bData["ymin"], (bData["ymax"] - 1) do
- mon.setCursorPos(bData["xmin"], j)
- if j == yspot then
- if xspot<0 then
- mon.setCursorPos((bData["xmin"]+xspot), j)
- mon.write(name)
- else
- for k=0, bData["xmax"] - bData["xmin"] - string.len(name) do
- if k == xspot then
- mon.write(name)
- else
- mon.write(" ")
- end
- end
- end
- else
- for i = bData["xmin"], bData["xmax"]-1 do
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- end
- function refresh (name)
- printButton(name,buttonMenus[group][name])
- end
- local flashTime=0.2
- function setGlobalFlashTime(tim)
- flashTime=tim
- end
- function toggle(name,button)
- if button["locked"]~=true then
- button["active"] = not button["active"]
- refresh(name)
- button["func"]()
- end
- end
- function flash(name,button)
- if button["locked"]~=true then
- local tgroup=group
- if button["active"]==false then
- toggle(name,button)
- end
- if tgroup==group then
- sleep(flashTime)
- button["active"] = not button["active"]
- refresh(name)
- else
- button["active"] = not button["active"]
- end
- end
- end
- function flashInvert(name,button)
- if button["locked"]~=true then
- local tgroup=group
- if tgroup==group then
- sleep(flashTime)
- button["active"] = not button["active"]
- refresh(name)
- else
- button["active"] = not button["active"]
- end
- if button["active"]==false then
- toggle(name,button)
- end
- end
- end
- function no(name,button)
- if button["locked"]~=true then
- button["func"]()
- end
- end
- function empty() end
- function redrawScreen()
- clearMonitor()
- printHeading()
- for name,data in pairs(buttonMenus[group]) do
- printButton(name, data)
- end
- end
- function setButtonLock(state, name, gr)
- if gr==nil then
- gr=group
- end
- if buttonMenus[gr][name]==nil then
- throw("button does not exist")
- end
- buttonMenus[gr][name]["locked"]=state
- end
- function changeButtonLock( name, gr)
- if gr==nil then
- gr=group
- end
- if buttonMenus[gr][name]==nil then
- throw("button does not exist")
- end
- buttonMenus[gr][name]["locked"]=not buttonMenus[gr][name]["locked"]
- end
- function getButtonLock(name,gr)
- if gr==nil then
- gr=group
- end
- if buttonMenus[gr][name]==nil then
- throw("button does not exist")
- end
- return buttonMenus[gr][name]["locked"]
- end
- function useButton(name,gr)
- if gr==nil then
- gr=group
- end
- if buttonMenus[gr][name]==nil then
- throw("button does not exist")
- end
- if buttonMenus[gr][name]["locked"]~=true then
- buttonMenus[gr][name]["mode"](name,buttonMenus[gr][name])
- end
- end
- function setButtonState(state,name,gr)
- if gr==nil then
- gr=group
- end
- if buttonMenus[gr][name]==nil then
- throw("button does not exist")
- end
- if buttonMenus[gr][name]["active"]~= state then
- toggle (name,buttonMenus[gr][name])
- end
- end
- function getButtonState(name,gr)
- if gr==nil then
- gr=group
- end
- if buttonMenus[gr][name]==nil then
- throw("button does not exist")
- end
- return buttonMenus[gr][name]["active"]
- end
- function getButton(name,gr)
- if gr==nil then
- gr=group
- end
- if buttonMenus[gr][name]==nil then
- throw("button does not exist")
- end
- return buttonMenus[gr][name]
- end
- function getGroup(gr)
- if gr==nil then
- gr=group
- end
- return buttonMenus[gr]
- end
- function checkxy(x, y)
- for name, data in pairs(buttonMenus[group]) do
- if y>=data["ymin"] and y < data["ymax"] then
- if x>=data["xmin"] and x< data["xmax"] then
- print ("")print ("")
- data["mode"](name,data)
- return true
- end
- end
- end
- return false
- end
- function getClick()
- local event,side,x,y = os.pullEvent("monitor_touch")
- checkxy(x,y)
- end
- function printButtonData()
- local monX,monY = mon.getSize()
- print(monX)
- print (monY)
- for name, data in pairs(buttonMenus) do
- for names, dataup in pairs(data) do
- print (name.." "..names .." xmin:"..dataup["xmin"].." xmax:"..dataup["xmax"].." ymin:"..dataup["ymin"].." ymax:"..dataup["ymax"])
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment