Advertisement
Tacnuke

reactorbutton

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