From 7e7cfe376368adb5158fb79933fa97855b3802cf Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 28 Nov 2020 14:17:48 +0100 Subject: [PATCH] chore: create a generic guiElement --- birb/modules/menusystem/init.lua | 12 +- birb/modules/menusystem/menus/parent.lua | 139 ++++++----------------- birb/modules/menusystem/parent.lua | 119 +++++++++++++++++++ 3 files changed, 154 insertions(+), 116 deletions(-) create mode 100644 birb/modules/menusystem/parent.lua diff --git a/birb/modules/menusystem/init.lua b/birb/modules/menusystem/init.lua index 4c3fd30..a742379 100644 --- a/birb/modules/menusystem/init.lua +++ b/birb/modules/menusystem/init.lua @@ -199,9 +199,7 @@ function MenuSystem:update(dt) if (self.isActive) then self:removeDestroyedMenus() for _, guiElement in pairs(self.menus) do - guiElement:update(dt) - --TODO: move this inside the menu - guiElement:updateWidgets(dt) + guiElement:updateElement(dt) end if self.menus[self.focusedMenu] ~= nil then @@ -274,13 +272,7 @@ function MenuSystem:draw(dt) for _, drawObject in ipairs(self.drawList) do local guiElement = self.menus[drawObject.name] if (guiElement.isVisible) then - guiElement:draw(dt) - end - end - - if self.menus[self.focusedMenu] ~= nil then - if (self.menus[self.focusedMenu].isVisible) then - self.menus[self.focusedMenu]:drawCursor() + guiElement:drawElement(dt) end end end diff --git a/birb/modules/menusystem/menus/parent.lua b/birb/modules/menusystem/menus/parent.lua index f1aeb71..2b678a8 100644 --- a/birb/modules/menusystem/menus/parent.lua +++ b/birb/modules/menusystem/menus/parent.lua @@ -21,9 +21,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] -local Rect = require "birb.objects.2D.rect" +local GuiElement = require "birb.modules.menusystem.parent" -local Menu = Rect:extend() +local Menu = GuiElement:extend() local function updateWidgetByOrder(a, b) if a.order ~= b.order then @@ -37,10 +37,8 @@ end -- Initialize and configure functions. function Menu:new(menusystem, name, x, y, w, h) - self.menusystem = menusystem - self.name = name - - Menu.super.new(self, x, y, w, h) + Menu.super.new(self, name, x, y, w, h) + self.menusystem = self:getGui() self.widget = {} self.widget.list = {} @@ -49,76 +47,7 @@ function Menu:new(menusystem, name, x, y, w, h) self.widget.cancel = 0 self:updateWidgetSize() - self.isDestroyed = false - self.isVisible = true - self.isActive = true - self.isLocked = false - self.isAlwaysVisible = false - - self.depth = 0 - self:resetSound() - - self:register() -end - -function Menu:setDepth(depth) - self.depth = depth or 0 -end - -function Menu:setVisibility(visibility) - if self.isLocked == false and self.isAlwaysVisible == false then - -- if the menu is locked (thus is always active), it should also - -- be always visible. - self.isVisible = visibility - else - self.isVisible = true - end -end - -function Menu:setActivity(activity) - if self.isLocked == false then - self.isActive = activity - else - self.isActive = true - end -end - -function Menu:lock(lock) - self.isLocked = lock -end - -function Menu:lockVisibility(lock) - self.isAlwaysVisible = lock -end - -function Menu:getFocus() - self.menusystem.focusedMenu = self.name -end - -function Menu:haveFocus() - return (self.menusystem.focusedMenu == self.name) -end - -function Menu:register() - self.menusystem:addMenu(self.name, self) -end - -function Menu:setCancelWidget(id) - self.widget.cancel = #self.widget.list -end - -function Menu:updateWidgetSize() - self.widget.h = 0 - self.widget.w = 0 -end - -function Menu:getWidgetSize(id) - return self.widget.w, self.widget.h -end - -function Menu:getWidgetNumber() - return #self.widget.list end -- ACTION FUNCTIONS @@ -141,26 +70,21 @@ function Menu:resize(x,y,w,h) self:updateWidgetSize() end -function Menu:destroy() - self.destroyed = true -end - -function Menu:updateWidgetsOrder() - table.sort(self.widget.list, updateWidgetByOrder) -end - -- UPDATE FUNCTIONS --- Update the menu every game update -function Menu:update(dt) - -- Cette fonction ne contient rien par défaut +function Menu:updateElement(dt) + self:update(dt) + self:updateWidgets(dt) end -- DRAW FUNCTIONS -- Draw the menu and its content -function Menu:draw() - -- nothing here +function Menu:drawElement() + self:draw() + if (self:haveFocus()) then + self:drawCursor() + end end function Menu:drawCursor() @@ -171,24 +95,6 @@ function Menu:drawCanvas() end --- KEYBOARD FUNCTIONS --- Handle key press - -function Menu:keyreleased(key) - -- Cette fonction ne contient rien par défaut -end - --- MOUSE FUNCTIONS --- Handle pointers (clic/touch) - -function Menu:mousemoved(x, y) - -- Cette fonction ne contient rien par défaut -end - -function Menu:mousepressed( x, y, button, istouch ) - -- Cette fonction ne contient rien par défaut -end - -- WIDGET FUNCTIONS -- Handle widgets of the functions @@ -209,6 +115,10 @@ function Menu:updateWidgets(dt) end end +function Menu:updateWidgetsOrder() + table.sort(self.widget.list, updateWidgetByOrder) +end + function Menu:updateWidgetsID() for i,v in ipairs(self.widget.list) do v.id = i @@ -223,6 +133,23 @@ function Menu:removeDestroyedWidgets() -- On retire les widgets marquées comme end end +function Menu:setCancelWidget(id) + self.widget.cancel = #self.widget.list +end + +function Menu:updateWidgetSize() + self.widget.h = 0 + self.widget.w = 0 +end + +function Menu:getWidgetSize(id) + return self.widget.w, self.widget.h +end + +function Menu:getWidgetNumber() + return #self.widget.list +end + -- CURSOR FUNCTIONS -- Set or move the cursor of the menu diff --git a/birb/modules/menusystem/parent.lua b/birb/modules/menusystem/parent.lua new file mode 100644 index 0000000..059fd34 --- /dev/null +++ b/birb/modules/menusystem/parent.lua @@ -0,0 +1,119 @@ +local Rect = require "birb.objects.2D.rect" + +local GuiElement = Rect:extend() + +function GuiElement:new(name, x, y, w, h) + GuiElement.super.new(self, x, y, w, h) + self.name = name + + self.isDestroyed = false + self.isVisible = true + self.isActive = true + self.isLocked = false + self.isAlwaysVisible = false + + self.depth = 0 + + self:register() +end + +function GuiElement:getGui() + local scene = core.scenemanager.currentScene + return scene.menusystem +end + +function GuiElement:register() + local gui = self:getGui() + gui:addMenu(self.name, self) +end + +function GuiElement:destroy() + self.destroyed = true +end + +-- VISIBILITY/ACTIVITY +-- Handle drawing and how we interact with + +function GuiElement:setDepth(depth) + self.depth = depth or 0 +end + +function GuiElement:setVisibility(visibility) + if self.isLocked == false and self.isAlwaysVisible == false then + self.isVisible = visibility + else + -- if the element is locked (thus is always active), it should also + -- be always visible. + self.isVisible = true + end +end + +function GuiElement:setActivity(activity) + if self.isLocked == false then + self.isActive = activity + else + self.isActive = true + end +end + +function GuiElement:lock(lock) + self.isLocked = lock +end + +function GuiElement:lockVisibility(lock) + self.isAlwaysVisible = lock +end + +function GuiElement:getFocus() + local gui = self:getGui() + gui.focusedMenu = self.name +end + +function GuiElement:haveFocus() + local gui = self:getGui() + return (gui.focusedMenu == self.name) +end + +-- UPDATE FUNCTIONS +-- Update the menu every game update + +-- External update function +function GuiElement:updateElement(dt) + self:update(dt) +end + +-- Internal update function +function GuiElement:update(dt) + -- Cette fonction ne contient rien par défaut +end + +-- DRAW FUNCTIONS +-- Draw the menu and its content + +function GuiElement:drawElement() + self:draw() +end + +function GuiElement:draw() + -- nothing here +end + +-- KEYBOARD FUNCTIONS +-- Handle key press + +function GuiElement:keyreleased(key) + -- Cette fonction ne contient rien par défaut +end + +-- MOUSE FUNCTIONS +-- Handle pointers (clic/touch) + +function GuiElement:mousemoved(x, y) + -- Cette fonction ne contient rien par défaut +end + +function GuiElement:mousepressed(x, y, button, istouch) + -- Cette fonction ne contient rien par défaut +end + +return GuiElement