Advertisement
Guest User

.bootMenu

a guest
Jan 16th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. local menu_options = { --This is our menu table. It contains basic data about the menu
  2.   [1] = {text="Boot from right Disk", color=colors.white},
  3.   [2] = {text="Boot from top Disk", color=colors.white},
  4.   [3] = {text="Get OS from Pastebin", color=colors.white},
  5.   [4] = {text="Reboot", color=colors.white}
  6. }
  7.  
  8. local termX, termY = term.getSize() --The x/y size of the terminal
  9. local function menuDraw(selected) --Our main draw function
  10.   local yPos = termY - #menu_options --The initial y position
  11.   for index, data in pairs(menu_options) do
  12.     menu_options[index].bounds = { --Create a new table in each option with the boundary data
  13.       x1 = 1;
  14.       x2 = 1;
  15.       y = yPos - 13;
  16.     }
  17.     term.setTextColor(data.color)
  18.     term.setCursorPos(data.bounds.x1, data.bounds.y)
  19.  
  20.     local text =
  21.       index==selected and " ["..data.text.."]" or
  22.       "  "..data.text.."  " --Essentially an if statement, but in a contracted formattedTime
  23.     term.write(text)
  24.     yPos = yPos+1 --Increment the initial y pos so we can move on the next line
  25.   end
  26. end
  27.  
  28. local function checkClick(x,y) --Check the mouse click to see if there's a menu option
  29.   for index, data in pairs(menu_options) do
  30.     if x>= data.bounds.x1 and x<= data.bounds.x2 and y==data.bounds.y then
  31.       return index --Returns the index of the clicked option
  32.     end
  33.   end
  34.   return false --If it went through the entire for loop without success, return false
  35. end
  36.  
  37. term.setBackgroundColor(colors.black)
  38. term.clear()
  39.  
  40. local selector = 1 --Our selector
  41. while true do --The main loop. I would generally put this inside of a function for a program.
  42.   menuDraw(selector) --Draw the menu first
  43.   local e = {os.pullEvent()} --Pull an event and put the returned values into a table
  44.   if e[1] == "key" then --If it's a key...
  45.     if e[2] == keys.down then -- ... and it's the down arrow
  46.       selector = selector < #menu_options and selector+1 or 1 --Increment the selector if the selector < #menu_options. Otherwise reset it to 1
  47.     elseif e[2] == keys.up then
  48.       selector = selector > 1 and selector-1 or #menu_options --Decrement the selector if the selector > 1. Otherwise, reset it to #menu_options
  49.     elseif e[2] == keys.enter then
  50.       break --Break out of the loop
  51.     end
  52.   elseif e[1] == "mouse_click" then
  53.     local value = checkClick(e[3], e[4]) --Check the mouse click
  54.     if value then --If checkClick returns a value and not false
  55.       selector = value --Set the selector to that value and break out of the loop
  56.       break
  57.     end
  58.   end
  59. end
  60.  
  61. term.clear()
  62. term.setCursorPos(1,1)
  63. term.write("Selected: "..selector)
  64.  
  65. if selector == 2 then
  66.   term.clear()
  67.   shell.run("disk/startup")
  68.   term.setCursorPos(1, 1)
  69. end
  70.  
  71. if selector == 4 then
  72.   term.clear()
  73.   term.setCursorPos(1, 1)
  74.   os.reboot()
  75. end
  76.  
  77. if selector == 3 then
  78.   term.clear()
  79.   term.setCursorPos(1, 1)
  80.   shell.run("disk/.DownloadOS")
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement