Hiranus

Button API

Sep 29th, 2014
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.54 KB | None | 0 0
  1. --credits: Direwolf20: for his button api that gave birth to this creation.
  2.  
  3. function search(perToFind)
  4.     local printMode = false
  5.     if perToFind == "print" or perToFind==nil then
  6.             printMode=true
  7.     end
  8.     if printMode == true then
  9.         term.clear()
  10.         term.setCursorPos(1,1)
  11.     end
  12.     local perip = peripheral.getNames()
  13.     for i=1, #perip do
  14.         if printMode == true then
  15.             print(perip[i] .. " -- " ..peripheral.getType(perip[i]))
  16.         else
  17.             if perToFind==peripheral.getType(perip[i]) or perToFind==perip[i] then
  18.                 return perip[i]
  19.             end
  20.         end
  21.     end
  22.     if printMode == false then
  23.         throw("No such peripheral: " ..perToFind)
  24.     end
  25. end
  26.  
  27. function wsearch (perToFind)
  28.     return peripheral.wrap(search(perToFind))
  29. end
  30.  
  31. function throw (text)
  32.     printError(text)
  33.     error()
  34. end
  35.  
  36. local mon
  37.  
  38. function getMonitor ()
  39.     return mon
  40. end
  41.  
  42. function findMonitor (monitor)
  43.     if monitor == nil then
  44.         monitor = "monitor"
  45.     end
  46.     mon = wsearch(monitor)
  47. end
  48.  
  49. function setMonitor (monitor)
  50.     mon = monitor
  51. end
  52.  
  53. function prepareMonitor ()
  54.     if mon == nil then
  55.         findMonitor()
  56.     end
  57.     mon.setTextScale(1)
  58.     mon.setTextColor(colors.white)
  59.     mon.setBackgroundColor(colors.black)
  60.     mon.clear()
  61. end
  62.  
  63. function clearMonitor()
  64.     mon.clear()
  65. end
  66.  
  67. local buttonMenus = {}
  68. local groupInfo = {}
  69. local group
  70.  
  71. function selectGroup (name)
  72.     group=name
  73.     if buttonMenus[group]== nil then
  74.         buttonMenus[group]={}
  75.         groupInfo[group]={}
  76.         groupInfo[group]["num"]=0
  77.     end
  78. end
  79.  
  80. function addCustomButton ( name, func, ymin, ymax, xmin, xmax, mode, colorOFF, colorON)
  81.     if group == nil then
  82.         selectGroup("A")
  83.     end
  84.     buttonMenus[group][name]={}
  85.     buttonMenus[group][name]["func"] = func
  86.     buttonMenus[group][name]["active"] = false
  87.     buttonMenus[group][name]["locked"] = false
  88.     buttonMenus[group][name]["xmin"] = xmin
  89.     buttonMenus[group][name]["ymin"] = ymin
  90.     buttonMenus[group][name]["xmax"] = xmax
  91.     buttonMenus[group][name]["ymax"] = ymax
  92.     buttonMenus[group][name]["mode"] = mode
  93.     buttonMenus[group][name]["custom"] = true
  94.     if colorOFF == nil then
  95.         buttonMenus[group][name]["colorOFF"]=colors.red
  96.     else
  97.         buttonMenus[group][name]["colorOFF"]=colorOFF
  98.     end
  99.     if colorON == nil then
  100.         buttonMenus[group][name]["colorON"]=colors.lime
  101.     else
  102.         buttonMenus[group][name]["colorON"]=colorON
  103.     end
  104. end
  105.  
  106. function addCustomCornerButton(name, func, mode, sizeX, sizeY, corner, colorOFF, colorON)
  107.     local monX,monY = mon.getSize()
  108.     local xmin,xmax,ymin,ymax
  109.     if string.find(corner,"r")~=nil or string.find(corner,"R")~=nil then
  110.         xmax=monX+1
  111.         xmin=monX-sizeX+1
  112.     else
  113.         xmin=1
  114.         xmax=sizeX+1
  115.     end
  116.     if string.find(corner,"t")~=nil or string.find(corner,"T")~=nil then
  117.         ymax=sizeY+1
  118.         ymin=1
  119.     else
  120.         ymin=monY-sizeY+1
  121.         ymax=monY+1
  122.     end
  123.     addCustomButton(name,func,ymin,ymax,xmin,xmax,mode, colorOFF, colorON)
  124. end
  125.  
  126. function addBasicButton(name, func, mode, colorOFF, colorON)
  127.     if group == nil then
  128.         selectGroup("A")
  129.     end
  130.     buttonMenus[group][name]={}
  131.     buttonMenus[group][name]["func"] = func
  132.     buttonMenus[group][name]["active"] = false
  133.     buttonMenus[group][name]["mode"] = mode
  134.     buttonMenus[group][name]["custom"] = false
  135.     buttonMenus[group][name]["locked"] = false
  136.     groupInfo[group]["num"]=groupInfo[group]["num"] + 1
  137.     if colorOFF == nil then
  138.         buttonMenus[group][name]["colorOFF"]=colors.red
  139.     else
  140.         buttonMenus[group][name]["colorOFF"]=colorOFF
  141.     end
  142.     if colorON == nil then
  143.         buttonMenus[group][name]["colorON"]=colors.lime
  144.     else
  145.         buttonMenus[group][name]["colorON"]=colorON
  146.     end
  147. end
  148.  
  149.  
  150. local heading= ""
  151.  
  152. function setHeading (text)
  153.     heading=text
  154. end
  155.  
  156. function printHeading()
  157.     local head=""
  158.     if groupInfo[group]["heading"]~= nil then
  159.         head=groupInfo[group]["heading"]
  160.     else
  161.         head=heading
  162.     end
  163.     local w, h = mon.getSize()
  164.     mon.setCursorPos((w-string.len(head))/2+1, 1)
  165.     mon.write(head)
  166. end
  167.  
  168. local button={}
  169.  
  170. function setGlobalButtonVar (width, height,Xaxis)
  171.     button["buttonX"]= width
  172.     button["buttonY"] = height
  173.     button["buttonsX"] = Xaxis
  174. end
  175.  
  176. local monitorBorder={}
  177.  
  178. function setGlobalMonitorBorder (mBT,mBR,mBD,mBL)
  179.     monitorBorder["monitorBorderTop"] = mBT
  180.     monitorBorder["monitorBorderRight"] = mBR
  181.     monitorBorder["monitorBorderDown"] = mBD
  182.     monitorBorder["monitorBorderLeft"] = mBL
  183. end
  184. setGlobalMonitorBorder(0,0,0,0)
  185.  
  186. local space={}
  187. function setGlobalButtonBorder (sX,sY)
  188.     space["spaceX"] = sX
  189.     space["spaceY"] = sY
  190. end
  191. ------------------------------------------------------------------------------------------------------------------
  192. function setGroupMonitorBorder (mBT,mBR,mBD,mBL)
  193.     if groupInfo[group]["MB"]==nil then
  194.         groupInfo[group]["MB"]={}
  195.     end
  196.     groupInfo[group]["MB"]["monitorBorderTop"] = mBT
  197.     groupInfo[group]["MB"]["monitorBorderRight"] = mBR
  198.     groupInfo[group]["MB"]["monitorBorderDown"] = mBD
  199.     groupInfo[group]["MB"]["monitorBorderLeft"] = mBL
  200. end
  201.  
  202. function setGroupButtonVar (width, height,Xaxis)
  203.     if groupInfo[group]["BV"]==nil then
  204.         groupInfo[group]["BV"]={}
  205.     end
  206.     groupInfo[group]["BV"]["buttonX"]= width
  207.     groupInfo[group]["BV"]["buttonY"]= height
  208.     groupInfo[group]["BV"]["buttonsX"] = Xaxis
  209. end
  210. function setGroupButtonBorder (sX,sY)
  211.     if groupInfo[group]["BB"]==nil then
  212.         groupInfo[group]["BB"]={}
  213.     end
  214.     groupInfo[group]["BB"]["spaceX"] = sX
  215.     groupInfo[group]["BB"]["spaceY"] = sY
  216. end
  217. function setGroupHeading (text)
  218.     groupInfo[group]["heading"]=text
  219. end
  220. ----------------------------------------------------------------------------------------------------------
  221.  
  222. function calculateButtons()
  223.     local monX,monY = mon.getSize()
  224.  
  225.     local cButton , cVar , cMonitor
  226.    
  227.     local buttonsY
  228.     local buttonColumn, buttonRow
  229.     for name, groupData in pairs(buttonMenus) do
  230.        
  231.         if groupInfo[name]["BB"] ~= nil then
  232.             cButton=groupInfo[name]["BB"]
  233.         else
  234.             cButton=space
  235.         end
  236.        
  237.         if groupInfo[name]["MB"] ~= nil then
  238.             cMonitor=groupInfo[name]["MB"]
  239.         else
  240.             cMonitor=monitorBorder
  241.         end
  242.        
  243.         if groupInfo[name]["BV"] ~= nil then
  244.             cVar=groupInfo[name]["BV"]
  245.         else
  246.             cVar=button
  247.         end
  248.        
  249.         if cButton == nil then
  250.             throw("You forgot to call 'setBasicButtonVar'")
  251.         end
  252.        
  253.        
  254.         if heading ~= "" then
  255.             if cMonitor["monitorBorderTop"] < 2 then
  256.                 cMonitor["monitorBorderTop"]= 2
  257.             end
  258.         end
  259.         if cButton["spaceX"] == nil  then
  260.             cButton["spaceX"]=math.floor  ((monX - (cVar["buttonX"] * cVar["buttonsX"]) ) / (cVar["buttonsX"]+ 1 ) )
  261.            
  262.         end
  263.         buttonsY=math.ceil(groupInfo[name]["num"] / cVar["buttonsX"])
  264.        
  265.         if cButton["spaceY"] == nil then
  266.             cButton["spaceY"]=math.floor(( monY  - (cVar["buttonY"] * buttonsY) ) / (buttonsY + 1 ) )
  267.         end
  268.        
  269.         buttonColumn=-1
  270.         buttonRow=0
  271.         for bName, bData in pairs(groupData) do
  272.             if bData["custom"] == false then
  273.                 buttonColumn=buttonColumn+1
  274.                 if buttonColumn==cVar["buttonsX"] then
  275.                     buttonColumn=0
  276.                     buttonRow=buttonRow+1
  277.                 end
  278.                
  279.                 local areaNeededX = (cVar["buttonX"] * cVar["buttonsX"]) + (cButton["spaceX"] * (cVar["buttonsX"] - 1))
  280.                 local areaNeededY = (cVar["buttonY"] * buttonsY) + (cButton["spaceY"] * (buttonsY - 1))
  281.                 local arealeftX = monX - (areaNeededX + cMonitor["monitorBorderLeft"] + cMonitor["monitorBorderRight"])
  282.                 local arealeftY = monY - (areaNeededY + cMonitor["monitorBorderTop"] + cMonitor["monitorBorderDown"])
  283.                 local shiftX = math.floor(arealeftX/2)
  284.                 local shiftY = math.ceil(arealeftY/2)
  285.                
  286.                 bData["xmin"] = (cMonitor["monitorBorderLeft"]+shiftX+((cButton["spaceX"] + cVar["buttonX"])  * buttonColumn))  +1
  287.                 bData["xmax"]= (cMonitor["monitorBorderLeft"]+shiftX+((cButton["spaceX"] + cVar["buttonX"])  * buttonColumn)  + cVar["buttonX"] )+1
  288.                 bData["ymin"]=  (cMonitor["monitorBorderTop"]+shiftY+((cButton["spaceY"] + cVar["buttonY"])  * buttonRow) ) +1
  289.                 bData["ymax"]= (cMonitor["monitorBorderTop"]+shiftY+((cButton["spaceY"] + cVar["buttonY"])  * buttonRow) + cVar["buttonY"]) +1
  290.                
  291.                 if bData["xmax"]-1>(monX-cMonitor["monitorBorderRight"]) or bData["ymax"]-1>(monY-cMonitor["monitorBorderDown"]) then
  292.                     throw("Button outside border ")
  293.                 end
  294.                 if bData["xmin"]<=cMonitor["monitorBorderLeft"] or bData["ymin"]<=cMonitor["monitorBorderTop"] then
  295.                     throw("Button outside border ")
  296.                 end
  297.             end
  298.         end
  299.     end
  300. end
  301.  
  302. function printButton (name, bData)
  303.     local color
  304.     local on = bData["active"]
  305.     if on == true then color = bData["colorON"] else color = bData["colorOFF"] end
  306.    mon.setBackgroundColor(color)
  307.    if bData["ymin"] == nil then
  308.     throw ("You forgot to call calculateButtons")
  309.     end
  310.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  311.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(name)) /2) +0
  312.  
  313.    for j = bData["ymin"], (bData["ymax"] - 1) do
  314.       mon.setCursorPos(bData["xmin"], j)
  315.       if j == yspot then
  316.         if xspot<0 then
  317.             mon.setCursorPos((bData["xmin"]+xspot), j)
  318.             mon.write(name)
  319.         else
  320.              for k=0, bData["xmax"] - bData["xmin"] - string.len(name)  do
  321.                 if k == xspot then
  322.                    mon.write(name)
  323.                 else
  324.                    mon.write(" ")
  325.                 end
  326.              end
  327.             end
  328.       else
  329.          for i = bData["xmin"], bData["xmax"]-1 do
  330.             mon.write(" ")
  331.          end
  332.       end
  333.    end
  334.    mon.setBackgroundColor(colors.black)
  335. end
  336.  
  337. function refresh (name)
  338.     printButton(name,buttonMenus[group][name])
  339. end
  340.  
  341. local flashTime=0.2
  342.  
  343. function setGlobalFlashTime(tim)
  344.     flashTime=tim
  345. end
  346.  
  347. function toggle(name,button)
  348.     if button["locked"]~=true then
  349.         button["active"] = not button["active"]
  350.         refresh(name)
  351.         button["func"]()
  352.     end
  353. end
  354.  
  355. function flash(name,button)
  356.     if button["locked"]~=true then
  357.         local tgroup=group
  358.         if button["active"]==false then
  359.             toggle(name,button)
  360.         end
  361.  
  362.         if tgroup==group then
  363.             sleep(flashTime)
  364.             button["active"] = not button["active"]
  365.             refresh(name)
  366.         else
  367.             button["active"] = not button["active"]
  368.         end
  369.     end
  370. end  
  371.  
  372. function flashInvert(name,button)
  373.     if button["locked"]~=true then
  374.         local tgroup=group
  375.         if tgroup==group then
  376.             sleep(flashTime)
  377.             button["active"] = not button["active"]
  378.             refresh(name)
  379.         else
  380.             button["active"] = not button["active"]
  381.         end
  382.         if button["active"]==false then
  383.             toggle(name,button)
  384.         end
  385.     end
  386. end  
  387.  
  388. function no(name,button)
  389.     if button["locked"]~=true then
  390.         button["func"]()
  391.     end
  392. end
  393.  
  394.  
  395. function empty() end
  396.  
  397. function redrawScreen()
  398.     clearMonitor()
  399.     printHeading()
  400.     for name,data in pairs(buttonMenus[group]) do
  401.         printButton(name, data)
  402.     end
  403. end
  404.  
  405. function setButtonLock(state, name, gr)
  406.     if gr==nil then
  407.         gr=group
  408.     end
  409.     if buttonMenus[gr][name]==nil then
  410.         throw("button does not exist")
  411.     end
  412.     buttonMenus[gr][name]["locked"]=state
  413. end
  414. function changeButtonLock( name, gr)
  415.     if gr==nil then
  416.         gr=group
  417.     end
  418.     if buttonMenus[gr][name]==nil then
  419.         throw("button does not exist")
  420.     end
  421.     buttonMenus[gr][name]["locked"]=not buttonMenus[gr][name]["locked"]
  422. end
  423. function getButtonLock(name,gr)
  424.     if gr==nil then
  425.         gr=group
  426.     end
  427.     if buttonMenus[gr][name]==nil then
  428.         throw("button does not exist")
  429.     end
  430.     return buttonMenus[gr][name]["locked"]
  431. end
  432.  
  433. function useButton(name,gr)
  434.     if gr==nil then
  435.         gr=group
  436.     end
  437.     if buttonMenus[gr][name]==nil then
  438.         throw("button does not exist")
  439.     end
  440.     if buttonMenus[gr][name]["locked"]~=true then
  441.         buttonMenus[gr][name]["mode"](name,buttonMenus[gr][name])
  442.     end
  443. end
  444.  
  445. function setButtonState(state,name,gr)
  446.     if gr==nil then
  447.         gr=group
  448.     end
  449.     if buttonMenus[gr][name]==nil then
  450.         throw("button does not exist")
  451.     end
  452.     if buttonMenus[gr][name]["active"]~= state then
  453.         toggle (name,buttonMenus[gr][name])
  454.     end
  455. end
  456.  
  457. function getButtonState(name,gr)
  458.     if gr==nil then
  459.         gr=group
  460.     end
  461.     if buttonMenus[gr][name]==nil then
  462.         throw("button does not exist")
  463.     end
  464.     return buttonMenus[gr][name]["active"]
  465. end
  466.  
  467. function getButton(name,gr)
  468.     if gr==nil then
  469.         gr=group
  470.     end
  471.     if buttonMenus[gr][name]==nil then
  472.         throw("button does not exist")
  473.     end
  474.     return buttonMenus[gr][name]
  475. end
  476.  
  477. function getGroup(gr)
  478.     if gr==nil then
  479.         gr=group
  480.     end
  481.     return buttonMenus[gr]
  482. end
  483.  
  484. function checkxy(x, y)
  485.    for name, data in pairs(buttonMenus[group]) do
  486.  
  487.           if y>=data["ymin"] and  y < data["ymax"] then
  488.              if x>=data["xmin"] and x< data["xmax"] then
  489.                  print ("")print ("")
  490.                     data["mode"](name,data)
  491.                     return true
  492.              end
  493.           end
  494.  
  495.     end
  496.    return false
  497. end
  498.  
  499. function getClick()
  500.     local event,side,x,y = os.pullEvent("monitor_touch")
  501.     checkxy(x,y)
  502. end
  503.  
  504. function printButtonData()
  505.     local monX,monY = mon.getSize()
  506.     print(monX)
  507.     print (monY)
  508.     for name, data in pairs(buttonMenus) do
  509.        
  510.         for names, dataup in pairs(data) do
  511.             print (name.."  "..names .." xmin:"..dataup["xmin"].." xmax:"..dataup["xmax"].." ymin:"..dataup["ymin"].." ymax:"..dataup["ymax"])
  512.         end
  513.  
  514.     end
  515.  
  516. end
Advertisement
Add Comment
Please, Sign In to add comment