From fa8f59cf2c37ab8f0a733ba8beb4dcb986036c8a Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 28 Aug 2021 15:48:27 +0200 Subject: [PATCH] feat: use indirect rendering for menus --- .../birb/modules/gui/elements/canvas.lua | 65 +++++++++++++++++++ .../birb/modules/gui/menus/flowbox.lua | 6 +- .../birb/modules/gui/menus/grid.lua | 6 +- .../birb/modules/gui/menus/hlistbox.lua | 6 +- .../birb/modules/gui/menus/listbox.lua | 7 +- .../birb/modules/gui/menus/parent.lua | 10 ++- 6 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 sonic-radiance.love/birb/modules/gui/elements/canvas.lua diff --git a/sonic-radiance.love/birb/modules/gui/elements/canvas.lua b/sonic-radiance.love/birb/modules/gui/elements/canvas.lua new file mode 100644 index 0000000..a1f654e --- /dev/null +++ b/sonic-radiance.love/birb/modules/gui/elements/canvas.lua @@ -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 \ No newline at end of file diff --git a/sonic-radiance.love/birb/modules/gui/menus/flowbox.lua b/sonic-radiance.love/birb/modules/gui/menus/flowbox.lua index f0a40fb..52a8870 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/flowbox.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/flowbox.lua @@ -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) diff --git a/sonic-radiance.love/birb/modules/gui/menus/grid.lua b/sonic-radiance.love/birb/modules/gui/menus/grid.lua index 8cd62e3..1bd0bf1 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/grid.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/grid.lua @@ -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 diff --git a/sonic-radiance.love/birb/modules/gui/menus/hlistbox.lua b/sonic-radiance.love/birb/modules/gui/menus/hlistbox.lua index faec6bc..c10d772 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/hlistbox.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/hlistbox.lua @@ -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 diff --git a/sonic-radiance.love/birb/modules/gui/menus/listbox.lua b/sonic-radiance.love/birb/modules/gui/menus/listbox.lua index d99d614..2a6f30c 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/listbox.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/listbox.lua @@ -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 diff --git a/sonic-radiance.love/birb/modules/gui/menus/parent.lua b/sonic-radiance.love/birb/modules/gui/menus/parent.lua index 80f0893..dba9084 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/parent.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/parent.lua @@ -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