Advertisement
NanoBob

button API

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