2021-08-28 15:48:27 +02:00
|
|
|
local Parent = require "birb.modules.gui.elements.parent"
|
|
|
|
local CanvasElement = Parent:extend()
|
|
|
|
|
|
|
|
function CanvasElement:new(name, x, y, w, h, r,sx,sy,ox,oy, opacity, depth)
|
|
|
|
self:initCanvas()
|
|
|
|
CanvasElement.super.new(self, name, x, y, w, h)
|
|
|
|
self.r = r or 0
|
|
|
|
self.sx, self.sy = sx or 1, sy or 1
|
|
|
|
self.ox, self.oy = self:parseOrigin(ox, w), self:parseOrigin(oy, h)
|
|
|
|
self.opacity = opacity or 1
|
|
|
|
self.depth = depth or 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasElement:initCanvas()
|
|
|
|
self.canvas = {}
|
|
|
|
self.canvas.needRedraw = true
|
|
|
|
self.canvas.texture = nil
|
|
|
|
self.canvas.isAnimated = false
|
|
|
|
self.canvas.padding = 8
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasElement:getCanvasDimensions()
|
|
|
|
return self:getDimensions()
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasElement:redraw()
|
|
|
|
local w, h = self:getDimensions()
|
|
|
|
|
|
|
|
local canvas = love.graphics.newCanvas(w + (self.canvas.padding*2), h + (self.canvas.padding*2))
|
|
|
|
love.graphics.setCanvas(canvas)
|
|
|
|
|
|
|
|
self:drawTexture()
|
|
|
|
self.canvas.needRedraw = false
|
|
|
|
love.graphics.setCanvas()
|
|
|
|
if (self.canvas.isAnimated) then
|
|
|
|
self.canvas.texture = canvas
|
|
|
|
else
|
|
|
|
local imageData = canvas:newImageData()
|
|
|
|
self.canvas.texture = love.graphics.newImage(imageData)
|
|
|
|
canvas:release()
|
|
|
|
imageData:release()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasElement:drawTexture()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasElement:parseOrigin(origin, size)
|
|
|
|
if (origin == "center") then
|
|
|
|
return size/2
|
|
|
|
elseif (origin == "end") then
|
2021-08-28 18:07:29 +02:00
|
|
|
return size
|
2021-08-28 15:48:27 +02:00
|
|
|
else
|
2021-08-28 18:07:29 +02:00
|
|
|
return origin or 0
|
2021-08-28 15:48:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function CanvasElement:draw()
|
|
|
|
love.graphics.setColor(1, 1, 1, self.opacity)
|
2021-08-28 18:07:29 +02:00
|
|
|
love.graphics.draw(self.canvas.texture, self.x - self.canvas.padding,self.y - self.canvas.padding,self.r,self.sx,self.sy,self.ox,self.oy)
|
2021-08-28 15:48:27 +02:00
|
|
|
love.graphics.setColor(1, 1, 1, 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
return CanvasElement
|