View difference between Paste ID: cEVuuTmV and YsqzVY8d
SHOW: | | - or go back to the newest paste.
1
local menu_options = { --This is our menu table. It contains basic data about the menu
2-
  [1] = {text="Menu Option 1", color=colors.blue},
2+
  [1] = {text="Menu Option 1", color=colors.green},
3-
  [2] = {text="Menu Option 2", color=colors.orange},
3+
  [2] = {text="Menu Option 2", color=colors.yellow},
4-
  [3] = {text="Menu Option 3", color=colors.cyan}
4+
  [3] = {text="Menu Option 3", color=colors.red},
5
  [4] = {text="Menu Option 3", color=colors.black}
6
}
7
local termX, termY = term.getSize() --The x/y size of the terminal
8
local function menuDraw(selected) --Our main draw function
9
  local yPos = termY/2 - #menu_options/2 --The initial y position
10
  for index, data in pairs(menu_options) do
11
    menu_options[index].bounds = { --Create a new table in each option with the boundary data
12
      x1 = termX/2 - (#data.text+4)/2;
13
      x2 = termX/2 + (#data.text+4)/2;
14
      y = yPos;
15
    }
16
    term.setTextColor(data.color)
17
    term.setCursorPos(data.bounds.x1, data.bounds.y)
18
19
    local text =
20
      index==selected and "[ "..data.text.." ]" or
21
      "  "..data.text.."  " --Essentially an if statement, but in a contracted form
22
    term.write(text)
23
    yPos = yPos+1 --Increment the initial y pos so we can move on the next line
24
  end
25
end
26
27
local function checkClick(x,y) --Check the mouse click to see if there's a menu option
28
  for index, data in pairs(menu_options) do
29
    if x>= data.bounds.x1 and x<= data.bounds.x2 and y==data.bounds.y then
30
      return index --Returns the index of the clicked option
31
    end
32
  end
33
  return false --If it went through the entire for loop without success, return false
34
end
35
36
term.setBackgroundColor(colors.white)
37
term.clear()
38
39
local selector = 1 --Our selector
40
while true do --The main loop. I would generally put this inside of a function for a program.
41
  menuDraw(selector) --Draw the menu first
42
  local e = {os.pullEvent()} --Pull an event and put the returned values into a table
43
  if e[1] == "key" then --If it's a key...
44
    if e[2] == keys.down then -- ... and it's the down arrow
45
      selector = selector < #menu_options and selector+1 or 1 --Increment the selector if the selector < #menu_options. Otherwise reset it to 1
46
    elseif e[2] == keys.up then
47
      selector = selector > 1 and selector-1 or #menu_options --Decrement the selector if the selector > 1. Otherwise, reset it to #menu_options
48
    elseif e[2] == keys.enter then
49
      break --Break out of the loop
50
    end
51
  elseif e[1] == "mouse_click" then
52
    local value = checkClick(e[3], e[4]) --Check the mouse click
53
    if value then --If checkClick returns a value and not false
54
      selector = value --Set the selector to that value and break out of the loop
55
      break
56
    end
57
  end
58
end
59
60
term.clear()
61
term.setCursorPos(1,1)
62
term.write("Selected: "..selector)