chore: make the code more readable in menumanager

This commit is contained in:
Kazhnuz 2020-11-27 19:35:00 +01:00
parent 74d8e904b2
commit dca462803f

View file

@ -110,15 +110,15 @@ function MenuSystem:menuExist(name)
return (self.menus[name] ~= nil) return (self.menus[name] ~= nil)
end end
function MenuSystem:switchMenu(menu) function MenuSystem:switchMenu(menuName)
for k,v in pairs(self.menus) do for name, guiElement in pairs(self.menus) do
if k == menu then if (name == menuName) then
v:getFocus() guiElement:getFocus()
v:setVisibility(true) guiElement:setVisibility(true)
v:setActivity(true) guiElement:setActivity(true)
else else
v:setVisibility(false) guiElement:setVisibility(false)
v:setActivity(false) guiElement:setActivity(false)
end end
end end
end end
@ -156,38 +156,39 @@ function MenuSystem:setMenuVisibility(menu, visibility)
end end
function MenuSystem:setAllMenuVisibility(visibility) function MenuSystem:setAllMenuVisibility(visibility)
for k,v in pairs(self.menus) do for _, guiElement in pairs(self.menus) do
v:setVisibility(visibility) guiElement:setVisibility(visibility)
end end
end end
function MenuSystem:setAllMenuActivity(activity) function MenuSystem:setAllMenuActivity(activity)
for k,v in pairs(self.menus) do for _, guiElement in pairs(self.menus) do
v.isActive = activity guiElement.isActive = activity
end end
end end
function MenuSystem:removeDestroyedMenus() function MenuSystem:removeDestroyedMenus()
-- On retire les entitées marquées comme supprimées -- On retire les entitées marquées comme supprimées
for k,v in pairs(self.menus) do for name, guiElement in pairs(self.menus) do
if (v.isDestroyed == true) then if (guiElement.isDestroyed == true) then
self.menus[k] = nil self.menus[name] = nil
end end
end end
end end
-- SOUND FUNCTIONS -- SOUND FUNCTIONS
-- Add sounds to every menus -- Add sounds to every menus
-- TODO: rework to be used directly by widgets later
function MenuSystem:setSoundFromSceneAssets(soundname) function MenuSystem:setSoundFromSceneAssets(soundname)
for k,v in pairs(self.menus) do for _, guiElement in pairs(self.menus) do
v:setSoundFromSceneAssets(soundname) guiElement:setSoundFromSceneAssets(soundname)
end end
end end
function MenuSystem:setSound(soundasset) function MenuSystem:setSound(soundasset)
for k,v in pairs(self.menus) do for _, guiElement in pairs(self.menus) do
v:setSound(soundasset) guiElement:setSound(soundasset)
end end
end end
@ -197,17 +198,18 @@ end
function MenuSystem:update(dt) function MenuSystem:update(dt)
if (self.isActive) then if (self.isActive) then
self:removeDestroyedMenus() self:removeDestroyedMenus()
for k,v in pairs(self.menus) do for _, guiElement in pairs(self.menus) do
v:update(dt) guiElement:update(dt)
v:updateWidgets(dt) --TODO: move this inside the menu
guiElement:updateWidgets(dt)
end end
if self.menus[self.focusedMenu] ~= nil then if self.menus[self.focusedMenu] ~= nil then
-- Only check buttons if the current focused menu is actually active -- Only check buttons if the current focused menu is actually active
if self.menus[self.focusedMenu].isActive then if self.menus[self.focusedMenu].isActive then
for k,v in pairs(self.keys) do for keyname, keydata in pairs(self.keys) do
if self.keys[k].isPressed then if self.keys[keyname].isPressed then
self.menus[self.focusedMenu]:keyreleased(k) self.menus[self.focusedMenu]:keyreleased(keyname)
end end
end end
end end
@ -221,10 +223,11 @@ end
function MenuSystem:mousemoved(x, y, dx, dy) function MenuSystem:mousemoved(x, y, dx, dy)
if (self.isActive) then if (self.isActive) then
for k,v in pairs(self.menus) do for _, guiElement in pairs(self.menus) do
if v.isActive then if guiElement.isActive then
if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then if guiElement:areCoordInside(x, y) then
v:mousemoved(x - v.x, y - v.y) local xx, yy = guiElement:getRelativeCoordinate(x, y)
guiElement:mousemoved(xx, yy)
break; break;
end end
end end
@ -235,10 +238,11 @@ end
function MenuSystem:mousepressed( x, y, button, istouch ) function MenuSystem:mousepressed( x, y, button, istouch )
if (self.isActive) then if (self.isActive) then
for k,v in pairs(self.menus) do for _, guiElement in pairs(self.menus) do
if v.isActive then if guiElement.isActive then
if (x > v.x) and (x < v.x + v.w) and (y > v.y) and (y < v.y + v.h) then if guiElement:areCoordInside(x, y) then
v:mousepressed(x - v.x, y - v.y, button, istouch ) local xx, yy = guiElement:getRelativeCoordinate(x, y)
guiElement:mousepressed(xx, yy, button, istouch )
break; break;
end end
end end
@ -251,10 +255,10 @@ end
function MenuSystem:getDrawList() function MenuSystem:getDrawList()
local drawList = {} local drawList = {}
for k,v in pairs(self.menus) do for name, guiElement in pairs(self.menus) do
local drawObject = {} local drawObject = {}
drawObject.name = k drawObject.name = name
drawObject.depth = v.depth drawObject.depth = guiElement.depth
table.insert(drawList, drawObject) table.insert(drawList, drawObject)
end end
table.sort(drawList, function(a,b) return a.depth > b.depth 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 -- Draw all the menus
self.drawList = self:getDrawList() self.drawList = self:getDrawList()
for i,v in ipairs(self.drawList) do for _, drawObject in ipairs(self.drawList) do
local v2 = self.menus[v.name] local guiElement = self.menus[drawObject.name]
if (v2.isVisible) then if (guiElement.isVisible) then
v2:draw(dt) guiElement:draw(dt)
end end
end end