Advertisement
jesusthekiller

Tabs system public

Jun 19th, 2013
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.77 KB | None | 0 0
  1. --[[
  2.     Creator: Jesusthekiller
  3.     Date: 19 June 2013
  4.    
  5.     Visit www.jesusthekiller.com for more projects :)
  6.     ---
  7.    
  8.     Feel free to use this code as you want, but:
  9.     1. This header has to stay unedited
  10.     2. This header has to be placed before ANY line of code in file that uses this code
  11.     3. Give credit in forums thread / advert / etc.
  12.       * Font minimum 14px
  13.       * Cannot be placed inside spoiler
  14.       * Cannot be "strike trough"
  15.       * Cannot be Comic Sans  <-- Not kidding.
  16.     4. IT CANNOT BE SOLD FOR MONEY, PROFIT OR PRIVILAGES
  17.    
  18.     Rights given once are perpetual and cannot be taken if license if fulfilled.
  19. ]]--
  20.  
  21. -- Settings:
  22. local header = "Tabs system"
  23. local footer = "By JTK =)"
  24.  
  25. -- Center print/write function
  26. local function cwrite(t)
  27.     t = tostring(t)
  28.     local x, y = term.getCursorPos()
  29.     term.setCursorPos((51-#t)/2, y)
  30.     write(t)
  31. end
  32.  
  33. local function cprint(t)
  34.     cwrite(t)
  35.     write("\n") -- So we don't count \n in string length
  36. end
  37.  
  38. -- Display table:
  39. local disp = {
  40.     -- Tabs
  41.     {
  42.         ['name'] = "Tab 1",
  43.         -- Rows
  44.         ['rows'] = {
  45.             -- Row
  46.             {
  47.                 ['name'] = "Row 1",
  48.                 -- function to be called when row selected and user hits enter
  49.                 -- Don't define if function unwanted
  50.                 ['func'] = function() term.setCursorPos(1, 19); write("Row 1 clicked!"); sleep(1) end,
  51.             },
  52.            
  53.             {
  54.                 ['name'] = "Row 2",
  55.             },
  56.            
  57.             {
  58.                 ['name'] = "Row 3",
  59.             },
  60.         },
  61.     },
  62.    
  63.     {
  64.         ['name'] = "Tab 2",
  65.         ['rows'] = {
  66.             {
  67.                 ['name'] = "1st Row",
  68.             },
  69.            
  70.             {
  71.                 ['name'] = "2nd Row",
  72.             },
  73.            
  74.             {
  75.                 ['name'] = "3rd Row",
  76.             },
  77.         },
  78.     },
  79.    
  80.     {
  81.         ['name'] = "Tab 3",
  82.         ['rows'] = {
  83.             {
  84.                 ['name'] = "Row a",
  85.             },
  86.            
  87.             {
  88.                 ['name'] = "Row b",
  89.             },
  90.            
  91.             {
  92.                 ['name'] = "Row c",
  93.             },
  94.         },
  95.     },
  96.    
  97.     {
  98.         ['name'] = "Time",
  99.         -- This tab does not have any rows to be selected
  100.         ['rows'] = false,
  101.         -- It also calls this function when this tab is selected
  102.         ['func'] = function()
  103.             term.setTextColor(colors.black)
  104.             term.setBackgroundColor(colors.white)
  105.            
  106.             parallel.waitForAny(
  107.                 function()
  108.                     while true do
  109.                         local e, k = os.pullEvent("key")
  110.                         if k == keys.left or k == keys.right then
  111.                             os.queueEvent("key", k)
  112.                             return
  113.                         end
  114.                     end
  115.                 end,
  116.                
  117.                 function()
  118.                     while true do
  119.                         term.setCursorPos(1, 4)
  120.                         term.clearLine()
  121.                         cprint("Time: "..textutils.formatTime(os.time(), true))
  122.                         sleep(0.3)
  123.                     end
  124.                 end
  125.             )
  126.         end,
  127.     },
  128.    
  129.     {
  130.         ['name'] = "Exit",
  131.         ['rows'] = {
  132.             {
  133.                 ['name'] = "Exit",
  134.                 ['func'] = function() return "EXIT" end, -- Return "EXIT" (Special return code that exits program)
  135.             },
  136.         },
  137.     },
  138. }
  139.  
  140. -- Function to be called on quit (Press F12)
  141. local function onquit()
  142.     term.setBackgroundColor(colors.black)
  143.     term.setTextColor(colors.white)
  144.     term.clear()
  145.     term.setCursorPos(1, 1)
  146.     print("Bye bye!")
  147. end
  148.  
  149. -- Screen write func
  150. local function screen(tab, row)
  151.  
  152.     -- Clear
  153.     term.setBackgroundColor(colors.white)
  154.     term.clear()
  155.    
  156.     -- Write header
  157.     term.setCursorPos(1, 1) -- First line
  158.     term.setBackgroundColor(colors.cyan)
  159.     term.setTextColor(colors.black)
  160.     term.clearLine()
  161.    
  162.     cwrite(header)
  163.    
  164.     -- Write tabs
  165.     term.setCursorPos(1, 2)
  166.     term.setBackgroundColor(colors.blue)
  167.     term.clearLine()
  168.    
  169.     for i = 1, #disp do
  170.         write((i == 1) and "" or " ") -- Write begging space
  171.        
  172.         term.setBackgroundColor((i == tab) and colors.white or colors.blue) -- Set colors for text
  173.         term.setTextColor((i == tab) and colors.black or colors.white)
  174.        
  175.         write(disp[i].name) -- Write text
  176.        
  177.         term.setBackgroundColor(colors.blue)
  178.        
  179.         write((i == #disp) and "" or " ") -- Write ending space
  180.     end
  181.    
  182.     -- Write line
  183.     term.setTextColor(colors.blue)
  184.     term.setBackgroundColor(colors.white)
  185.     term.setCursorPos(1, 3)
  186.    
  187.     for i = 1, 51 do -- Write line
  188.         write("-")
  189.     end
  190.    
  191.     local spos = 0
  192.     local length = #disp[tab].name + 2 -- Calculate length
  193.    
  194.     for i = 1, tab - 1 do -- Calculate hole's start position
  195.         spos = spos + #disp[i].name + 2
  196.     end
  197.    
  198.     term.setCursorPos(spos, 3)
  199.    
  200.     for i = 1, length do -- Write 'hole'
  201.         write(" ")
  202.     end
  203.    
  204.     -- Write rows
  205.     term.setTextColor(colors.black)
  206.     term.setCursorPos(1, 4)
  207.    
  208.     local rows = disp[tab].rows
  209.    
  210.     if rows then -- Check for no rows
  211.         for i = 1, #rows do
  212.             local cursor = (i == row) and "> " or "  "
  213.             print(cursor..rows[i].name)
  214.         end
  215.     end
  216.    
  217.     -- Write footer
  218.     term.setCursorPos(term.getSize()) -- Last line
  219.     term.setBackgroundColor(colors.cyan)
  220.     term.setTextColor(colors.black)
  221.     term.clearLine()
  222.    
  223.     cwrite(footer)
  224. end
  225.  
  226. -- Initialize:
  227. local tab = 1
  228. local row = 1
  229.  
  230. -- Initial screen draw
  231. screen(tab, row)
  232.  
  233. while true do
  234.    
  235.     -- Wait for key
  236.     local e, k = os.pullEvent("key")
  237.    
  238.     -- Change tabs
  239.     if k == keys.right and tab < #disp then
  240.         tab = tab + 1
  241.         row = 1
  242.        
  243.         -- Redraw screen
  244.         screen(tab, row)
  245.        
  246.         -- Call tab's function
  247.         if type(disp[tab].func) == "function" then
  248.             disp[tab].func ()
  249.         end
  250.     end
  251.    
  252.     if k == keys.left and tab > 1 then
  253.         tab = tab - 1
  254.         row = 1
  255.        
  256.         -- Redraw screen
  257.         screen(tab, row)
  258.        
  259.         -- Call tab's function
  260.         if type(disp[tab].func) == "function" then
  261.             disp[tab].func ()
  262.         end
  263.     end
  264.    
  265.     -- Change rows
  266.     if disp[tab].rows then -- Check for no rows
  267.         if k == keys.down and row < #disp[tab].rows then
  268.             row = row + 1
  269.            
  270.             -- Redraw screen
  271.             screen(tab, row)
  272.         end
  273.        
  274.         if k == keys.up and row > 1 then
  275.             row = row - 1
  276.            
  277.             -- Redraw screen
  278.             screen(tab, row)
  279.         end
  280.     end
  281.    
  282.     -- Quit on F12
  283.     if k == keys.f12 then
  284.         onquit()
  285.         return
  286.     end
  287.    
  288.     -- Call function if enter
  289.     if k == keys.enter then
  290.         if type(disp[tab]['rows'][row]['func']) == "function" then
  291.             local ret = disp[tab]['rows'][row]['func'] () -- Call function
  292.            
  293.             if ret == "EXIT" then -- If EXIT then die
  294.                 onquit()
  295.                 return
  296.             end
  297.         end
  298.     end
  299. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement