Advertisement
NanoBob

Button API V4

Aug 8th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.80 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}
  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()
  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.             local x=buttonInfo['x']
  81.             local y=buttonInfo['y']
  82.             local width=buttonInfo['width']
  83.             local height=buttonInfo['height']
  84.             if buttonInfo['relative']==true then
  85.                 x=math.floor(x*screenx+0.5)
  86.                 y=math.floor(y*screeny+0.5)
  87.                 width=math.floor(width*screenx+0.5)
  88.                 height=math.floor(height*screeny+0.5)
  89.             end
  90.             monitor.setCursorPos(x,y)
  91.             if buttonInfo['active']==true then 
  92.                 if buttonInfo['activeColor']==nil then
  93.                     monitor.setBackgroundColor(activatedColor)
  94.                 else
  95.                     monitor.setBackgroundColor(buttonInfo['activeColor'])
  96.                 end
  97.             else
  98.                 if buttonInfo['idleColor']==nil then
  99.                     monitor.setBackgroundColor(idleColor)
  100.                 else
  101.                     monitor.setBackgroundColor(buttonInfo['idleColor'])
  102.                 end
  103.             end
  104.             for i=0,height-1 do
  105.                 monitor.setCursorPos(x,y+i)
  106.                 for i=1,width do
  107.                     monitor.write(" ")
  108.                 end
  109.             end
  110.             if buttonInfo['textColor']==nil then
  111.                 monitor.setTextColor(textColor)
  112.             else
  113.                 monitor.setTextColor(buttonInfo['textColor'])
  114.             end
  115.             stringX=x+(width/2-string.len(buttonInfo['text'])/2)
  116.             stringY=y+(math.floor(height)/2)
  117.             monitor.setCursorPos(stringX,stringY)
  118.             monitor.write(buttonInfo['text'])
  119.         end
  120.     end
  121. end
  122.  
  123. function getButtonInfo(buttonID)
  124.     if monitor==nil then
  125.         if debugMode==true then
  126.             print("Monitor is not yet defined! Use setMonitorSide(side)")
  127.         end
  128.         return false,"Monitor is not yet defined! Use setMonitorSide(side)"
  129.     else
  130.         if buttonID==nil then
  131.             if debugMode==true then
  132.                 print("Not all required arguments are specified.")
  133.             end
  134.             return false,"Not all required arguments are specified."
  135.         else
  136.             if buttons[buttonID]==nil then
  137.                 if debugMode==true then
  138.                     print("That button ID does not correspond with a button!")
  139.                 end
  140.                 return false,"That button ID does not correspond with a button!"
  141.             else
  142.                 if debugMode==true then
  143.                     for infoID,info in pairs(buttons[buttonID]) do
  144.                         if type(info)=="boolean" then
  145.                             if info==false then
  146.                                 info="false"
  147.                             else
  148.                                 info="true"
  149.                             end
  150.                         elseif type(info)=="function" then
  151.                             info="function"
  152.                         end
  153.                             print(infoID.." :"..info)
  154.                        
  155.                     end
  156.                 end
  157.                 return buttons[buttonID]
  158.             end
  159.         end
  160.     end
  161. end
  162.  
  163. function setButtonInfo(buttonID,infoID,info)
  164.     if monitor==nil then
  165.         if debugMode==true then
  166.             print("Monitor is not yet defined! Use setMonitorSide(side)")
  167.         end
  168.         return false,"Monitor is not yet defined! Use setMonitorSide(side)"
  169.     else
  170.         if buttonID==nil then
  171.             if debugMode==true then
  172.                 print("Not all required arguments are specified.")
  173.             end
  174.             return false,"Not all required arguments are specified."
  175.         else
  176.             if buttons[buttonID]==nil then
  177.                 if debugMode==true then
  178.                     print("That button ID does not correspond with a button!")
  179.                 end
  180.                 return false,"That button ID does not correspond with a button!"
  181.             else
  182.                 buttons[buttonID][infoID]=info
  183.             end
  184.         end
  185.     end
  186. end
  187.  
  188. function run(side,clickX,clickY)
  189.     for buttonID,buttonInfo in pairs(buttons) do
  190.         local x=buttonInfo['x']
  191.         local y=buttonInfo['y']
  192.         local width=buttonInfo['width']
  193.         local height=buttonInfo['height']
  194.         if buttonInfo['relative']==true then
  195.             x=math.floor(x*screenx+0.5)
  196.             y=math.floor(y*screeny+0.5)
  197.             width=math.floor(width*screenx+0.5)
  198.             height=math.floor(height*screeny+0.5)
  199.         end
  200.         if debugMode==true then
  201.             print("Pos:"..x..","..y)
  202.         end
  203.         if clickX>=x and clickX<=x+width and clickY>=y and clickY<=y+height then
  204.             if debugMode==true then
  205.                 print("Clicked :"..buttonID)
  206.             end
  207.             if buttonInfo['toggle']==true then
  208.                 buttonInfo['active']=not buttonInfo['active']
  209.                 drawAllButtons()
  210.                 if buttonInfo['active']==true then
  211.                     events.triggerEvent("onButtonClick",buttonID,true)
  212.                 else
  213.                     events.triggerEvent("onButtonClick",buttonID,false)
  214.                 end
  215.             else
  216.                 buttonInfo['active']=true
  217.                 drawAllButtons()
  218.                 events.triggerEvent("onButtonClick",buttonID)
  219.                 sleep(0)
  220.                 if buttonInfo~=nil then
  221.                     buttonInfo['active']=false
  222.                 end
  223.                 drawAllButtons()
  224.             end
  225.            
  226.         end
  227.     end
  228. end
  229. events.addEventHandler("monitor_touch",run)
  230. events.addEventHandler("mouse_click",run)
  231.  
  232. function resised(side)
  233.     if side==monitorSide then
  234.         setMonitorSide(side)
  235.         drawAllButtons()
  236.     end
  237. end
  238. events.addEventHandler("monitor_resize",resised)
  239.  
  240. --[[
  241. function updateButtonPositions()
  242.     for buttonID,buttonInfo in pairs(buttons) do
  243.         if buttonInfo['relative']==true then
  244.             x=
  245.             y=
  246.             width=
  247.             height=
  248.             x=math.floor(x*screenx+0.5)
  249.             y=math.floor(y*screeny+0.5)
  250.             width=math.floor(width*screenx+0.5)
  251.             height=math.floor(height*screeny+0.5)
  252.         end    
  253.     end
  254. end
  255. ]]
  256.  
  257. function setMonitorSide(side)
  258.     if side==nil then
  259.         if debugMode==true then
  260.             print("Not all required arguments are specified.")
  261.         end
  262.         return false,"Not all required arguments are specified."
  263.     else
  264.         monitor=peripheral.wrap(side)
  265.         screenx,screeny=monitor.getSize()
  266.         monitorSide=side
  267.     end
  268. end
  269.  
  270. function setMonitor(peripheral)
  271.     if peripheral==nil then
  272.         if debugMode==true then
  273.             print("Not all required arguments are specified.")
  274.         end
  275.         return false,"Not all required arguments are specified."
  276.     else
  277.         monitor=peripheral
  278.         screenx,screeny=monitor.getSize()
  279.         monitorSide=peripheral
  280.     end
  281. end
  282.  
  283. function setDebugMode(bool)
  284.     if bool==nil then
  285.         if debugMode==true then
  286.             print("Not all required arguments are specified.")
  287.         end
  288.         return false,"Not all required arguments are specified."
  289.     else
  290.         debugMode=bool
  291.     end
  292. end
  293.  
  294. function setIdleColor(color)
  295.     if color==nil then
  296.         if debugMode==true then
  297.             print("Not all required arguments are specified.")
  298.         end
  299.         return false,"Not all required arguments are specified."
  300.     else
  301.         idleColor=color
  302.         drawAllButtons()
  303.     end
  304. end
  305.  
  306. function setActiveColor(color)
  307.     if color==nil then
  308.         if debugMode==true then
  309.             print("Not all required arguments are specified.")
  310.         end
  311.         return false,"Not all required arguments are specified."
  312.     else
  313.         activatedColor=color
  314.         drawAllButtons()
  315.     end
  316. end
  317.  
  318. function setBackgroundColor(color)
  319.     if color==nil then
  320.         if debugMode==true then
  321.             print("Not all required arguments are specified.")
  322.         end
  323.         return false,"Not all required arguments are specified."
  324.     else
  325.         backgroundColor=color
  326.         drawAllButtons()
  327.     end
  328. end
  329.  
  330. function setTextColor(color)
  331.     if color==nil then
  332.         if debugMode==true then
  333.             print("Not all required arguments are specified.")
  334.         end
  335.         return false,"Not all required arguments are specified."
  336.     else
  337.         textColor=color
  338.         drawAllButtons()
  339.     end
  340. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement