-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu
67 lines (58 loc) · 1.14 KB
/
menu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
local termWidth, termHeight = term.getSize()
local selectedItem = 1
local onMainMenu = true
function Choice1()
term.clear()
term.setCursorPos(1,1)
print("CHOICE 1")
sleep(1)
end
function Choice2()
term.clear()
term.setCursorPos(1,1)
print("CHOICE 2")
sleep(1)
end
function Exit()
onMainMenu = false
end
mainMenu = {
[1] = { text = "Choice 1", handler = Choice1 },
[2] = { text = "Choice 2", handler = Choice2 },
[3] = { text = "Exit", handler = Exit }
}
function printMenu( menu )
for i=1,#menu do
if i == selectedItem then
print("->> "..menu[i].text)
else
print(" "..menu[i].text)
end
end
end
function onKeyPressed( key, menu )
if key == keys.enter then
onItemSelected(menu)
elseif key == keys.up then
if selectedItem > 1 then
selectedItem = selectedItem - 1
end
elseif key == keys.down then
if selectedItem < #menu then
selectedItem = selectedItem + 1
end
end
end
function onItemSelected( menu )
menu[selectedItem].handler()
end
function main()
while onMainMenu do
term.clear()
term.setCursorPos(1,1)
printMenu(mainMenu)
event, key = os.pullEvent("key")
onKeyPressed(key,mainMenu)
end
end
main()