quantumr8

buttonAPI

Feb 27th, 2021 (edited)
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. local API = {}
  2. local button = {}
  3.  
  4.  
  5. local component = require("component")
  6. local gpu = component.gpu
  7. local event = require("event")
  8. local term = require("term")
  9.  
  10.  
  11. function API.clearTable()
  12.  
  13.   button = {}
  14.  
  15. end
  16.  
  17.  
  18. function API.heading(text)
  19.  
  20.   w, h = gpu.getResolution()
  21.   term.setCursor((w-string.len(text))/2+1, 1)
  22.   term.write(text)
  23.  
  24. end
  25.  
  26.      
  27. function API.label(w, h, text)
  28.  
  29.   term.setCursor(w, h)
  30.   term.write(text)
  31.  
  32. end
  33.  
  34.  
  35. function API.getState(name)
  36.  
  37.   return button[name]["active"]
  38.  
  39. end
  40.  
  41.  
  42. function API.setTable(name, func, xmin, ymin, xmax, ymax, text, colors) -- color is an object { on : 0x000000, off 0xAAAAAA}
  43.  
  44.   button[name] = {}
  45.  
  46.   button[name]["text"] = text
  47.  
  48.   button[name]["func"] = func
  49.  
  50.   button[name]["active"] = false
  51.  
  52.   button[name]["xmin"] = xmin
  53.  
  54.   button[name]["ymin"] = ymin
  55.  
  56.   button[name]["xmax"] = xmax
  57.  
  58.   button[name]["ymax"] = ymax
  59.  
  60.   button[name]["colors"] = colors
  61.  
  62. end
  63.  
  64.  
  65. function API.fill(bData)
  66.  
  67.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  68.   local xspot = math.floor((bData["xmin"] + bData["xmax"]) /2) - math.floor((string.len(bData["text"])/2))
  69.   local oldColor = gpu.getBackground()
  70.   local curColor = bData["colors"].on
  71.  
  72.   if bData["active"] then
  73.  
  74.     curColor = bData["colors"].off
  75.  
  76.   end
  77.  
  78.   gpu.setBackground(curColor)
  79.   gpu.fill(bData["xmin"], bData["ymin"], bData["xmax"] - bData["xmin"] + 1, bData["ymax"] - bData["ymin"] + 1, " ")
  80.   gpu.set(xspot, yspot, bData["text"])
  81.   gpu.setBackground(oldColor)
  82.  
  83. end
  84.  
  85.  
  86. function API.screen()
  87.  
  88.   for name,data in pairs(button) do
  89.  
  90.      API.fill(data)
  91.  
  92.   end
  93.  
  94. end
  95.  
  96.  
  97. function API.toggleButton(name)
  98.  
  99.   button[name]["active"] = not button[name]["active"]
  100.   API.screen() -- not sure about this one here
  101.  
  102. end
  103.  
  104.  
  105. function API.flash(name,length)
  106.  
  107.   API.toggleButton(name)
  108.   API.screen()
  109.   os.sleep(length)
  110.   API.toggleButton(name)
  111.   API.screen()
  112.  
  113. end
  114.  
  115.  
  116. function API.checkxy(_, _, x, y, _, _)
  117.  
  118.   for name, data in pairs(button) do
  119.  
  120.     if y >= data["ymin"] and y <= data["ymax"] then
  121.  
  122.       if x >= data["xmin"] and x <= data["xmax"] then
  123.  
  124.         data["func"]()
  125.  
  126.         return true
  127.  
  128.       end
  129.  
  130.     end
  131.  
  132.   end
  133.  
  134.   return false
  135.  
  136. end
  137.  
  138.  
  139.  
  140.  
  141. return API
Add Comment
Please, Sign In to add comment