-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathScene.lua
278 lines (256 loc) · 6.92 KB
/
Scene.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
local mexico = require "mexico"
--
-- Class: Scene
--
-- Properties:
-- + startBeforeEnter [boolean] : Indicates to call start before scene will enter the stage. Default is true.
-- + resumeAfterEnter [boolean] : Indicates to call resume after scene has entered the stage. Default is false.
-- + pauseBeforeLeave [boolean] : Indicates to call pause before scene leaves the stage. Default is false.
-- + stopAfterLeave [boolean] : Indicates to call stop after scene has left the stage. Default is true.
-- + isPaused [boolean] : Indicates whether the scene is paused or not.
-- + isOnStage [boolean] : Indicates whether the scene is currently on stage or not.
-- Methods:
--
--
local Scene = mexico.class(mexico.View, mexico.EventSource)
--
-- Used to load and instanciate a scene class from the application's
-- scene folder ('scenes/<name>').
--
Scene.fromFile = function(name, ...)
local SceneClass = require ("scenes." .. name)
if not SceneClass then error("Failed to load scene from " .. tostring(name) .. ".") end
return SceneClass(name, ...)
end
--
-- Constructor
--
-- Creates a hidden display group for the scene.
--
function Scene:init(name)
mexico.View.init(self)
self.name = name
self.startBeforeEnter = true
self.resumeAfterEnter = false
self.pauseBeforeLeave = false
self.stopAfterLeave = true
self.isStarted = false
self.isPaused = false
self.transitions = mexico.TransitionManager(true)
end
--
-- Clear the scene.
--
function Scene:clear()
mexico.Object.assert(self)
self:stop() -- always stop scene before clear
mexico.View.clear(self) -- base or super call
end
--
-- Start the scene.
--
function Scene:start()
mexico.Object.assert(self)
-- already started?
if self.isStarted then
self:Stop() -- stop the scene first
elseif self.isPaused then
return false -- start not possible because scene is paused
end
-- create event object
local event = {
name = "start",
target = self,
phase = "began",
}
-- dispatch event
print(self:toString() .. ": start " .. event.phase)
self:onStart(event)
-- start all transitions
self.transitions:resumeAll();
-- dispatch event again
event.phase = "ended"
print(self:toString() .. ": start " .. event.phase)
self:onStart(event)
-- scene is started
self.isStarted = true
return true
end
--
-- Stop the scene.
--
function Scene:stop()
mexico.Object.assert(self)
-- resumable?
if not self.isStarted then
return false
end
-- create event object
local event = {
name = "stop",
target = self,
phase = "began",
}
-- dispatch event
print(self:toString() .. ": stop " .. event.phase)
self:onResume(event)
-- resume all transitions
self.transitions:cancelAll();
-- dispatch event again
event.phase = "ended"
print(self:toString() .. ": stop " .. event.phase)
self:onStop(event)
-- scene stopped
self.isStarted = false
self.isPaused = false
return true
end
--
-- Pause the scene.
--
function Scene:pause()
mexico.Object.assert(self)
-- already paused?
if self.isPaused then return false end
-- create event object
local event = {
name = "pause",
target = self,
phase = "began",
}
-- dispatch event
print(self:toString() .. ": pause " .. event.phase)
self:onPause(event)
-- pause all transitions
self.transitions:pauseAll();
-- dispatch event again
event.phase = "ended"
print(self:toString() .. ": pause " .. event.phase)
self:onPause(event)
-- scene paused
self.isPaused = true
return true
end
--
-- Resume the scene.
--
function Scene:resume()
mexico.Object.assert(self)
-- resumable?
if not self.isPaused then return false end
-- create event object
local event = {
name = "resume",
target = self,
phase = "began",
}
-- dispatch event
print(self:toString() .. ": resume " .. event.phase)
self:onResume(event)
-- resume all transitions
self.transitions:resumeAll();
-- dispatch event again
event.phase = "ended"
print(self:toString() .. ": resume " .. event.phase)
self:onResume(event)
-- scene resumed
self.isPaused = false
return true
end
--
-- IMPORTANT !!! DO NOT call or override this method !!!
--
-- Called by <SceneDirector> before and after a scene will enter or has been entered the stage.
--
function Scene:enter(event)
assert(event)
mexico.Object.assert(self)
-- update event object
event.name = "enter"
event.target = self
if (event.phase == "began") then
print(self:toString() .. ": enter " .. event.phase)
self:onEnter(event)
end
if (self.startBeforeEnter) and (event.phase == "began") and (not self.isStarted) then
self:start() -- start scene if not already started
if (self.resumeAfterEnter) then
self:pause() -- immediatly pause if we must resume after enter
end
elseif (self.resumeAfterEnter) and (event.phase == "ended") and (self.isPaused) then
self:resume() -- resume after enter if scene is paused
end
if (event.phase == "ended") then
print(self:toString() .. ": enter " .. event.phase)
self:onEnter(event)
end
end
--
-- IMPORTANT !!! DO NOT call or override this method !!!
--
-- Called by <SceneDirector> before and after a scene will leave or has been left the stage.
--
function Scene:leave(event)
assert(event)
mexico.Object.assert(self)
-- update event object
event.name = "leave"
event.target = self
if (event.phase == "began") then
print(self:toString() .. ": leave " .. event.phase)
self:onLeave(event)
end
if (self.pauseBeforeLeave) and (event.phase == "began") and (self.isStarted) and (not self.isPaused) then
self:pause()
elseif (self.stopAfterLeave) and (event.phase == "ended") and (self.isStarted) then
self:stop()
end
if (event.phase == "ended") then
print(self:toString() .. ": leave " .. event.phase)
self:onLeave(event)
end
end
--
-- Callback that is invoked every time the scene will enter or has entered the stage.
--
function Scene:onEnter(event)
self:dispatchEvent(event)
end
--
-- Callback that is invoked every time the scene will leave or has left the stage.
--
function Scene:onLeave(event)
self:dispatchEvent(event)
end
--
-- Callback that is invoked it the scene will start or has been started.
--
function Scene:onStart(event)
self:dispatchEvent(event)
end
--
-- Callback that is invoked if the scene will stop or has been stopped.
--
function Scene:onStop(event)
self:dispatchEvent(event)
end
--
-- Callback that is invoked it the scene will pause or has been paused.
--
function Scene:onPause(event)
self:dispatchEvent(event)
end
--
-- Callback that is invoked if the scene will resume or has been resumed.
--
function Scene:onResume(event)
self:dispatchEvent(event)
end
--
-- Returns a string representation of the scene (Scene <id>).
--
function Scene:toString()
mexico.Object.assert(self)
return "Scene " .. (self.name or "Unknown");
end
return Scene