feat: add API to change focus on showing a screen

This commit is contained in:
Kazhnuz 2021-12-31 15:39:50 +01:00
parent 1a889163ae
commit 3c90fd1fe8
4 changed files with 29 additions and 8 deletions

View file

@ -58,8 +58,8 @@ function GuiElement:setVisibility(visibility)
self.isVisible = visibility
end
function GuiElement:getFocus()
self.gui:setFocus(self.name)
function GuiElement:getFocus(widgetId, page)
self.gui:setFocus(self.name, widgetId, page)
end
function GuiElement:haveFocus()
@ -72,6 +72,10 @@ function GuiElement:looseFocus()
end
end
function GuiElement:setSubFocus()
-- Useless for basic element
end
-- UPDATE FUNCTIONS
-- Update the menu every game update

View file

@ -122,6 +122,13 @@ function Menu:back()
self:resetView()
end
function GuiElement:setSubFocus(widgetId, pageName)
if (pageName ~= nil) then
self.widget:switch(pageName)
end
self.widget:trySelectWidget(widgetId)
end
-- ACTION FUNCTIONS
-- Send actions to the widgets

View file

@ -17,11 +17,14 @@ function ElementList:deleteElement(name)
self.elements[name] = nil
end
function ElementList:setFocus(name)
function ElementList:setFocus(name, widgetId, page)
assert(self:elementExists(name), "Element " .. name .. " doesn't exists")
self:storeLastFocus()
self.focusedElement = name
self.elements[name].isVisible = true
if (widgetId ~= nil) then
self.elements[name]:setSubFocus(widgetId, page)
end
end
function ElementList:removeFocus()

View file

@ -17,6 +17,8 @@ function GuiScreen:new(name)
self:reset()
self:registerElements()
self.gui:addScreen(name, self)
self.defaultFocus = nil
end
function GuiScreen:initWrapper()
@ -32,19 +34,20 @@ function GuiScreen:update(dt)
self.tweens:update(dt)
end
function GuiScreen:show()
self:showSimple()
--TODO: show parent if we show directly the child
function GuiScreen:show(focusElement, widgetId, page)
self:showSimple(focusElement, widgetId, page)
if (self.set ~= nil) then
self.set.owner:show()
end
end
function GuiScreen:showSimple()
function GuiScreen:showSimple(focusElement, widgetId, page)
focusElement = focusElement or self.defaultFocus
local time = 0
if (not self.isVisible) then
self.isVisible = true
if (self.transforms["show"] ~= nil) then
self:playTransform("show")
time = self:playTransform("show")
end
if (self.subscreens ~= nil) then
@ -54,6 +57,10 @@ function GuiScreen:showSimple()
if (self.set ~= nil) then
self.set:setCurrentScreen(self.name)
end
if (focusElement) then
self.tweens:newFunc(time, "focus", function () self.gui:setFocus(focusElement, widgetId, page) end)
end
end
end