Advertisement
Zantag

Direwolf20 Button API

Sep 2nd, 2013
1,289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. local mon = peripheral.wrap("right")
  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. function reset()
  17. button = {}
  18. end
  19.  
  20. function funcName()
  21. print("You clicked buttonText")
  22. end
  23.  
  24. function buttonRename(name, newName)
  25. button[name] = newName
  26.  
  27. end
  28.  
  29. function fillTable()
  30. setTable("ButtonText", funcName, 5, 25, 4, 8)
  31. end
  32.  
  33. function fill(text, color, bData)
  34. mon.setBackgroundColor(color)
  35. local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  36. local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  37. for j = bData["ymin"], bData["ymax"] do
  38. mon.setCursorPos(bData["xmin"], j)
  39. if j == yspot then
  40. for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  41. if k == xspot then
  42. mon.write(text)
  43. else
  44. mon.write(" ")
  45. end
  46. end
  47. else
  48. for i = bData["xmin"], bData["xmax"] do
  49. mon.write(" ")
  50. end
  51. end
  52. end
  53. mon.setBackgroundColor(colors.black)
  54. end
  55.  
  56. function screen()
  57. local currColor
  58. for name,data in pairs(button) do
  59. local on = data["active"]
  60. if on == true then currColor = colors.lime else currColor = colors.red end
  61. fill(name, currColor, data)
  62. end
  63. end
  64.  
  65. function toggleButton(name)
  66. button[name]["active"] = not button[name]["active"]
  67. screen()
  68. end
  69.  
  70. function flash(name)
  71. toggleButton(name)
  72. screen()
  73. sleep(0.15)
  74. toggleButton(name)
  75. screen()
  76. end
  77.  
  78. function checkxy(x, y)
  79. for name, data in pairs(button) do
  80. if y>=data["ymin"] and y <= data["ymax"] then
  81. if x>=data["xmin"] and x<= data["xmax"] then
  82. data["func"]()
  83. return true
  84. --data["active"] = not data["active"]
  85. --print(name)
  86. end
  87. end
  88. end
  89. return false
  90. end
  91.  
  92. function heading(text)
  93. w, h = mon.getSize()
  94. mon.setCursorPos((w-string.len(text))/2+1, 1)
  95. mon.write(text)
  96. end
  97.  
  98. function label(w, h, text)
  99. mon.setCursorPos(w, h)
  100. mon.write(text)
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement