Advertisement
DuckStrom

Computercraft buttonAPI

Feb 11th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1. --[[
  2. EyeDeck's button API
  3. Handles drawing buttons to a screen, and mapping
  4. raw (x,y) input coords to drawn buttons.
  5. --]]
  6.  
  7. button_registry = {}
  8.  
  9. --monitor to write to, top left (x,y), width and height
  10. --text color, background color, button name (optional)
  11. function drawButton(m,x,y,w,h,txtcolor,bgcolor,text,name)
  12.   local old_term = term.redirect(m)
  13.   local old_bg = term.getBackgroundColor()
  14.   local old_tx = term.getTextColor()
  15.   local old_posx, old_posy = term.getCursorPos()
  16.  
  17.   paintutils.drawLine(x+1,   y,     x+w-2, y,     colors.lightGray)
  18.   paintutils.drawLine(x+1,   y+h-1, x+w-2, y+h-1, colors.gray)
  19.   paintutils.drawLine(x,     y+1,   x,     y+h-2, colors.gray)
  20.   paintutils.drawLine(x+w-1, y+1,   x+w-1, y+h-2, colors.lightGray)
  21.   paintutils.drawFilledBox(x+1, y+1, x+w-2, y+h-2, bgcolor)
  22.  
  23.   term.setTextColor(txtcolor)
  24.   term.setBackgroundColor(bgcolor)
  25.   term.setCursorPos(x+math.floor((w-string.len(text))/2),y+math.floor((h-1)/2))
  26.   term.write(text)
  27.  
  28.   term.setCursorPos(old_posx, old_posy)
  29.   term.setTextColor(old_tx)
  30.   term.setBackgroundColor(old_bg)  
  31.   term.redirect(old_term)
  32.  
  33.   if name ~= nil then
  34.     table.insert(button_registry,{x=x+1,y=y+1,x2=x+w-2,y2=y+h-2,name=name})
  35.   end
  36.  
  37. end
  38.  
  39. function getButton(x,y)
  40.   for _,v in pairs(button_registry) do
  41.     if x >= v.x and x <= v.x2 and y >= v.y and y <= v.y2 then
  42.       return v.name
  43.     end
  44.   end
  45.  
  46.   return false
  47. end
  48.  
  49. function removeButton(name)
  50.   for i,v in pairs(button_registry) do
  51.     if v.name == name then
  52.       table.remove(button_registry, i)
  53.       return true
  54.     end
  55.   end
  56.  
  57.   return false
  58. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement