Advertisement
HPWebcamAble

Button API (Terminal)

Feb 20th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.27 KB | None | 0 0
  1. --Coded by HPWebcamAble--
  2.  
  3. --[[
  4. NOTE: This is used for displaying buttons
  5. in the computer terminal, not on a monitor
  6. ]]
  7.  
  8. function clear(cbt)
  9.   term.clear()
  10.   term.setCursorPos(1,1)
  11.   if cbt then
  12.     buttons = {}
  13.   end
  14. end
  15.  
  16. function draw(name,x,y,onColor,offColor,inacColor,borderX,borderY,func)
  17.   buttons[name] = {}
  18.   buttons[name]["xpos"] = x
  19.   buttons[name]["ypos"] = y
  20.   buttons[name]["onColor"] = onColor
  21.   buttons[name]["offColor"] = offColor
  22.   buttons[name]["inacColor"] = inacColor
  23.   buttons[name]["borderX"] = borderX
  24.   buttons[name]["borderY"] = borderY
  25.   buttons[name]["state"] = 1
  26.   buttons[name]["func"] = func
  27.   drawButtons()
  28. end
  29.  
  30. function toggleButton(name)
  31.   if buttons[name]["state"] == 1 then
  32.     buttons[name]["state"] = 2
  33.   elseif buttons[name]["state"] == 2 then
  34.     buttons[name]["state"] = 1
  35.   end
  36.   drawButtons()
  37. end
  38.  
  39. function setState(name,state)
  40.   buttons[name]["state"] = state
  41.   drawButtons()
  42. end
  43.  
  44. function flashButton(name,time)
  45.   buttons[name]["state"] = not buttons[name]["state"]
  46.   drawButtons()
  47.   sleep(time)
  48.   buttons[name]["state"] = not buttons[name]["state"]
  49.   drawButtons()
  50. end
  51.  
  52. function drawButtons()
  53.   for name,data in pairs(buttons) do
  54.     local xpos = buttons[name]["xpos"]
  55.     local ypos = buttons[name]["ypos"]
  56.     local xborder = buttons[name]["borderX"]
  57.     local yborder = buttons[name]["borderY"]
  58.     term.setCursorPos(xpos,ypos)
  59.     if buttons[name]["state"] == 1 then
  60.       term.setBackgroundColor(buttons[name]["onColor"])
  61.     elseif buttons[name]["state"] == 2 then
  62.       term.setBackgroundColor(buttons[name]["offColor"])
  63.     elseif buttons[name]["state"] == 3 then
  64.       term.setBackgroundColor(buttons[name]["inacColor"])
  65.     end
  66.     term.write(name)
  67.     term.setCursorPos(xpos-xborder,ypos-yborder)
  68.     tempx,tempy = term.getCursorPos()
  69.     tempx2 = tempx
  70.     for i = 1, yborder do
  71.       tempx = tempx2
  72.       for j = 1, buttons[name]["borderX"]*2+string.len(name)  do
  73.         term.setCursorPos(tempx,tempy)
  74.         term.write(" ")
  75.         tempx = tempx+1
  76.       end
  77.       tempy = tempy+1
  78.     end
  79.     term.setCursorPos(buttons[name]["xpos"]-xborder,buttons[name]["ypos"])
  80.     for i = 1, xborder do
  81.       term.write(" ")
  82.     end
  83.     term.setCursorPos(buttons[name]["xpos"]+string.len(name),buttons[name]["ypos"])
  84.     for i = 1, xborder do
  85.       term.write(" ")
  86.     end
  87.     tempx = buttons[name]["xpos"]-xborder
  88.     tempy = buttons[name]["ypos"]+1
  89.    
  90.     for i = 1, buttons[name]["borderY"] do
  91.       tempx = tempx2
  92.       for j = 1, xborder*2+string.len(name) do
  93.         term.setCursorPos(tempx,tempy)  
  94.         term.write(" ")
  95.         tempx = tempx+1
  96.       end
  97.       tempy = tempy+1
  98.     end
  99.   end
  100.   term.setBackgroundColor(colors.black)
  101. end
  102.  
  103. function checkCords(tx,ty)
  104.   for name,data in pairs(buttons) do
  105.     tempx = buttons[name]["xpos"]
  106.     tempy = buttons[name]["ypos"]
  107.     xborder = buttons[name]["borderX"]
  108.     yborder = buttons[name]["borderY"]
  109.     minx = tempx-xborder
  110.     miny = tempy-yborder
  111.     maxx = tempx+string.len(name)+xborder
  112.     maxy = tempy+yborder
  113.     if tx >= minx and tx <= maxx and ty >= miny and ty <= maxy then
  114.       if buttons[name]["func"] ~= nil then
  115.         buttons[name]["func"]()
  116.       else
  117.         return name
  118.       end
  119.     end
  120.   end
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement