wasliebob

ButtonApi

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