From 4a84d2dc53fb88bb1f0656d212a5dea85b94a5c4 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Tue, 31 Aug 2021 23:47:01 +0200 Subject: [PATCH] improvement: better wrapper handling in guiElement --- .../birb/modules/gui/elements/assets.lua | 4 +-- .../birb/modules/gui/elements/composite.lua | 2 +- .../birb/modules/gui/elements/parent.lua | 33 +++++++------------ .../birb/modules/gui/elements/text.lua | 7 ++-- .../birb/modules/gui/menus/parent.lua | 7 ++-- .../birb/modules/gui/menus/widgets/base.lua | 17 +++++----- .../birb/modules/gui/screen.lua | 18 +++++----- .../game/modules/confirmdialog/init.lua | 3 +- 8 files changed, 41 insertions(+), 50 deletions(-) diff --git a/sonic-radiance.love/birb/modules/gui/elements/assets.lua b/sonic-radiance.love/birb/modules/gui/elements/assets.lua index 95990c0..b12ca50 100644 --- a/sonic-radiance.love/birb/modules/gui/elements/assets.lua +++ b/sonic-radiance.love/birb/modules/gui/elements/assets.lua @@ -2,8 +2,8 @@ local Parent = require "birb.modules.gui.elements.drawable" local AssetElement = Parent:extend() function AssetElement:new(name, assetType, assetName, x, y,r,sx,sy,ox,oy, opacity) - local gui = self:getGui() - local asset = gui.scene.assets[assetType][assetName] + self:initWrapper() + local asset = self.assets[assetType][assetName] assert(asset ~= nil, assetName .. ' (' .. assetType .. ") doesn't exist") AssetElement.super.new(self, name, asset, x, y,r,sx,sy,ox,oy, opacity) diff --git a/sonic-radiance.love/birb/modules/gui/elements/composite.lua b/sonic-radiance.love/birb/modules/gui/elements/composite.lua index 92c072b..d4d9aff 100644 --- a/sonic-radiance.love/birb/modules/gui/elements/composite.lua +++ b/sonic-radiance.love/birb/modules/gui/elements/composite.lua @@ -24,7 +24,7 @@ end function CompositeElement:update(dt) for _, child in ipairs(self.children) do - local childElement = self.getGui().elements[child.name] + local childElement = self.gui.elements[child.name] childElement.x = self.x + child.relx childElement.y = self.y + child.rely childElement.isVisible = self.isVisible diff --git a/sonic-radiance.love/birb/modules/gui/elements/parent.lua b/sonic-radiance.love/birb/modules/gui/elements/parent.lua index 10af95c..f2dcad3 100644 --- a/sonic-radiance.love/birb/modules/gui/elements/parent.lua +++ b/sonic-radiance.love/birb/modules/gui/elements/parent.lua @@ -13,33 +13,27 @@ function GuiElement:new(name, x, y, w, h) self.depth = 10 self.tweens = TweenManager(self) - self.assets = self:getAssets() + self:initWrapper() self:register() end +function GuiElement:initWrapper() + self.scene = core.scenemanager.nextScene or core.scenemanager.currentScene + self.gui = self.scene.gui + self.assets = self.scene.assets +end + function GuiElement:setKeyPressAction(func) self.func = func end -function GuiElement:getGui() - local scene = core.scenemanager.nextScene or core.scenemanager.currentScene - return scene.gui -end - -function GuiElement:getAssets() - local scene = core.scenemanager.nextScene or core.scenemanager.currentScene - return scene.assets -end - function GuiElement:register() - local gui = self:getGui() - self.creationId = gui:addElement(self.name, self) + self.creationId = self.gui:addElement(self.name, self) end function GuiElement:destroy() - local gui = self:getGui() - gui:deleteElement(self.name) + self.gui:deleteElement(self.name) if (self.screen ~= nil) then self.screen:deleteElement(self.name) end @@ -65,19 +59,16 @@ function GuiElement:setVisibility(visibility) end function GuiElement:getFocus() - local gui = self:getGui() - gui:setFocus(self.name) + self.gui:setFocus(self.name) end function GuiElement:haveFocus() - local gui = self:getGui() - return (gui.focusedElement == self.name) + return (self.gui.focusedElement == self.name) end function GuiElement:looseFocus() if (self:haveFocus()) then - local gui = self:getGui() - gui:removeFocus() + self.gui:removeFocus() end end diff --git a/sonic-radiance.love/birb/modules/gui/elements/text.lua b/sonic-radiance.love/birb/modules/gui/elements/text.lua index c5f9871..ce24c2b 100644 --- a/sonic-radiance.love/birb/modules/gui/elements/text.lua +++ b/sonic-radiance.love/birb/modules/gui/elements/text.lua @@ -2,11 +2,10 @@ local Parent = require "birb.modules.gui.elements.parent" local TextElement = Parent:extend() function TextElement:new(name, fontName, text, x, y, align) - self.text = text - local gui = self:getGui() - self.font = gui.scene.assets.fonts[fontName] - TextElement.super.new(self, name, x, y, 1, 1) + self.text = text + self.font = self.assets.fonts[fontName] + self.align = align end diff --git a/sonic-radiance.love/birb/modules/gui/menus/parent.lua b/sonic-radiance.love/birb/modules/gui/menus/parent.lua index bee182a..51e24c7 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/parent.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/parent.lua @@ -33,7 +33,9 @@ local menuUtils = require "birb.modules.gui.utils" function Menu:new(name, x, y, w, h) Menu.super.new(self, name, x, y, w, h) - self.menusystem = self:getGui() + + --TODO: remove this + self.menusystem = self.gui self.widget = MenuModel() self.widgetSize = {} @@ -216,8 +218,7 @@ function Menu:playNavigationSound() end function Menu:playSFX(name) - local gui = self:getGui() - gui:playSFX(name) + self.gui:playSFX(name) end -- VIEW FUNCTIONS diff --git a/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua b/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua index e295bb8..9caee47 100644 --- a/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua +++ b/sonic-radiance.love/birb/modules/gui/menus/widgets/base.lua @@ -26,9 +26,8 @@ local BaseWidget = Object:extend() -- Initialize and configure the widget function BaseWidget:new(menuName) - self.scene = self:getScene() + self:initWrapper() self.menu = self:getMenuByName(menuName) - self.assets = self:getAssets() self.destroyed = false self.selectable = false @@ -45,25 +44,25 @@ function BaseWidget:new(menuName) self.type = "select" end +function BaseWidget:initWrapper() + self.scene = core.scenemanager.nextScene or core.scenemanager.currentScene + self.gui = self.scene.gui + self.assets = self.scene.assets +end + function BaseWidget:setFunc(func) self.func = func end function BaseWidget:getMenuByName(name) - local gui = self:getGui() assert(name ~= nil, "Name cant be nil") - return gui.elements[name] + return self.gui.elements[name] end function BaseWidget:getScene() return core.scenemanager.nextScene or core.scenemanager.currentScene end -function BaseWidget:getGui() - local scene = core.scenemanager.nextScene or core.scenemanager.currentScene - return scene.gui -end - function BaseWidget:getAssets() local scene = core.scenemanager.nextScene or core.scenemanager.currentScene return scene.assets diff --git a/sonic-radiance.love/birb/modules/gui/screen.lua b/sonic-radiance.love/birb/modules/gui/screen.lua index 5ced2b2..4a6eb49 100644 --- a/sonic-radiance.love/birb/modules/gui/screen.lua +++ b/sonic-radiance.love/birb/modules/gui/screen.lua @@ -10,7 +10,8 @@ local elementDataStruct = require "birb.structures.elementData" function GuiScreen:new(name, controller) - self.controller = controller or self:getGui() + self:initWrapper() + self.controller = controller or self.gui self.name = name self.isVisible = false self.transforms = {} @@ -21,6 +22,13 @@ function GuiScreen:new(name, controller) self.controller:addScreen(name, self) end +function GuiScreen:initWrapper() + local scene = core.scenemanager.nextScene or core.scenemanager.currentScene + self.scene = scene + self.gui = scene.gui + self.assets = scene.assets +end + function GuiScreen:update(dt) self.tweens:update(dt) for _, screen in pairs(self.screens) do @@ -56,13 +64,7 @@ function GuiScreen:addTransform(name, transform) end function GuiScreen:playTransform(name, delay) - local gui = self:getGui() - return gui:transform(self.transforms[name], delay) -end - -function GuiScreen:getGui() - local scene = core.scenemanager.nextScene or core.scenemanager.currentScene - return scene.gui + return self.gui:transform(self.transforms[name], delay) end function GuiScreen:reset() diff --git a/sonic-radiance.love/game/modules/confirmdialog/init.lua b/sonic-radiance.love/game/modules/confirmdialog/init.lua index 5ace915..9f9b502 100644 --- a/sonic-radiance.love/game/modules/confirmdialog/init.lua +++ b/sonic-radiance.love/game/modules/confirmdialog/init.lua @@ -78,8 +78,7 @@ function ConfirmDialog:doAction(choice) end function ConfirmDialog:dismiss() - local guiSystem = self:getGui() - guiSystem:setLastFocus() + self.gui:setLastFocus() self:destroy() end