Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  1. local menu_options = { --This is our menu table. It contains basic data about the menu
  2.   [1] = {text="Zombie", color=colors.blue},
  3.   [2] = {text="Sheep", color=colors.orange},
  4.   [3] = {text="Blaze", 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.  
  38. local selector = 1 --Our selector
  39. while true do --The main loop. I would generally put this inside of a function for a program.
  40.   menuDraw(selector) --Draw the menu first
  41.   local e = {os.pullEvent()} --Pull an event and put the returned values into a table
  42.   if e[1] == "key" then --If it's a key...
  43.     if e[2] == keys.down then -- ... and it's the down arrow
  44.       selector = selector < #menu_options and selector+1 or 1 --Increment the selector if the selector < #menu_options. Otherwise reset it to 1
  45.     elseif e[2] == keys.up then
  46.       selector = selector > 1 and selector-1 or #menu_options --Decrement the selector if the selector > 1. Otherwise, reset it to #menu_options
  47.     elseif e[2] == keys.enter then
  48.       break --Break out of the loop
  49.     end
  50.   elseif e[1] == "mouse_click" then
  51.     local value = checkClick(e[3], e[4]) --Check the mouse click
  52.     if value then --If checkClick returns a value and not false
  53.       selector = value --Set the selector to that value and break out of the loop
  54.       break
  55.     end
  56.   end
  57. end
  58.  
  59. term.clear()
  60. term.setCursorPos(1,1)
  61. term.write("Selected: "..selector)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement