Advertisement
Master0r0

OpenC Button API

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