mistamadd001

button api_edit

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