Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Creator: Jesusthekiller
- Date: 19 June 2013
- Visit www.jesusthekiller.com for more projects :)
- ---
- Feel free to use this code as you want, but:
- 1. This header has to stay unedited
- 2. This header has to be placed before ANY line of code in file that uses this code
- 3. Give credit in forums thread / advert / etc.
- * Font minimum 14px
- * Cannot be placed inside spoiler
- * Cannot be "strike trough"
- * Cannot be Comic Sans <-- Not kidding.
- 4. IT CANNOT BE SOLD FOR MONEY, PROFIT OR PRIVILAGES
- Rights given once are perpetual and cannot be taken if license if fulfilled.
- ]]--
- -- Settings:
- local header = "Tabs system"
- local footer = "By JTK =)"
- -- Center print/write function
- local function cwrite(t)
- t = tostring(t)
- local x, y = term.getCursorPos()
- term.setCursorPos((51-#t)/2, y)
- write(t)
- end
- local function cprint(t)
- cwrite(t)
- write("\n") -- So we don't count \n in string length
- end
- -- Display table:
- local disp = {
- -- Tabs
- {
- ['name'] = "Tab 1",
- -- Rows
- ['rows'] = {
- -- Row
- {
- ['name'] = "Row 1",
- -- function to be called when row selected and user hits enter
- -- Don't define if function unwanted
- ['func'] = function() term.setCursorPos(1, 19); write("Row 1 clicked!"); sleep(1) end,
- },
- {
- ['name'] = "Row 2",
- },
- {
- ['name'] = "Row 3",
- },
- },
- },
- {
- ['name'] = "Tab 2",
- ['rows'] = {
- {
- ['name'] = "1st Row",
- },
- {
- ['name'] = "2nd Row",
- },
- {
- ['name'] = "3rd Row",
- },
- },
- },
- {
- ['name'] = "Tab 3",
- ['rows'] = {
- {
- ['name'] = "Row a",
- },
- {
- ['name'] = "Row b",
- },
- {
- ['name'] = "Row c",
- },
- },
- },
- {
- ['name'] = "Time",
- -- This tab does not have any rows to be selected
- ['rows'] = false,
- -- It also calls this function when this tab is selected
- ['func'] = function()
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- parallel.waitForAny(
- function()
- while true do
- local e, k = os.pullEvent("key")
- if k == keys.left or k == keys.right then
- os.queueEvent("key", k)
- return
- end
- end
- end,
- function()
- while true do
- term.setCursorPos(1, 4)
- term.clearLine()
- cprint("Time: "..textutils.formatTime(os.time(), true))
- sleep(0.3)
- end
- end
- )
- end,
- },
- {
- ['name'] = "Exit",
- ['rows'] = {
- {
- ['name'] = "Exit",
- ['func'] = function() return "EXIT" end, -- Return "EXIT" (Special return code that exits program)
- },
- },
- },
- }
- -- Function to be called on quit (Press F12)
- local function onquit()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
- print("Bye bye!")
- end
- -- Screen write func
- local function screen(tab, row)
- -- Clear
- term.setBackgroundColor(colors.white)
- term.clear()
- -- Write header
- term.setCursorPos(1, 1) -- First line
- term.setBackgroundColor(colors.cyan)
- term.setTextColor(colors.black)
- term.clearLine()
- cwrite(header)
- -- Write tabs
- term.setCursorPos(1, 2)
- term.setBackgroundColor(colors.blue)
- term.clearLine()
- for i = 1, #disp do
- write((i == 1) and "" or " ") -- Write begging space
- term.setBackgroundColor((i == tab) and colors.white or colors.blue) -- Set colors for text
- term.setTextColor((i == tab) and colors.black or colors.white)
- write(disp[i].name) -- Write text
- term.setBackgroundColor(colors.blue)
- write((i == #disp) and "" or " ") -- Write ending space
- end
- -- Write line
- term.setTextColor(colors.blue)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(1, 3)
- for i = 1, 51 do -- Write line
- write("-")
- end
- local spos = 0
- local length = #disp[tab].name + 2 -- Calculate length
- for i = 1, tab - 1 do -- Calculate hole's start position
- spos = spos + #disp[i].name + 2
- end
- term.setCursorPos(spos, 3)
- for i = 1, length do -- Write 'hole'
- write(" ")
- end
- -- Write rows
- term.setTextColor(colors.black)
- term.setCursorPos(1, 4)
- local rows = disp[tab].rows
- if rows then -- Check for no rows
- for i = 1, #rows do
- local cursor = (i == row) and "> " or " "
- print(cursor..rows[i].name)
- end
- end
- -- Write footer
- term.setCursorPos(term.getSize()) -- Last line
- term.setBackgroundColor(colors.cyan)
- term.setTextColor(colors.black)
- term.clearLine()
- cwrite(footer)
- end
- -- Initialize:
- local tab = 1
- local row = 1
- -- Initial screen draw
- screen(tab, row)
- while true do
- -- Wait for key
- local e, k = os.pullEvent("key")
- -- Change tabs
- if k == keys.right and tab < #disp then
- tab = tab + 1
- row = 1
- -- Redraw screen
- screen(tab, row)
- -- Call tab's function
- if type(disp[tab].func) == "function" then
- disp[tab].func ()
- end
- end
- if k == keys.left and tab > 1 then
- tab = tab - 1
- row = 1
- -- Redraw screen
- screen(tab, row)
- -- Call tab's function
- if type(disp[tab].func) == "function" then
- disp[tab].func ()
- end
- end
- -- Change rows
- if disp[tab].rows then -- Check for no rows
- if k == keys.down and row < #disp[tab].rows then
- row = row + 1
- -- Redraw screen
- screen(tab, row)
- end
- if k == keys.up and row > 1 then
- row = row - 1
- -- Redraw screen
- screen(tab, row)
- end
- end
- -- Quit on F12
- if k == keys.f12 then
- onquit()
- return
- end
- -- Call function if enter
- if k == keys.enter then
- if type(disp[tab]['rows'][row]['func']) == "function" then
- local ret = disp[tab]['rows'][row]['func'] () -- Call function
- if ret == "EXIT" then -- If EXIT then die
- onquit()
- return
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement