Advertisement
zeplintwo

button dw20RTB

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