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

160 lines
4.1 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"
2021-12-31 14:27:35 +01:00
local ScreenSet = require "birb.modules.gui.screen.screenset"
2021-08-26 23:23:22 +02:00
local elementDataStruct = require "birb.structures.elementData"
2021-12-08 11:26:27 +01:00
function GuiScreen:new(name)
self:initWrapper()
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()
2021-12-08 11:26:27 +01:00
self.gui:addScreen(name, self)
self.defaultFocus = nil
2021-08-26 23:23:22 +02:00
end
function GuiScreen:initWrapper()
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene
self.scene = scene
self.gui = scene.gui
2021-12-08 11:26:27 +01:00
-- Présent pour la compatibilité
self.controller = self.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(focusElement, widgetId, page)
self:showSimple(focusElement, widgetId, page)
2021-12-31 14:27:35 +01:00
if (self.set ~= nil) then
self.set.owner:show()
end
end
function GuiScreen:showSimple(focusElement, widgetId, page)
focusElement = focusElement or self.defaultFocus
2021-08-26 23:23:22 +02:00
if (not self.isVisible) then
self.isVisible = true
2022-01-04 17:11:45 +01:00
local delay = 0
2021-12-31 14:27:35 +01:00
if (self.set ~= nil) then
2022-01-04 17:11:45 +01:00
delay = self.set:setCurrentScreen(self.name)
2021-12-31 14:27:35 +01:00
end
2022-01-04 17:11:45 +01:00
if (self.transforms["show"] ~= nil) then
if (delay == 0) then
self:showWithSubScreen(focusElement, widgetId, page)
else
self.tweens:newFunc(delay, "focus", function () self:showWithSubScreen(focusElement, widgetId, page) end)
end
end
2021-08-26 23:23:22 +02:00
end
2022-01-04 17:11:45 +01:00
end
function GuiScreen:showWithSubScreen(focusElement, widgetId, page)
self:playTransform("show")
if (focusElement ~= nil) then
self.gui:setFocus(focusElement, widgetId, page)
end
2021-12-31 14:27:35 +01:00
2022-01-04 17:11:45 +01:00
if (self.subscreens ~= nil) then
self.subscreens:show()
end
2021-08-26 23:23:22 +02:00
end
function GuiScreen:setDatas(datas)
end
2021-08-26 23:23:22 +02:00
function GuiScreen:hide()
2022-01-04 17:11:45 +01:00
local time = 0
2021-08-26 23:23:22 +02:00
if (self.isVisible) then
if (self.transforms["hide"] ~= nil) then
2022-01-04 17:11:45 +01:00
time = self:playTransform("hide")
2021-08-26 23:23:22 +02:00
self.tweens:newFunc(time, "hide", function ()
self.isVisible = false
end)
end
2021-12-31 14:27:35 +01:00
if (self.subscreens ~= nil) then
self.subscreens:hideCurrent()
end
2021-08-26 23:23:22 +02:00
end
2022-01-04 17:11:45 +01:00
return time
2021-08-26 23:23:22 +02:00
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
2021-12-31 14:27:35 +01:00
function GuiScreen:setParentSet(set)
self.set = set
end
function GuiScreen:addSubscreen(screen)
self:initSubscreen()
self.subscreens:add(screen)
end
function GuiScreen:showSubscreen(screenname)
if (self.subscreens ~= nil) then
self.subscreens:show(screenname)
end
end
function GuiScreen:initSubscreen()
if (self.subscreens == nil) then
self.subscreens = ScreenSet(self)
end
end
2021-08-21 12:45:49 +02:00
return GuiScreen