Advertisement
MinoCraft72

Easy Menu

Oct 8th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.25 KB | None | 0 0
  1. -- easyMenu by pingoleon60
  2. -- split function by Egor Skriptunoff
  3.  
  4. function split(str, max_line_length)
  5.  local lines = { }
  6.  local line
  7.  str:gsub('(%s*)(%S+)',
  8.   function(spc, word)
  9.    if not line or string.len(line) + string.len(spc) + string.len(word) > max_line_length then
  10.     table.insert(lines, line)
  11.     line = word
  12.    else
  13.     line = line..spc..word
  14.    end
  15.   end
  16.  )
  17.  table.insert(lines, line)
  18.  return lines
  19. end
  20.  
  21. renderBox = function(x, y, x2, name, text)
  22.  term.setCursorPos(x, y)
  23.  write("+-"..name)
  24.  for i = 1, x2 - x - 2 - string.len(name) do
  25.   write("-")
  26.  end
  27.  write("+")
  28.  term.setCursorPos(x, y+1)
  29.  term.write("|")
  30.  term.setCursorPos(x2-1, y+1)
  31.  term.write("|")
  32.  local nowY = y + 2
  33.    for _, line in ipairs(split(text, x2 - x - 4)) do
  34.   term.setCursorPos(x, nowY)
  35.   write("| ")
  36.   write(line)
  37.   term.setCursorPos(x2-1, nowY)
  38.   write("|")
  39.   nowY = nowY + 1
  40.  end
  41.  term.setCursorPos(x, nowY)
  42.  term.write("|")
  43.  term.setCursorPos(x2-1, nowY)
  44.  term.write("|")
  45.  nowY = nowY + 1
  46.  term.setCursorPos(x, nowY)
  47.  term.write("+")
  48.  for i = 1, x2 - x - 1 do
  49.   term.write("-")
  50.  end
  51.  term.write("+")
  52. end
  53.  
  54. --Objects
  55. _easyMenuObjectDontTouch = { }
  56. _easyMenuObjectDontTouch.x = 1
  57. _easyMenuObjectDontTouch.y = 1
  58. _easyMenuObjectDontTouch.weight = 20
  59. _easyMenuObjectDontTouch.name = "easyMenu"
  60. _easyMenuObjectDontTouch.message = "Customize the message with easyMenu.setMessage !"
  61. _easyMenuObjectDontTouch.render = false
  62.  
  63. function setMessage(message)
  64.  _easyMenuObjectDontTouch.message = message
  65.  renderBoxObject()  
  66. end
  67.  
  68. function setCoords(x, y)
  69.  _easyMenuObjectDontTouch.x = x
  70.  _easyMenuObjectDontTouch.y = y
  71.  renderBoxObject()
  72. end
  73.  
  74. function setWeight(weight)
  75.  _easyMenuObjectDontTouch.weight = weight
  76.  renderBoxObject()
  77. end
  78.  
  79. function setName(name)
  80.  _easyMenuObjectDontTouch.name = name
  81.  renderBoxObject()
  82. end
  83.  
  84. function setRender(boolean)
  85.  if type(boolean) == "boolean" then
  86.   _easyMenuObjectDontTouch.render = boolean
  87.   renderBoxObject()
  88.  end
  89. end
  90.  
  91. function renderBoxObject()
  92.  if _easyMenuObjectDontTouch.render then
  93.   renderBox(_easyMenuObjectDontTouch.x, _easyMenuObjectDontTouch.y, _easyMenuObjectDontTouch.x + _easyMenuObjectDontTouch.weight, _easyMenuObjectDontTouch.name, _easyMenuObjectDontTouch.message)
  94.  end
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement