kk258966

buttin

Sep 21st, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. -- Edited by arshia111
  2. -- Thanks to GufNZ for helping me out
  3.  
  4. local mon = peripheral.wrap("top")
  5. mon.setTextScale(1)
  6. mon.setTextColor(colors.white)
  7. local button={}
  8. backgroundColor = colors.blue
  9. mon.setBackgroundColor(backgroundColor)
  10.  
  11. -- If you want the default
  12. -- "green when on, red when off, white text" button
  13. -- just set tcolor, bcolorOn and bcolorOff to nil
  14. function setTable(name, func, xmin, xmax, ymin, ymax, tcolor, bcolorOn, bcolorOff)
  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. button[name]["tcolor"] = tcolor
  23. button[name]["bcolorOn"] = bcolorOn
  24. button[name]["bcolorOff"] = bcolorOff
  25. end
  26.  
  27. function clearTable()
  28. button = {}
  29. mon.clear()
  30. end
  31.  
  32. function funcName()
  33. print("You clicked buttonText")
  34. end
  35.  
  36. function fillTable()
  37. setTable("ButtonText", funcName, 5, 25, 4, 8)
  38. end
  39.  
  40. function fill(text, color, bData)
  41. mon.setBackgroundColor(color)
  42. local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  43. local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  44. for j = bData["ymin"], bData["ymax"] do
  45. mon.setCursorPos(bData["xmin"], j)
  46. if j == yspot then
  47. for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  48. if k == xspot then
  49. mon.write(text)
  50. else
  51. mon.write(" ")
  52. end
  53. end
  54. else
  55. for i = bData["xmin"], bData["xmax"] do
  56. mon.write(" ")
  57. end
  58. end
  59. end
  60. mon.setBackgroundColor(backgroundColor)
  61. end
  62.  
  63. function screen()
  64. local currColor
  65. for name,data in pairs(button) do
  66. mon.setTextColor(data["tcolor"] or colors.white)
  67. local on = data["active"]
  68. if on == false then
  69. currColor = data["bcolorOff"] or colors.red
  70. elseif on == true then
  71. currColor = data["bcolorOn"] or colors.lime
  72. end
  73. fill(name, currColor, data)
  74. end
  75. end
  76.  
  77. function toggleButton(name)
  78. button[name]["active"] = not button[name]["active"]
  79. screen()
  80. end
  81.  
  82. function flashButton(name, time)
  83. toggleButton(name)
  84. sleep(time)
  85. toggleButton(name)
  86. end
  87.  
  88. function checkxy(x, y)
  89. for name, data in pairs(button) do
  90. if y>=data["ymin"] and y <= data["ymax"] then
  91. if x>=data["xmin"] and x<= data["xmax"] then
  92. data["func"]()
  93. return true
  94. --data["active"] = not data["active"]
  95. --print(name)
  96. end
  97. end
  98. end
  99. return false
  100. end
  101.  
  102. function centerText(text, y)
  103. w, h = mon.getSize()
  104. if string.len(text) > w then
  105. print("The text to be centered at line "..y.." is too long!")
  106. end
  107. if y > h then
  108. print("The text on line "..y.." is too low!")
  109. end
  110. mon.setCursorPos((w-string.len(text))/2+1, y)
  111. mon.write(text)
  112. end
  113.  
  114. function label(w, h, text)
  115. mon.setCursorPos(w, h)
  116. mon.write(text)
  117. end
Advertisement
Add Comment
Please, Sign In to add comment