Advertisement
jesusthekiller

PixelToast BIOS

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