Advertisement
krakaen

buttonAPI.v2

Dec 1st, 2016
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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]["name"] = name
  16.   button[name]["func"] = func
  17.   button[name]["active"] = false
  18.   button[name]["xmin"] = xmin
  19.   button[name]["ymin"] = ymin
  20.   button[name]["xmax"] = xmax
  21.   button[name]["ymax"] = ymax
  22.   button[name]["colors"] = colors
  23. end
  24.  
  25. function API.fill(bData)
  26.  
  27.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  28.   local xspot = math.floor((bData["xmin"] + bData["xmax"]) /2) - math.floor((string.len(bData["text"])/2))
  29.   local oldColor = gpu.getBackground()
  30.   local curColor = bData["colors"].on
  31.   local curText = " "
  32.   if type(bData["text"]) == "table" then
  33.     curColor = bData["text"].on
  34.     if bData["active"] then
  35.       curColor = bData["text"].off
  36.     end
  37.   else
  38.     curText = bData["text"]
  39.   end
  40.  
  41.   if bData["active"] then
  42.     curColor = bData["colors"].off
  43.   end
  44.   gpu.setBackground(curColor)
  45.   gpu.fill(bData["xmin"], bData["ymin"], bData["xmax"] - bData["xmin"] + 1, bData["ymax"] - bData["ymin"] + 1, " ")
  46.   gpu.set(xspot, yspot, curText)
  47.   gpu.setBackground(oldColor)
  48. end
  49.  
  50. function API.screen()
  51.   for name,data in pairs(button) do
  52.      API.fill(data)
  53.   end
  54. end
  55.  
  56. function API.toggleButton(bData)
  57.  bData["active"] = not bData["active"]
  58.  API.screen()
  59. end
  60.  
  61. function API.checkxy(_, _, x, y, _, _)
  62.   for name, data in pairs(button) do
  63.     if y >= data["ymin"] and y <= data["ymax"] then
  64.       if x >= data["xmin"] and x <= data["xmax"] then
  65.         data["func"]()
  66.         toggleButton(data)
  67.         return true
  68.       end
  69.     end
  70.   end
  71.   return false
  72. end
  73.  
  74.  
  75. return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement