PaymentOption

Basic Menu with Tables

Sep 13th, 2012
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.11 KB | None | 0 0
  1. -- Basic Menu written by PaymentOption --
  2. --[[
  3.     We'll be using tables to achieve this menu. Tables are much better for
  4. creating a menu because the methods we'll be writing to manipulate the menu
  5. won't require any modifications if we add more options to the menu. This might
  6. not make sense immediately, but hopefully you'll understand a little better
  7. once we write the code.
  8. ]]--
  9.  
  10. -- First, we'll start with declaring the variables we know we'll need.
  11. -- We'll be creating a 1 level menu with 3 selections.
  12.  
  13. local bRunning = true
  14. -- The above is the boolean value (true/false) that tells the loop at the bottom of
  15. -- this program to keep running the code inside it. If this variable was false, the
  16. -- loop would stop executing and our program would end.
  17.  
  18. local nScreenWidth, nScreenHeight = term.getSize()
  19. -- The above is the x and y dimensions of the screen we're currently working with.
  20. -- These numbers will help us with drawing our text to the right positions of the screen.
  21.  
  22. local nSelection = 1
  23. -- The above is our current selection. This variable will help us keep track of
  24. -- what option we've got currently selected in the menu.
  25.  
  26. local tMainMenu = {
  27. [1] = { sTitle = "First Option", fAssociatedFunction = FirstOption },
  28. [2] = { sTitle = "Second Option", fAssociatedFunction = SecondOption },
  29. [3] = { sTitle = "Exit", fAssociatedFunction = function() bRunning = false end }
  30. }
  31. -- The above is our main menu table.
  32. -- It consists of 3 sub tables keyed at 1, 2, and 3.
  33. -- Each contains two variables: sTitle and fAssociatedFunction.
  34. -- sTitle is the name of the option, or the name we'll print out when the program is run.
  35. -- fAssociatedFunction is the identifier, or name, of the function this option will be linked to.
  36. -- In other words, if we select this option it will run the function assigned to fAssociatedFunction.
  37.  
  38. -- Now that we've got our variables declared, we'll right our methods, or functions.
  39. -- I like to keep my methods orgainzed by purpose. If I have methods that draw stuff,
  40. -- they'll be under draw methods. If I have methods that do networking stuff, they'll
  41. -- be under networking methods.
  42.  
  43. -- Drawing Methods --
  44.  
  45. -- PrintCentered is a function that prints the text we give it to the exact center of the terminal.
  46. -- Its two parameters are the height, or y coordinate, on the screen we want to print to, and the
  47. -- text that we want to print there.
  48. function PrintCentered( nHeight, sString )
  49.     term.setCursorPos( nScreenWidth/2 - string.len( sString )/2, nHeight )
  50.     -- The above line positions the cursor on the center of the screen on the given height.
  51.     -- It accomplishes this, if you're interested, by taking the width of the screen and dividing it in 2,
  52.     -- so we're now at the center of the screen, then dividing the length of the text we want to print by 2
  53.     -- then subtracting that number from the screen width divided by 2 to get the exact center of the screen.
  54.     term.write( sString )
  55.     -- The above statement simply prints the string on the current position of the cursor.
  56. end
  57.  
  58. -- ClearScreen is a simple function that clears the screen of all text and reset
  59. -- the position of the cursor to the upper left hand corner of the screen.
  60. function ClearScreen()
  61.     term.clear()
  62.     -- The above statement clears the screen of all text.
  63.     term.setCursorPos( 1, 1 )
  64.     -- The above statement repositions the cursor to the coordinates 1,1 (the upper left hand corner).
  65. end
  66.  
  67. -- PrintMenu simply prints out all of the options in the menu passed
  68. -- in the center of the screen starting at the given y value.
  69. -- !WARNING!
  70. --  This method assumes that the menu table passed has the format we used above:
  71. --  tMenu[n] = { sTitle, fAssociatedFunction }
  72. function PrintMenu( tMenu, nStartHeight )
  73.     -- We'll loop through the table to make sure we print every option.
  74.     -- This is where tables come in handy: if we just use a bunch of if
  75.     -- statements then we'd have to add a new if every time we added an
  76.     -- option. This loop and table method will handle all of that for you!
  77.     for index = 1, #tMenu do
  78.         -- Start the iterator at 1 and we'll use it as our index.
  79.         -- Run this loop as many times as there are indexes in the tMenu table.
  80.        
  81.         -- If the iterator has reached the option that is currently selected in the table
  82.         -- we want to signal to the user somehow that it is selected. We'll do this here
  83.         -- by drawing brackets around the option, while leaving the other options without
  84.         -- brackets.
  85.         if index == nSelection then
  86.             PrintCentered( nStartHeight + ( index - 1 ), "[" .. tMenu[index].sTitle .. "]" )
  87.         else
  88.             PrintCentered( nStartHeight + ( index - 1 ), " " .. tMenu[index].sTitle .. " " )
  89.         end
  90.     end
  91. end
  92.  
  93. -- End Drawing Methods --
  94.  
  95. -- Key Handling Methods --
  96.  
  97. -- HandleKeyPress takes a key code, or value, and
  98. -- carries out the appropriate action. For example,
  99. -- if we pressed enter then this method would execute the
  100. -- associated function of the selected option.
  101. function HandleKeyPress( nKey, tMenu )
  102.     -- I like to use the key codes instead of the keys API, so bare with me.
  103.     -- This is another place that a table as a menu comes in handy!
  104.     -- Instead of having to check for each individual enter press on which option,
  105.     -- we can just run the associated function for that selection in the table!
  106.    
  107.     -- If the enter key is pressed then run the associated function of the selected option.
  108.     if nKey == 28 then
  109.         tMenu[nSelection].fAssociatedFunction()
  110.     -- If the up key is pressed and the selection is greater than 1 then move the selection up once.
  111.     -- We check the current selection to make sure we don't move off of the menu when pressing keys.
  112.     elseif nKey == 200 and nSelection > 1 then
  113.         nSelection = nSelection - 1
  114.     -- If the down key is pressed and the selection is less than the amount of selections in the given table.
  115.     elseif nKey == 208 and nSelection < 3 then
  116.         nSelection = nSelection + 1
  117.     end
  118. end
  119.  
  120. -- End Key Handling Methods --
  121.  
  122. -- Associated Functions --
  123.  
  124. -- FirstOption is the method that is run when
  125. -- the first option is selected and enter is pressed
  126. -- on the menu.
  127. function FirstOption()
  128.     ClearScreen() -- Clear the screen.
  129.     PrintCentered( 5, "First option selected." ) -- Notify the user that they just pressed the first option.
  130.     sleep( 2 ) -- Sleep for a couple seconds so the text isn't printed then instantly cleared.
  131. end
  132.  
  133. -- Second option works just like FirstOption.
  134. function SecondOption()
  135.     ClearScreen() -- Clear the screen.
  136.     PrintCentered( 5, "Second option selected." ) -- Notify the user that they just pressed the second option.
  137.     sleep( 2 ) -- Sleep for a couple seconds so the text isn't printed then instantly cleared.
  138. end
  139.  
  140. -- End Associated Functions --
  141.  
  142. -- Main Program Loop --
  143.  
  144. -- Loop this code until bRunning is not true (the program has been exited).
  145.  
  146. while bRunning do
  147.     ClearScreen() -- Wipe the screen so we can update it with the newest frame.
  148.     PrintMenu( tMainMenu, 5, nSelect ) -- Print the main menu starting on line 5.
  149.    
  150.     local sEvent, nKey = os.pullEvent( "key" ) -- Wait for any key presses so we can update the menu.
  151.     HandleKeyPress( nKey, tMainMenu ) -- Perform the appropriate actions for the key pressed.
  152. end
  153.  
  154. -- End Main Program Loop --
Advertisement
Add Comment
Please, Sign In to add comment