From 936cdc57f3cead334a0640d20d9960114fc085ad Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 22 Mar 2019 18:02:33 +0100 Subject: [PATCH] modules/menus:add a basic depth system to the menus Fix #2 --- examples/test_menus/init.lua | 7 +++++++ gamecore/modules/menusystem/init.lua | 22 +++++++++++++++++++--- gamecore/modules/menusystem/parent.lua | 6 ++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/examples/test_menus/init.lua b/examples/test_menus/init.lua index c270b8e..d6ec700 100644 --- a/examples/test_menus/init.lua +++ b/examples/test_menus/init.lua @@ -90,6 +90,13 @@ function TestScene:new() MenuType(self.menusystem, "text_menu5", x, y, w, h, 8) MenuType(self.menusystem, "text_menu6", x, y, w, h, 8) + self.menusystem.menus["text_menu1"]:setDepth(-1) + self.menusystem.menus["text_menu2"]:setDepth(-1) + self.menusystem.menus["text_menu3"]:setDepth(-1) + self.menusystem.menus["text_menu4"]:setDepth(-1) + self.menusystem.menus["text_menu5"]:setDepth(-1) + self.menusystem.menus["text_menu6"]:setDepth(-1) + self:addTextWidget("text_menu1", 4) self:addTextWidget("text_menu2", 10) self:addTextWidget("text_menu3", 5) diff --git a/gamecore/modules/menusystem/init.lua b/gamecore/modules/menusystem/init.lua index 7e88941..6078277 100644 --- a/gamecore/modules/menusystem/init.lua +++ b/gamecore/modules/menusystem/init.lua @@ -106,10 +106,26 @@ function MenuSystem:mousepressed( x, y, button, istouch ) end end -function MenuSystem:draw(dt) -- On dessine les entitées +function MenuSystem:getDrawList() + local drawList = {} for k,v in pairs(self.menus) do - if (v.isVisible) then - v:draw(dt) + local drawObject = {} + drawObject.name = k + drawObject.depth = v.depth + table.insert(drawList, drawObject) + end + table.sort(drawList, function(a,b) return a.depth > b.depth end) + + return drawList +end + +function MenuSystem:draw(dt) -- On dessine les entitées + 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) end end diff --git a/gamecore/modules/menusystem/parent.lua b/gamecore/modules/menusystem/parent.lua index 834bffa..b9f6135 100644 --- a/gamecore/modules/menusystem/parent.lua +++ b/gamecore/modules/menusystem/parent.lua @@ -21,6 +21,8 @@ function Menu:new(menusystem, name, x, y, w, h) self.isActive = true self.isLocked = false + self.depth = 0 + self.sound = {} self.sound.asset = nil self.sound.active = false @@ -28,6 +30,10 @@ function Menu:new(menusystem, name, x, y, w, h) self:register() end +function Menu:setDepth(depth) + self.depth = depth or 0 +end + function Menu:setVisibility(visibility) if self.isLocked == false then self.isVisible = visibility