Arimus

buttonAPI

Aug 12th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.84 KB | None | 0 0
  1. -- Updater Information **IGNORE**
  2. apiVersion = "1.2"
  3.  
  4. function version()
  5.   return apiVersion
  6. end
  7.  
  8. -- START OF PROGRAM
  9. -- Array which holds the buttons
  10. buttons = {}
  11.  
  12. -- Sets what side the monitor is on
  13. function setMon(side)
  14.   m = peripheral.wrap(side)
  15. end
  16.  
  17. -- Determines if a point is inside a rectangle
  18. function ptinrect(x1, y1, x2, y2)
  19.  
  20.   if (x >= x1) and (x <= x2) and (y >= y1) and (y <= y2) then
  21.     return true
  22.   else
  23.     return false
  24.   end
  25.  
  26. end
  27.  
  28. -- Prints text in the centre of a button
  29. function cPrint(button)
  30.  
  31.   butWidth1 = buttons[button].x2 - buttons[button].x1
  32.   butHeight = buttons[button].y2 - buttons[button].y1
  33.  
  34.   textX1 = buttons[button].x1 + (butWidth1/2)
  35.   textX = (textX1 - (string.len(buttons[button].label)/2))
  36.  
  37.   m.setCursorPos(textX + 1, buttons[button].y1 + (butHeight/2))
  38.  
  39.   -- Set text color to the button-specific color
  40.   m.setTextColor(buttons[button].textColor)
  41.  
  42.   m.write(buttons[button].label)
  43.  
  44.   -- Reset text color
  45.   m.setTextColor(colors.white)
  46.  
  47. end
  48.  
  49.  
  50. -- Adds a button to the array
  51. function add(butName, butLabel, butMode, butX1, butY1, butX2, butY2, butColor, butSecColor, butTextColor, butFunc)
  52.   table.insert(buttons, {
  53.     name = butName,
  54.     label = butLabel,
  55.     x1 = butX1,
  56.     y1 = butY1,
  57.     x2 = butX2,
  58.     y2 = butY2,
  59.     color = butColor,
  60.     secColor = butSecColor,
  61.     textColor = butTextColor,
  62.     func = butFunc,
  63.     mode = butMode,
  64.     toggled = ""
  65.   })
  66. end
  67.  
  68. -- Sets the toggled state of a button on "toggle" mode
  69. function setState(butName, state)
  70.   for i = 1, #buttons do
  71.     if buttons[i].name == butName then
  72.       buttons[i].toggled = state
  73.     end
  74.   end
  75. end
  76.  
  77. -- Allows a program to get the state of a button
  78. function getState(butName)
  79.   for i = 1, #buttons do
  80.     if buttons[i].name == butName then
  81.       return buttons[i].toggled
  82.     end
  83.   end
  84. end
  85.  
  86. -- Makes a button flash between two colors
  87. function flash(button)
  88.  
  89.   --Draw the button in its secondary color
  90.   m.setBackgroundColor(buttons[button].secColor)
  91.   for y = buttons[button].y1, buttons[button].y2 do
  92.     for x = buttons[button].x1, buttons[button].x2 do
  93.       m.setCursorPos(x, y)
  94.       m.write(" ")  
  95.     end
  96.   end
  97.  
  98.   cPrint(button)
  99.   sleep(0.15)
  100.  
  101.   -- Draws the button in its primary color once again
  102.   m.setBackgroundColor(buttons[button].color)
  103.   for y = buttons[button].y1, buttons[button].y2 do
  104.     for x = buttons[button].x1, buttons[button].x2 do
  105.     m.setCursorPos(x, y)
  106.     m.write(" ")
  107.     end
  108.   end
  109.  
  110.   cPrint(button)
  111.   m.setBackgroundColor(colors.black)
  112.  
  113. end
  114.  
  115. -- Toggles the button on or off depending on the variable in the array
  116. function toggle(button)
  117.  
  118.   -- If the button is on, turn it off
  119.   if buttons[button].toggled == true then
  120.  
  121.     m.setBackgroundColor(buttons[button].secColor)
  122.        
  123.     for y = buttons[button].y1, buttons[button].y2 do
  124.       for x = buttons[button].x1, buttons[button].x2 do
  125.         m.setCursorPos(x, y)
  126.         m.write(" ")
  127.       end
  128.     end
  129.    
  130.     cPrint(button)
  131.    
  132.     buttons[button].toggled = false
  133.     m.setBackgroundColor(colors.black)
  134.  
  135.  
  136.    
  137.   -- If the button is off, turn it on
  138.   elseif buttons[button].toggled == false then
  139.    
  140.     m.setBackgroundColor(buttons[button].color)
  141.    
  142.     for y = buttons[button].y1, buttons[button].y2 do
  143.       for x = buttons[button].x1, buttons[button].x2 do
  144.         m.setCursorPos(x, y)
  145.         m.write(" ")
  146.       end
  147.     end
  148.    
  149.     cPrint(button)
  150.    
  151.     buttons[button].toggled = true
  152.     m.setBackgroundColor(colors.black)
  153.    
  154.   else
  155.     error("You have not defined the state of the "..buttons[button].name.." button.")
  156.   end
  157.  
  158. end
  159.  
  160. -- Draws the buttons on the screen
  161. function draw()
  162.  
  163.   m.clear()
  164.  
  165.   for button = 1, #buttons do
  166.     if buttons[button].mode == "flash" then
  167.       -- Sets the color of the button
  168.       m.setBackgroundColor(buttons[button].color)
  169.    
  170.       -- Draws the button
  171.       for y = buttons[button].y1, buttons[button].y2 do
  172.         for x = buttons[button].x1, buttons[button].x2 do
  173.           m.setCursorPos(x, y)
  174.           m.write(" ")
  175.         end
  176.       end
  177.      
  178.       cPrint(button)
  179.      
  180.       -- Resets the background color
  181.       m.setBackgroundColor(colors.black)
  182.      
  183.     elseif buttons[button].mode == "toggle" then
  184.    
  185.       if buttons[button].toggled == true then
  186.        
  187.         m.setBackgroundColor(buttons[button].color)
  188.        
  189.         for y = buttons[button].y1, buttons[button].y2 do
  190.           for x = buttons[button].x1, buttons[button].x2 do
  191.             m.setCursorPos(x, y)
  192.             m.write(" ")
  193.           end
  194.         end
  195.        
  196.         cPrint(button)
  197.        
  198.         m.setBackgroundColor(colors.black)
  199.        
  200.       elseif buttons[button].toggled == false then
  201.        
  202.         m.setBackgroundColor(buttons[button].secColor)
  203.      
  204.         for y = buttons[button].y1, buttons[button].y2 do
  205.           for x = buttons[button].x1, buttons[button].x2 do
  206.             m.setCursorPos(x, y)
  207.             m.write(" ")
  208.           end
  209.         end
  210.        
  211.         cPrint(button)
  212.        
  213.         m.setBackgroundColor(colors.black)
  214.        
  215.       else
  216.         error("You have not defined a mode for this button.")
  217.       end
  218.     end
  219.   end
  220. end
  221.  
  222. -- Waits for a touch on an adjacent advanced monitor and responds accordingly
  223. function check()
  224.  
  225.   event, side, x, y = os.pullEvent("monitor_touch")
  226.  
  227.   -- Match it to a button and execute the function
  228.   for button = 1, #buttons do
  229.     if ptinrect(buttons[button].x1, buttons[button].y1, buttons[button].x2, buttons[button].y2) then
  230.      
  231.       if buttons[button].mode == "flash" then
  232.         flash(button)
  233.       elseif buttons[button].mode == "toggle" then
  234.         toggle(button)
  235.       else
  236.         error("You have not defined a mode for this button.")
  237.       end
  238.  
  239.       buttons[button].func()
  240.            
  241.     end
  242.   end
  243.  
  244. end
Add Comment
Please, Sign In to add comment