chore: utilisation d'une liste pour stocker les scenes

This commit is contained in:
Kazhnuz 2024-10-26 13:21:51 +02:00
parent 7fd8db592d
commit 89f57e36d7
5 changed files with 43 additions and 28 deletions

View file

@ -30,32 +30,36 @@ local SceneManager = Object:extend()
function SceneManager:new(controller) function SceneManager:new(controller)
self.controller = controller self.controller = controller
self.currentScene = nil
self.nextScene = nil self.nextScene = nil
self.storage = {} self.storage = {}
self.scenes = {}
end end
function SceneManager:setScene(scene) function SceneManager:setScene(scene)
--self.currentScene = nil
self.nextScene = scene self.nextScene = scene
end end
function SceneManager:back()
self.nextScene = "back"
end
function SceneManager:haveStoredScene(name) function SceneManager:haveStoredScene(name)
return (self.storage[name] ~= nil) return (self.storage[name] ~= nil)
end end
function SceneManager:storeCurrentScene(name) function SceneManager:storeCurrentScene(name)
self.storage[name] = self.currentScene self.storage[name] = self:getCurrent()
end end
function SceneManager:setStoredScene(name) function SceneManager:setStoredScene(name)
local storedScene = self.storage[name] local storedScene = self.storage[name]
if storedScene ~= nil then if storedScene ~= nil then
self.currentScene = storedScene self.nextScene = storedScene
self.storage[name] = nil self.storage[name] = nil
collectgarbage() collectgarbage()
self.currentScene:restored() self.nextScene:restored()
end end
end end
@ -64,7 +68,11 @@ function SceneManager:clearStorage()
end end
function SceneManager:clearScene() function SceneManager:clearScene()
self.currentScene = nil self.scenes = {}
end
function SceneManager:getCurrent()
return self.scenes[#self.scenes]
end end
-- UPDATE FUNCTIONS -- UPDATE FUNCTIONS
@ -72,13 +80,17 @@ end
function SceneManager:update(dt) function SceneManager:update(dt)
if (self.nextScene ~= nil) then if (self.nextScene ~= nil) then
self.currentScene = self.nextScene if (self.nextScene == "back") then
table.remove(self.scenes, #self.scenes)
else
table.insert(self.scenes, self.nextScene)
end
self.nextScene = nil self.nextScene = nil
collectgarbage() collectgarbage()
end end
if (self.currentScene ~= nil) then if (self:getCurrent() ~= nil) then
self.currentScene:updateScene(dt) self:getCurrent():updateScene(dt)
end end
end end
@ -86,16 +98,17 @@ end
-- Send pointer data to the scene -- Send pointer data to the scene
function SceneManager:mousemoved(x, y, dx, dy) function SceneManager:mousemoved(x, y, dx, dy)
if (self.currentScene ~= nil) then if (self:getCurrent() ~= nil) then
self.currentScene.mouse.x, local scene = self:getCurrent()
self.currentScene.mouse.y = x, y scene.mouse.x,
self.currentScene:mousemoved(x, y, dx, dy) scene.mouse.y = x, y
scene:mousemoved(x, y, dx, dy)
end end
end end
function SceneManager:mousepressed( x, y, button, istouch ) function SceneManager:mousepressed( x, y, button, istouch )
if (self.currentScene ~= nil) then if (self:getCurrent() ~= nil) then
self.currentScene:mousepressed( x, y, button, istouch ) self:getCurrent():mousepressed( x, y, button, istouch )
end end
end end
@ -103,14 +116,14 @@ end
-- Add send keys functions to the scene -- Add send keys functions to the scene
function SceneManager:keypressed( key, scancode, isrepeat ) function SceneManager:keypressed( key, scancode, isrepeat )
if (self.currentScene ~= nil) then if (self:getCurrent() ~= nil) then
self.currentScene:keypressed( key, scancode, isrepeat ) self:getCurrent():keypressed( key, scancode, isrepeat )
end end
end end
function SceneManager:keyreleased( key ) function SceneManager:keyreleased( key )
if (self.currentScene ~= nil) then if (self:getCurrent() ~= nil) then
self.currentScene:keyreleased( key ) self:getCurrent():keyreleased( key )
end end
end end
@ -119,14 +132,16 @@ end
function SceneManager:draw() function SceneManager:draw()
self.controller.screen:apply() self.controller.screen:apply()
if (self.currentScene ~= nil) then if (self:getCurrent() ~= nil) then
self.currentScene:drawScene() self:getCurrent():drawScene()
end end
self.controller.screen:cease() self.controller.screen:cease()
end end
function SceneManager:redraw() function SceneManager:redraw()
self.currentScene:redraw() if (self:getCurrent() ~= nil) then
self:getCurrent():redraw()
end
end end
return SceneManager return SceneManager

View file

@ -19,7 +19,7 @@ function GuiElement:new(name, x, y, w, h)
end end
function GuiElement:initWrapper() function GuiElement:initWrapper()
self.scene = core.scenemanager.nextScene or core.scenemanager.currentScene self.scene = core.scenemanager.nextScene or core.scenemanagerself:getCurrent()
self.gui = self.scene.gui self.gui = self.scene.gui
self.assets = self.scene.assets self.assets = self.scene.assets
end end

View file

@ -46,7 +46,7 @@ function BaseWidget:new(menuName)
end end
function BaseWidget:initWrapper() function BaseWidget:initWrapper()
self.scene = core.scenemanager.nextScene or core.scenemanager.currentScene self.scene = core.scenemanager.nextScene or core.scenemanager:getCurrent()
self.gui = self.scene.gui self.gui = self.scene.gui
self.assets = self.scene.assets self.assets = self.scene.assets
end end
@ -61,11 +61,11 @@ function BaseWidget:getMenuByName(name)
end end
function BaseWidget:getScene() function BaseWidget:getScene()
return core.scenemanager.nextScene or core.scenemanager.currentScene return core.scenemanager.nextScene or core.scenemanager:getCurrent()
end end
function BaseWidget:getAssets() function BaseWidget:getAssets()
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene local scene = core.scenemanager.nextScene or core.scenemanager:getCurrent()
return scene.assets return scene.assets
end end

View file

@ -22,7 +22,7 @@ function GuiScreen:new(name)
end end
function GuiScreen:initWrapper() function GuiScreen:initWrapper()
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene local scene = core.scenemanager.nextScene or core.scenemanager:getCurrent()
self.scene = scene self.scene = scene
self.gui = scene.gui self.gui = scene.gui
-- Présent pour la compatibilité -- Présent pour la compatibilité

View file

@ -66,7 +66,7 @@ function TextMenu:generateSubmenu(pageName, label, parent, list, func, backWidge
end end
function TextMenu:setFont(fontName) function TextMenu:setFont(fontName)
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene local scene = core.scenemanager.nextScene or core.scenemanager:getCurrent()
self.font = scene.assets:getFont(fontName) self.font = scene.assets:getFont(fontName)
end end