Advertisement
Guest User

buttontest.lua

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