feat: use indirect rendering for menus

This commit is contained in:
Kazhnuz 2021-08-28 15:48:27 +02:00
parent 02edc4fc07
commit fa8f59cf2c
6 changed files with 87 additions and 13 deletions

View file

@ -0,0 +1,65 @@
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
return size - self.canvas.padding
else
return origin or self.canvas.padding
end
end
function CanvasElement:draw()
love.graphics.setColor(1, 1, 1, self.opacity)
love.graphics.draw(self.canvas.texture, self.x,self.y,self.r,self.sx,self.sy,self.ox,self.oy)
love.graphics.setColor(1, 1, 1, 1)
end
return CanvasElement

View file

@ -112,10 +112,10 @@ end
-- DRAW FUNCTIONS
-- Draw the menu and its content
function FlowBox:draw()
function FlowBox:drawTexture()
self.view:updateFirstSlot(self.widget:getSelected())
local widgety = self.y
local widgetx = self.x
local widgety = self.canvas.padding
local widgetx = self.canvas.padding
local listWidget = self.widget:getList(self.view.firstSlot, self.view.slotNumber)

View file

@ -247,12 +247,12 @@ end
-- DRAW FUNCTIONS
-- Draw the menu and its content
function GridBox:draw()
function GridBox:drawTexture()
for i,v in ipairs(self.slots) do
if self:haveWidget(i) then
local widgetx = self.x + (v.x * self.widgetSize.w)
local widgety = self.y + (v.y * self.widgetSize.h)
local widgetx = self.canvas.padding + (v.x * self.widgetSize.w)
local widgety = self.canvas.padding + (v.y * self.widgetSize.h)
if self.widget:getSelected() == v.widgetID and self:haveFocus() == true then
self.widget.list[v.widgetID]:drawSelected(widgetx, widgety)
else

View file

@ -77,15 +77,15 @@ end
-- DRAW FUNCTIONS
-- Draw the menu and its content
function HListBox:draw()
function HListBox:drawTexture()
self.view:updateFirstSlot(self.widget:getSelected())
local widgetx = self.x
local widgetx = self.canvas.padding
local listWidget = self.widget:getList(self.view.firstSlot, self.view.slotNumber)
for _, widget in ipairs(listWidget) do
widget:drawWidget(widgetx, self.y, self.widgetSize.w, self.h)
widget:drawWidget(widgetx, self.canvas.padding, self.widgetSize.w, self.h)
widgetx = widgetx + self.widgetSize.w
end
end

View file

@ -77,14 +77,15 @@ end
-- DRAW FUNCTIONS
-- draw the menu and the rest of content.
function ListBox:draw()
function ListBox:drawTexture()
self.view:updateFirstSlot(self.widget:getSelected())
local widgety = self.y
local widgety = self.canvas.padding
local listWidget = self.widget:getList(self.view.firstSlot, self.view.slotNumber)
for _, widget in ipairs(listWidget) do
widget:drawWidget(self.x, widgety, self.w, self.widgetSize.h)
print(self.canvas.padding, widgety)
widget:drawWidget(self.canvas.padding, widgety, self.w, self.widgetSize.h)
widgety = widgety + self.widgetSize.h
end
end

View file

@ -21,7 +21,7 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local GuiElement = require "birb.modules.gui.elements.parent"
local GuiElement = require "birb.modules.gui.elements.canvas"
local Menu = GuiElement:extend()
local MenuModel = require "birb.modules.gui.menus.model"
@ -40,6 +40,7 @@ function Menu:new(name, x, y, w, h)
self:updateWidgetSize()
self:resetSound()
self:initCanvas()
end
-- INTERACTION FUNCTIONS
@ -78,10 +79,12 @@ end
function Menu:actionAndCancel(key)
if key == "A" then
self.widget:selectedAction()
self.canvas.needRedraw = true
end
if key == "B" then
self:cancelAction()
self.canvas.needRedraw = true
end
end
@ -140,6 +143,9 @@ end
function Menu:updateElement(dt)
self:update(dt)
self.widget:update(dt)
if (self.canvas.needRedraw or self.canvas.isAnimated) then
self:redraw()
end
end
-- DRAW FUNCTIONS
@ -194,11 +200,13 @@ end
function Menu:setCursor(cursorid)
self.widget:setCursor(cursorid)
self.canvas.needRedraw = true
end
function Menu:moveCursor(new_selected)
self:playNavigationSound()
self.widget:moveCursorAbsolute(new_selected)
self.canvas.needRedraw = true
end
-- SOUND FUNCTION