kingcoon

CC Gui menu Final

Jan 16th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.14 KB | None | 0 0
  1.     --[[
  2.     State is the variable representing the current menu.
  3.     It can be anything as long as it's a string.
  4.     It is easier if you keep the string 'state' as short as possible.
  5.     ]]--
  6.    
  7.     --[[
  8.     check lights
  9.     ]]--
  10.    
  11.     l = rs.getBundledInput("back")
  12.     print(l)
  13.    
  14.     local state = "main"
  15.     local doorsState = "closed"
  16.     local lightsState = "on"
  17.     running = true
  18.      
  19.     --[[
  20.     Here we define a table containing the options for
  21.     the main menu and the menu on the 'info' screen.
  22.     ]]--
  23.     local tMainOptions = { "[ Programs ]", "[ Info ]", "[ Quit ]" }
  24.     local tInfoOptions = { "[ Credits ]", "[ Back ]" }
  25.     local tProgramsList = { "[ Door Control ]", "[ Lights ]", "[ Back ]" }
  26.     local tDoorControl = { "[ Open ]", "[ Close ]", "[ Back ]" }
  27.     local tLightsControl = { "[ On ]", "[ Off ]", "[ Back ]" }
  28.      
  29.     --[[
  30.     The following lines of code are function definitions for the
  31.     draw functions of the menu.
  32.     ]]--
  33.      
  34.     local function drawMain() -- defines the drawMain function, which obviously draws the main menu
  35.             term.clear()
  36.             term.setCursorPos(2,1)
  37.             term.write("Main Menu")
  38.             for i = 1, #tMainOptions do -- starts a for loop to print all the options in the table 'tMainOptions'
  39.                     term.setCursorPos(2,1 + (i * 2)) -- makes the cursor skip a line every time it prints an option
  40.                     term.write(tMainOptions[i]) -- prints the menu options as a 'button'
  41.             end
  42.     end
  43.      
  44.     local function drawPrograms() -- defines the drawPrograms function, which draws the contents of the 'game'
  45.             term.clear()
  46.             term.setCursorPos(2,1)
  47.             term.write("List of available programs")
  48.             for i = 1, #tProgramsList do
  49.                     term.setCursorPos(2,1 + (i * 2))
  50.                     term.write(tProgramsList[i])
  51.             end
  52.     end
  53.      
  54.     local function drawDoors() -- defines the drawDoors function, which draws the contents of the 'game'
  55.             term.clear()
  56.             term.setCursorPos(2,1)
  57.             term.write("Doors: " .. doorsState)
  58.             for i = 1, #tDoorControl do
  59.                     term.setCursorPos(2,1 + (i * 2))
  60.                     term.write(tDoorControl[i])
  61.             end
  62.     end
  63.      
  64.     local function drawLights() -- defines the drawLights function, which draws the contents of the 'game'
  65.             term.clear()
  66.             term.setCursorPos(2,1)
  67.             term.write("Lights: " .. lightsState)
  68.             for i = 1, #tLightsControl do
  69.                     term.setCursorPos(2,1 + (i * 2))
  70.                     term.write(tLightsControl[i])
  71.             end
  72.     end
  73.      
  74.     local function drawInfo() -- defines the drawInfo function, which is similar to the function 'drawMain()'
  75.             term.clear()
  76.             term.setCursorPos(2,1)
  77.             term.write("Info")
  78.             for i = 1, #tInfoOptions do
  79.                     term.setCursorPos(2,1 + (i * 2))
  80.                     term.write(tInfoOptions[i])
  81.             end
  82.     end
  83.      
  84.     local function drawCredits() -- defines the drawCredits function
  85.             term.clear()
  86.             term.setCursorPos(2,1)
  87.             term.write("Credits")
  88.             term.setCursorPos(2,2)
  89.             term.write("Chris is a Nigga    ")
  90.             term.write("    NIGNOG")
  91.     end
  92.      
  93.     --[[
  94.     Here we start the menu's functionality
  95.     ]]--
  96.     while running do -- start an infinite loop
  97.             if state == "main" then -- if the current menu is the main menu
  98.                     drawMain() -- calls the drawMain() function, which draws the main menu
  99.                     local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  100.                     if button == 1 then -- if the user clicks with the left button (1 is left, 2 is right)
  101.                             for i = 1, #tMainOptions do -- some 'complicated' code to check where the mouse click was
  102.                                     if X >= 2 and X <= #(tMainOptions[i]) + 2 and Y == 1 + (i * 2) then
  103.                                             if i == 1 then
  104.                                                     state = "programs"
  105.                                             elseif i == 2 then
  106.                                                     state = "info"
  107.                                             elseif i == 3 then
  108.                                                     state = "exit"
  109.                                             end
  110.                                     end
  111.                             end
  112.                     end
  113.             elseif state == "programs" then
  114.                     drawPrograms() -- draws the contents of the 'game'    
  115.                     local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  116.                     if button == 1 then
  117.                             for i = 1, #tProgramsList do
  118.                                     if X >= 2 and X <= #(tProgramsList[i]) + 2 and Y == 1 + (i * 2) then
  119.                                             if i == 1 then
  120.                                                     state = "door_control"
  121.                                             elseif i == 2 then
  122.                                                     state = "lights"
  123.                                             elseif i == 3 then
  124.                                                     state = "main"
  125.                                             end
  126.                                     end
  127.                             end
  128.                     end
  129.             elseif state == "door_control" then
  130.                     drawDoors() -- draws the contents of the 'lights'
  131.                     local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  132.                     if button == 1 then
  133.                             for i = 1, #tDoorControl do
  134.                                     if X >= 2 and X <= #(tDoorControl[i]) + 2 and Y == 1 + (i * 2) then
  135.                                             if i == 1 then
  136.                                                     doorsState = "open"
  137.                                             elseif i == 2 then
  138.                                                     doorsState = "closed"
  139.                                             elseif i == 3 then
  140.                                                     state = "programs"
  141.                                             end
  142.                                     end    
  143.                             end
  144.                     end
  145.             elseif state == "lights" then
  146.                     drawLights() -- draws the contents of the 'lights'
  147.                     local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  148.                     if button == 1 then
  149.                             for i = 1, #tLightsControl do
  150.                                     if X >= 2 and X <= #(tLightsControl[i]) + 2 and Y == 1 + (i * 2) then
  151.                                             if i == 1 then
  152.                                                     lightsState = "on"
  153.                                             elseif i == 2 then
  154.                                                     lightsState = "off"
  155.                                             elseif i == 3 then
  156.                                                     state = "programs"
  157.                                             end
  158.                                     end
  159.                             end
  160.                     end
  161.             elseif state == "info" then
  162.                     drawInfo()
  163.                     local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
  164.                     if button == 1 then
  165.                             for i = 1, #tInfoOptions do
  166.                                     if X >= 2 and X <= #(tMainOptions[i]) + 2 and Y == 1 + (i * 2) then
  167.                                             if i == 1 then
  168.                                                     state = "credits"
  169.                                             elseif i == 2 then
  170.                                                     state = "main"
  171.                                             end
  172.                                     end
  173.                             end
  174.                     end
  175.             elseif state == "credits" then
  176.                     drawCredits() -- draw the credits
  177.                     sleep(2) -- wait 2 seconds
  178.                     state = "main" -- return to the main menu
  179.             elseif state == "exit" then
  180.                     running = false
  181.             end
  182.    
  183.         if lightsState == "on" then
  184.                 rs.setBundledOutput("back",colors.combine(rs.getBundledOutput("back"), colors.yellow))
  185.         elseif lightsState == "off" then
  186.                 rs.setBundledOutput("back",colors.subtract(rs.getBundledOutput("back"), colors.yellow))
  187.         end    
  188.        
  189.         if doorsState == "open" then
  190.             rs.setBundledOutput("back",colors.combine(rs.getBundledOutput("back"), colors.blue))
  191.         elseif doorsState == "closed" then
  192.             rs.setBundledOutput("back",colors.subtract(rs.getBundledOutput("back"), colors.blue))
  193.         end
  194.    
  195.    
  196.     end
  197.      
  198.     -- Clean up
  199.     term.clear()
  200.     term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment