From dca462803fdd3b9598ef5735432b67684f74d8a3 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 27 Nov 2020 19:35:00 +0100 Subject: [PATCH] chore: make the code more readable in menumanager --- birb/modules/menusystem/init.lua | 84 +++++++++++++++++--------------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/birb/modules/menusystem/init.lua b/birb/modules/menusystem/init.lua index 769077c..4c3fd30 100644 --- a/birb/modules/menusystem/init.lua +++ b/birb/modules/menusystem/init.lua @@ -110,15 +110,15 @@ function MenuSystem:menuExist(name) return (self.menus[name] ~= nil) end -function MenuSystem:switchMenu(menu) - for k,v in pairs(self.menus) do - if k == menu then - v:getFocus() - v:setVisibility(true) - v:setActivity(true) +function MenuSystem:switchMenu(menuName) + for name, guiElement in pairs(self.menus) do + if (name == menuName) then + guiElement:getFocus() + guiElement:setVisibility(true) + guiElement:setActivity(true) else - v:setVisibility(false) - v:setActivity(false) + guiElement:setVisibility(false) + guiElement:setActivity(false) end end end @@ -156,38 +156,39 @@ function MenuSystem:setMenuVisibility(menu, visibility) end function MenuSystem:setAllMenuVisibility(visibility) - for k,v in pairs(self.menus) do - v:setVisibility(visibility) + for _, guiElement in pairs(self.menus) do + guiElement:setVisibility(visibility) end end function MenuSystem:setAllMenuActivity(activity) - for k,v in pairs(self.menus) do - v.isActive = activity + for _, guiElement in pairs(self.menus) do + guiElement.isActive = activity end end function MenuSystem:removeDestroyedMenus() -- On retire les entitées marquées comme supprimées - for k,v in pairs(self.menus) do - if (v.isDestroyed == true) then - self.menus[k] = nil + for name, guiElement in pairs(self.menus) do + if (guiElement.isDestroyed == true) then + self.menus[name] = nil end end end -- SOUND FUNCTIONS -- Add sounds to every menus +-- TODO: rework to be used directly by widgets later function MenuSystem:setSoundFromSceneAssets(soundname) - for k,v in pairs(self.menus) do - v:setSoundFromSceneAssets(soundname) + for _, guiElement in pairs(self.menus) do + guiElement:setSoundFromSceneAssets(soundname) end end function MenuSystem:setSound(soundasset) - for k,v in pairs(self.menus) do - v:setSound(soundasset) + for _, guiElement in pairs(self.menus) do + guiElement:setSound(soundasset) end end @@ -197,17 +198,18 @@ end function MenuSystem:update(dt) if (self.isActive) then self:removeDestroyedMenus() - for k,v in pairs(self.menus) do - v:update(dt) - v:updateWidgets(dt) + for _, guiElement in pairs(self.menus) do + guiElement:update(dt) + --TODO: move this inside the menu + guiElement:updateWidgets(dt) end if self.menus[self.focusedMenu] ~= nil then -- Only check buttons if the current focused menu is actually active if self.menus[self.focusedMenu].isActive then - for k,v in pairs(self.keys) do - if self.keys[k].isPressed then - self.menus[self.focusedMenu]:keyreleased(k) + for keyname, keydata in pairs(self.keys) do + if self.keys[keyname].isPressed then + self.menus[self.focusedMenu]:keyreleased(keyname) end end end @@ -221,10 +223,11 @@ end function MenuSystem:mousemoved(x, y, dx, dy) if (self.isActive) then - for k,v in pairs(self.menus) do - if v.isActive then - if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then - v:mousemoved(x - v.x, y - v.y) + for _, guiElement in pairs(self.menus) do + if guiElement.isActive then + if guiElement:areCoordInside(x, y) then + local xx, yy = guiElement:getRelativeCoordinate(x, y) + guiElement:mousemoved(xx, yy) break; end end @@ -235,10 +238,11 @@ end function MenuSystem:mousepressed( x, y, button, istouch ) if (self.isActive) then - for k,v in pairs(self.menus) do - if v.isActive then - if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then - v:mousepressed(x - v.x, y - v.y, button, istouch ) + for _, guiElement in pairs(self.menus) do + if guiElement.isActive then + if guiElement:areCoordInside(x, y) then + local xx, yy = guiElement:getRelativeCoordinate(x, y) + guiElement:mousepressed(xx, yy, button, istouch ) break; end end @@ -251,10 +255,10 @@ end function MenuSystem:getDrawList() local drawList = {} - for k,v in pairs(self.menus) do + for name, guiElement in pairs(self.menus) do local drawObject = {} - drawObject.name = k - drawObject.depth = v.depth + drawObject.name = name + drawObject.depth = guiElement.depth table.insert(drawList, drawObject) end table.sort(drawList, function(a,b) return a.depth > b.depth end) @@ -267,10 +271,10 @@ function MenuSystem:draw(dt) -- Draw all the menus self.drawList = self:getDrawList() - for i,v in ipairs(self.drawList) do - local v2 = self.menus[v.name] - if (v2.isVisible) then - v2:draw(dt) + for _, drawObject in ipairs(self.drawList) do + local guiElement = self.menus[drawObject.name] + if (guiElement.isVisible) then + guiElement:draw(dt) end end