Advertisement
Miticzky

Egyszerű menü alap

Mar 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.54 KB | None | 0 0
  1. --[[    Segítsé
  2. http://youtu.be/AU48NSPl4rM   X
  3.  
  4. ]]--
  5.  
  6.  
  7.                 --[[    Local Variables         ]]--
  8. local termWidth, termHeight = term.getSize()
  9. local selectedItem = 1
  10. local runing = true
  11.  
  12.  
  13.  
  14.                 --[[    Menu Metods             ]]--
  15. function Choice1( ... )
  16.         print("Hello World")
  17.         clear()
  18.         read("")
  19. end
  20.  
  21. function Choice2( ... )
  22.         -- second menu function
  23. end
  24.  
  25. function readme( ... )
  26.         clear()
  27.         print([[If U like this menu system
  28. please subscribe
  29. my YT chanel:
  30. youtube.com/c/DelfaverMatteo_Games
  31.  
  32. Ha tetszett a program kerlek
  33. iratkozz fel a csatornamra!
  34. YT: youtube.com/c/DelfaverMatteo_Games
  35.  
  36. Thank U, Koszi ;)
  37.  
  38. Press enter to continue
  39. ]])
  40.         read("")
  41. end
  42.  
  43. function exit( ... )    --exit menu function
  44.         runing = false
  45. end
  46.  
  47.  
  48.  
  49.                 --[[    Men Definitions         ]]--
  50. --[[
  51. EN
  52. Menus, if U need more menu options, just continue the lines, like the basic lines.
  53. sample: [4] = { text = "Manu name here", handler = "function name here (Menu Metods)"},
  54. HUN
  55. Menuk, ha tobb menupontot szeretnel, csak folytasd a sorokat, ugy mint a tobbi sor van.
  56. pelda: [4] = { text = "Menu elnevezese", handler = "function neve (Menu Metods- resz)"},
  57. ]]
  58. local mainMenu = {
  59.         [1] = { text = "Choice1", handler = Choice1},
  60.         [2] = { text = "Choice2", handler = Choice2},
  61.         [3] = { text = "ReadME", handler = readme},
  62.         [4] = { text = "Exit", handler = exit},
  63.  
  64. }
  65. --[[
  66. EN
  67. if U change menu position, just chane the two number ( 10 and 5) like this: term.setCursorPos(1, (i+0))
  68. this position is the 1, 1. Monitor left up side.
  69. HUN
  70. Ha változtatni szeretned a menu poziciojat, csak 2 számot kell atirj ( 10 es 5) pelda: term.setCursorPos(1, (i+0))
  71. ez a pozicio a monitor bal felso sarka 1, 1
  72.  
  73. function printMenu( menu )
  74.         for i=1,#menu do
  75.                 if i == selectedItem then
  76.                         term.setCursorPos(1, i)
  77.                         print(">> " .. menu[i].text .. " <<")
  78.                 else
  79.                         term.setCursorPos(1, i)
  80.                         print("   " .. menu[i].text)
  81.                 end
  82.         end
  83. end
  84. ]]
  85. function printMenu( menu )
  86.         for i=1,#menu do
  87.                 if i == selectedItem then
  88.                         term.setCursorPos(10, (i+5))
  89.                         print(">> " .. menu[i].text .. " <<")
  90.                 else
  91.                         term.setCursorPos(10, (i+5))
  92.                         print("   " .. menu[i].text)
  93.                 end
  94.         end
  95. end
  96.  
  97.  
  98.  
  99.                 --[[    Handler Method          ]]--
  100. function onKeyPressed( key, menu )
  101.         if key == keys.enter then
  102.                 onItemSelected(menu)
  103.         elseif key == keys.up then
  104.                 if selectedItem > 1 then selectedItem = selectedItem - 1 end
  105.         elseif key == keys.down then
  106.                 if selectedItem < #menu then selectedItem = selectedItem +1 end
  107.         end
  108. end
  109.  
  110. function onItemSelected( menu )
  111.         menu[selectedItem].handler()
  112. end
  113.  
  114. function clear( ... )
  115.         term.clear()
  116.         term.setCursorPos(18,19)
  117.         print("The menu is created by Matteo 2015")
  118.         term.setCursorPos(1,1)
  119. end
  120.  
  121.  
  122.  
  123.                 --[[    Main function   ]]--
  124. function main( ... )
  125.         while runing do
  126.                 clear()
  127.                 term.setCursorPos(10, 4)
  128.                 printMenu(mainMenu)
  129.                 event, key = os.pullEvent("key")
  130.                 onKeyPressed(key, mainMenu)
  131.         end
  132. end
  133.  
  134. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement