Advertisement
koki2000

Enchanted menu

Oct 26th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | None | 0 0
  1. local width, height = term.getSize()
  2. print("width: ", width, "\n", "height: ", height)
  3. sleep(1)
  4. local selectedMenu = 1
  5. local selectedItem = 1
  6. local running = true
  7.  
  8. function clear()
  9. term.clear()
  10. term.setCursorPos(1,1)
  11. end
  12.  
  13. function choice1()
  14. clear()
  15. print("valasztott menu: 1")
  16. print("key: ", key)
  17. print("menu", menu)
  18. print("selectedItem: ", selectedItem)
  19. event, key = os.pullEvent("key")
  20. end
  21.  
  22. function choice15()
  23. selectedItem = 1
  24. selectedMenu = 2
  25. end
  26.  
  27. function choice22()
  28. selectedItem = 1
  29. selectedMenu = 1
  30. end
  31.  
  32. function Exit()
  33.  running = false
  34. end
  35.  
  36. local mainMenu1 = {
  37. [1] = { text = "menu1", handler = choice1 },
  38. [2] = { text = "menu2", handler = choice2 },
  39. [3] = { text = "menu3", handler = choice3 },
  40. [4] = { text = "menu4", handler = choice4 },
  41. [5] = { text = "menu5", handler = choice5 },
  42. [6] = { text = "menu6", handler = choice6 },
  43. [7] = { text = "menu7", handler = choice7 },
  44. [8] = { text = "menu8", handler = choice8 },
  45. [9] = { text = "menu9", handler = choice9 },
  46. [10] = { text = "menu10", handler = choice10 },
  47. [11] = { text = "menu11", handler = choice11 },
  48. [12] = { text = "menu12", handler = choice12 },
  49. [13] = { text = "menu13", handler = choice13 },
  50. [14] = { text = "menu14", handler = choice14 },
  51. [15] = { text = "menu15", handler = choice14 },
  52. [16] = { text = "menu16", handler = choice14 },
  53. [17] = { text = "menu17", handler = choice14 },
  54. [18] = { text = "menu18", handler = choice14 },
  55. [19] = { text = "tovab2", handler = choice15 },
  56. [20] = { text = "Exit20", handler = Exit }
  57. }
  58.  
  59. local mainMenu2 = {
  60. [1] = { text = "menu1", handler = choice21 },
  61. [2] = { text = "tovabb1", handler = choice22 },
  62. [3] = { text = "Exit20", handler = Exit }
  63. }
  64.  
  65. function printMenu( menu )
  66.   for i=1,#menu do
  67.     if i < height then
  68.       if i == selectedItem then
  69.         term.setCursorPos(1, (i))
  70.         print(">> "..menu[i].text .. " <<")
  71.       else
  72.         term.setCursorPos(1, (i))
  73.         print("   "..menu[i].text)
  74.       end
  75.     elseif i > height - 1 then
  76.       if i == selectedItem then
  77.         term.setCursorPos(10, (i-18))
  78.         print(">> "..menu[i].text .. " <<")
  79.       else
  80.         term.setCursorPos(10, (i-18))
  81.         print("   "..menu[i].text)
  82.       end
  83.     end
  84.   end
  85. end
  86.  
  87. function onKeyPressed( key, menu )
  88. term.setCursorPos(35,18)
  89. print("keypressed: ", key)
  90. sleep(.01)
  91.  if key == 28 then
  92.   onItemSelected(menu)
  93.  elseif key == 200 then
  94.   if selectedItem > 1 then
  95.    selectedItem = selectedItem - 1
  96.   end
  97.  elseif key == 208 then
  98.   if selectedItem < #menu then
  99.    selectedItem = selectedItem + 1
  100.   end
  101.  elseif key == 203 then
  102.   if selectedItem > 18 then
  103.    selectedItem = selectedItem - 18
  104.   end
  105.  elseif key == 205 then
  106.   vissza = selectedItem
  107.   if selectedItem < #menu then
  108.    selectedItem = selectedItem + 18
  109.     if selectedItem > #menu then
  110.       selectedItem = vissza
  111.     end
  112.   end
  113.  end
  114. end
  115.  
  116. function onItemSelected( menu )
  117.  menu[selectedItem].handler()
  118. end
  119.  
  120. function main()
  121.  
  122. while running do
  123.  if selectedMenu == 1 then
  124.   term.clear()
  125.   term.setCursorPos(1,1)
  126.   printMenu(mainMenu1)
  127.   event, key = os.pullEvent("key")
  128.   onKeyPressed(key,mainMenu1)
  129.   elseif selectedMenu == 2 then
  130.   term.clear()
  131.   term.setCursorPos(1,1)
  132.   printMenu(mainMenu2)
  133.   event, key = os.pullEvent("key")
  134.   onKeyPressed(key,mainMenu2)
  135.   end
  136.  end
  137. end
  138.  
  139. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement