From c3179dcf9d0ccbbe723520d7a6a1223ea5a98021 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 5 Dec 2021 12:26:58 +0100 Subject: [PATCH] fix: fix main menu --- examples/scenes/gameplay/plateform/pause.lua | 6 +- examples/scenes/mainmenu/init.lua | 99 +------------------- examples/scenes/mainmenu/menu.lua | 47 ++++++++++ examples/scenes/menus/options/widgets.lua | 14 +-- 4 files changed, 62 insertions(+), 104 deletions(-) create mode 100644 examples/scenes/mainmenu/menu.lua diff --git a/examples/scenes/gameplay/plateform/pause.lua b/examples/scenes/gameplay/plateform/pause.lua index 771b200..4b93cbd 100644 --- a/examples/scenes/gameplay/plateform/pause.lua +++ b/examples/scenes/gameplay/plateform/pause.lua @@ -25,7 +25,7 @@ end -- All widgets used by the pause menu function ResumeWidget:new() - self.scene = core.scenemanager.currentScene + self.scene = core.scenemanager:getScene() local label = "resume" ResumeWidget.super.new(self, MENU_NAME, "medium", label) end @@ -36,7 +36,7 @@ function ResumeWidget:action() end function RestartWidget:new() - self.scene = core.scenemanager.currentScene + self.scene = core.scenemanager:getScene() local label = "restart" RestartWidget.super.new(self, MENU_NAME, "medium", label) end @@ -47,7 +47,7 @@ function RestartWidget:action() end function ExitWidget:new() - self.scene = core.scenemanager.currentScene + self.scene = core.scenemanager:getScene() local label = "exit" ExitWidget.super.new(self, MENU_NAME, "medium", label) end diff --git a/examples/scenes/mainmenu/init.lua b/examples/scenes/mainmenu/init.lua index 22a1ca3..3930438 100644 --- a/examples/scenes/mainmenu/init.lua +++ b/examples/scenes/mainmenu/init.lua @@ -1,4 +1,4 @@ --- scenes/mainmenu :: the main menu of the different birb scenes +-- scenes/mainmenu :: the main menu of the different birb examples --[[ Copyright © 2019 Kazhnuz @@ -24,101 +24,12 @@ local Scene = require "birb.modules.scenes" local MainMenu = Scene:extend() -local TextMenu = require "birb.modules.gui.textmenu" +local Menu = require "scenes.mainmenu.menu" -local SceneWidget = TextMenu.baseWidgets.Base:extend() -local ExitWidget = TextMenu.baseWidgets.Base:extend() - -local MENU_NAME = "mainMenu" - -function MainMenu:start() +function MainMenu:new() + MainMenu.super.new(self, true, true) self.assets:batchImport("scenes.mainmenu.assets") - print(self.assets:getFont("medium")) - local menu = self:addMenu() - for i=1, 4 do - local name = i .. "player" - menu:addSubmenu(name, core.lang:translate("commons", name)) - if i == 1 then - self:addScene(scenes.Plateformer, "plateform", i) - end - self:addScene(scenes.MovePlayer, "topdown", i) - self:addScene(scenes.MovePlayer3D, "topdown3D", i) - self:addScene(scenes.Action3D, "bigmap3D", i) - if i > 1 then - self:addScene(scenes.MovePlayer, "topdown (zoom)", i, "zoom") - self:addScene(scenes.MovePlayer3D, "topdown3D (zoom)", i, "zoom") - end - end - self:addSubMenu("menus", "menu") - self:addScene(scenes.Inventory, "inventory") - self:addScene(scenes.Options, "options") - self:addScene(scenes.TestMenu, "tests") - - - self.menusystem:setSoundFromSceneAssets("navigate") - local menu = self.menusystem.menus[MENU_NAME] - menu:switch("main") - - ExitWidget(self, "main") - self.menusystem:switchMenu(MENU_NAME) -end - --- MENU FUNCTION --- Functions that serve the handling of menus - -function MainMenu:addMenu() - local w = 424/2 - local x, y = w / 2, 24 - return TextMenu(MENU_NAME, "medium", x, y, w, 8, 0) -end - -function MainMenu:addSubMenu(name, fullname) - local menu = self.menusystem.menus[MENU_NAME] - menu:addSubmenu(name, core.lang:translate("mainmenu", fullname)) -end - -function MainMenu:addScene(scene, fullname, arg1, arg2, arg3, arg4, arg5) - local args = {arg1, arg2, arg3, arg4, arg5} - SceneWidget(self, MENU_NAME, scene, fullname, args) -end - -function MainMenu:draw() - love.graphics.setColor(.3, .1, .4, 1) - love.graphics.rectangle("fill", 0, 0, 424, 240) - utils.graphics.resetColor() - love.graphics.print(math.floor(game.playtime), 8, 8) -end - --- WIDGETS --- Widgets used by menus - --- Scene widget :: switch scene - -function SceneWidget:new(scene, menu, newscene, fullname, args) - self.scene = scene - self.args = args - self.newscene = newscene - local label = core.lang:translate("mainmenu", fullname) - SceneWidget.super.new(self, MENU_NAME, label) -end - -function SceneWidget:action() - core.scenemanager:storeCurrentScene("mainmenu") - core.scenemanager:prepareTransition() - self.newscene(self.args[1], self.args[2], self.args[3], self.args[4], self.args[5]) -end - --- Exit Widget : exit the scenes - -function ExitWidget:new(scene, menu) - self.scene = scene - local label = core.lang:translate("commons", "exit") - SceneWidget.super.new(self, MENU_NAME, label) -end - -function ExitWidget:action() - game:write() - love.event.quit() + Menu(self) end return MainMenu diff --git a/examples/scenes/mainmenu/menu.lua b/examples/scenes/mainmenu/menu.lua new file mode 100644 index 0000000..5ecd2ba --- /dev/null +++ b/examples/scenes/mainmenu/menu.lua @@ -0,0 +1,47 @@ +local Parent = require "birb.modules.gui.textmenu" +local MainMenu = Parent:extend() + +local defTransitions = require "birb.modules.transitions" + +local MENU_X, MENU_Y = 24, 48 +local MENU_W = 424/3 +local MENU_ITEM_NUMBER = 8 + +function MainMenu:new() + MainMenu.super.new(self, "mainmenu", "medium", MENU_X, MENU_Y, MENU_W, MENU_ITEM_NUMBER, false) + + for i=1, 4 do + local name = i .. "player" + self:switch("main") + self:addSubmenu(name, i .. " Players") + if i == 1 then + self:addItem("Plateformer", "left", function() self:launchScene(scenes.Plateformer, i) end) + end + self:addItem("Overworld", "left", function() self:launchScene(scenes.MovePlayer, i) end) + self:addItem("Basic 3D", "left", function() self:launchScene(scenes.MovePlayer3D, i) end) + self:addItem("Complexe 3D", "left", function() self:launchScene(scenes.Action3D, i) end) + if i > 1 then + self:addItem("Basic 3D (zoom)", "left", function() self:launchScene(scenes.MovePlayer3D, i, "zoom") end) + self:addItem("Complexe 3D (zoom)", "left", function() self:launchScene(scenes.Action3D, i, "zoom") end) + end + end + self:switch("main") + self:addSubmenu("menus", "Menus") + self:addItem("Inventory", "left", function() self:launchScene(scenes.Inventory) end) + self:addItem("Options", "left", function() self:launchScene(scenes.Options) end) + self:addItem("Tests", "left", function() self:launchScene(scenes.TestMenu) end) + self:switch("main") + self:addItem("Exit game", "left", function() self:exitGame() end) + self:getFocus() +end + +function MainMenu:launchScene(scene, arg1, arg2, arg3, arg4, arg5) + local args = {arg1, arg2, arg3, arg4, arg5} + core.screen:startTransition(defTransitions.circle, defTransitions.default, function() scene(args) end, 424/2, 240/2) +end + +function MainMenu:exitGame() + love.event.quit("000") +end + +return MainMenu diff --git a/examples/scenes/menus/options/widgets.lua b/examples/scenes/menus/options/widgets.lua index 8860bc2..1711237 100644 --- a/examples/scenes/menus/options/widgets.lua +++ b/examples/scenes/menus/options/widgets.lua @@ -136,7 +136,7 @@ end function widgets.Switch:action() self:modifyKey() - local scene = core.scenemanager.currentScene + local scene = core.scenemanager:getScene() scene.assets:playSFX("confirm") self:replaceLabel(2, self:getLabel()) core.options:write() @@ -163,7 +163,7 @@ function widgets.Resolution:action() end self:replaceLabel(2, self:getLabel()) core.screen:applySettings() - local scene = core.scenemanager.currentScene + local scene = core.scenemanager:getScene() scene.assets:playSFX("confirm") self:invalidateCanvas() core.options:write() @@ -179,7 +179,7 @@ function widgets.Lang:new(lang) end function widgets.Lang:action() - local scene = core.scenemanager.currentScene + local scene = core.scenemanager:getScene() scene.assets:playSFX("confirm") core.options:setLanguage(self.lang) --self.scene.menusystem:invalidateAllWidgets() @@ -198,7 +198,7 @@ function widgets.PlayerSubMenu:new(scene, menu, sourceid) end function widgets.PlayerSubMenu:action() - local scene = core.scenemanager.currentScene + local scene = core.scenemanager:getScene() scene.assets:playSFX("confirm") scene.menusystem:switchMenu(self.newmenu) end @@ -223,14 +223,14 @@ function widgets.Key:getLabel() end function widgets.Key:action() - local scene = core.scenemanager.currentScene + local scene = core.scenemanager:getScene() scene.assets:playSFX("navigate") scene:changeKey(self) scene.menusystem:deactivate() end function widgets.Key:receiveKey( key ) - local scene = core.scenemanager.currentScene + local scene = core.scenemanager:getScene() scene.assets:playSFX("confirm") core.options:setInputKey(self.source, self.key, key) self:replaceLabel(2, self:getLabel()) @@ -289,7 +289,7 @@ end function widgets.Audio:action() local value = self:getVolume() self:setVolume(value - 20) - local scene = core.scenemanager.currentScene + local scene = core.scenemanager:getScene() scene.assets:playSFX("confirm") core.music:applyVolume() core.options:write()