KaoSDlanor

Advanced computer menu, double scroll and graphical addons

Jan 17th, 2013
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.27 KB | None | 0 0
  1. local function menu(tOpts,x,y,wd,ht,tInfo,col1,col2)
  2.     term.setCursorBlink(false)
  3.     local selected=1
  4.     local scrolled=0
  5.     local scrolledx=0
  6.     tInfo=tInfo or {}
  7.     local max=0
  8.     for k,v in ipairs(tOpts) do
  9.         tOpts[k]=tostring(v) or ""
  10.         tInfo[k]=tInfo[k] or {}
  11.         max=math.max(max,#v)
  12.     end
  13.     local tColMax={}
  14.     for i=1,#tInfo do
  15.         for k,v in pairs(tInfo[i]) do
  16.             tInfo[i][k]=tostring(v) or ""
  17.             tColMax[k]=math.max(tColMax[k] or 0,#v)
  18.         end
  19.     end
  20.     local totMax=max+2
  21.     for k,v in pairs(tColMax) do
  22.         totMax=totMax+v
  23.     end
  24.     while true do
  25.         term.setTextColor(colors.black)
  26.         for cy=1,ht do
  27.             term.setBackgroundColor(selected-scrolled==cy and (col1 or colors.magenta) or cy==ht and colors.gray or (col2 or colors.purple))
  28.             term.setCursorPos(x,y+cy-1)
  29.             write(string.rep(" ",wd-1))
  30.             term.setBackgroundColor(colors.gray)
  31.             write(" ")
  32.         end
  33.         for k,v in ipairs(tOpts) do
  34.             if k>scrolled and k-scrolled<=ht-2 then
  35.                 term.setCursorPos(x,y+k-1-scrolled)
  36.                 term.setBackgroundColor(selected==k and (col1 or colors.magenta) or (col2 or colors.purple))
  37.                 local line=(k==selected and "[" or " ")..v..(k==selected and "]" or " ")..string.rep(" ",max-#v)
  38.                 for col,sInfo in pairs(tInfo[k]) do
  39.                     line=line..sInfo..string.rep(" ",tColMax[col]-#sInfo+1)
  40.                 end
  41.                 write(string.sub(line,1+scrolledx,wd+scrolledx-2))
  42.             end
  43.         end
  44.         vL=math.ceil(ht*ht/#tOpts)
  45.         hL=math.ceil(wd*wd/totMax)
  46.         term.setBackgroundColor(colors.black)
  47.         term.setCursorPos(math.floor(scrolledx*(totMax-wd)/(totMax-hL)+x),y+ht-1)
  48.         write(string.rep(" ",math.min(hL,wd-math.floor(scrolledx*(totMax-wd)/(totMax-hL)+x)+1)))
  49.         for cy=1,math.min(vL,ht-math.floor(scrolled*(ht-vL-1)/(#tOpts-ht)+y)+1) do
  50.             term.setCursorPos(x+wd-1,math.floor(scrolled*(ht-vL-1)/(#tOpts-ht)+y)+cy-1)
  51.             write(" ")
  52.         end
  53.         term.setCursorPos(x+wd-1,y+ht-1)
  54.         write(" ")
  55.  
  56.         term.setBackgroundColor(colors.black)
  57.         term.setTextColor(colors.white)
  58.         local evts={os.pullEvent()}
  59.         if ((evts[1]=="mouse_scroll" and evts[2]==-1) or (evts[1]=="key" and evts[2]==200)) and selected>1 then
  60.             selected=selected-1
  61.         elseif ((evts[1]=="mouse_scroll" and evts[2]==1) or (evts[1]=="key" and evts[2]==208)) and selected<#tOpts then
  62.             selected=selected+1
  63.         elseif evts[1]=="key" and evts[2]==203 and scrolledx>0 then
  64.             scrolledx=scrolledx-1
  65.         elseif evts[1]=="key" and evts[2]==205 and scrolledx<totMax-wd+2 then
  66.             scrolledx=scrolledx+1
  67.         elseif evts[1]=="key" and evts[2]==28 then
  68.             return tOpts[selected]
  69.         elseif evts[1]=="mouse_click" and evts[2]==1 and evts[3]>=x and evts[3]<x+wd and tOpts[evts[4]-y+scrolled+1] then
  70.             return tOpts[evts[4]-y+scrolled+1]
  71.         end
  72.         if selected<=scrolled then
  73.             scrolled=selected-1
  74.         elseif selected>scrolled+ht-2 then
  75.             scrolled=selected-ht+2
  76.         end
  77.     end
  78. end
  79.  
  80.  
  81. local tOpts,tInfo={},{}
  82. while true do
  83.     local curDir=shell.dir()
  84.     local dirList=fs.list(shell.dir())
  85.     if shell.dir()~="" then table.insert(dirList,1,"..\\") end
  86.     for num,sName in pairs(dirList) do
  87.         table.insert(tOpts,sName)
  88.         table.insert(tInfo,{tostring(fs.isDir(fs.combine(curDir,sName))),tostring(fs.getSize(fs.combine(curDir,sName)))})
  89.     end
  90.     local size={term.getSize()}
  91.     local resp=fs.combine(curDir,menu(tOpts,2,2,size[1]-2,size[2]-2,tInfo))
  92.     if fs.isDir(resp) then
  93.         shell.setDir(resp)
  94.         tOpts={}
  95.     else
  96.         tOpts={}
  97.     end
  98. end
Advertisement
Add Comment
Please, Sign In to add comment