AssossaGPB

Tabbed Multitasking Api

Jun 27th, 2014
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.85 KB | None | 0 0
  1. --+-------------------------+--
  2. --| Tabbed Multitasking Api |--
  3. --|       By Assossa        |--
  4. --+-------------------------+--
  5.  
  6. local w, h = term.getSize()
  7.  
  8. --[ Local Variables ]--
  9. local tabs = {}
  10. local tabNames = {}
  11. local currentTab = 1
  12. local tempTab = nil
  13. local tempTabName = nil
  14. local tabProgs = {}
  15.  
  16. --[ Local Functions ]--
  17. local function wrapTabs(num,dir,list)
  18.     local i = num
  19.     if dir == 1 then
  20.         while i <= #list do
  21.             list[num]=list[num+1]
  22.             i = i + 1
  23.         end
  24.         list[#list] = nil
  25.     elseif dir == 2 then
  26.         while i >= num do
  27.             list[i+1]=list[i]
  28.             i = i - 1
  29.         end
  30.         list[num] = nil
  31.     end
  32. end
  33.  
  34. local function spanClickEvent(x1, y1, x2, y2, button1)
  35.  local event, button, xB, yB = os.pullEvent("mouse_click")
  36.  if x1 <= xB and y1 <= yB then
  37.   if x2 >= xB and y2 >= yB then
  38.    if button1 == button then
  39.     return true
  40.    else
  41.     return false
  42.    end
  43.   else
  44.    return false
  45.   end
  46.  else
  47.   return false
  48.  end
  49. end
  50.  
  51. --[ Public Functions ]--
  52. function newTab(func, tabname)
  53.     local num = #tabs+1
  54.     tabs[num]=coroutine.create(func)
  55.     tabNames[num]=tabname
  56. end
  57.  
  58. function setTabName(index,name)
  59.     tabNames[index]=name
  60. end
  61.  
  62. function getTabName(index)
  63.     return tabNames[index]
  64. end
  65.  
  66. function getTabNames()
  67.     return tabNames
  68. end
  69.  
  70. function getTabs()
  71.     return tabs
  72. end
  73.  
  74. function getCurrentTab()
  75.     return currentTab
  76. end
  77.  
  78. function switchTab(new)
  79.     currentTab=new
  80. end
  81.  
  82. function moveTab(num,num2)
  83.     tempTab = tabs[num]
  84.     tabs = wrapTabs(num,1,tabs)
  85.     tabs = wrapTabs(num2,2,tabs)
  86.     tabs[num2] = tempTab
  87.    
  88.     tempTabName = tabNames[num]
  89.     tabNames = wrapTabs(num,1,tabNames)
  90.     tabNames = wrapTabs(num2,2,tabNames)
  91.     tabNames[num2] = tempTabName
  92. end
  93.  
  94. function closeTab(num)
  95.     tabs[num]=nil
  96.     tabNames[num]=nil
  97.     wrapTabs(num,1,tabs)
  98.     wrapTabs(num,1,tabNames)
  99. end
  100.  
  101. function runTabs(...)
  102.     for i,v in pairs(tabs) do
  103.         if coroutine.status(v) ~= "dead" then
  104.             coroutine.resume(v, arg)
  105.         else
  106.             closeTab(v)
  107.         end
  108.     end
  109. end
  110.  
  111. function runCurrentTab(...)
  112.     for i,v in pairs(tabs) do
  113.     if coroutine.status(v) ~= "dead" then
  114.         if i == currentTab then
  115.             coroutine.resume(v, arg)
  116.         else
  117.             coroutine.yield(v)
  118.         end
  119.     else
  120.         closeTab(v)
  121.     end
  122. end
  123.  
  124. --[ Tabbed MultiTasking Gui ]--
  125. local tabScroll = 0
  126.  
  127. function drawArrows()
  128.     term.setCursorPos(1,1)
  129.     term.setBackgroundColor(colors.gray)
  130.     term.setTextColor(colors.black)
  131.     term.write("<")
  132.     term.setCursorPos(w,1)
  133.     term.setBackgroundColor(colors.gray)
  134.     term.setTextColor(colors.black)
  135.     term.write(">")
  136. end
  137.  
  138. function drawTabs()
  139.     for i,v in pairs(tabNames) do
  140.         term.setCursorPos(i*5,1)
  141.         term.setBackgroundColor(colors.white)
  142.         term.setTextColor(colors.black)
  143.         term.write(v:sub(1,4).."|")
  144.     end
  145.    
  146.     drawArrows()
  147.    
  148.     if #tabNames <= math.floor(w / 5) then
  149.         tabScroll = 0
  150.     end
  151. end
  152.  
  153. function tabClick()
  154.     local lArrow = spanClickEvent(1,1,1,1,"left")
  155.     local rArrow = spanClickEvent(kernel.x,1,kernel.x,1,"left")
  156.     local tabClicks = {}
  157.    
  158.     for i=0,math.floor(kernel.x/5) do
  159.         tabClicks[i+1] = spanClickEvent((i*5)+1, 1, (i*5)+6, 1, "left")
  160.     end
  161.    
  162.     if lArrow and tabScroll > 0 then
  163.         tabScroll = tabScroll - 1
  164.     end
  165.     if rArrow and tabScroll < #tabmulti.getTabs() * 5 then
  166.         tabScroll = tabScroll + 1
  167.     end
  168.     for i,v in pairs(tabClicks) do
  169.         if v then
  170.             switchTab(i+math.floor(tabScroll/5))
  171.         end
  172.     end
  173. end
  174.  
  175. local function runProg(args)
  176.     shell.run(args[1])
  177. end
  178.  
  179. function tabRun(program, name)
  180.     tabmulti.newTab(runProg, name)
  181.     tabProgs[name] = program
  182. end
  183.  
  184. --[[
  185. local function tabMain()
  186.     tabRun("Default Tab Program Name", "First Tab")
  187.     switchTab("First Tab")
  188.     while true do
  189.         drawTabs()
  190.         tabClick()
  191.         local argNum = nil
  192.         for i,v in pairs(tabProgs) do
  193.             if tabmulti.getTabName(tabmulti.getCurrentTab) == v then
  194.                 argNum = i
  195.             end
  196.         end
  197.         if argNum ~= nil then
  198.             tabmulti.runCurrentTab(tabProgs[argNum])
  199.         else
  200.             tabmulti.runCurrentTab()
  201.         end
  202.     end
  203. end
  204. --]]
Advertisement
Add Comment
Please, Sign In to add comment