feat: improve focus handling

This commit is contained in:
Kazhnuz 2021-08-31 10:01:24 +02:00
parent 700eebd395
commit 75bd09f18b
2 changed files with 22 additions and 3 deletions

View file

@ -10,7 +10,7 @@ function GuiElement:new(name, x, y, w, h)
self.isVisible = true
self.screen = nil
self.depth = 0
self.depth = 10
self.tweens = TweenManager(self)
self.assets = self:getAssets()
@ -62,7 +62,7 @@ end
function GuiElement:getFocus()
local gui = self:getGui()
gui.focusedElement = self.name
gui:setFocus(self.name)
end
function GuiElement:haveFocus()
@ -73,7 +73,7 @@ end
function GuiElement:looseFocus()
if (self:haveFocus()) then
local gui = self:getGui()
gui.focusedElement = nil
gui:removeFocus()
end
end

View file

@ -3,6 +3,7 @@ local ElementList = Object:extend()
function ElementList:initElements()
self.elements = {}
self.focusedElement = nil
self.lastFocused = nil
self.nbrElement = 0
end
@ -18,10 +19,28 @@ end
function ElementList:setFocus(name)
assert(self:elementExists(name), "Element " .. name .. " doesn't exists")
self:storeLastFocus()
self.focusedElement = name
self.elements[name].isVisible = true
end
function ElementList:removeFocus()
self:storeLastFocus()
self.focusedElement = nil
end
function ElementList:storeLastFocus()
if (self.focusedElement ~= nil) then
self.lastFocused = self.focusedElement
end
end
function ElementList:setLastFocus()
if (self:elementExists(self.lastFocused)) then
self:setFocus(self.lastFocused)
end
end
function ElementList:elementExists(name)
return (self:getElement(name) ~= nil)
end