KaoSDlanor

MOUSE OPERATED MENU FUNCTION USAGE EXAMPLE

Nov 21st, 2012
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.11 KB | None | 0 0
  1. function menu(t,x,y,ht)
  2.     local size={term.getSize()}
  3.     ht=math.min(ht or 0,#t)
  4.     local scrolled=0
  5.     local scroll=false
  6.     if #t>ht then scroll=true end
  7.     local max=0
  8.     for i=1,#t do
  9.         max=math.max(max,#t[i])
  10.     end
  11.    
  12.     while true do
  13.         for i=1,(scroll and ht or #t) do
  14.             term.setCursorPos(x,y+i-1)
  15.             write(string.rep(' ',max))
  16.         end
  17.         for i=1,(scroll and ht or #t) do
  18.             term.setCursorPos((scrolled>0 and i==1 and math.floor(max/2+x) or scrolled+ht<#t and i==ht and scroll and math.floor(max/2+x) or x),y+i-1)
  19.             write(scrolled>0 and i==1 and '^' or scrolled+ht<#t and i==ht and scroll and 'v' or t[i+scrolled])
  20.         end
  21.         --if scroll then term.setCursorPos(x,y+ht-1) write('v') end
  22.         local input={os.pullEvent()}
  23.         if input[1]=='mouse_click' and input[2]==1 and input[3]>=x and input[3]<=x+max-1 and input[4]>=y and input[4]<=y+ht-1 then
  24.             if scrolled>0 and input[4]==y then
  25.                 scrolled=scrolled-1
  26.             elseif scrolled+ht<#t and scroll and input[4]==y+ht-1 then
  27.                 scrolled=scrolled+1
  28.             else
  29.                 return t[input[4]+scrolled-y+1]
  30.             end
  31.         end
  32.     end
  33. end
  34.  
  35. term.clear()
  36. print(menu({'111','222','333','444','555'},2,2,6))
Advertisement
Add Comment
Please, Sign In to add comment