From 75bd09f18b691b98d0f89ae63d55443b1b8a09e5 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Tue, 31 Aug 2021 10:01:24 +0200 Subject: [PATCH] feat: improve focus handling --- .../birb/modules/gui/elements/parent.lua | 6 +++--- .../birb/modules/gui/mixins/elements.lua | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/sonic-radiance.love/birb/modules/gui/elements/parent.lua b/sonic-radiance.love/birb/modules/gui/elements/parent.lua index a3b244b..1d44c39 100644 --- a/sonic-radiance.love/birb/modules/gui/elements/parent.lua +++ b/sonic-radiance.love/birb/modules/gui/elements/parent.lua @@ -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 diff --git a/sonic-radiance.love/birb/modules/gui/mixins/elements.lua b/sonic-radiance.love/birb/modules/gui/mixins/elements.lua index 8afa4c9..3bcffb6 100644 --- a/sonic-radiance.love/birb/modules/gui/mixins/elements.lua +++ b/sonic-radiance.love/birb/modules/gui/mixins/elements.lua @@ -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