Advertisement
Crispkat

buttontest

Jul 9th, 2015
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. function boot_logo()
  2. term.clear()
  3. local boot = {
  4.  
  5.  
  6. }
  7. for i = 1, #boot do
  8. while false do
  9. print(" ")
  10. break
  11. end
  12. end
  13. print("Loading DevilLabs, please wait...")
  14. term.setCursorPos(1,19)
  15. term.setTextColour(colours.lime)
  16. textutils.slowPrint("LOADING...")
  17. sleep(1)
  18. end
  19. boot_logo()
  20.  
  21.  
  22. local buttons = {
  23. [1] = {
  24. x = 10,
  25. y = 10,
  26. term.setTextScale(5)
  27. on_state = "Door Closed",
  28. off_state = "Door Open",
  29. current_state = true, -- Initially the button is off
  30. onClick = function(state) -- Open of close the door
  31. if state then -- Currently on, closing
  32. --rednet.open("back")
  33. term.setTextScale(1)
  34. term.setCursorPos(1,18)
  35. term.setTextColour(colours.lime)
  36. textutils.slowPrint("Pocessing...")
  37. term.setCursorPos(1,19)
  38. term.setTextColour(colours.blue)
  39. textutils.slowPrint("Complete!")
  40. --rednet.broadcast("Closed")
  41. --rednet.close("back")
  42.  
  43. sleep(1)
  44. os.reboot()
  45.  
  46. -- The current state is off: Open The Door
  47. else
  48. --rednet.open(back")
  49. term.settextScale(1)
  50. term.setCursorPos(1,18)
  51. term.setTextColour(colours.lime)
  52. textutils.slowPrint("Pocessing...")
  53. term.setCursorPos(1,19)
  54. term.setTextColour(colours.blue)
  55. textutils.slowPrint("Complete!")
  56. --rednet.broadcast("Open")
  57. --rednet.close("back")
  58. sleep(1)
  59. os.reboot()
  60. -- The current state is off: Close The Door
  61. end
  62. end
  63. }
  64. }
  65.  
  66. local function drawButton(button)
  67. term.setCursorPos(button.x, button.y)
  68. if button.current_state then -- If it’s on, our button is green
  69. term.setBackgroundColor(colors.green)
  70. else -- Otherwise it’s red
  71. term.setBackgroundColor(colors.red)
  72. end
  73.  
  74. term.setTextColor(colors.white)
  75.  
  76. local button_text -- We’ll store what text we want to write here
  77. if button.current_state then
  78. button_text = button.on_state
  79. --rednet.broadcast("Close")
  80. else
  81. button_text = button.off_state
  82. --rednet.broadcast("Open")
  83. end
  84.  
  85. local button_length = #button.on_state -- How long is our button?
  86. if #button.off_state > #button.on_state then -- We want the longest it ever gets
  87. button_length = #button.off_state
  88. end
  89.  
  90. term.write(string.rep(" ", 2 + button_length)) -- Give it a little space on each side
  91. term.setCursorPos(button.x + 1 + button_length / 2 -
  92. #button_text / 2, button.y) -- Nothing too complicated here: just centering the text inside of the button
  93. term.write(button_text) -- Draw our text
  94. end
  95.  
  96. local function checkClick(x, y)
  97. for index, button in pairs(buttons) do
  98. local button_length = #button.on_state + 4
  99. if #button.off_state > #button.on_state then
  100. button_length = #button.off_state + 4
  101. end
  102.  
  103. if button.y == y and
  104. x >= button.x and
  105. x <= button.x + button_length then
  106. -- Yep, we clicked this button
  107. button.onClick(button.current_state)
  108. button.current_state = not button.current_state
  109. end
  110. end
  111. end
  112.  
  113. local width, height = term.setSize()
  114.  
  115. local buttons = {
  116. [1] = {
  117. x = width / 2 - 10 / 2,
  118. ... -- The rest stays the same
  119. }, [2] = { ... }
  120. }
  121.  
  122. while true do
  123. term.setBackgroundColor(colors.black)
  124. term.clear()
  125. for index, button in pairs(buttons) do drawButton(button) end
  126. local e = {os.pullEvent("mouse_click")} -- Change to "monitor_touch" if you’re working with a monitor
  127. checkClick(e[3], e[4])
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement