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 self.isVisible = visibility
end end
function GuiElement:getFocus() function GuiElement:getFocus(widgetId, page)
self.gui:setFocus(self.name) self.gui:setFocus(self.name, widgetId, page)
end end
function GuiElement:haveFocus() function GuiElement:haveFocus()
@ -72,6 +72,10 @@ function GuiElement:looseFocus()
end end
end end
function GuiElement:setSubFocus()
-- Useless for basic element
end
-- UPDATE FUNCTIONS -- UPDATE FUNCTIONS
-- Update the menu every game update -- Update the menu every game update

View file

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

View file

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

View file

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