improvement: better wrapper handling in guiElement

This commit is contained in:
Kazhnuz 2021-08-31 23:47:01 +02:00
parent 4ccb055672
commit 4a84d2dc53
8 changed files with 41 additions and 50 deletions

View file

@ -2,8 +2,8 @@ local Parent = require "birb.modules.gui.elements.drawable"
local AssetElement = Parent:extend() local AssetElement = Parent:extend()
function AssetElement:new(name, assetType, assetName, x, y,r,sx,sy,ox,oy, opacity) function AssetElement:new(name, assetType, assetName, x, y,r,sx,sy,ox,oy, opacity)
local gui = self:getGui() self:initWrapper()
local asset = gui.scene.assets[assetType][assetName] local asset = self.assets[assetType][assetName]
assert(asset ~= nil, assetName .. ' (' .. assetType .. ") doesn't exist") assert(asset ~= nil, assetName .. ' (' .. assetType .. ") doesn't exist")
AssetElement.super.new(self, name, asset, x, y,r,sx,sy,ox,oy, opacity) AssetElement.super.new(self, name, asset, x, y,r,sx,sy,ox,oy, opacity)

View file

@ -24,7 +24,7 @@ end
function CompositeElement:update(dt) function CompositeElement:update(dt)
for _, child in ipairs(self.children) do 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.x = self.x + child.relx
childElement.y = self.y + child.rely childElement.y = self.y + child.rely
childElement.isVisible = self.isVisible childElement.isVisible = self.isVisible

View file

@ -13,33 +13,27 @@ function GuiElement:new(name, x, y, w, h)
self.depth = 10 self.depth = 10
self.tweens = TweenManager(self) self.tweens = TweenManager(self)
self.assets = self:getAssets() self:initWrapper()
self:register() self:register()
end 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) function GuiElement:setKeyPressAction(func)
self.func = func self.func = func
end 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() function GuiElement:register()
local gui = self:getGui() self.creationId = self.gui:addElement(self.name, self)
self.creationId = gui:addElement(self.name, self)
end end
function GuiElement:destroy() function GuiElement:destroy()
local gui = self:getGui() self.gui:deleteElement(self.name)
gui:deleteElement(self.name)
if (self.screen ~= nil) then if (self.screen ~= nil) then
self.screen:deleteElement(self.name) self.screen:deleteElement(self.name)
end end
@ -65,19 +59,16 @@ function GuiElement:setVisibility(visibility)
end end
function GuiElement:getFocus() function GuiElement:getFocus()
local gui = self:getGui() self.gui:setFocus(self.name)
gui:setFocus(self.name)
end end
function GuiElement:haveFocus() function GuiElement:haveFocus()
local gui = self:getGui() return (self.gui.focusedElement == self.name)
return (gui.focusedElement == self.name)
end end
function GuiElement:looseFocus() function GuiElement:looseFocus()
if (self:haveFocus()) then if (self:haveFocus()) then
local gui = self:getGui() self.gui:removeFocus()
gui:removeFocus()
end end
end end

View file

@ -2,11 +2,10 @@ local Parent = require "birb.modules.gui.elements.parent"
local TextElement = Parent:extend() local TextElement = Parent:extend()
function TextElement:new(name, fontName, text, x, y, align) 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) TextElement.super.new(self, name, x, y, 1, 1)
self.text = text
self.font = self.assets.fonts[fontName]
self.align = align self.align = align
end end

View file

@ -33,7 +33,9 @@ local menuUtils = require "birb.modules.gui.utils"
function Menu:new(name, x, y, w, h) function Menu:new(name, x, y, w, h)
Menu.super.new(self, 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.widget = MenuModel()
self.widgetSize = {} self.widgetSize = {}
@ -216,8 +218,7 @@ function Menu:playNavigationSound()
end end
function Menu:playSFX(name) function Menu:playSFX(name)
local gui = self:getGui() self.gui:playSFX(name)
gui:playSFX(name)
end end
-- VIEW FUNCTIONS -- VIEW FUNCTIONS

View file

@ -26,9 +26,8 @@ local BaseWidget = Object:extend()
-- Initialize and configure the widget -- Initialize and configure the widget
function BaseWidget:new(menuName) function BaseWidget:new(menuName)
self.scene = self:getScene() self:initWrapper()
self.menu = self:getMenuByName(menuName) self.menu = self:getMenuByName(menuName)
self.assets = self:getAssets()
self.destroyed = false self.destroyed = false
self.selectable = false self.selectable = false
@ -45,25 +44,25 @@ function BaseWidget:new(menuName)
self.type = "select" self.type = "select"
end 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) function BaseWidget:setFunc(func)
self.func = func self.func = func
end end
function BaseWidget:getMenuByName(name) function BaseWidget:getMenuByName(name)
local gui = self:getGui()
assert(name ~= nil, "Name cant be nil") assert(name ~= nil, "Name cant be nil")
return gui.elements[name] return self.gui.elements[name]
end end
function BaseWidget:getScene() function BaseWidget:getScene()
return core.scenemanager.nextScene or core.scenemanager.currentScene return core.scenemanager.nextScene or core.scenemanager.currentScene
end end
function BaseWidget:getGui()
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene
return scene.gui
end
function BaseWidget:getAssets() function BaseWidget:getAssets()
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene local scene = core.scenemanager.nextScene or core.scenemanager.currentScene
return scene.assets return scene.assets

View file

@ -10,7 +10,8 @@ local elementDataStruct = require "birb.structures.elementData"
function GuiScreen:new(name, controller) function GuiScreen:new(name, controller)
self.controller = controller or self:getGui() self:initWrapper()
self.controller = controller or self.gui
self.name = name self.name = name
self.isVisible = false self.isVisible = false
self.transforms = {} self.transforms = {}
@ -21,6 +22,13 @@ function GuiScreen:new(name, controller)
self.controller:addScreen(name, self) self.controller:addScreen(name, self)
end 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) function GuiScreen:update(dt)
self.tweens:update(dt) self.tweens:update(dt)
for _, screen in pairs(self.screens) do for _, screen in pairs(self.screens) do
@ -56,13 +64,7 @@ function GuiScreen:addTransform(name, transform)
end end
function GuiScreen:playTransform(name, delay) function GuiScreen:playTransform(name, delay)
local gui = self:getGui() return self.gui:transform(self.transforms[name], delay)
return gui:transform(self.transforms[name], delay)
end
function GuiScreen:getGui()
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene
return scene.gui
end end
function GuiScreen:reset() function GuiScreen:reset()

View file

@ -78,8 +78,7 @@ function ConfirmDialog:doAction(choice)
end end
function ConfirmDialog:dismiss() function ConfirmDialog:dismiss()
local guiSystem = self:getGui() self.gui:setLastFocus()
guiSystem:setLastFocus()
self:destroy() self:destroy()
end end