sonic-radiance/sonic-radiance.love/birb/modules/gui/screen.lua

99 lines
2.6 KiB
Lua
Raw Normal View History

2021-08-21 12:45:49 +02:00
local GuiScreen = Object:extend()
local ElementList = require "birb.modules.gui.mixins.elements"
GuiScreen:implement(ElementList)
2021-08-26 23:23:22 +02:00
local TweenManager = require "birb.classes.time"
local elementDataStruct = require "birb.structures.elementData"
2021-08-26 23:23:22 +02:00
function GuiScreen:new(name, controller)
self:initWrapper()
self.controller = controller or self.gui
2021-08-21 12:45:49 +02:00
self.name = name
self.isVisible = false
2021-08-26 23:23:22 +02:00
self.transforms = {}
self.tweens = TweenManager(self)
2021-08-21 12:45:49 +02:00
self:reset()
2021-08-26 23:23:22 +02:00
self:registerElements()
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
2021-08-26 23:23:22 +02:00
function GuiScreen:update(dt)
self.tweens:update(dt)
end
function GuiScreen:show()
if (not self.isVisible) then
self.isVisible = true
if (self.transforms["show"] ~= nil) then
self:playTransform("show")
end
end
end
function GuiScreen:hide()
if (self.isVisible) then
if (self.transforms["hide"] ~= nil) then
local time = self:playTransform("hide")
print(time)
self.tweens:newFunc(time, "hide", function ()
self.isVisible = false
end)
end
end
end
function GuiScreen:addTransform(name, transform)
self.transforms[name] = transform
end
function GuiScreen:playTransform(name, delay)
return self.gui:transform(self.transforms[name], delay)
2021-08-21 12:45:49 +02:00
end
function GuiScreen:reset()
self:initElements()
end
function GuiScreen:registerElements()
local elementList = self:createElements()
for _, rawElement in ipairs(elementList) do
if (rawElement.is ~= nil) then
self:addElement(rawElement.name, rawElement)
rawElement.screen = self
2021-08-21 12:45:49 +02:00
else
local elemData = utils.table.parse(rawElement, elementDataStruct, 3)
local element = elemData.element
self:addElement(element.name, element)
2021-08-31 18:30:28 +02:00
if (elemData.focus == true) then
element:getFocus()
end
if (elemData.delay > 0) then
element.isVisible = false
element:newSwitch(elemData.delay, {"isVisible"})
2021-08-21 12:45:49 +02:00
end
if (elemData.depth ~= nil) then
element.depth = elemData.depth
end
if (elemData.keypress ~= nil) then
element:setKeyPressAction(elemData.keypress)
end
element.screen = self
2021-08-21 12:45:49 +02:00
end
end
end
function GuiScreen:createElements()
-- Empty function
end
return GuiScreen