Advertisement
krakaen

openComputer - buttonAPI

Nov 9th, 2016
87,965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. local API = {}
  2. local button = {}
  3.  
  4. local component = require("component")
  5. local gpu = component.gpu
  6. local event = require("event")
  7.  
  8. function API.clearTable()
  9.   button = {}
  10. end
  11.  
  12. function API.setTable(name, func, xmin, ymin, xmax, ymax, text, colors) -- color is an object { on : 0x000000, off 0xAAAAAA}
  13.   button[name] = {}
  14.   button[name]["text"] = text
  15.   button[name]["func"] = func
  16.   button[name]["active"] = false
  17.   button[name]["xmin"] = xmin
  18.   button[name]["ymin"] = ymin
  19.   button[name]["xmax"] = xmax
  20.   button[name]["ymax"] = ymax
  21.   button[name]["colors"] = colors
  22. end
  23.  
  24. function API.fill(bData)
  25.  
  26.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  27.   local xspot = math.floor((bData["xmin"] + bData["xmax"]) /2) - math.floor((string.len(bData["text"])/2))
  28.   local oldColor = gpu.getBackground()
  29.   local curColor = bData["colors"].on
  30.  
  31.   if bData["active"] then
  32.     curColor = bData["colors"].off
  33.   end
  34.   gpu.setBackground(curColor)
  35.   gpu.fill(bData["xmin"], bData["ymin"], bData["xmax"] - bData["xmin"] + 1, bData["ymax"] - bData["ymin"] + 1, " ")
  36.   gpu.set(xspot, yspot, bData["text"])
  37.   gpu.setBackground(oldColor)
  38. end
  39.  
  40. function API.screen()
  41.   for name,data in pairs(button) do
  42.      API.fill(data)
  43.   end
  44. end
  45.  
  46. function API.toggleButton(name)
  47.   button[name]["active"] = not button[name]["active"]
  48.   API.screen() -- not sure about this one here
  49. end
  50.  
  51. function API.flash(name)
  52.   API.toggleButton(name)
  53.   API.screen() -- or here
  54. end
  55.  
  56. function API.checkxy(_, _, x, y, _, _)
  57.   for name, data in pairs(button) do
  58.     if y >= data["ymin"] and y <= data["ymax"] then
  59.       if x >= data["xmin"] and x <= data["xmax"] then
  60.         data["func"]()
  61.         return true
  62.       end
  63.     end
  64.   end
  65.   return false
  66. end
  67.  
  68.  
  69. return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement