Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- w, h = term.getSize()
- run = true
- selitem = 1
- function choice1()
- print("choice1") sleep(1)
- end
- function choice2()
- print("choice2") sleep(1)
- end
- function exiting()
- print("exiting")
- run = false
- end
- mainmenu = {
- [1] = {text = "choice1", handler = choice1},
- [2] = {text = "choice2", handler = choice2},
- [3] = {text = "exit", handler = exiting}
- }
- function printmenu(menu)
- term.clear()
- for i = 1, #menu do
- local hossz = string.len(menu[i].text)
- if i == selitem then
- term.setCursorPos(w/2-(hossz/2), i)
- term.setBackgroundColor(colors.red)
- print(">"..menu[i].text.."<")
- else
- term.setCursorPos(w/2-(hossz/2), i)
- term.setBackgroundColor(colors.brown)
- print(" "..menu[i].text.." ")
- end
- term.setBackgroundColor(colors.black)
- end
- end
- function onkeypressed(key, menu)
- if key == 28 then
- menu[selitem].handler()
- elseif key == 200 then
- if selitem > 1 then
- selitem = selitem - 1
- end
- elseif key == 208 then
- if selitem < #menu then
- selitem = selitem + 1
- end
- end
- end
- function click(i, y)
- if i == y then
- return true
- end
- return false
- end
- while run do
- printmenu(mainmenu)
- local event, par1, par2, par3 = os.pullEventRaw()
- if event == "key" then
- printmenu(mainmenu)
- onkeypressed(par1, mainmenu)
- elseif event == "mouse_click" then
- for i = 1, #mainmenu do
- if click(i, par3) then
- if i ~= selitem then
- selitem = i
- else
- mainmenu[selitem].handler()
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement