kingcoon

CC GUI Menu

Dec 26th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.25 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 = { "[ Programs ]", "[ Info ]", "[ Quit ]" }
  17.     local tInfoOptions = { "[ Credits ]", "[ Back ]" }
  18.     local tProgramsList = { "[ Door Control ]", "[ lights ]", "[ Back ]" }
  19.    
  20.      
  21.     --[[
  22.     The following lines of code are function definitions for the
  23.     draw functions of the menu.
  24.     ]]--
  25.      
  26.     local function drawMain() -- defines the drawMain function, which obviously draws the main menu
  27.      
  28.       term.clear()
  29.    
  30.       term.setCursorPos(2,1)
  31.       term.write("Mouse menu tutorial - by InputUsername")
  32.      
  33.       for i = 1, #tMainOptions do -- starts a for loop to print all the options in the table 'tMainOptions'
  34.             term.setCursorPos(2,1 + (i * 2)) -- makes the cursor skip a line every time it prints an option
  35.             term.write(tMainOptions[i]) -- prints the menu options as a 'button'
  36.       end
  37.      
  38.     end
  39.      
  40.     local function drawList()
  41.      
  42.       term.clear()
  43.  
  44.       term.setCursorPos(2,1)
  45.       term.write("List of available programs") 
  46.  
  47.       for x = 1, #tProgramsList do
  48.             term.setCursorPos(2,1 + (x * 2))
  49.             term.write(tProgramsList[x])
  50.         end
  51.     end    
  52.      
  53.      
  54.      
  55.      
  56.      --[[
  57.     local function drawGame() -- defines the drawGame function, which draws the contents of the 'game'
  58.      
  59.       term.clear()
  60.      
  61.       term.setCursorPos(2,1)
  62.       term.write("Programs:")
  63.      
  64.       term.setCursorPos(2,3)
  65.       term.write("[ Back ]")
  66.     end
  67.     ]]--
  68.      
  69.     local function drawInfo() -- defines the drawInfo function, which is similar to the function 'drawMain()'
  70.      
  71.       term.clear()
  72.      
  73.       term.setCursorPos(2,1)
  74.       term.write("Info")
  75.      
  76.       for i = 1, #tInfoOptions do
  77.             term.setCursorPos(2,1 + (i * 2))
  78.             term.write(tInfoOptions[i])
  79.       end
  80.      
  81.     end
  82.      
  83.     local function drawCredits() -- defines the drawCredits function
  84.      
  85.       term.clear()
  86.      
  87.       term.setCursorPos(2,1)
  88.       term.write("Credits")
  89.      
  90.       term.setCursorPos(2,2)
  91.       term.write("Mouse menu tutorial, [c]2012 InputUsername")
  92.     end
  93.      
  94.      
  95.     --[[
  96.     Here we start the menu's functionality
  97.     ]]--
  98.     while true do -- start an infinite loop
  99.      
  100.       if state == "main" then -- if the current menu is the main menu
  101.             drawMain() -- calls the drawMain() function, which draws the main menu
  102.            
  103.             local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  104.            
  105.             if button == 1 then -- if the user clicks with the left button (1 is left, 2 is right)
  106.               for i = 1, #tMainOptions do -- some 'complicated' code to check where the mouse click was
  107.                     if X >= 2 and X <= #(tMainOptions[i]) + 2 and Y == 1 + (i * 2) then
  108.                       if i == 1 then
  109.                             state = "List"
  110.                       elseif i == 2 then
  111.                         state = "info"
  112.                       elseif i == 3 then
  113.                         state = "exit"
  114.                       end
  115.                     end
  116.              
  117.             end
  118.      
  119.      
  120.            
  121.       elseif state == "List" then
  122.         drawList() -- draws the contents of the 'Programs List'
  123.            
  124.             local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  125.            
  126.             if button == 1 then
  127.               for i = 1, #tProgramsList do
  128.                     if X >= 2 and X <= #(tProgramsList[i]) + 2 and Y == 1 + (i * 2) then
  129.                       if i == 1 then
  130.                             shell.run(light_on)            
  131.                             print("lights are on")
  132.                             sleep(4)
  133.                       elseif i == 2 then
  134.                             shell.run(lights)
  135.                             sleep(3)
  136.                       elseif i == 3 then
  137.                             state = "main"
  138.                      
  139.                       end
  140.                     end
  141.               end
  142.             end
  143.            
  144.       elseif state == "info" then
  145.         drawInfo()
  146.            
  147.             local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  148.            
  149.             if button == 1 then
  150.               for i = 1, #tInfoOptions do
  151.                     if X >= 2 and X <= #(tMainOptions[i]) + 2 and Y == 1 + (i * 2) then
  152.                       if i == 1 then
  153.                             state = "credits"
  154.                       elseif i == 2 then
  155.                             state = "main"
  156.                       end
  157.                     end
  158.               end
  159.             end
  160.            
  161.       elseif state == "credits" then
  162.         drawCredits() -- draw the credits
  163.            
  164.             sleep(2) -- wait 2 seconds
  165.            
  166.             state = "main" -- return to the main menu
  167.       elseif state == "exit" then
  168.         break
  169.       end
  170.      
  171.     end
  172.     end
  173.     term.clear()
  174.     term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment