From 42fbda0c58e6416708ce3e39bb091806dfbbdb7c Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Tue, 24 Aug 2021 19:41:25 +0200 Subject: [PATCH] fea(gui): add initial support for inputs --- .../birb/modules/gui/elements/parent.lua | 14 ++++++++---- sonic-radiance.love/birb/modules/gui/init.lua | 22 +++++++++++++++++++ .../birb/modules/gui/mixins/elements.lua | 21 +++++++++++++++++- sonic-radiance.love/birb/modules/scenes.lua | 1 + sonic-radiance.love/birb/structures/tween.lua | 1 + 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/sonic-radiance.love/birb/modules/gui/elements/parent.lua b/sonic-radiance.love/birb/modules/gui/elements/parent.lua index 7b5f9e1..5cc2bc7 100644 --- a/sonic-radiance.love/birb/modules/gui/elements/parent.lua +++ b/sonic-radiance.love/birb/modules/gui/elements/parent.lua @@ -56,18 +56,18 @@ end function GuiElement:getFocus() local gui = self:getGui() - gui.focusedMenu = self.name + gui.focusedElement = self.name end function GuiElement:haveFocus() local gui = self:getGui() - return (gui.focusedMenu == self.name) + return (gui.focusedElement == self.name) end function GuiElement:looseFocus() if (self:haveFocus()) then local gui = self:getGui() - gui.focusedMenu = nil + gui.focusedElement = nil end end @@ -97,7 +97,13 @@ function GuiElement:newMovement(start, duration, x, y, easing) end function GuiElement:newSwitch(start, bools) - self:newSwitch(start, bools) + self.tweens:newSwitch(start, bools) +end + +function GuiElement:delayFocus(start) + self.tweens:newFunc(start, "focus", function () + self:getFocus() + end) end -- DRAW FUNCTIONS diff --git a/sonic-radiance.love/birb/modules/gui/init.lua b/sonic-radiance.love/birb/modules/gui/init.lua index a826238..416f066 100644 --- a/sonic-radiance.love/birb/modules/gui/init.lua +++ b/sonic-radiance.love/birb/modules/gui/init.lua @@ -67,6 +67,8 @@ function Gui:transformOne(rawTransform) self:newMovement(transform.name, transform.start, transform.duration, transform.x, transform.y, transform.easing) elseif transform.type == "switch" then self:newSwitch(transform.name, transform.start, transform.bools) + elseif transform.type == "delayFocus" then + self:delayFocus(transform.name, transform.start) end end @@ -82,6 +84,26 @@ function Gui:newSwitch(element, start, bools) self.elements[element]:newSwitch(start, bools) end +function Gui:delayFocus(element, start) + self.elements[element]:delayFocus(start) +end + +-- KEYBOARD FUNCTIONS +-- Handle keyboard + +function Gui:keycheck(keys) + local haveFocus = self:haveFocus() + if (haveFocus) then + local elem = self:getFocusedElement() + for key,_ in pairs(keys) do + if keys[key].isPressed then + elem:keypressed(key) + end + end + end + return haveFocus +end + -- DRAW FUNCTIONS -- Draw the menu and its content diff --git a/sonic-radiance.love/birb/modules/gui/mixins/elements.lua b/sonic-radiance.love/birb/modules/gui/mixins/elements.lua index a7a5020..e2dfb10 100644 --- a/sonic-radiance.love/birb/modules/gui/mixins/elements.lua +++ b/sonic-radiance.love/birb/modules/gui/mixins/elements.lua @@ -2,7 +2,7 @@ local ElementList = Object:extend() function ElementList:initElements() self.elements = {} - self.focusedElem = nil + self.focusedElement = nil self.nbrElement = 0 end @@ -16,6 +16,25 @@ function ElementList:deleteElement(name) self.elements[name] = nil end +function ElementList:elementExists(name) + return (self:getElement(name) ~= nil) +end + +function ElementList:haveFocus() + return self:elementExists(self.focusedElement) +end + +function ElementList:getFocusedElement() + return self:getElement(self.focusedElement) +end + +function ElementList:getElement(name) + if (not utils.string.isEmpty(name)) then + return self.elements[name] + end + return nil +end + function ElementList:getVisibleElement(topLayer) local visibleList = {} for _, element in pairs(self.elements) do diff --git a/sonic-radiance.love/birb/modules/scenes.lua b/sonic-radiance.love/birb/modules/scenes.lua index 45218ac..552a088 100644 --- a/sonic-radiance.love/birb/modules/scenes.lua +++ b/sonic-radiance.love/birb/modules/scenes.lua @@ -107,6 +107,7 @@ function Scene:updateMenus(dt) if (self.menusystem ~= nil) then self.menusystem:update(dt) if (core.screen:isActive() and (self.dialog == nil)) then + self.gui:keycheck(self:getKeys(1)) self.menusystem:keycheck() end end diff --git a/sonic-radiance.love/birb/structures/tween.lua b/sonic-radiance.love/birb/structures/tween.lua index 2b84b41..cbc6147 100644 --- a/sonic-radiance.love/birb/structures/tween.lua +++ b/sonic-radiance.love/birb/structures/tween.lua @@ -2,4 +2,5 @@ return { tween = {"name", "type", "start", "duration", "target", "easing"}, movement = {"name", "type", "start", "duration", "x", "y", "easing"}, switch = {"name", "type", "start", "bools"}, + delayFocus = {"name", "type", "start"}, } \ No newline at end of file