Dodedodo33

buttonAPI (not written by me, just edited a bit)

May 24th, 2013
58
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 setTable(name, func, xmin, xmax, ymin, ymax)
  8. button[name] = {}
  9. button[name]["func"] = func
  10. button[name]["active"] = false
  11. button[name]["xmin"] = xmin
  12. button[name]["ymin"] = ymin
  13. button[name]["xmax"] = xmax
  14. button[name]["ymax"] = ymax
  15. end
  16.  
  17. function funcName() --Debug tool
  18. print("You clicked buttonText")
  19. end
  20.  
  21. function fill(text, color, bData)
  22. mon.setBackgroundColor(color)
  23. local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  24. local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  25. for j = bData["ymin"], bData["ymax"] do
  26. mon.setCursorPos(bData["xmin"], j)
  27. if j == yspot then
  28. for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  29. if k == xspot then
  30. mon.write(text)
  31. else
  32. mon.write(" ")
  33. end
  34. end
  35. else
  36. for i = bData["xmin"], bData["xmax"] do
  37. mon.write(" ")
  38. end
  39. end
  40. end
  41. mon.setBackgroundColor(colors.black)
  42. end
  43.  
  44. function screen()
  45. local currColor
  46. for name,data in pairs(button) do
  47. local on = data["active"]
  48. if on == true then currColor = colors.lime else currColor = colors.red end
  49. fill(name, currColor, data)
  50. end
  51. end
  52.  
  53. function toggleButton(name)
  54. button[name]["active"] = not button[name]["active"]
  55. screen()
  56. end
  57.  
  58. function flash(name)
  59. toggleButton(name)
  60. screen()
  61. sleep(0.15)
  62. toggleButton(name)
  63. screen()
  64. end
  65.  
  66. function checkxy(x, y)
  67. for name, data in pairs(button) do
  68. if y>=data["ymin"] and y <= data["ymax"] then
  69. if x>=data["xmin"] and x<= data["xmax"] then
  70. data["func"]()
  71. return true
  72. --data["active"] = not data["active"]
  73. --print(name)
  74. end
  75. end
  76. end
  77. return false
  78. end
  79.  
  80. function heading(text)
  81. w, h = mon.getSize()
  82. mon.setCursorPos((w-string.len(text))/2+1, 1)
  83. mon.write(text)
  84. end
  85.  
  86. function label(w, h, text)
  87. mon.setCursorPos(w, h)
  88. mon.write(text)
  89. end
Advertisement
Add Comment
Please, Sign In to add comment