Advertisement
MoparDan

buttonAPI.lua

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