Advertisement
oli414

Pixel Canvas API

Dec 12th, 2015
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.55 KB | None | 0 0
  1. Character = {}
  2. Character.__index = Character
  3.  
  4. function Character.create()
  5.     local self = {}
  6.     setmetatable(self, Character)
  7.    
  8.     self.textColor = colors.white
  9.     self.backgroundColor = colors.black
  10.     self.pixelMode = false
  11.     self.character = " "
  12.     self.pixel = {{false, false, false},{false, false, false}}
  13.     self.invert = false
  14.     return self
  15. end
  16.  
  17. function Character:update()
  18.     if self.pixelMode then
  19.         local char = 128
  20.         if not self.pixel[2][3] then
  21.             char = char + (self.pixel[1][1] and 1 or 0)
  22.             char = char + (self.pixel[2][1] and 2 or 0)
  23.             char = char + (self.pixel[1][2] and 4 or 0)
  24.             char = char + (self.pixel[2][2] and 8 or 0)
  25.             char = char + (self.pixel[1][3] and 16 or 0)
  26.             self.invert = false
  27.         else
  28.             char = char + (self.pixel[1][1] and 0 or 1)
  29.             char = char + (self.pixel[2][1] and 0 or 2)
  30.             char = char + (self.pixel[1][2] and 0 or 4)
  31.             char = char + (self.pixel[2][2] and 0 or 8)
  32.             char = char + (self.pixel[1][3] and 0 or 16)
  33.             self.invert = true
  34.         end
  35.         self.character = string.char(char)
  36.     end
  37. end
  38.  
  39. function Character:draw()
  40.     if not self.invert then
  41.         term.setBackgroundColor(self.backgroundColor)
  42.         term.setTextColor(self.textColor)
  43.     else
  44.         term.setBackgroundColor(self.textColor)
  45.         term.setTextColor(self.backgroundColor)
  46.     end
  47.     term.write(self.character)
  48. end
  49.  
  50. Canvas = {}
  51. Canvas.__index = Canvas
  52.  
  53. function Canvas.create()
  54.     local self = {}
  55.     setmetatable (self, {__index=Canvas})
  56.    
  57.     self.x = 1
  58.     self.y = 1
  59.     self.character = {}
  60.     local w, h = term.getSize()
  61.     self:setSize(w, h)
  62.    
  63.     return self
  64. end
  65.  
  66. function Canvas:setSize(width, height)
  67.     self.__width = width
  68.     self.__height = height
  69.     self.character = {}
  70.     for i=1, self.__width do
  71.         self.character[i] = {}
  72.         for j=1, self.__height do
  73.             self.character[i][j] = Character.create()
  74.         end
  75.     end
  76. end
  77.  
  78. function Canvas:draw()
  79.     for j=self.y,(self.__height + self.y - 1) do
  80.         term.setCursorPos(self.x, j)
  81.         for i=self.x,(self.__width + self.x - 1) do
  82.             self.character[i][j]:draw()
  83.         end
  84.     end
  85. end
  86.  
  87. function Canvas:setPixel(x, y, value)
  88.     x = x - 1
  89.     y = y - 1
  90.     local charX = math.floor(x / 2)
  91.     local charY = math.floor(y / 3)
  92.     pixelX = x - charX * 2
  93.     pixelY = y - charY * 3
  94.     charX = charX + 1
  95.     charY = charY + 1
  96.     self.character[charX][charY].pixelMode = true
  97.     self.character[charX][charY].pixel[pixelX + 1][pixelY + 1] = value;
  98.     self.character[charX][charY]:update()
  99. end
  100.  
  101. function Canvas:setCharacter(x, y, char)
  102.     self.character[charX][charY].invert = false
  103.     self.character[charX][charY].pixelMode = false
  104.     self.character[charX][charY].character = char;
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement