KaoSDlanor

Menu creating API - KaoS

Sep 1st, 2012
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1. --[[SYNTAX:
  2. apiname.generate(table, separator, x1, y1, x2, y2, delim1, delim2)
  3.  
  4. an example of its use can be found at http://pastebin.com/LTBY9xcW
  5.  
  6. table - a table of all the options selectable and what to do if each option is selected
  7. the format of table is:
  8. table[y][x][1]='displayed option'
  9. table[y][x][2]=another table where the first value is the function to call (exclude the brackets after it and do not make it a string, it is a function) and all values after it are passed to the function as arguments
  10.  
  11. separator - the x distance between the first character of each option (default is 10)
  12.  
  13. x1,y1,x2 and y2 are used to generate a box in which the menu is generated
  14. x1 and y1 are the co-ordinates of the top left corner
  15. x2 and y2 are the co-ordinates of the bottom right corner
  16.  
  17. delim1 and 2 are the character to display before and after the selected option (default is '>' and '<')
  18. ]]
  19.  
  20. function generate(input,dist,offsetx,offsety,limx,limy,delim1,delim2)
  21.     --[Required functions]
  22.     local function pullfirst(input)
  23.         local first=input[1]
  24.         local temp={}
  25.         for k,v in pairs(input) do
  26.             if k~=1 then
  27.                 temp[#temp+1]=v
  28.             end
  29.         end
  30.         return first,temp
  31.     end
  32.  
  33.     local function cleararea(...)
  34.         local pos={term.getCursorPos()}
  35.         local args={...}
  36.         for y=args[2], args[2]+args[4]-2 do
  37.             term.setCursorPos(args[1],y)
  38.             write(string.rep(' ',args[3]-args[1]+1))
  39.         end
  40.         term.setCursorPos(pos[1],pos[2])
  41.     end
  42.    
  43.     --[Gathering initial information and declaring variables]
  44.     local scrolledx=0
  45.     local scrolledy=0
  46.     local selectedx=1
  47.     local selectedy=1
  48.     local lastx=0
  49.     dist=dist or 10
  50.     local columnmaxlen={}
  51.     local maxlen=0
  52.     for b,y in pairs(input) do
  53.         for a,x in pairs(y) do
  54.             if columnmaxlen[a] or 0<string.len(x[1]) then
  55.                 columnmaxlen[a]=string.len(x[1])
  56.             end
  57.         end
  58.     end
  59.     for a,b in pairs(columnmaxlen) do
  60.         if string.len(b)>maxlen then
  61.             maxlen=string.len(b)
  62.         end
  63.     end
  64.    
  65.     while true do
  66.         --[Render the menu]
  67.         cleararea(offsetx,offsety,limx,limy)
  68.         lastx=0
  69.         for b,y in pairs(input) do
  70.             for a,x in pairs(y) do
  71.                 currentlen=dist*(a-scrolledx-1)+columnmaxlen[a]
  72.                
  73.                 if b>scrolledy and b<=limy-offsety+scrolledy+1 and a>scrolledx and currentlen<limx-offsetx+1 then
  74.                     lastx=(lastx<=a and a) or lastx
  75.                     if a==selectedx and b==selectedy then
  76.                         term.setCursorPos(offsetx+a*dist-scrolledx*dist-dist,offsety+b-1-scrolledy)
  77.                         write((delim1 or '>')..x[1]..(delim2 or delim1 or '<'))
  78.                     else
  79.                         term.setCursorPos(offsetx+1+a*dist-scrolledx*dist-dist,offsety+b-1-scrolledy)
  80.                         write(x[1])
  81.                     end
  82.                 end
  83.             end
  84.         end
  85.         print('')
  86.        
  87.         --[Detect keypresses for interface and adjust variables accordingly]
  88.         while true do
  89.             local event,key=os.pullEvent('key')
  90.             if key==203 and selectedx>1 then
  91.                 selectedx=selectedx-1
  92.                 if selectedx<=scrolledx then
  93.                     scrolledx=scrolledx-1
  94.                 end
  95.                 break
  96.             elseif key==205 and selectedx<#input[selectedy] then
  97.                 selectedx=selectedx+1
  98.                 if selectedx>lastx then
  99.                     scrolledx=scrolledx+1
  100.                 end
  101.                 break
  102.             elseif key==200 and selectedy>1 then
  103.                 selectedy=selectedy-1
  104.                 if selectedy<=scrolledy then
  105.                 scrolledy=scrolledy-1
  106.                 end
  107.                 break
  108.             elseif key==208 and selectedy<#input then
  109.                 selectedy=selectedy+1
  110.                 if selectedy>limy-offsety+scrolledy+1 then
  111.                     scrolledy=scrolledy+1
  112.                 end
  113.                 break
  114.             elseif key==28 then
  115.                 local cmd={pullfirst(input[selectedy][selectedx][2])}
  116.                 return cmd[1](unpack(cmd[2]))
  117.             end
  118.         end
  119.         if selectedx>#input[selectedy] then
  120.             selectedx=#input[selectedy]
  121.         end
  122.     end
  123. end
Advertisement
Add Comment
Please, Sign In to add comment