Advertisement
HPWebcamAble

Button API (Monitor)

Apr 20th, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.38 KB | None | 0 0
  1. --Coded by HPWebcamAble--
  2.  
  3. --[[
  4. Note: This is designed to create buttons
  5. on an ADVANCED monitor
  6.  
  7. How to use:
  8. 1. In your program, do button.wrapMon(<sideofyourmonitor>)
  9. 2. Then use button.draw(<parameters>) to add buttons
  10.   name - The name of the button (as a string)
  11.   x - The x cord of the FIRST LETTER of the NAME OF THE BUTTON
  12.   Y - The y cord of the FIRST LETTER of the NAME OF THE BUTTON
  13.   bOnColor - The color of the background of the button when its on
  14.   bOffColor - The color of the background of the button when its off
  15.   tOnColor - The color of the text of the button when its on
  16.   tOffColor - The color of the text of the button when its off
  17.   borderX - The number of pixels of the boarder on the x axis
  18.   borderY - The number of pixels of the boarder on the y axis
  19.   func - The only optional one, any function the button should run when activated
  20. 3. With an os.pullEvent(), find when someone touches the monitor, and then use
  21.   button.checkCords(x,y) <-- x,y is the cords of the touch
  22.   It returns the name of any button in the cords, or runs the function specified by draw()
  23.  
  24. Still confused?
  25. Message me on the ComputerCraft Forums:
  26. http://www.computercraft.info/forums2/index.php?/user/25696-hpwebcamable/
  27. ]]
  28.  
  29. function monClear()
  30.   m.clear()
  31. end
  32.  
  33. function wrapMon(side)
  34.   m = peripheral.wrap(side)
  35.   monClear()
  36.   buttons = {}
  37. end
  38.  
  39. function clear()
  40.   term.clear()
  41.   term.setCursorPos(1,1)
  42. end
  43.  
  44. function getButtons()
  45.   return buttons
  46. end
  47.  
  48. function setState(name,state)
  49.   buttons[name]["state"] = state or false
  50. end
  51.  
  52. function draw(name,x,y,bOnColor,bOffColor,tOnColor,tOffColor,borderX,borderY,func)
  53.   buttons[name] = {}
  54.   buttons[name]["xpos"] = x
  55.   buttons[name]["ypos"] = y
  56.   buttons[name]["bOnColor"] = bOnColor
  57.   buttons[name]["bOffColor"] = bOffColor
  58.   buttons[name]["tOnColor"] = tOnColor
  59.   buttons[name]["tOffColor"] = tOffColor
  60.   buttons[name]["borderX"] = borderX
  61.   buttons[name]["borderY"] = borderY
  62.   buttons[name]["state"] = false
  63.   buttons[name]["func"] = func
  64.   drawButtons()
  65. end
  66.  
  67. function toggleButton(name)
  68.   buttons[name]["state"] = not buttons[name]["state"]
  69.   drawButtons()
  70.   return buttons[name]["state"]
  71. end
  72.  
  73. function flashButton(name,time)
  74.   buttons[name]["state"] = not buttons[name]["state"]
  75.   drawButtons()
  76.   sleep(time)
  77.   buttons[name]["state"] = not buttons[name]["state"]
  78. end
  79.  
  80. function drawButtons()
  81.   for name,data in pairs(buttons) do
  82.     local xpos = buttons[name]["xpos"]
  83.     local ypos = buttons[name]["ypos"]
  84.     local xborder = buttons[name]["borderX"]
  85.     local yborder = buttons[name]["borderY"]
  86.     m.setCursorPos(xpos,ypos)
  87.     if buttons[name]["state"] then
  88.       m.setBackgroundColor(buttons[name]["bOnColor"])
  89.       m.setTextColor(buttons[name]["tOnColor"])
  90.     else
  91.       m.setBackgroundColor(buttons[name]["bOffColor"])
  92.       m.setTextColor(buttons[name]["tOffColor"])
  93.     end
  94.     m.write(name)
  95.     m.setCursorPos(xpos-xborder,ypos-yborder)
  96.     tempx,tempy = m.getCursorPos()
  97.     tempx2 = tempx
  98.     for i = 1, yborder do
  99.       tempx = tempx2
  100.       for j = 1, buttons[name]["borderX"]*2+string.len(name)  do
  101.         m.setCursorPos(tempx,tempy)
  102.         m.write(" ")
  103.         tempx = tempx+1
  104.       end
  105.       tempy = tempy+1
  106.     end
  107.     m.setCursorPos(buttons[name]["xpos"]-xborder,buttons[name]["ypos"])
  108.     for i = 1, xborder do
  109.       m.write(" ")
  110.     end
  111.     m.setCursorPos(buttons[name]["xpos"]+string.len(name),buttons[name]["ypos"])
  112.     for i = 1, xborder do
  113.       m.write(" ")
  114.     end
  115.     tempx = buttons[name]["xpos"]-xborder
  116.     tempy = buttons[name]["ypos"]+1
  117.    
  118.     for i = 1, buttons[name]["borderY"] do
  119.       tempx = tempx2
  120.       for j = 1, xborder*2+string.len(name) do
  121.         m.setCursorPos(tempx,tempy)  
  122.         m.write(" ")
  123.         tempx = tempx+1
  124.       end
  125.       tempy = tempy+1
  126.     end
  127.   end
  128.   m.setBackgroundColor(colors.black)
  129. end
  130.  
  131. function checkCords(x,y)
  132.   for name,data in pairs(buttons) do
  133.     tempx = buttons[name]["xpos"]
  134.     tempy = buttons[name]["ypos"]
  135.     xborder = buttons[name]["borderX"]
  136.     yborder = buttons[name]["borderY"]
  137.     minx = tempx-xborder
  138.     miny = tempy-yborder
  139.     maxx = tempx+string.len(name)+xborder
  140.     maxy = tempy+yborder
  141.     if x >= minx and x <= maxx and y >= miny and y <= maxy then
  142.       if buttons[name]["func"] ~= nil then
  143.         buttons[name]["func"]()
  144.       else
  145.         return name
  146.       end
  147.     end
  148.   end
  149. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement