Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

Take a look at this canvas.lua module, canvas:draw() function specifically: [code] Now…

  1. User

    Take a look at this canvas.lua module, canvas:draw() function specifically:

    ---@class char
    ---@field [1] string character
    ---@field [2] color foreground
    ---@field [3] color background
    ---@field isEmpty boolean
    
    local canvas = {}
    
    ---@alias layer char[][]
    
    ---@type layer[]
    canvas.layers = {}
    canvas.layerCount = 1
    canvas.width = 0
    canvas.height = 0
    canvas.showWidth = 16
    canvas.showHeight = 16
    canvas.showX = 1
    canvas.showY = 1
    canvas.offsetX = 2
    canvas.offsetY = 2
    canvas.showAll = true
    
    -- 1 - default layer
    canvas.activeLayer = 1
    
    ---@param symbol string
    ---@param fcol integer
    ---@param bcol integer
    ---@param empty? boolean
    ---@return char
    local function makeChar(symbol, fcol, bcol, empty)
        empty = empty or false
        return {symbol, fcol, bcol, isEmpty = empty}
    end
    
    local empty_char = makeChar(" ", colors.white, colors.black, true)
    local back_char = makeChar(string.char(164), colors.gray, colors.black, true)
    local bound_char = makeChar(string.char(164), colors.red, colors.gray, true)
    
    ---@param x number
    ---@param y number
    ---@return integer, integer
    local function inBounds(x, y)
        return math.min(math.max(math.floor(x), 1), canvas.width), math.min(math.max(math.floor(y), 1), canvas.height)
    end
    
    --- Initializes the canvas
    ---@param width integer
    ---@param height integer
    ---@param layers? integer
    function canvas:init(width, height, layers)
        layers = layers or 1
        for l = 1, layers do
            self.layers[l] = {}
            for y = 1, height do
                self.layers[l][y] = {}
                for x = 1, width do
                    self.layers[l][y][x] = empty_char
                end
            end
        end
        self.showWidth = width
        self.showHeight = height
        self.width = width
        self.height = height
        self.layerCount = layers
    end
    
    ---@param x integer
    ---@param y integer
    ---@param char char
    ---@param layer? integer
    function canvas:setChar(x, y, char, layer)
        layer = layer or self.activeLayer
        x, y = inBounds(x, y)
        self.layers[layer][y][x] = makeChar(char[1], char[2], char[3])
    end
    
    ---@param x integer
    ---@param y integer
    ---@param symbol string
    ---@param fcol color
    ---@param bcol color
    ---@param layer? integer
    function canvas:compChar(x, y, symbol, fcol, bcol, layer)
        layer = layer or self.activeLayer
        x, y = inBounds(x, y)
        self.layers[layer][y][x] = makeChar(symbol, fcol, bcol)
    end
    
    function canvas:addLayer()
        self.layers[#self.layers+1] = {}
        for y = 1, self.height do
            self.layers[#self.layers][y] = {}
            for x = 1, self.width do
                self.layers[#self.layers][y][x] = empty_char
            end
        end
        self.layerCount = #self.layers
    end
    
    ---@param pos? integer
    function canvas:removeLayer(pos)
        if self.layerCount == 1 then
            return
        end
        table.remove(self.layers, pos)
        self.layerCount = #self.layers
    end
    
    
    ---@param x integer
    ---@param y integer
    ---@param layer? integer
    ---@return char
    function canvas:getChar(x, y, layer)
        layer = layer or self.activeLayer
        x, y = inBounds(x, y)
        return self.layers[layer][y][x]
    end
    
    ---@param x integer
    ---@param y integer
    ---@param layer? integer
    function canvas:eraseChar(x, y, layer)
        layer = layer or self.activeLayer
        x, y = inBounds(x, y)
        self.layers[layer][y][x] = empty_char
    end
    
    ---@param layer? integer
    function canvas:clearLayer(layer)
        layer = layer or self.activeLayer
        for y = 1, self.height do
            for x = 1, self.width do
                self.layers[layer][y][x] = empty_char
            end
        end
    end
    
    function canvas:clearAll()
        for l = 1, self.layerCount do
            for y = 1, self.height do
                for x = 1, self.width do
                    self.layers[l][y][x] = empty_char
                end
            end
        end
    end
    
    function canvas:draw()
        -- draw the bounding box
        for y = self.offsetY-1, self.showHeight+2 do
            betterTerm.setCursorPos(self.offsetX-1, y)
            betterTerm.blit(bound_char[1]:rep(self.showWidth+2), ToHex(bound_char[2]):rep(self.showWidth+2), ToHex(bound_char[3]):rep(self.showWidth+2))
        end
        for y = self.showY, self.showHeight do
            local textstr = ""
            local forestr = ""
            local backstr = ""
            for x = self.showX, self.showWidth do
                ---@type char
                local char
                for l = self.layerCount, 1, -1 do
                    char = self.layers[l][y][x]
                    if char.isEmpty == false then break end
                end
                if char.isEmpty == true then char = back_char end
                textstr = textstr..char[1]
                forestr = forestr..ToHex(char[2])
                backstr = backstr..ToHex(char[3])
            end
            betterTerm.setCursorPos(self.offsetX, self.offsetY+y-self.showY)
            betterTerm.blit(textstr, forestr, backstr)
        end
    end
    
    return canvas
    

    Now rewrite a function so that if canvas.showAll is false, then it would only draw an active layer.

  2. Assistant

    Replace the canvas:draw() function with the following code:

    function canvas:draw()
        -- draw the bounding box
        for y = self.offsetY-1, self.showHeight+2 do
            betterTerm.setCursorPos(self.offsetX-1, y)
            betterTerm.blit(bound_char[1]:rep(self.showWidth+2), ToHex(bound_char[2]):rep(self.showWidth+2), ToHex(bound_char[3]):rep(self.showWidth+2))
        end
        for y = self.showY, self.showHeight do
            local textstr = ""
            local forestr = ""
            local backstr = ""
            for x = self.showX, self.showWidth do
                ---@type char
                local char
                if self.showAll then
                    for l = self.layerCount, 1, -1 do
                        char = self.layers[l][y][x]
                        if char.isEmpty == false then break end
                    end
                else
                    char = self.layers[self.activeLayer][y][x]
                end
                if char.isEmpty == true then char = back_char end
                textstr = textstr..char[1]
                forestr = forestr..ToHex(char[2])
                backstr = backstr..ToHex(char[3])
            end
            betterTerm.setCursorPos(self.offsetX, self.offsetY+y-self.showY)
            betterTerm.blit(textstr, forestr, backstr)
        end
    end
    

    This modified canvas:draw() function checks if canvas.showAll is set to false. If it is, the function only draws the active layer (self.layers[self.activeLayer][y][x]). Otherwise, it continues to draw all layers as before.