chore: utilisation d'une liste pour stocker les scenes
This commit is contained in:
parent
7fd8db592d
commit
89f57e36d7
5 changed files with 43 additions and 28 deletions
|
@ -30,32 +30,36 @@ local SceneManager = Object:extend()
|
|||
|
||||
function SceneManager:new(controller)
|
||||
self.controller = controller
|
||||
self.currentScene = nil
|
||||
self.nextScene = nil
|
||||
|
||||
self.storage = {}
|
||||
|
||||
self.scenes = {}
|
||||
end
|
||||
|
||||
function SceneManager:setScene(scene)
|
||||
--self.currentScene = nil
|
||||
self.nextScene = scene
|
||||
end
|
||||
|
||||
function SceneManager:back()
|
||||
self.nextScene = "back"
|
||||
end
|
||||
|
||||
function SceneManager:haveStoredScene(name)
|
||||
return (self.storage[name] ~= nil)
|
||||
end
|
||||
|
||||
function SceneManager:storeCurrentScene(name)
|
||||
self.storage[name] = self.currentScene
|
||||
self.storage[name] = self:getCurrent()
|
||||
end
|
||||
|
||||
function SceneManager:setStoredScene(name)
|
||||
local storedScene = self.storage[name]
|
||||
if storedScene ~= nil then
|
||||
self.currentScene = storedScene
|
||||
self.nextScene = storedScene
|
||||
self.storage[name] = nil
|
||||
collectgarbage()
|
||||
self.currentScene:restored()
|
||||
self.nextScene:restored()
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -64,7 +68,11 @@ function SceneManager:clearStorage()
|
|||
end
|
||||
|
||||
function SceneManager:clearScene()
|
||||
self.currentScene = nil
|
||||
self.scenes = {}
|
||||
end
|
||||
|
||||
function SceneManager:getCurrent()
|
||||
return self.scenes[#self.scenes]
|
||||
end
|
||||
|
||||
-- UPDATE FUNCTIONS
|
||||
|
@ -72,13 +80,17 @@ end
|
|||
|
||||
function SceneManager:update(dt)
|
||||
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
|
||||
collectgarbage()
|
||||
end
|
||||
|
||||
if (self.currentScene ~= nil) then
|
||||
self.currentScene:updateScene(dt)
|
||||
if (self:getCurrent() ~= nil) then
|
||||
self:getCurrent():updateScene(dt)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -86,16 +98,17 @@ end
|
|||
-- Send pointer data to the scene
|
||||
|
||||
function SceneManager:mousemoved(x, y, dx, dy)
|
||||
if (self.currentScene ~= nil) then
|
||||
self.currentScene.mouse.x,
|
||||
self.currentScene.mouse.y = x, y
|
||||
self.currentScene:mousemoved(x, y, dx, dy)
|
||||
if (self:getCurrent() ~= nil) then
|
||||
local scene = self:getCurrent()
|
||||
scene.mouse.x,
|
||||
scene.mouse.y = x, y
|
||||
scene:mousemoved(x, y, dx, dy)
|
||||
end
|
||||
end
|
||||
|
||||
function SceneManager:mousepressed( x, y, button, istouch )
|
||||
if (self.currentScene ~= nil) then
|
||||
self.currentScene:mousepressed( x, y, button, istouch )
|
||||
if (self:getCurrent() ~= nil) then
|
||||
self:getCurrent():mousepressed( x, y, button, istouch )
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -103,14 +116,14 @@ end
|
|||
-- Add send keys functions to the scene
|
||||
|
||||
function SceneManager:keypressed( key, scancode, isrepeat )
|
||||
if (self.currentScene ~= nil) then
|
||||
self.currentScene:keypressed( key, scancode, isrepeat )
|
||||
if (self:getCurrent() ~= nil) then
|
||||
self:getCurrent():keypressed( key, scancode, isrepeat )
|
||||
end
|
||||
end
|
||||
|
||||
function SceneManager:keyreleased( key )
|
||||
if (self.currentScene ~= nil) then
|
||||
self.currentScene:keyreleased( key )
|
||||
if (self:getCurrent() ~= nil) then
|
||||
self:getCurrent():keyreleased( key )
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -119,14 +132,16 @@ end
|
|||
|
||||
function SceneManager:draw()
|
||||
self.controller.screen:apply()
|
||||
if (self.currentScene ~= nil) then
|
||||
self.currentScene:drawScene()
|
||||
if (self:getCurrent() ~= nil) then
|
||||
self:getCurrent():drawScene()
|
||||
end
|
||||
self.controller.screen:cease()
|
||||
end
|
||||
|
||||
function SceneManager:redraw()
|
||||
self.currentScene:redraw()
|
||||
if (self:getCurrent() ~= nil) then
|
||||
self:getCurrent():redraw()
|
||||
end
|
||||
end
|
||||
|
||||
return SceneManager
|
||||
|
|
|
@ -19,7 +19,7 @@ function GuiElement:new(name, x, y, w, h)
|
|||
end
|
||||
|
||||
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.assets = self.scene.assets
|
||||
end
|
||||
|
|
|
@ -46,7 +46,7 @@ function BaseWidget:new(menuName)
|
|||
end
|
||||
|
||||
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.assets = self.scene.assets
|
||||
end
|
||||
|
@ -61,11 +61,11 @@ function BaseWidget:getMenuByName(name)
|
|||
end
|
||||
|
||||
function BaseWidget:getScene()
|
||||
return core.scenemanager.nextScene or core.scenemanager.currentScene
|
||||
return core.scenemanager.nextScene or core.scenemanager:getCurrent()
|
||||
end
|
||||
|
||||
function BaseWidget:getAssets()
|
||||
local scene = core.scenemanager.nextScene or core.scenemanager.currentScene
|
||||
local scene = core.scenemanager.nextScene or core.scenemanager:getCurrent()
|
||||
return scene.assets
|
||||
end
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ function GuiScreen:new(name)
|
|||
end
|
||||
|
||||
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.gui = scene.gui
|
||||
-- Présent pour la compatibilité
|
||||
|
|
|
@ -66,7 +66,7 @@ function TextMenu:generateSubmenu(pageName, label, parent, list, func, backWidge
|
|||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue