Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ MOUSE MENU TUTORIAL ]]--
- --[[ Copyright ?A9 2012 InputUsername ]]--
- --[[
- State is the variable representing the current menu.
- It can be anything as long as it's a string.
- It is easier if you keep the string 'state' as short as possible.
- ]]--
- local state = "main"
- --[[
- Here we define a table containing the options for
- the main menu and the menu on the 'info' screen.
- ]]--
- local tMainOptions = { "[ Programs ]", "[ Info ]", "[ Quit ]" }
- local tInfoOptions = { "[ Credits ]", "[ Back ]" }
- local tProgramsList = { "[ Door Control ]", "[ lights ]", "[ Back ]" }
- --[[
- The following lines of code are function definitions for the
- draw functions of the menu.
- ]]--
- local function drawMain() -- defines the drawMain function, which obviously draws the main menu
- term.clear()
- term.setCursorPos(2,1)
- term.write("Mouse menu tutorial - by InputUsername")
- for i = 1, #tMainOptions do -- starts a for loop to print all the options in the table 'tMainOptions'
- term.setCursorPos(2,1 + (i * 2)) -- makes the cursor skip a line every time it prints an option
- term.write(tMainOptions[i]) -- prints the menu options as a 'button'
- end
- end
- local function drawList()
- term.clear()
- term.setCursorPos(2,1)
- term.write("List of available programs")
- for x = 1, #tProgramsList do
- term.setCursorPos(2,1 + (x * 2))
- term.write(tProgramsList[x])
- end
- end
- --[[
- local function drawGame() -- defines the drawGame function, which draws the contents of the 'game'
- term.clear()
- term.setCursorPos(2,1)
- term.write("Programs:")
- term.setCursorPos(2,3)
- term.write("[ Back ]")
- end
- ]]--
- local function drawInfo() -- defines the drawInfo function, which is similar to the function 'drawMain()'
- term.clear()
- term.setCursorPos(2,1)
- term.write("Info")
- for i = 1, #tInfoOptions do
- term.setCursorPos(2,1 + (i * 2))
- term.write(tInfoOptions[i])
- end
- end
- local function drawCredits() -- defines the drawCredits function
- term.clear()
- term.setCursorPos(2,1)
- term.write("Credits")
- term.setCursorPos(2,2)
- term.write("Mouse menu tutorial, [c]2012 InputUsername")
- end
- --[[
- Here we start the menu's functionality
- ]]--
- while true do -- start an infinite loop
- if state == "main" then -- if the current menu is the main menu
- drawMain() -- calls the drawMain() function, which draws the main menu
- local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
- if button == 1 then -- if the user clicks with the left button (1 is left, 2 is right)
- for i = 1, #tMainOptions do -- some 'complicated' code to check where the mouse click was
- if X >= 2 and X <= #(tMainOptions[i]) + 2 and Y == 1 + (i * 2) then
- if i == 1 then
- state = "List"
- elseif i == 2 then
- state = "info"
- elseif i == 3 then
- state = "exit"
- end
- end
- end
- elseif state == "List" then
- drawList() -- draws the contents of the 'Programs List'
- local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
- if button == 1 then
- for i = 1, #tProgramsList do
- if X >= 2 and X <= #(tProgramsList[i]) + 2 and Y == 1 + (i * 2) then
- if i == 1 then
- shell.run(light_on)
- print("lights are on")
- sleep(4)
- elseif i == 2 then
- shell.run(lights)
- sleep(3)
- elseif i == 3 then
- state = "main"
- end
- end
- end
- end
- elseif state == "info" then
- drawInfo()
- local event, button, X, Y = os.pullEvent("mouse_click") -- waits for a mouse click
- if button == 1 then
- for i = 1, #tInfoOptions do
- if X >= 2 and X <= #(tMainOptions[i]) + 2 and Y == 1 + (i * 2) then
- if i == 1 then
- state = "credits"
- elseif i == 2 then
- state = "main"
- end
- end
- end
- end
- elseif state == "credits" then
- drawCredits() -- draw the credits
- sleep(2) -- wait 2 seconds
- state = "main" -- return to the main menu
- elseif state == "exit" then
- break
- end
- end
- end
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment