Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- =========================================
- Trystan Cannon
- 9/29/12
- Website for lieu
- A website designed for 1lann's firefox
- for lieu's company.
- =========================================
- --]]
- --=Constants===========================--
- local nScreenWidth, nScreenHeight = term.getSize() -- Grab the dimensions of the screen ot put into the constants table.
- tConstants = {
- ["SCREEN_HEIGHT"] = nScreenHeight,
- ["SCREEN_WIDTH"] = nScreenWidth
- }
- nScreenWidth, nScreenHeight = nil, nil -- Free up the variabels for nScreenWidth and nScreenHeight because they're in the constants table now.
- --=====================================--
- --=Basic Gui Methods=============--
- -- Prints the passed string in the exact center of the screen on the line (nHeight) provided.
- function printCentered(nHeight, sString)
- term.setCursorPos(tConstants["SCREEN_WIDTH"]/2 - sString:len()/2, nHeight)
- term.write(sString)
- end
- -- Prints the passed string to the very right of the screen on the line (nHeight) provided.
- function printRight(nHeight, sString)
- term.setCursorPos(tConstants["SCREEN_WIDTH"] - sString:len(), nHeight)
- term.write(sString)
- end
- -- Clears the screen of all text, and repositions the cursor to the upper right (1,1) of the screen.
- function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- --===============================--
- --=Website specific GUI methods====================--
- -- Prints the screen border with the format:
- -- +-----+
- -- | |
- -- +-----+
- function printBorder()
- -- Prints a horizontal line with the border format.
- local function printHorizontalLine(xPos, yPos)
- term.setCursorPos(xPos, yPos)
- term.write("+" .. string.rep("-", tConstants["SCREEN_WIDTH"] - 2) .. "+")
- end
- -- Print the top line of the screen.
- printHorizontalLine(1, 1)
- -- Print the center of the screen with horizontal characters (pipes: | |).
- for nHeight = 2, tConstants["SCREEN_HEIGHT"] - 1 do
- term.setCursorPos(1, nHeight)
- term.write("|")
- term.setCursorPos(tConstants["SCREEN_WIDTH"], nHeight)
- term.write("|")
- end
- -- Print the bottom line of the screen.
- printHorizontalLine(1, tConstants["SCREEN_HEIGHT"])
- end
- -- Prints a box with the passed corner, horizontal, and vertical characters with the dimensions of the passed width and height with
- -- the upper left hand corner of the box beginning at (xPos, yPos).
- function printBox(cCornerChar, cHorizontalChar, cVerticalChar, nWidth, nHeight, xPos, yPos)
- -- Establish the horizontal and vertical line local methods.
- local function printHorizontalLine(cCornerChar, cHorizontalChar, nLength)
- print(cCornerChar .. string.rep(cHorizontalChar, nLength - 2) .. cCornerChar)
- end
- local function printVerticalLine(cVerticalChar, nLength)
- print(cVerticalChar .. string.rep(" ", nLength - 2) .. cVerticalChar)
- end
- -- Set the cursor to the position of the upper left hand corner
- -- of where the box will begin.
- term.setCursorPos(xPos, yPos)
- -- Print the top line of the box.
- printHorizontalLine(cCornerChar, cHorizontalChar, nWidth)
- -- Print as many vertical lines as the height of the box to create the "body" of the box.
- for nLine=1, nHeight do
- term.setCursorPos(xPos, yPos+nLine)
- printVerticalLine(cVerticalChar, nWidth)
- end
- -- Print the bottom line of the box.
- term.setCursorPos(xPos, yPos+nHeight)
- printHorizontalLine(cCornerChar, cHorizontalChar, nWidth)
- end
- -- Prints the header of the company website.
- function printHeader(sTitle)
- -- Print a box that is wide enough for the title with two spaces on the outside; we can center the text inside the box.
- printBox("*", "*", "*", sTitle:len() + 2, 3, tConstants["SCREEN_WIDTH"]/2 - (sTitle:len() + 4)/2, 2)
- -- Print the title in the center of the box.
- printCentered(3, sTitle) -- Assuming that the box has been centered correctly, this line should appear directly in the center of the box.
- end
- --=================================================--
- --=Menu printing methods==============================--
- -- Prints a menu table with the format: tMenu[n] = {sTitle, fAssociatedFunction}
- -- *fAssociated function is the identifier of the method that will be called if
- -- that selection is selected by the user. (Enter is pressed on that selection).
- function printMenu(tMenu, nSelection, xPos, yPos)
- for nOption, tOptionTable in ipairs(tMenu) do
- term.setCursorPos(xPos, yPos + (nOption - 1)) -- Subtract one from the option when printing on the height to make sure that we use
- -- the original yPos provided in the parameters.
- -- If the option that is being printed is selected, then print brackets around it.
- if nSelection == nOption then
- term.write("[" .. tOptionTable.sTitle .. "]")
- -- If the option that is being printed is not selected, then just print it regularly.
- else
- term.write(" " .. tOptionTable.sTitle .. " ") -- Print spaces around the option to compensate for the lack of brackets in the spacing.
- end
- end
- end
- --====================================================--
- --=Key handling methods (for the menu)======================--
- -- Takes a key code and performs the appropriate operation depending on what the key code was.
- -- Executes the option depending on the menu provided with the format: tMenu[n] = {sTitle, fAssociatedFunction}
- -- WILL RETURN THE SELECTION!!!
- function handleKeyPress(nKey, nSelection, tMenu)
- -- If the ENTER key was pressed, then execute the associated function with the option.
- if nKey == 28 then
- -- Execute the associated function.
- tMenu[nSelection].fAssociatedFunction()
- -- If the up key was pressed and the selection is currently greater than one, move the selection up.
- elseif nKey == 200 and nSelection > 1 then
- -- Move the selection up one.
- nSelection = nSelection - 1
- -- If the down key was pressed and the selection is currently less than the total number of selections, move the selection down.
- elseif nKey == 208 and nSelection < #tMenu then
- -- Move the selection down one.
- nSelection = nSelection + 1
- end
- return nSelection
- end
- --==========================================================--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement