Advertisement
pandorrama

buttonAPI.lua

Mar 15th, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.39 KB | None | 0 0
  1. --download with: pastebin get vNvmxtZM buttonAPI.lua
  2.  
  3. local API = {}
  4. local button={}
  5.  
  6. local component = require("component")
  7. local colors = require("colors")
  8. local term = require("term")
  9. local mon = component.gpu
  10. local w, h = mon.getResolution()
  11. local wlan = component.modem
  12. local wlanAnswerAddress = "46c19e44-7e0c-4a24-886d-ffd707de5f68"
  13. local wlanPort = 23
  14. local Green = 0x00AA00
  15. local Red = 0xAA0000
  16. local Black = 0x000000
  17.  
  18. buttonStatus = nil
  19.  
  20. function API.clear()
  21.   mon.setBackground(Black)
  22.   mon.fill(1, 1, w, h, " ")
  23. end
  24.  
  25. function API.clearTable()
  26.   button = {}
  27.   API.clear()
  28. end
  29.                
  30. function API.setTable(name, func, xmin, xmax, ymin, ymax)
  31.   button[name] = {}
  32.   button[name]["func"] = func
  33.   button[name]["active"] = false
  34.   button[name]["xmin"] = xmin
  35.   button[name]["ymin"] = ymin
  36.   button[name]["xmax"] = xmax
  37.   button[name]["ymax"] = ymax
  38. end
  39.  
  40. function API.fill(text, color, bData)
  41.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  42.   local xspot = math.floor((bData["xmax"] + bData["xmin"] - string.len(text)) /2)+1
  43.   local oldColor = mon.setBackground(color)
  44.   mon.fill(bData["xmin"], bData["ymin"], (bData["xmax"]-bData["xmin"]+1), (bData["ymax"]-bData["ymin"]+1), " ")
  45.   mon.set(xspot, yspot, text)
  46.   mon.setBackground(oldColor)
  47. end
  48.      
  49. function API.screen()
  50.   local currColor
  51.   for name,data in pairs(button) do
  52.     local on = data["active"]
  53.     if on == true then currColor = Green else currColor = Red end
  54.     API.fill(name, currColor, data)
  55.   end
  56. end
  57.  
  58. function API.toggleButton(name)
  59.   button[name]["active"] = not button[name]["active"]
  60.   buttonStatus = button[name]["active"]
  61.   API.screen()
  62. end    
  63.  
  64. function API.flash(name,length)
  65.   API.toggleButton(name)
  66.   API.screen()
  67.   os.sleep(length)
  68.   API.toggleButton(name)
  69.   API.screen()
  70. end
  71.                                              
  72. function API.checkxy(x, y)
  73.   for name, data in pairs(button) do
  74.     if y>=data["ymin"] and  y <= data["ymax"] then
  75.       if x>=data["xmin"] and x<= data["xmax"] then
  76.         wlan.send(wlanAnswerAddress, wlanPort, data["func"])
  77.         return true
  78.       end
  79.     end
  80.   end
  81.   return false
  82. end
  83.      
  84. function API.heading(text)
  85.   w, h = mon.getResolution()
  86.   term.setCursor((w-string.len(text))/2+1, 1)
  87.   term.write(text)
  88. end
  89.      
  90. function API.label(w, h, text)
  91.   term.setCursor(w, h)
  92.   term.write(text)
  93. end
  94.  
  95. return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement