Advertisement
NanoBob

Button API

Mar 1st, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.73 KB | None | 0 0
  1. local idleColor=256
  2. local activatedColor=8192
  3. local backgroundColor=32768
  4. local textColor=1
  5. local buttons={}
  6. local monitor=nil
  7. local debugMode=false
  8. local monitorSide=nil
  9.  
  10. if fs.exists("events")==false then
  11.     shell.run("pastebin get bmKBU4Rv events")
  12. end
  13. os.loadAPI("events")
  14.  
  15. function addButton(x,y,width,height,text,toggle,relative,idleColor,activeColor,textColor)
  16.     if monitor==nil then
  17.         if debugMode==true then
  18.             print("Monitor is not yet defined! Use setMonitorSide(side)")
  19.         end
  20.         return false,"Monitor is not yet defined! Use setMonitorSide(side)"
  21.     elseif x==nil or y==nil or width==nil or height==nil or text==nil then
  22.         if debugMode==true then
  23.             print("Not all required arguments are specified.")
  24.         end
  25.         return false,"Not all required arguments are specified."
  26.     else
  27.         buttonID=#buttons+1
  28.         if string.len(text)>width and relative~=true then
  29.             text=string.sub(text,0,width)
  30.         elseif string.len(text)>width*screenx then
  31.             text=string.sub(text,0,width*screenx)
  32.         end
  33.         buttons[buttonID]={['x']=x,['y']=y,['width']=width,['height']=height,['text']=text,['active']=false,['toggle']=toggle,['relative']=relative,['activeColor']=activeColor,['idleColor']=idleColor,['textColor']=textColor,['visible']=true}
  34.         drawAllButtons{}
  35.         return buttonID
  36.     end
  37. end
  38.  
  39. function removeAllButtons()
  40.     buttons={}
  41.     drawAllButtons()
  42. end
  43.  
  44. function removeButton(buttonID)
  45.     if monitor==nil then
  46.         if debugMode==true then
  47.             print("Monitor is not yet defined! Use setMonitorSide(side)")
  48.         end
  49.         return false,"Monitor is not yet defined! Use setMonitorSide(side)"
  50.     else
  51.         if buttonID==nil then
  52.             if debugMode==true then
  53.                 print("Not all required arguments are specified.")
  54.             end
  55.             return false,"Not all required arguments are specified."
  56.         else
  57.             if buttons[buttonID]==nil then
  58.                 if debugMode==true then
  59.                     print("That button ID does not correspond with a button!")
  60.                 end
  61.                 return false,"That button ID does not correspond with a button!"
  62.             else
  63.                 buttons[buttonID]={"Removed"}
  64.             end
  65.         end
  66.     end
  67. end
  68.  
  69.  
  70. function drawAllButtons(event)
  71.     if monitor==nil then
  72.         if debugMode==true then
  73.             print("Monitor is not yet defined! Use setMonitorSide(side)")
  74.         end
  75.         return false,"Monitor is not yet defined! Use setMonitorSide(side)"
  76.     else
  77.         monitor.setBackgroundColor(backgroundColor)
  78.         monitor.clear()
  79.         for buttonID,buttonInfo in pairs(buttons) do
  80.             if buttonInfo['visible']==true then
  81.                 local x=buttonInfo['x']
  82.                 local y=buttonInfo['y']
  83.                 local width=buttonInfo['width']
  84.                 local height=buttonInfo['height']
  85.                 if buttonInfo['relative']==true then
  86.                     x=math.floor(x*screenx+0.5)
  87.                     y=math.floor(y*screeny+0.5)
  88.                     width=math.floor(width*screenx+0.5)
  89.                     height=math.floor(height*screeny+0.5)
  90.                 end
  91.                 monitor.setCursorPos(x,y)
  92.                 if buttonInfo['active']==true then 
  93.                     if buttonInfo['activeColor']==nil then
  94.                         monitor.setBackgroundColor(activatedColor)
  95.                     else
  96.                         monitor.setBackgroundColor(buttonInfo['activeColor'])
  97.                     end
  98.                 else
  99.                     if buttonInfo['idleColor']==nil then
  100.                         monitor.setBackgroundColor(idleColor)
  101.                     else
  102.                         monitor.setBackgroundColor(buttonInfo['idleColor'])
  103.                     end
  104.                 end
  105.                 for i=0,height-1 do
  106.                     monitor.setCursorPos(x,y+i)
  107.                     for i=1,width do
  108.                         monitor.write(" ")
  109.                     end
  110.                 end
  111.                 if buttonInfo['textColor']==nil then
  112.                     monitor.setTextColor(textColor)
  113.                 else
  114.                     monitor.setTextColor(buttonInfo['textColor'])
  115.                 end
  116.                 stringX=x+(width/2-string.len(buttonInfo['text'])/2)
  117.                 stringY=y+(math.floor(height)/2)
  118.                 monitor.setCursorPos(stringX,stringY)
  119.                 monitor.write(buttonInfo['text'])
  120.             end
  121.         end
  122.     end
  123.     if event==true then return end
  124.     events.triggerEvent("onButtonsDrawn",true)
  125. end
  126.  
  127. function getButtonInfo(buttonID)
  128.     if monitor==nil then
  129.         if debugMode==true then
  130.             print("Monitor is not yet defined! Use setMonitorSide(side)")
  131.         end
  132.         return false,"Monitor is not yet defined! Use setMonitorSide(side)"
  133.     else
  134.         if buttonID==nil then
  135.             if debugMode==true then
  136.                 print("Not all required arguments are specified.")
  137.             end
  138.             return false,"Not all required arguments are specified."
  139.         else
  140.             if buttons[buttonID]==nil then
  141.                 if debugMode==true then
  142.                     print("That button ID does not correspond with a button!")
  143.                 end
  144.                 return false,"That button ID does not correspond with a button!"
  145.             else
  146.                 if debugMode==true then
  147.                     for infoID,info in pairs(buttons[buttonID]) do
  148.                         if type(info)=="boolean" then
  149.                             if info==false then
  150.                                 info="false"
  151.                             else
  152.                                 info="true"
  153.                             end
  154.                         elseif type(info)=="function" then
  155.                             info="function"
  156.                         end
  157.                             print(infoID.." :"..info)
  158.                        
  159.                     end
  160.                 end
  161.                 return buttons[buttonID]
  162.             end
  163.         end
  164.     end
  165. end
  166.  
  167. function setButtonInfo(buttonID,infoID,info)
  168.     if monitor==nil then
  169.         if debugMode==true then
  170.             print("Monitor is not yet defined! Use setMonitorSide(side)")
  171.         end
  172.         return false,"Monitor is not yet defined! Use setMonitorSide(side)"
  173.     else
  174.         if buttonID==nil then
  175.             if debugMode==true then
  176.                 print("Not all required arguments are specified.")
  177.             end
  178.             return false,"Not all required arguments are specified."
  179.         else
  180.             if buttons[buttonID]==nil then
  181.                 if debugMode==true then
  182.                     print("That button ID does not correspond with a button!")
  183.                 end
  184.                 return false,"That button ID does not correspond with a button!"
  185.             else
  186.                 buttons[buttonID][infoID]=info
  187.             end
  188.         end
  189.     end
  190. end
  191.  
  192. function setVisible(buttonID,bool)
  193.     if monitor==nil then
  194.         if debugMode==true then
  195.             print("Monitor is not yet defined! Use setMonitorSide(side)")
  196.         end
  197.         return false,"Monitor is not yet defined! Use setMonitorSide(side)"
  198.     else
  199.         if buttonID==nil then
  200.             if debugMode==true then
  201.                 print("Not all required arguments are specified.")
  202.             end
  203.             return false,"Not all required arguments are specified."
  204.         else
  205.             if buttons[buttonID]==nil then
  206.                 if debugMode==true then
  207.                     print("That button ID does not correspond with a button!")
  208.                 end
  209.                 return false,"That button ID does not correspond with a button!"
  210.             else
  211.                 buttons[buttonID]['visible']=bool
  212.             end
  213.         end
  214.     end
  215.     --drawAllButtons()
  216. end
  217.  
  218. function run(side,clickX,clickY)
  219.     for buttonID,buttonInfo in pairs(buttons) do
  220.         local x=buttonInfo['x']
  221.         local y=buttonInfo['y']
  222.         local width=buttonInfo['width']
  223.         local height=buttonInfo['height']
  224.         if buttonInfo['relative']==true then
  225.             x=math.floor(x*screenx+0.5)
  226.             y=math.floor(y*screeny+0.5)
  227.             width=math.floor(width*screenx+0.5)
  228.             height=math.floor(height*screeny+0.5)
  229.         end
  230.         if debugMode==true then
  231.             print("Pos:"..x..","..y)
  232.         end
  233.         if clickX>=x and clickX<=x+width and clickY>=y and clickY<=y+height and buttonInfo['visible']==true then
  234.             if debugMode==true then
  235.                 print("Clicked :"..buttonID)
  236.             end
  237.             if buttonInfo['toggle']==true then
  238.                 buttonInfo['active']=not buttonInfo['active']
  239.                 drawAllButtons()
  240.                 if buttonInfo['active']==true then
  241.                     events.triggerEvent("onButtonClick",buttonID,true)
  242.                 else
  243.                     events.triggerEvent("onButtonClick",buttonID,false)
  244.                 end
  245.             else
  246.                 buttonInfo['active']=true
  247.                 drawAllButtons()
  248.                 events.triggerEvent("onButtonClick",buttonID)
  249.                 sleep(0)
  250.                 if buttonInfo~=nil then
  251.                     buttonInfo['active']=false
  252.                 end
  253.                 drawAllButtons()
  254.             end
  255.            
  256.         end
  257.     end
  258. end
  259. events.addEventHandler("monitor_touch",run)
  260. events.addEventHandler("mouse_click",run)
  261.  
  262. function resised(side)
  263.     if side==monitorSide then
  264.         setMonitorSide(side)
  265.         drawAllButtons()
  266.     end
  267. end
  268. events.addEventHandler("monitor_resize",resised)
  269.  
  270. --[[
  271. function updateButtonPositions()
  272.     for buttonID,buttonInfo in pairs(buttons) do
  273.         if buttonInfo['relative']==true then
  274.             x=
  275.             y=
  276.             width=
  277.             height=
  278.             x=math.floor(x*screenx+0.5)
  279.             y=math.floor(y*screeny+0.5)
  280.             width=math.floor(width*screenx+0.5)
  281.             height=math.floor(height*screeny+0.5)
  282.         end    
  283.     end
  284. end
  285. ]]
  286.  
  287. function setMonitorSide(side)
  288.     if side==nil then
  289.         if debugMode==true then
  290.             print("Not all required arguments are specified.")
  291.         end
  292.         return false,"Not all required arguments are specified."
  293.     else
  294.         monitor=peripheral.wrap(side)
  295.         screenx,screeny=monitor.getSize()
  296.         monitorSide=side
  297.     end
  298. end
  299.  
  300. function setMonitor(peripheral)
  301.     if peripheral==nil then
  302.         if debugMode==true then
  303.             print("Not all required arguments are specified.")
  304.         end
  305.         return false,"Not all required arguments are specified."
  306.     else
  307.         monitor=peripheral
  308.         screenx,screeny=monitor.getSize()
  309.         monitorSide=peripheral
  310.     end
  311. end
  312.  
  313. function setDebugMode(bool)
  314.     if bool==nil then
  315.         if debugMode==true then
  316.             print("Not all required arguments are specified.")
  317.         end
  318.         return false,"Not all required arguments are specified."
  319.     else
  320.         debugMode=bool
  321.     end
  322. end
  323.  
  324. function setIdleColor(color)
  325.     if color==nil then
  326.         if debugMode==true then
  327.             print("Not all required arguments are specified.")
  328.         end
  329.         return false,"Not all required arguments are specified."
  330.     else
  331.         idleColor=color
  332.         drawAllButtons()
  333.     end
  334. end
  335.  
  336. function setActiveColor(color)
  337.     if color==nil then
  338.         if debugMode==true then
  339.             print("Not all required arguments are specified.")
  340.         end
  341.         return false,"Not all required arguments are specified."
  342.     else
  343.         activatedColor=color
  344.         drawAllButtons()
  345.     end
  346. end
  347.  
  348. function setBackgroundColor(color)
  349.     if color==nil then
  350.         if debugMode==true then
  351.             print("Not all required arguments are specified.")
  352.         end
  353.         return false,"Not all required arguments are specified."
  354.     else
  355.         backgroundColor=color
  356.         drawAllButtons()
  357.     end
  358. end
  359.  
  360. function setTextColor(color)
  361.     if color==nil then
  362.         if debugMode==true then
  363.             print("Not all required arguments are specified.")
  364.         end
  365.         return false,"Not all required arguments are specified."
  366.     else
  367.         textColor=color
  368.         drawAllButtons()
  369.     end
  370. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement