From f37eaef79aa8a6f85c5fd8962cfa464a07dac1f6 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 15 Apr 2019 16:30:45 +0200 Subject: [PATCH] modules/menusystem: add a way to manage menusystem activity --- gamecore/modules/menusystem/init.lua | 95 ++++++++++++++++++---------- 1 file changed, 60 insertions(+), 35 deletions(-) diff --git a/gamecore/modules/menusystem/init.lua b/gamecore/modules/menusystem/init.lua index 556014d..1380743 100644 --- a/gamecore/modules/menusystem/init.lua +++ b/gamecore/modules/menusystem/init.lua @@ -41,12 +41,28 @@ MenuSystem.Widget = require(cwd .. "widgets") function MenuSystem:new() self.menus = {} self.focusedMenu = "" + self.isActive = true end function MenuSystem:reset() self.menus = {} end +-- ACTIVATION FUNCTIONS +-- Activate and deactivate the whole menusystem + +function MenuSystem:activate() + self.isActive = true +end + +function MenuSystem:deactivate() + self.isActive = false +end + +function MenuSystem:getActiveState() + return self.isActive +end + -- MENUS FUNCTIONS -- Controle the menus of the menusystem @@ -128,45 +144,52 @@ end -- Update the menus of the menusystem function MenuSystem:update(dt) - self:removeDestroyedMenus() - for k,v in pairs(self.menus) do - v:update(dt) - v:updateWidgets(dt) - end + if (self.isActive) then + self:removeDestroyedMenus() + for k,v in pairs(self.menus) do + v:update(dt) + v: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) + 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) + end end end end end - end -- MOUSE FUNCTIONS -- Send mouse inputs to the menu function MenuSystem:mousemoved(x, y, dx, dy) - 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) - break; + 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) + break; + end end end + end end function MenuSystem:mousepressed( x, y, button, istouch ) - 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 ) - break; + 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 ) + break; + end end end end @@ -188,22 +211,24 @@ function MenuSystem:getDrawList() return drawList end -function MenuSystem:draw(dt) -- On dessine les entitées - self.drawList = self:getDrawList() +function MenuSystem:draw(dt) + if (self.isActive) then + -- 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 i,v in ipairs(self.drawList) do + local v2 = self.menus[v.name] + if (v2.isVisible) then + v2:draw(dt) + end + end + + if self.menus[self.focusedMenu] ~= nil then + if (self.menus[self.focusedMenu].isVisible) then + self.menus[self.focusedMenu]:drawCursor() + end end end - - if self.menus[self.focusedMenu] ~= nil then - if (self.menus[self.focusedMenu].isVisible) then - self.menus[self.focusedMenu]:drawCursor() - end - end - end return MenuSystem