From 19aa49327cae26cb4b57ee9b38722532632ff9ab Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 22 Mar 2019 17:27:59 +0100 Subject: [PATCH] modules/menus: add a simple way to switch menu --- examples/test_menus/init.lua | 7 +++---- gamecore/modules/menusystem/init.lua | 13 +++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/examples/test_menus/init.lua b/examples/test_menus/init.lua index fd0f11e..77ac6ec 100644 --- a/examples/test_menus/init.lua +++ b/examples/test_menus/init.lua @@ -33,9 +33,10 @@ local MenuWidget = Widget.Text:extend() function TestScene:new() TestScene.super.new(self) + self.assets:addImageFont("medium", "examples/test_menus/assets/medium") MenuType(self.menusystem, "testMenu1", 32, 32, 100, 24*8, 4) - Widget.Base(self.menusystem.menus["testMenu1"]) + MenuWidget(self, "testMenu1", "text_menu1") Widget.Base(self.menusystem.menus["testMenu1"]) Widget.Base(self.menusystem.menus["testMenu1"]) Widget.Base(self.menusystem.menus["testMenu1"]) @@ -78,8 +79,6 @@ function TestScene:new() self.menusystem.menus["testMenu2"].isLocked = true self.menusystem.menus["testMenu3"].isLocked = true - self.assets:addImageFont("medium", "examples/test_menus/assets/medium") - local w, h = 424/2, 240 - 48 local x, y = w / 2, 24 @@ -106,7 +105,7 @@ function TestScene:new() MenuWidget(self, "text_menu3", "text_menu6") MenuWidget(self, "text_menu3", "text_menu6") - self.menusystem:setAllMenuVisibility(false) + self.menusystem:switchMenu("testMenu1") end function TestScene:update(dt) diff --git a/gamecore/modules/menusystem/init.lua b/gamecore/modules/menusystem/init.lua index 0b0b843..7e88941 100644 --- a/gamecore/modules/menusystem/init.lua +++ b/gamecore/modules/menusystem/init.lua @@ -46,6 +46,19 @@ function MenuSystem:update(dt) end +function MenuSystem:switchMenu(menu) + for k,v in pairs(self.menus) do + if k == menu then + v:getFocus() + v:setVisibility(true) + v.isActive = true + else + v:setVisibility(false) + v.isActive = false + end + end +end + function MenuSystem:setAllMenuVisibility(visibility) for k,v in pairs(self.menus) do v:setVisibility(visibility)