Guest User

Untitled

a guest
Nov 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. -- Menu API
  2. -- by RichardG867
  3.  
  4. local menu = {
  5.     display = function(self)
  6.         selCharLength = #self.selChar
  7.         baseY = 0
  8.         maxX, maxY = term.getSize()
  9.        
  10.         term.clear()
  11.         term.setCursorPos(1,1)
  12.        
  13.         if self.title ~= nil then
  14.             baseY = #self.title + 1
  15.            
  16.             for i = 1, #self.title do
  17.                 print(self.title[i]:sub(1, maxX-1))
  18.             end
  19.         end
  20.        
  21.         columns = math.ceil(#self.entries / (maxY - baseY))
  22.         columnWidth = math.floor(maxX / columns)
  23.         curColumn = 1
  24.         entryOffset = {}
  25.         entryRow = {}
  26.        
  27.         j = 1
  28.         for i = 1, #self.entries do
  29.             if baseY + j > maxY then
  30.                 curColumn = curColumn + 1
  31.                 j = 1
  32.             end
  33.        
  34.             entryOffset[i] = (curColumn - 1) * columnWidth
  35.             entryRow[i] = j
  36.             if self.entries[i] ~= "" then
  37.                 term.setCursorPos(entryOffset[i] + selCharLength + 1, baseY + j)
  38.                 term.write(self.entries[i]:sub(1, columnWidth-selCharLength-1))
  39.             end
  40.             j = j + 1
  41.         end
  42.        
  43.         curEntry = 1
  44.         while curEntry > 0 and self.entries[curEntry] == "" do
  45.             curEntry = curEntry + 1
  46.         end
  47.         if curEntry > #self.entries then error("Empty entry list") end
  48.        
  49.         while true do      
  50.             term.setCursorPos(entryOffset[curEntry] + 1, baseY + entryRow[curEntry])
  51.             term.write(self.selChar)
  52.            
  53.             event, key = os.pullEvent("key")
  54.             clearSel = 0
  55.            
  56.             if key == 200 then
  57.                 clearSel = curEntry
  58.                 curEntry = curEntry - 1
  59.                
  60.                 while curEntry > 0 and self.entries[curEntry] == "" do
  61.                     curEntry = curEntry - 1
  62.                 end
  63.                 if curEntry < 1 then curEntry = clearSel end
  64.             elseif key == 208 then
  65.                 clearSel = curEntry
  66.                 curEntry = curEntry + 1
  67.                
  68.                 while curEntry <= #self.entries and self.entries[curEntry] == "" do
  69.                     curEntry = curEntry + 1
  70.                 end
  71.                 if curEntry > #self.entries then curEntry = clearSel end
  72.             elseif key == 28 then
  73.                 term.clear()
  74.                 term.setCursorPos(1,1)
  75.                 return curEntry
  76.             end
  77.            
  78.             if clearSel > 0 then
  79.                 term.setCursorPos(entryOffset[clearSel] + 1, baseY + entryRow[clearSel])
  80.                 term.write(string.rep(" ",selCharLength))
  81.             end
  82.         end
  83.     end,
  84.     setSelChar = function(self, char)
  85.         self.selChar = char
  86.     end,
  87.     tostring = function(self)
  88.         return "Menu["..#self.entries.."]"
  89.     end
  90. }
  91.  
  92. local metatable = {
  93.     __index = menu,
  94.     __tostring = menu.tostring
  95. }
  96.  
  97. function new(title, entries)
  98.     if entries == nil or #entries < 1 then
  99.         error("Empty entry list")
  100.     end
  101.  
  102.     local newmenu = {
  103.         title = title or nil,
  104.         entries = entries,
  105.         selChar = "> "
  106.     }
  107.    
  108.     setmetatable(newmenu, metatable)
  109.     return newmenu
  110. end
Add Comment
Please, Sign In to add comment