Guest User

aOSv1.0.file

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