anonimo182

Menu Drawer

Nov 30th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.62 KB | None | 0 0
  1. function drawMenu(tMenu, xpos, ypos, curSel)
  2.     if xpos == true then
  3.         centered = true
  4.         else
  5.         centered = false
  6.     end
  7.     if curSel ~= nil or curSel~= "" then
  8.         sel = 1
  9.     else
  10.         sel = curSel
  11.     end
  12.     tPlaces = {}
  13.     while true do
  14.     for i=1, #tMenu do
  15.         iSel = i-1
  16.         local w, h = term.getSize()
  17.         table.remove(tPlaces, i)
  18.         if i == sel then
  19.         if centered == false then
  20.             term.setCursorPos(xpos, ypos+iSel)
  21.             term.clearLine()
  22.             table.insert(tPlaces, i, term.getCursorPos()+2)
  23.             write("[ "..tMenu[i].." ]")
  24.         else
  25.             term.setCursorPos(w/2-#tMenu[i]/2-2, ypos+iSel)
  26.             term.clearLine()
  27.             table.insert(tPlaces, i, term.getCursorPos()+2)
  28.             write("[ "..tMenu[i].." ]")
  29.         end
  30.         else
  31.         if centered == false then
  32.             term.setCursorPos(xpos, ypos+iSel)
  33.             term.clearLine()
  34.             table.insert(tPlaces, i, term.getCursorPos())
  35.             write(tMenu[i])
  36.         else
  37.             term.setCursorPos(w/2-#tMenu[i]/2, ypos+iSel)
  38.             term.clearLine()
  39.             table.insert(tPlaces, i, term.getCursorPos())
  40.             write(tMenu[i])
  41.         end
  42.         end
  43.     end
  44.     local event, key, x, y = os.pullEvent()
  45.     if event == "key" then
  46.     if key == 200 and sel > 1 and sel <= #tMenu then sel = sel-1
  47.     elseif key == 208 and sel >= 1 and sel < #tMenu then sel = sel+1
  48.     elseif key == 208 and sel == #tMenu then sel = 1
  49.     elseif key == 200 and sel == 1 then sel = #tMenu
  50.     end
  51.     for v = 1, #tMenu do
  52.         if key == 28 and sel == v then
  53.           return tMenu[v], sel
  54.         end
  55.     end
  56.     elseif event == "mouse_click" then
  57.         for s = 1, #tMenu do
  58.             iSel = s-1
  59.             if s == sel then
  60.             if x >= tPlaces[s] and x < tPlaces[s]+#tMenu[s] and y == ypos+iSel then
  61.                 return tMenu[s], s
  62.             end
  63.         end
  64.     end
  65.     end
  66.     end
  67. end
Advertisement
Add Comment
Please, Sign In to add comment