Skip to content

Commit 09be06e

Browse files
committed
Added clean methods
1 parent dd5f61c commit 09be06e

File tree

2 files changed

+67
-33
lines changed

2 files changed

+67
-33
lines changed

sceneman.lua

+56-18
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@ sceneman.active = nil
55
-- Create a new scene
66
function sceneman:newscene()
77
local scene = {}
8+
scene.loaded = false
9+
scene.started = false
810

911
function scene:load() end
1012
function scene:start() end
1113
function scene:tofront() end
1214
function scene:toback() end
13-
function scene:stop() end
14-
function scene:quit(...)
15-
-- By default we simply call the
16-
-- stop function
17-
self:stop(...)
15+
function scene:clean() end
16+
function scene:stop(...) end
17+
function scene:quit(...) end
18+
19+
function scene:isloaded()
20+
return self.loaded
1821
end
1922

2023
function scene:isactive()
2124
return sceneman.active == self
2225
end
2326

27+
function scene:isstarted()
28+
return self.started
29+
end
30+
2431
function scene:update() end
2532
function scene:draw() end
2633

@@ -30,8 +37,6 @@ end
3037
-- Create and register a new scene
3138
function sceneman:new(name)
3239
local scene = self:newscene()
33-
-- local index = #self.scenes
34-
-- self.scenes[index + 1] = scene
3540
self.scenes[name] = scene
3641
return scene
3742
end
@@ -50,11 +55,20 @@ function sceneman:get(name)
5055
return scene
5156
end
5257

53-
-- Call the load function of all the
54-
-- scenes
55-
function sceneman:load(...)
56-
for _, scene in pairs(self.scenes) do
57-
scene:load(...)
58+
-- Call the load function of a
59+
-- a single scene
60+
function sceneman:load(name, ...)
61+
local scene = self:get(name)
62+
scene:load(...)
63+
scene.loaded = true
64+
return self
65+
end
66+
67+
-- Call the load function
68+
-- of every scenes
69+
function sceneman:loadall(...)
70+
for name, scene in pairs(self.scenes) do
71+
sceneman:load(name, ...)
5872
end
5973

6074
return self
@@ -71,6 +85,7 @@ end
7185
function sceneman:start(name, ...)
7286
local scene = self:get(name)
7387
scene:start(...)
88+
scene.started = true
7489
self.active = scene
7590
return self
7691
end
@@ -111,29 +126,52 @@ end
111126
function sceneman:stop(...)
112127
if self:hasactive() then
113128
self.active:stop(...)
129+
self.active.started = false
114130
end
115131

116132
self.active = nil
117133
return self
118134
end
119135

120-
-- Call the stop function of every
121-
-- registered scenes
136+
-- Stop all scenes wich
137+
-- are started
122138
function sceneman:stopall(...)
123139
for _, scene in pairs(self.scenes) do
124-
self.active:stop(...)
140+
if scene:isstarted() then
141+
scene:stop(...)
142+
scene.started = false
143+
end
125144
end
126145

127146
self.active = nil
128147
return self
129148
end
130149

131-
-- Call the stop function of each scenes
132-
function sceneman:quit(...)
133-
for _, scene in pairs(self.scenes) do
150+
-- Call the quit method of
151+
-- the given scene, wich
152+
-- unload allocated ressources
153+
-- trough the load function
154+
function sceneman:quit(name, ...)
155+
local scene = self:get(name)
156+
157+
if scene == self.active then
158+
error('scenman: can\'t quit active scene')
159+
end
160+
161+
if scene:isloaded() then
134162
scene:quit(...)
135163
end
136164

165+
scene.loaded = false
166+
return self
167+
end
168+
169+
-- Call the quit method of
170+
-- every scene
171+
function sceneman:quitall(...)
172+
for name, _ in pairs(self.scenes) do
173+
self:quit(name, ...)
174+
end
137175
return self
138176
end
139177

spec/sceneman_spec.lua

+11-15
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,24 @@ describe('Sceneman', function()
3232
end)
3333

3434
describe('load function', function()
35+
it('should call the load function of the given scene', function()
36+
local scene = sceneman:new('s')
37+
local spyfunc = spy.new(scene.load)
38+
scene.load = spyfunc
39+
sceneman:load('s')
40+
assert.spy(spyfunc).was_called(1)
41+
end)
42+
end)
43+
44+
describe('loadall function', function()
3545
it('should call the load function of each scene', function()
3646
local scene1 = sceneman:new('s1')
3747
local spyfunc1 = spy.new(scene1.load)
3848
scene1.load = spyfunc1
3949
local scene2 = sceneman:new('s2')
4050
local spyfunc2 = spy.new(scene2.load)
4151
scene2.load = spyfunc2
42-
sceneman:load()
52+
sceneman:loadall()
4353
assert.spy(spyfunc1).was_called(1)
4454
assert.spy(spyfunc2).was_called(1)
4555
end)
@@ -141,18 +151,4 @@ describe('Sceneman', function()
141151
assert.spy(spyfunc2).was_not_called()
142152
end)
143153
end)
144-
145-
describe('quit function', function()
146-
it('should call the quit function of each scene', function()
147-
local scene1 = sceneman:new('s1')
148-
local spyfunc1 = spy.new(scene1.quit)
149-
scene1.quit = spyfunc1
150-
local scene2 = sceneman:new('s2')
151-
local spyfunc2 = spy.new(scene2.quit)
152-
scene2.quit = spyfunc2
153-
sceneman:quit()
154-
assert.spy(spyfunc1).was_called(1)
155-
assert.spy(spyfunc2).was_called(1)
156-
end)
157-
end)
158154
end)

0 commit comments

Comments
 (0)