Advertisement
Guest User

menu

a guest
Sep 21st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.98 KB | None | 0 0
  1. local items = {}
  2. local width, height = term.getSize()
  3. local selected = 1
  4. local padding = 1
  5. local defaultTerm = term.current()
  6.  
  7. function addItem(name, callback, arg)
  8.   local item = {}
  9.  
  10.   item["name"] = name
  11.   item["callback"] = callback
  12.   item["arg"] = arg
  13.  
  14.   local x = 1 + padding
  15.   local y = nil
  16.  
  17.   if #items == 0 then
  18.     y = 1 + padding
  19.   else
  20.     y = 2 + (#items) * (1 + padding)
  21.   end
  22.  
  23.   local w = width - (2 * padding)
  24.   local h = 1
  25.  
  26.   item["window"] = window.create(defaultTerm, x, y, w, h, true)
  27.  
  28.   items[#items + 1] = item
  29. end
  30.  
  31. function createString(length)
  32.   local output = ""
  33.  
  34.   for i=1,length,1 do
  35.     output = output .. " "
  36.   end
  37.  
  38.   return output
  39. end
  40.  
  41. function draw()
  42.   term.setBackgroundColor(colors.blue)
  43.   term.setTextColor(colors.black)
  44.   term.clear()
  45.  
  46.   for i, v in pairs(items) do
  47.     if v["window"] ~= nil then
  48.       if i == selected then
  49.         v["window"].setBackgroundColor(colors.white)
  50.       else
  51.         v["window"].setBackgroundColor(colors.red)
  52.       end
  53.      
  54.       v["window"].setTextColor(colors.black)
  55.      
  56.       local x, y = v["window"].getSize()
  57.      
  58.       local line = createString(x)
  59.      
  60.       local textStart = (x/2) - math.ceil(#v["name"]/2)
  61.       local textStop = textStart + #v["name"]
  62.      
  63.       --v["window"].setCursorPos(1, 1)
  64.       --v["window"].write(line)
  65.      
  66.       v["window"].setCursorPos(1, 1)
  67.       v["window"].write(createString(textStart) .. v["name"] .. createString(x - textStop))
  68.      
  69.       --v["window"].setCursorPos(1, 3)
  70.       --v["window"].write(line)
  71.      
  72.       v["window"].redraw()
  73.     end
  74.   end
  75.  
  76.   return true
  77. end
  78.  
  79. function down()
  80.   if selected < #items then
  81.     selected = selected + 1
  82.     draw()
  83.    
  84.     return true
  85.   end
  86.  
  87.   return false
  88. end
  89.  
  90. function up()
  91.   if selected > 1 then
  92.     selected = selected - 1
  93.     draw()
  94.    
  95.     return true
  96.   end
  97.  
  98.   return false
  99. end
  100.  
  101. function getSelectedItem()
  102.   return items[selected]
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement