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