feat: use indirect rendering for menus
This commit is contained in:
parent
02edc4fc07
commit
fa8f59cf2c
6 changed files with 87 additions and 13 deletions
65
sonic-radiance.love/birb/modules/gui/elements/canvas.lua
Normal file
65
sonic-radiance.love/birb/modules/gui/elements/canvas.lua
Normal 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
|
|
@ -112,10 +112,10 @@ end
|
||||||
-- DRAW FUNCTIONS
|
-- DRAW FUNCTIONS
|
||||||
-- Draw the menu and its content
|
-- Draw the menu and its content
|
||||||
|
|
||||||
function FlowBox:draw()
|
function FlowBox:drawTexture()
|
||||||
self.view:updateFirstSlot(self.widget:getSelected())
|
self.view:updateFirstSlot(self.widget:getSelected())
|
||||||
local widgety = self.y
|
local widgety = self.canvas.padding
|
||||||
local widgetx = self.x
|
local widgetx = self.canvas.padding
|
||||||
|
|
||||||
local listWidget = self.widget:getList(self.view.firstSlot, self.view.slotNumber)
|
local listWidget = self.widget:getList(self.view.firstSlot, self.view.slotNumber)
|
||||||
|
|
||||||
|
|
|
@ -247,12 +247,12 @@ end
|
||||||
-- DRAW FUNCTIONS
|
-- DRAW FUNCTIONS
|
||||||
-- Draw the menu and its content
|
-- Draw the menu and its content
|
||||||
|
|
||||||
function GridBox:draw()
|
function GridBox:drawTexture()
|
||||||
|
|
||||||
for i,v in ipairs(self.slots) do
|
for i,v in ipairs(self.slots) do
|
||||||
if self:haveWidget(i) then
|
if self:haveWidget(i) then
|
||||||
local widgetx = self.x + (v.x * self.widgetSize.w)
|
local widgetx = self.canvas.padding + (v.x * self.widgetSize.w)
|
||||||
local widgety = self.y + (v.y * self.widgetSize.h)
|
local widgety = self.canvas.padding + (v.y * self.widgetSize.h)
|
||||||
if self.widget:getSelected() == v.widgetID and self:haveFocus() == true then
|
if self.widget:getSelected() == v.widgetID and self:haveFocus() == true then
|
||||||
self.widget.list[v.widgetID]:drawSelected(widgetx, widgety)
|
self.widget.list[v.widgetID]:drawSelected(widgetx, widgety)
|
||||||
else
|
else
|
||||||
|
|
|
@ -77,15 +77,15 @@ end
|
||||||
-- DRAW FUNCTIONS
|
-- DRAW FUNCTIONS
|
||||||
-- Draw the menu and its content
|
-- Draw the menu and its content
|
||||||
|
|
||||||
function HListBox:draw()
|
function HListBox:drawTexture()
|
||||||
self.view:updateFirstSlot(self.widget:getSelected())
|
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)
|
local listWidget = self.widget:getList(self.view.firstSlot, self.view.slotNumber)
|
||||||
|
|
||||||
for _, widget in ipairs(listWidget) do
|
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
|
widgetx = widgetx + self.widgetSize.w
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -77,14 +77,15 @@ end
|
||||||
-- DRAW FUNCTIONS
|
-- DRAW FUNCTIONS
|
||||||
-- draw the menu and the rest of content.
|
-- draw the menu and the rest of content.
|
||||||
|
|
||||||
function ListBox:draw()
|
function ListBox:drawTexture()
|
||||||
self.view:updateFirstSlot(self.widget:getSelected())
|
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)
|
local listWidget = self.widget:getList(self.view.firstSlot, self.view.slotNumber)
|
||||||
|
|
||||||
for _, widget in ipairs(listWidget) do
|
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
|
widgety = widgety + self.widgetSize.h
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
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 Menu = GuiElement:extend()
|
||||||
local MenuModel = require "birb.modules.gui.menus.model"
|
local MenuModel = require "birb.modules.gui.menus.model"
|
||||||
|
@ -40,6 +40,7 @@ function Menu:new(name, x, y, w, h)
|
||||||
self:updateWidgetSize()
|
self:updateWidgetSize()
|
||||||
|
|
||||||
self:resetSound()
|
self:resetSound()
|
||||||
|
self:initCanvas()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- INTERACTION FUNCTIONS
|
-- INTERACTION FUNCTIONS
|
||||||
|
@ -78,10 +79,12 @@ end
|
||||||
function Menu:actionAndCancel(key)
|
function Menu:actionAndCancel(key)
|
||||||
if key == "A" then
|
if key == "A" then
|
||||||
self.widget:selectedAction()
|
self.widget:selectedAction()
|
||||||
|
self.canvas.needRedraw = true
|
||||||
end
|
end
|
||||||
|
|
||||||
if key == "B" then
|
if key == "B" then
|
||||||
self:cancelAction()
|
self:cancelAction()
|
||||||
|
self.canvas.needRedraw = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -140,6 +143,9 @@ end
|
||||||
function Menu:updateElement(dt)
|
function Menu:updateElement(dt)
|
||||||
self:update(dt)
|
self:update(dt)
|
||||||
self.widget:update(dt)
|
self.widget:update(dt)
|
||||||
|
if (self.canvas.needRedraw or self.canvas.isAnimated) then
|
||||||
|
self:redraw()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- DRAW FUNCTIONS
|
-- DRAW FUNCTIONS
|
||||||
|
@ -194,11 +200,13 @@ end
|
||||||
|
|
||||||
function Menu:setCursor(cursorid)
|
function Menu:setCursor(cursorid)
|
||||||
self.widget:setCursor(cursorid)
|
self.widget:setCursor(cursorid)
|
||||||
|
self.canvas.needRedraw = true
|
||||||
end
|
end
|
||||||
|
|
||||||
function Menu:moveCursor(new_selected)
|
function Menu:moveCursor(new_selected)
|
||||||
self:playNavigationSound()
|
self:playNavigationSound()
|
||||||
self.widget:moveCursorAbsolute(new_selected)
|
self.widget:moveCursorAbsolute(new_selected)
|
||||||
|
self.canvas.needRedraw = true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- SOUND FUNCTION
|
-- SOUND FUNCTION
|
||||||
|
|
Loading…
Reference in a new issue