Sp1rit

window

Jan 9th, 2013
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. local window = {
  2.     getTitle = function(self)
  3.         return self.title
  4.     end,
  5.     setTitle = function(self, text)
  6.         if text ~= nil and string.len(text) > 0 then
  7.             self.title = text
  8.         else
  9.             self.title = ""
  10.         end
  11.     end,
  12.     setLine = function(self, line, text, color)
  13.         if text ~= nil and string.len(text) > 0 then
  14.             self.content[line] = {}
  15.             self.content[line]["text"] = text
  16.             self.content[line]["color"] = color or colors.white
  17.         end
  18.     end,
  19.     getLine = function(self, line)
  20.         return self.content[line]
  21.     end,
  22.     clearLine = function(self, line)
  23.         table.remove(self.content, line)
  24.     end,
  25.     setBackgroundColor = function(self, color)
  26.         if color ~= nil and tonumber(color) > 0 then
  27.             self.backgroundColor = color
  28.         else
  29.             self.backgroundColor = colors.black
  30.         end
  31.     end,
  32.     setBorderColor = function(self, color)
  33.         if color ~= nil and tonumber(color) > 0 then
  34.             self.borderColor = color
  35.         else
  36.             self.borderColor = colors.white
  37.         end
  38.     end,
  39.     setTitleColor = function(self, color)
  40.         if color ~= nil and tonumber(color) > 0 then
  41.             self.titleColor = color
  42.         else
  43.             self.titleColor = colors.white
  44.         end
  45.     end,
  46.     draw = function(self)
  47.         local drawTitle = (string.len(self.title) > 0)
  48.         local startPos = vector.new(self.x, self.y)
  49.         local endPos = startPos:add(vector.new(self.width - 1, self.height - 1))
  50.        
  51.         term.setBackgroundColor(self.backgroundColor)
  52.        
  53.         for y = startPos.y, endPos.y do
  54.             for x = startPos.x, endPos.x do
  55.                 term.setCursorPos(x, y)
  56.                
  57.                 if x == startPos.x or x == endPos.x then
  58.                     term.setTextColor(self.borderColor)
  59.                    
  60.                     if y == startPos.y then
  61.                         term.write(",")
  62.                     elseif y == endPos.y or y == startPos.y + 2 and drawTitle then
  63.                         term.write("+")
  64.                     else
  65.                         term.write("|")
  66.                     end
  67.                 else
  68.                     if y == startPos.y or y == endPos.y or y == startPos.y + 2 and drawTitle then
  69.                         term.setTextColor(self.borderColor)
  70.                         term.write("-")
  71.                     else
  72.                         term.write(" ")
  73.                     end
  74.                 end
  75.             end
  76.         end
  77.        
  78.         local textPos = startPos:add(vector.new(2, (drawTitle and 3 or 1)))
  79.         local lineCount = self.height - (drawTitle and 4 or 2)
  80.        
  81.         if drawTitle then
  82.             term.setCursorPos(textPos.x, textPos.y - 2)
  83.             term.setTextColor(self.titleColor)
  84.             term.write(self.title)
  85.         end
  86.        
  87.         term.setTextColor(colors.white)
  88.         for i = 1, lineCount do
  89.             if self.content[i] ~= nil then
  90.                 term.setCursorPos(textPos.x, textPos.y + i - 1)
  91.                 term.setTextColor(self.content[i].color)
  92.                 term.write(self.content[i].text)
  93.             end
  94.         end
  95.        
  96.         term.setCursorPos(endPos.x, endPos.y)
  97.         term.setBackgroundColor(colors.black)
  98.         term.setTextColor(colors.white)
  99.     end
  100. }
  101.  
  102. local wmetatable = {
  103.     __index = window
  104. }
  105.  
  106. function new(x, y, width, height)
  107.     local w = {
  108.         x = x or 0,
  109.         y = y or 0,
  110.         width = width or 0,
  111.         height = height or 0,
  112.         title = "",
  113.         content = {},
  114.         backgroundColor = colors.black,
  115.         borderColor = colors.white,
  116.         titleColor = colors.white
  117.     }
  118.     setmetatable( w, wmetatable )
  119.     return w
  120. end
Advertisement
Add Comment
Please, Sign In to add comment