Advertisement
asteroidsteam

Menu

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