feat: more screen support

This commit is contained in:
Kazhnuz 2021-08-26 23:23:22 +02:00
parent 42fbda0c58
commit 4322a80571
2 changed files with 95 additions and 10 deletions

View file

@ -46,48 +46,85 @@ function Gui:update(dt)
element:updateElement(dt)
end
end
for _, screen in pairs(self.screens) do
if (screen ~= nil) then
screen:update(dt)
end
end
end
-- TWEEN FUNCTIONS
-- Handle tweening
function Gui:transform(data)
function Gui:transform(data, delay)
delay = delay or 0
local time = 0
for _, rawTransform in ipairs(data) do
self:transformOne(rawTransform)
local newTime = self:transformOne(rawTransform, delay)
time = math.max(time, newTime)
end
return time
end
function Gui:transformOne(rawTransform)
function Gui:playScreenTransform(screenname, name, delay)
return self.screens[screenname]:playTransform(name, delay)
end
function Gui:transformOne(rawTransform, delay)
delay = delay or 0
local struct = TransformDataStruct[rawTransform[2]]
assert(struct ~= nil, "Structure " .. rawTransform[1] .. " doesn't exists ")
local transform = utils.table.parse(rawTransform, struct, 0)
local time = transform.start + delay
if transform.type == "tween" then
self:newTween(transform.name, transform.start, transform.duration, transform.target, transform.easing)
self:newTween(transform.name, time, transform.duration, transform.target, transform.easing)
elseif transform.type == "movement" then
self:newMovement(transform.name, transform.start, transform.duration, transform.x, transform.y, transform.easing)
self:newMovement(transform.name, time, transform.duration, transform.x, transform.y, transform.easing)
elseif transform.type == "switch" then
self:newSwitch(transform.name, transform.start, transform.bools)
self:newSwitch(transform.name, time, transform.bools)
elseif transform.type == "delayFocus" then
self:delayFocus(transform.name, transform.start)
self:delayFocus(transform.name, time)
end
if (transform.duration ~= nil) then
time = time + transform.duration
end
return time
end
function Gui:newTween(element, start, duration, target, easing)
assert(self.elements[element] ~= nil, element .. " does not exists")
self.elements[element]:newTween(start, duration, target, easing)
end
function Gui:newMovement(element, start, duration, x, y, easing)
assert(self.elements[element] ~= nil, element .. " does not exists")
self.elements[element]:newMovement(start, duration, x, y, easing)
end
function Gui:newSwitch(element, start, bools)
assert(self.elements[element] ~= nil, element .. " does not exists")
self.elements[element]:newSwitch(start, bools)
end
function Gui:delayFocus(element, start)
assert(self.elements[element] ~= nil, element .. " does not exists")
self.elements[element]:delayFocus(start)
end
function Gui:hideScreen(screenname)
self.screens[screenname]:hide()
end
function Gui:showScreen(screenname)
self.screens[screenname]:show()
end
-- KEYBOARD FUNCTIONS
-- Handle keyboard

View file

@ -4,15 +4,61 @@ local ScreenList = require "birb.modules.gui.mixins.screens"
GuiScreen:implement(ScreenList)
GuiScreen:implement(ElementList)
function GuiScreen:new(controller, name)
self.controller = controller
local TweenManager = require "birb.classes.time"
function GuiScreen:new(name, controller)
self.controller = controller or self:getGui()
self.name = name
self.isVisible = false
self.transforms = {}
self.tweens = TweenManager(self)
self:reset()
self:registerElements()
self.controller:addScreen(name, self)
end
function GuiScreen:update(dt)
self.tweens:update(dt)
for _, screen in pairs(self.screens) do
if (screen ~= nil) then
screen:update(dt)
end
end
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)
local gui = self:getGui()
return gui:transform(self.transforms[name], delay)
end
function GuiScreen:getGui()
local scene = core.scenemanager.currentScene
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene
return scene.gui
end
@ -26,11 +72,13 @@ function GuiScreen:registerElements()
for _, element in ipairs(elementList) do
if (element.is ~= nil) then
self:addElement(element.name, element)
element.screen = self
else
self:addElement(element[1].name, element[1])
if (element[2] == false) then
element[1].isVisible = false
end
element[1].screen = self
end
end
end