Advertisement
sanovskiy

Menu test

Aug 8th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.01 KB | None | 0 0
  1. local tab = {
  2.     "Hi",
  3.     "Goodbye",
  4.     "Hello",
  5.     "Exit",
  6.     'lololololololol',
  7.     'last',
  8. }
  9. local sX, sY = term.getSize()
  10.  
  11. local function getLongest( _table )
  12.     local longest = 0
  13.     for index, text in pairs( _table ) do
  14.         longest = #text > longest and #text or longest
  15.     end
  16.     return longest + 1
  17. end
  18.  
  19. local function menu( _table, startX, startY )
  20.     local function printMenu( _table, long, sel, yPos )
  21.         --for index, text in pairs( _table ) do
  22.         --  term.setCursorPos( startX, startY + index - 1 )
  23.         --  write( ( index == sel and '[ ' or '  ' ) .. text .. string.rep(' ', long - #text) .. ( index == sel and ']' or ' ' ) )
  24.         --end
  25.         for i = 1, sY do
  26.             if _table[i + yPos - 1] then
  27.                 local tmp = _table[i + yPos - 1]
  28.                 term.setCursorPos( startX, startY + i - 1 )
  29.                 write( ( i + yPos - 1 == sel and '[ ' or '  ' ) .. tmp .. string.rep(' ', long - #tmp) .. ( i + yPos - 1 == sel and ']' or ' ' ) )
  30.             else break end
  31.         end
  32.     end
  33.    
  34.     local longest = getLongest( _table )
  35.     local sel = 1
  36.     local yPos = 1
  37.    
  38.     while true do
  39.         printMenu( _table, longest, sel, yPos )
  40.         local e, key = os.pullEvent()
  41.         if e == "key" or e == "mouse_scroll" then
  42.             if key == keys.up or key == -1 then
  43.                 -- up
  44.                 if yPos > 1 then
  45.                     yPos = yPos - 1
  46.                 end
  47.                 if sel > 1 then
  48.                     sel = sel - 1
  49.                 end
  50.             elseif key == keys.down or key == 1 then
  51.                 -- down
  52.                 if sel < #_table then
  53.                     sel = sel + 1
  54.                 end
  55.                 if yPos < #_table - 18 and sel >= 20 then
  56.                     yPos = yPos + 1
  57.                 end
  58.             elseif key == keys.enter then
  59.                 return _table[sel], sel
  60.             end
  61.         end
  62.     end
  63. end
  64.  
  65. term.clear()
  66.  
  67. while true do
  68.     local sOption, sNumb = menu( tab, (sX-getLongest(tab))/2, 1 )
  69.    
  70.     term.clear() term.setCursorPos( 1, 1 )
  71.  
  72.     if sOption == "Hi" then
  73.         print( "Hello there! :D" )
  74.     elseif sOption  == "Goodbye" then
  75.         print( "Why are you going? :(" )
  76.     elseif sOption == "Hello" then
  77.         print( "Oh hello again :>" )
  78.     elseif sOption == "Exit" then
  79.         print( "Cheers :<" )
  80.         break
  81.     end
  82.     sleep(2)
  83.     term.clear() term.setCursorPos( 1, 1 )
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement