Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- return {
- new = function(w, h, color)
- local t = {
- x = 1,
- y = 1,
- w = w,
- h = h,
- color = color ~= nil,
- fg = colors.white,
- bg = colors.black,
- buffer = {},
- setPixel = function(self, x, y, char, bg, fg)
- local col = self.buffer[x]
- local _y = y * 3 - 2
- col[_y] = char
- col[_y + 1] = bg
- col[_y + 2] = fg
- return self
- end,
- getPixel = function(self, x, y)
- local col = self.buffer[x]
- local _y = y * 3 - 2
- return {
- char = col[_y],
- bg = col[_y + 1],
- fg = col[_y + 2]
- }
- end,
- write = function(self, str)
- for i = 1, string.len(str) do
- self:setPixel(self.x, self.y, string.sub(str, i, i), self.bg, self.fg)
- self.x = self.x + 1
- end
- return self
- end,
- scroll = function(self, diff)
- local oldLines = {}
- for i = 1, self.w do
- if not oldLines[i] then oldLines[i] = {} end
- for j = 1, self.h do
- oldLines[i][j] = self:getPixel(i, j)
- end
- end
- local newLines = {}
- for i = 1, self.w do
- local oldY = i + diff
- if ((oldY >= 1) and (oldY < self.h)) then
- newLines[i] = oldLines[oldY]
- else
- newLines[i] = {}
- for j = 1, self.h do
- newLines[j] = { char = " ", bg = colors.black, fg = colors.white }
- end
- end
- end
- for i = 1, self.w do
- local col = self.buffer[i]
- for j = 1, self.h do
- col[j] = newLines[i][j].char
- col[j + 1] = newLines[i][j].bg
- col[j + 2] = newLines[i][j].fg
- end
- end
- return self
- end,
- setCursorPos = function(self, x, y)
- self.x = x
- self.y = y
- return self
- end,
- getCursorPos = function(self)
- return self.x, self.y
- end,
- getSize = function(self)
- return self.w, self.h
- end,
- isColor = function(self)
- return self.isColor
- end,
- setTextColor = function(self, c)
- self.fg = c
- return self
- end,
- setBackgroundColor = function(self, c)
- self.bg = c
- return self
- end,
- getTextColor = function(self)
- return self.fg
- end,
- getBackgroundColor = function(self)
- return self.bg
- end,
- clear = function(self)
- for i = 1, self.w do
- for j = 1, self.h do
- self:setPixel(i, j, " ", self.bg, self.fg)
- end
- end
- return self
- end,
- clearLine = function(self)
- for i = 1, self.w do
- self:setPixel(i, self.y, " ", self.bg, self.fg)
- end
- end,
- draw = function(self, target)
- for i = 1, self.w do
- for j = 1, self.h do
- local pixel = self:getPixel(i, j)
- if type(pixel.fg) == "number" then target.setTextColor(pixel.fg) end
- if type(pixel.bg) == "number" then target.setBackgroundColor(pixel.bg) end
- target.setCursorPos(i, j)
- if pixel.char then target.write(pixel.char) else target.write(" ") end
- end
- end
- target.setTextColor(self.fg)
- target.setBackgroundColor(self.bg)
- target.setCursorPos(self.x, self.y)
- return self
- end
- }
- for i = 1, w do
- if not t.buffer[i] then t.buffer[i] = {} end
- local col = t.buffer[i]
- for j = 1, h do
- local y = j * 3 - 2
- col[y] = " "
- col[y + 1] = colors.black
- col[y + 2] = colors.white
- end
- end
- return t
- end
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement