Advertisement
NanoBob

Button API V3

Jul 18th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.81 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,toRunFunction,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,['function']=toRunFunction,['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.                     buttonInfo['function'](true,buttonID,buttonInfo.text)
  212.                 else
  213.                     buttonInfo['function'](false,buttonID,buttonInfo.text)
  214.                 end
  215.             else
  216.                 buttonInfo['active']=true
  217.                 drawAllButtons()
  218.                 buttonInfo['function'](buttonID,buttonInfo.text)
  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.  
  231. function resised(side)
  232.     if side==monitorSide then
  233.         setMonitorSide(side)
  234.         drawAllButtons()
  235.     end
  236. end
  237. events.addEventHandler("monitor_resize",resised)
  238.  
  239. --[[
  240. function updateButtonPositions()
  241.     for buttonID,buttonInfo in pairs(buttons) do
  242.         if buttonInfo['relative']==true then
  243.             x=
  244.             y=
  245.             width=
  246.             height=
  247.             x=math.floor(x*screenx+0.5)
  248.             y=math.floor(y*screeny+0.5)
  249.             width=math.floor(width*screenx+0.5)
  250.             height=math.floor(height*screeny+0.5)
  251.         end    
  252.     end
  253. end
  254. ]]
  255.  
  256. function setMonitorSide(side)
  257.     if side==nil then
  258.         if debugMode==true then
  259.             print("Not all required arguments are specified.")
  260.         end
  261.         return false,"Not all required arguments are specified."
  262.     else
  263.         monitor=peripheral.wrap(side)
  264.         screenx,screeny=monitor.getSize()
  265.         monitorSide=side
  266.     end
  267. end
  268.  
  269. function setMonitor(peripheral)
  270.     if peripheral==nil then
  271.         if debugMode==true then
  272.             print("Not all required arguments are specified.")
  273.         end
  274.         return false,"Not all required arguments are specified."
  275.     else
  276.         monitor=peripheral
  277.         screenx,screeny=monitor.getSize()
  278.         monitorSide=peripheral
  279.     end
  280. end
  281.  
  282. function setDebugMode(bool)
  283.     if bool==nil then
  284.         if debugMode==true then
  285.             print("Not all required arguments are specified.")
  286.         end
  287.         return false,"Not all required arguments are specified."
  288.     else
  289.         debugMode=bool
  290.     end
  291. end
  292.  
  293. function setIdleColor(color)
  294.     if color==nil then
  295.         if debugMode==true then
  296.             print("Not all required arguments are specified.")
  297.         end
  298.         return false,"Not all required arguments are specified."
  299.     else
  300.         idleColor=color
  301.         drawAllButtons()
  302.     end
  303. end
  304.  
  305. function setActiveColor(color)
  306.     if color==nil then
  307.         if debugMode==true then
  308.             print("Not all required arguments are specified.")
  309.         end
  310.         return false,"Not all required arguments are specified."
  311.     else
  312.         activatedColor=color
  313.         drawAllButtons()
  314.     end
  315. end
  316.  
  317. function setBackgroundColor(color)
  318.     if color==nil then
  319.         if debugMode==true then
  320.             print("Not all required arguments are specified.")
  321.         end
  322.         return false,"Not all required arguments are specified."
  323.     else
  324.         backgroundColor=color
  325.         drawAllButtons()
  326.     end
  327. end
  328.  
  329. function setTextColor(color)
  330.     if color==nil then
  331.         if debugMode==true then
  332.             print("Not all required arguments are specified.")
  333.         end
  334.         return false,"Not all required arguments are specified."
  335.     else
  336.         textColor=color
  337.         drawAllButtons()
  338.     end
  339. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement