Advertisement
CubePhoenix

SecuritySystem

Mar 1st, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.59 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. Doorside = "right"
  4. openTime = 3
  5. password = "example"
  6. passRight = false
  7. selector = 1
  8. selector2 = 1
  9. function menu(menu)
  10.  local menu_options = { --This is our menu table. It contains basic data about the menu
  11.   [1] = {text="Open the Door", color=colors.green},
  12.   [2] = {text="Change Password", color=colors.blue},
  13.   [3] = {text="Options", color=colors.red}
  14.  }
  15.  local termX, termY = term.getSize() --The x/y size of the terminal
  16.  local function menuDraw(selected) --Our main draw function
  17.    local yPos = termY/2 - #menu_options/2 --The initial y position
  18.    for index, data in pairs(menu_options) do
  19.      menu_options[index].bounds = { --Create a new table in each option with the boundary data
  20.        x1 = termX/2 - (#data.text+4)/2;
  21.        x2 = termX/2 + (#data.text+4)/2;
  22.        y = yPos;
  23.      }
  24.      term.setTextColor(data.color)
  25.      term.setCursorPos(data.bounds.x1, data.bounds.y)
  26.  
  27.      local text =
  28.        index==selected and "[ "..data.text.." ]" or
  29.        "  "..data.text.."  " --Essentially an if statement, but in a contracted form
  30.      term.write(text)
  31.      yPos = yPos+1 --Increment the initial y pos so we can move on the next line
  32.    end
  33.  end
  34.  
  35.  local function checkClick(x,y) --Check the mouse click to see if there's a menu option
  36.    for index, data in pairs(menu_options) do
  37.      if x>= data.bounds.x1 and x<= data.bounds.x2 and y==data.bounds.y then
  38.        return index --Returns the index of the clicked option
  39.      end
  40.    end
  41.    return false --If it went through the entire for loop without success, return false
  42.  end
  43.  
  44.  term.setBackgroundColor(colors.white)
  45.  term.clear()
  46.  
  47.  selector = 1 --Our selector
  48.  while true do --The main loop. I would generally put this inside of a function for a program.
  49.    menuDraw(selector) --Draw the menu first
  50.    local e = {os.pullEvent()} --Pull an event and put the returned values into a table
  51.    if e[1] == "key" then --If it's a key...
  52.      if e[2] == keys.down then -- ... and it's the down arrow
  53.        selector = selector < #menu_options and selector+1 or 1 --Increment the selector if the selector < #menu_options. Otherwise reset it to 1
  54.      elseif e[2] == keys.up then
  55.        selector = selector > 1 and selector-1 or #menu_options --Decrement the selector if the selector > 1. Otherwise, reset it to #menu_options
  56.      elseif e[2] == keys.enter then
  57.        break --Break out of the loop
  58.      end
  59.    elseif e[1] == "mouse_click" then
  60.      local value = checkClick(e[3], e[4]) --Check the mouse click
  61.      if value then --If checkClick returns a value and not false
  62.        selector = value --Set the selector to that value and break out of the loop
  63.        break
  64.      end
  65.    end
  66.  end
  67.  
  68.  term.clear()
  69.  term.setCursorPos(1,1)
  70. end
  71.  
  72. function testPassword()
  73.  print("password:")
  74.  local input = read("*")
  75.  if input == password then
  76.   print("Correct password!")
  77.   sleep(2)
  78.   passRight = true
  79.   menu()
  80.  else
  81.   print("Incorrect password!")
  82.   sleep(2)
  83.   passRight = false
  84.  end
  85. end
  86. funcion menu(options)
  87.  local menu_options = { --This is our menu table. It contains basic data about the menu
  88.   [1] = {text="Change door side", color=colors.blue},
  89.   [2] = {text="Change open time", color=colors.green},
  90.   [3] = {text="Reset settings", color=colors.yellow}
  91.  }
  92.  local termX, termY = term.getSize() --The x/y size of the terminal
  93.  local function menuDraw(selected) --Our main draw function
  94.    local yPos = termY/2 - #menu_options/2 --The initial y position
  95.    for index, data in pairs(menu_options) do
  96.      menu_options[index].bounds = { --Create a new table in each option with the boundary data
  97.        x1 = termX/2 - (#data.text+4)/2;
  98.        x2 = termX/2 + (#data.text+4)/2;
  99.        y = yPos;
  100.      }
  101.      term.setTextColor(data.color)
  102.      term.setCursorPos(data.bounds.x1, data.bounds.y)
  103.  
  104.      local text =
  105.        index==selected and "[ "..data.text.." ]" or
  106.        "  "..data.text.."  " --Essentially an if statement, but in a contracted form
  107.      term.write(text)
  108.      yPos = yPos+1 --Increment the initial y pos so we can move on the next line
  109.    end
  110.  end
  111.  
  112.  local function checkClick(x,y) --Check the mouse click to see if there's a menu option
  113.    for index, data in pairs(menu_options) do
  114.      if x>= data.bounds.x1 and x<= data.bounds.x2 and y==data.bounds.y then
  115.        return index --Returns the index of the clicked option
  116.      end
  117.    end
  118.    return false --If it went through the entire for loop without success, return false
  119.  end
  120.  
  121.  term.setBackgroundColor(colors.white)
  122.  term.clear()
  123.  
  124.  selector2 = 1 --Our selector
  125.   while true do --The main loop. I would generally put this inside of a function for a program.
  126.     menuDraw(selector) --Draw the menu first
  127.    local e = {os.pullEvent()} --Pull an event and put the returned values into a table
  128.    if e[1] == "key" then --If it's a key...
  129.     if e[2] == keys.down then -- ... and it's the down arrow
  130.        selector2 = selector2 < #menu_options and selector+1 or 1 --Increment the selector if the selector < #menu_options. Otherwise reset it to 1
  131.     elseif e[2] == keys.up then
  132.       selector2 = selector2 > 1 and selector2-1 or #menu_options --Decrement the selector if the selector > 1. Otherwise, reset it to #menu_options
  133.     elseif e[2] == keys.enter then
  134.       break --Break out of the loop
  135.     end
  136.    elseif e[1] == "mouse_click" then
  137.      local value = checkClick(e[3], e[4]) --Check the mouse click
  138.      if value then --If checkClick returns a value and not false
  139.        selector2 = value --Set the selector to that value and break out of the loop
  140.        break
  141.      end
  142.    end
  143.  end
  144. end
  145.  
  146.  term.clear()
  147.  term.setCursorPos(1,1)
  148.  
  149.  
  150. while passRight == false do
  151.  testPassword()
  152. end
  153. while true do
  154.  if selector == 1 then
  155.   sleep(1)
  156.   term.setBackgroundColor(colors.black)
  157.   term.clear()
  158.   print("Opening door...")
  159.   sleep(2)
  160.   term.clear()
  161.   rs.setOutput(Doorside,true)
  162.   sleep(openTime)
  163.   rs.setOutput(Doorside,false)
  164.   testPassword()
  165.  elseif selector == 2 then
  166.   sleep(1)
  167.   local input = read()
  168.   print("Please enter new password:")
  169.   input = password
  170.   print("Changing password...")
  171.   sleep(2)
  172.   menu()
  173.  elseif selector == 3 then
  174.   menu(options)
  175.  elseif selector2 == 1 then
  176.   print("New door side:")
  177.   local input = read()
  178.   input = Doorside
  179.   print("Changing door side...")
  180.   sleep(2)
  181.   menu(menu)
  182.  elseif selector2 == 2 then
  183.   print("New open time(in sec):")
  184.   local input = read()
  185.   input = openTime
  186.   print("Changing open time...")
  187.   sleep(2)
  188.   menu(menu)
  189.  elseif selector2 == 3 then
  190.   print("Resetting settings...")
  191.   sleep(2)
  192.   Doorside = "right"
  193.   password = "example"
  194.   openTime = 3  
  195.  end
  196. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement