sidekick_

Simple Menu

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