Guest User

mouseMenu

a guest
Nov 4th, 2012
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. --[[        MOUSE MENU TUTORIAL     ]]--    
  2. --[[ Copyright �A9 2012 InputUsername ]]--
  3.  
  4. --[[
  5. State is the variable representing the current menu.
  6. It can be anything as long as it's a string.
  7. It is easier if you keep the string 'state' as short as possible.
  8. ]]--
  9. local state = "main"
  10.  
  11.  
  12. --[[
  13. Here we define a table containing the options for
  14. the main menu and the menu on the 'info' screen.
  15. ]]--
  16. local tMainOptions = { "[ Play ]", "[ Info ]", "[ Quit ]" }
  17. local tInfoOptions = { "[ Credits ]", "[ Back ]" }
  18.  
  19.  
  20. --[[
  21. The following lines of code are function definitions for the
  22. draw functions of the menu.
  23. ]]--
  24.  
  25. local function drawMain() -- defines the drawMain function, which obviously draws the main menu
  26.  
  27.   term.clear()
  28.  
  29.   term.setCursorPos(2,1)
  30.   term.write("Mouse menu tutorial - by InputUsername")
  31.  
  32.   for i = 1, #tMainOptions do -- starts a for loop to print all the options in the table 'tMainOptions'
  33.     term.setCursorPos(2,1 + (i * 2)) -- makes the cursor skip a line every time it prints an option
  34.     term.write(tMainOptions[i]) -- prints the menu options as a 'button'
  35.   end
  36.  
  37. end
  38.  
  39. local function drawGame() -- defines the drawGame function, which draws the contents of the 'game'
  40.  
  41.   term.clear()
  42.  
  43.   term.setCursorPos(2,1)
  44.   term.write("Game!")
  45.  
  46.   term.setCursorPos(2,3)
  47.   term.write("[ Back ]")
  48. end
  49.  
  50. local function drawInfo() -- defines the drawInfo function, which is similar to the function 'drawMain()'
  51.  
  52.   term.clear()
  53.  
  54.   term.setCursorPos(2,1)
  55.   term.write("Info")
  56.  
  57.   for i = 1, #tInfoOptions do
  58.     term.setCursorPos(2,1 + (i * 2))
  59.     term.write(tInfoOptions[i])
  60.   end
  61.  
  62. end
  63.  
  64. local function drawCredits() -- defines the drawCredits function
  65.  
  66.   term.clear()
  67.  
  68.   term.setCursorPos(2,1)
  69.   term.write("Credits")
  70.  
  71.   term.setCursorPos(2,2)
  72.   term.write("Mouse menu tutorial, [c]2012 InputUsername")
  73. end
  74.  
  75.  
  76. --[[
  77. Here we start the menu's functionality
  78. ]]--
  79. while true do -- start an infinite loop
  80.  
  81.   if state == "main" then -- if the current menu is the main menu
  82.     drawMain() -- calls the drawMain() function, which draws the main menu
  83.    
  84.     local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  85.    
  86.     if button == 1 then -- if the user clicks with the left button (1 is left, 2 is right)
  87.       for i = 1, #tMainOptions do -- some 'complicated' code to check where the mouse click was
  88.         if X >= 2 and X <= #(tMainOptions[i]) + 2 and Y == 1 + (i * 2) then
  89.           if i == 1 then
  90.             state = "play"
  91.           elseif i == 2 then
  92.             state = "info"
  93.           elseif i == 3 then
  94.             state = "exit"
  95.           end
  96.         end
  97.       end
  98.     end
  99.    
  100.   elseif state == "play" then
  101.     drawGame() -- draws the contents of the 'game'
  102.    
  103.     local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  104.    
  105.     if button == 1 then
  106.       if X >= 2  and X <= 6 and Y == 3 then
  107.         state = "main"
  108.       end
  109.     end
  110.   elseif state == "info" then
  111.     drawInfo()
  112.    
  113.     local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  114.    
  115.     if button == 1 then
  116.       for i = 1, #tInfoOptions do
  117.         if X >= 2 and X <= #(tMainOptions[i]) + 2 and Y == 1 + (i * 2) then
  118.           if i == 1 then
  119.             state = "credits"
  120.           elseif i == 2 then
  121.             state = "main"
  122.           end
  123.         end
  124.       end
  125.     end
  126.    
  127.   elseif state == "credits" then
  128.     drawCredits() -- draw the credits
  129.    
  130.     sleep(2) -- wait 2 seconds
  131.    
  132.     state = "main" -- return to the main menu
  133.   elseif state == "exit" then
  134.     break
  135.   end
  136.  
  137. end
  138.  
  139. term.clear()
  140. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment