Advertisement
PaymentOption

GUI with tables

Aug 1st, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.30 KB | None | 0 0
  1. -- Gui using tables by PaymentOption --
  2. nSelect = 1
  3.  
  4. sMenuState = "MainMenu" -- This variable helps keep track of what menu you're on.
  5. tMainMenu = { [1] = { label = "Option1", associatedFunction = Option1Func }, -- Format: { label = string, associatedFunction = function }
  6.               [2] = { label = "Option2", associatedFunction = Option2Func }}; -- The label is to be displayed, and the
  7.                                                                                 -- Associated Function is what is executed upon pushing the button.
  8. -- Tab markers in notepad++, you don't understand...
  9. function clear() term.clear(); term.setCursorPos( 1, 1 ) end
  10.  
  11. function printScreen( tMenu )
  12.     for i=1, #tMenu do
  13.         if nSelect == i then print( "[ " .. tMenu[i].label .. " ]" )
  14.         else                 print( "  " .. tMenu[i].label .. "  " ) end
  15.     end
  16. end
  17.  
  18. function Option1Func()
  19.     term.setCursorPos( 1, 1 ); term.write( "OPTION 1 PRESSED" ); sleep( 2 )
  20. end
  21.  
  22. function Option2Func()
  23.     term.setCursorPos( 1, 2 ); term.write( "OPTION 2 PRESSED" ); sleep( 2 )
  24. end
  25.  
  26.  
  27. while true do
  28.     clear()
  29.     printScreen( tMainMenu )
  30.    
  31.     event, key = os.pullEvent( "key" )
  32.     if sMenuState == "MainMenu" then
  33.         if key == 200 and nSelect > 1 then nSelect = nSelect-1
  34.         elseif key == 208 and nSelect < #tMainMenu then nSelect = nSelect+1
  35.         elseif key == 28 then tMainMenu[nSelect].associatedFunction()
  36.         end
  37.     end
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement