Advertisement
Guest User

Direwolf20 Button API

a guest
Mar 29th, 2015
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. local mon = peripheral.wrap("left")
  2. mon.setTextScale(1)
  3. mon.setTextColor(colors.white)
  4. local button={}
  5. mon.setBackgroundColor(colors.black)
  6.  
  7. function clearTable()
  8. button = {}
  9. mon.clear()
  10. end
  11.  
  12. function setTable(name, func, xmin, xmax, ymin, ymax)
  13. button[name] = {}
  14. button[name]["func"] = func
  15. button[name]["active"] = false
  16. button[name]["xmin"] = xmin
  17. button[name]["ymin"] = ymin
  18. button[name]["xmax"] = xmax
  19. button[name]["ymax"] = ymax
  20. end
  21.  
  22. function fill(text, color, bData)
  23. mon.setBackgroundColor(color)
  24. local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  25. local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  26. for j = bData["ymin"], bData["ymax"] do
  27. mon.setCursorPos(bData["xmin"], j)
  28. if j == yspot then
  29. for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  30. if k == xspot then
  31. mon.write(text)
  32. else
  33. mon.write(" ")
  34. end
  35. end
  36. else
  37. for i = bData["xmin"], bData["xmax"] do
  38. mon.write(" ")
  39. end
  40. end
  41. end
  42. mon.setBackgroundColor(colors.black)
  43. end
  44.  
  45. function screen()
  46. local currColor
  47. for name,data in pairs(button) do
  48. local on = data["active"]
  49. if on == true then currColor = colors.lime else currColor = colors.cyan end
  50. fill(name, currColor, data)
  51. end
  52. end
  53.  
  54. function toggleButton(name)
  55. button[name]["active"] = not button[name]["active"]
  56. screen()
  57. end
  58.  
  59. function flash(name)
  60. toggleButton(name)
  61. screen()
  62. sleep(0.15)
  63. toggleButton(name)
  64. screen()
  65. end
  66.  
  67. function 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. --data["active"] = not data["active"]
  74. --print(name)
  75. end
  76. end
  77. end
  78. return false
  79. end
  80.  
  81. function heading(text)
  82. w, h = mon.getSize()
  83. mon.setCursorPos((w-string.len(text))/2+1, 1)
  84. mon.write(text)
  85. end
  86.  
  87. function label(w, h, text)
  88. mon.setCursorPos(w, h)
  89. mon.write(text)
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement