-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscenemanager.js
207 lines (173 loc) · 5.74 KB
/
scenemanager.js
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
//
// p5 SceneManager helps you create p5.js sketches with multiple states / scenes
// Each scene is a like a sketch within the main sketch. You focus on creating
// the scene like a regular sketch and SceneManager ensure scene switching
// routing the main setup(), draw(), mousePressed(), etc. events to the
// appropriate current scene.
//
// Author: Marian Veteanu
// http://github.com/mveteanu
//
function SceneManager(p)
{
this.scenes = [];
this.scene = null;
// Wire relevant p5.js events, except setup()
// If you don't call this method, you need to manually wire events
this.wire = function()
{
var me = this;
var o = p != null ? p : window;
o.draw = function(){ me.draw(); };
o.mousePressed = function(){ me.mousePressed(); };
o.mouseClicked = function(){ me.mouseClicked(); };
o.keyPressed = function(){ me.keyPressed(); };
o.windowResized = function(){ me.windowResized(); };
return me;
}
// Add a scene to the collection
// You need to add all the scenes if intend to call .showNextScene()
this.addScene = function( fnScene )
{
var oScene = new fnScene(p);
// inject p as a property of the scene
this.p = p;
// inject sceneManager as a property of the scene
oScene.sceneManager = this;
var o = { fnScene: fnScene,
oScene: oScene,
hasSetup : "setup" in oScene,
hasEnter : "enter" in oScene,
hasDraw : "draw" in oScene,
hasMousePressed : "mousePressed" in oScene,
hasMouseClicked : "mouseClicked" in oScene,
hasKeyPressed : "keyPressed" in oScene,
hasWindowResized : "windowResized" in oScene,
setupExecuted : false,
enterExecuted : false };
this.scenes.push(o);
return o;
}
// Return the index of a scene in the internal collection
this.findSceneIndex = function( fnScene )
{
for(var i = 0; i < this.scenes.length; i++)
{
var o = this.scenes[i];
if ( o.fnScene == fnScene )
return i;
}
return -1;
}
// Return a scene object wrapper
this.findScene = function( fnScene )
{
var i = this.findSceneIndex( fnScene );
return i >= 0 ? this.scenes[i] : null;
}
// Returns true if the current displayed scene is fnScene
this.isCurrent = function ( fnScene )
{
if ( this.scene == null )
return false;
return this.scene.fnScene == fnScene;
}
// Show a scene based on the function name
// Optionally you can send arguments to the scene
// Arguments will be retrieved in the scene via .sceneArgs property
this.showScene = function( fnScene, sceneArgs )
{
var o = this.findScene( fnScene );
if ( o == null )
o = this.addScene( fnScene );
// Re-arm the enter function at each show of the scene
o.enterExecuted = false;
this.scene = o;
// inject sceneArgs as a property of the scene
o.oScene.sceneArgs = sceneArgs;
}
// Show the next scene in the collection
// Useful if implementing demo applications
// where you want to advance scenes automatically
this.showNextScene = function( sceneArgs )
{
if ( this.scenes.length == 0 )
return;
var nextSceneIndex = 0;
if ( this.scene != null )
{
// search current scene...
// can be optimized to avoid searching current scene...
var i = this.findSceneIndex( this.scene.fnScene );
nextSceneIndex = i < this.scenes.length - 1 ? i + 1 : 0;
}
var nextScene = this.scenes[nextSceneIndex];
this.showScene( nextScene.fnScene, sceneArgs );
}
// This is the SceneManager .draw() method
// This will dispatch the main draw() to the
// current scene draw() method
this.draw = function()
{
if ( this.scene == null )
return;
if ( this.scene.hasSetup && !this.scene.setupExecuted )
{
this.scene.oScene.setup();
this.scene.setupExecuted = true;
}
if ( this.scene.hasEnter && !this.scene.enterExecuted )
{
this.scene.oScene.enter();
this.scene.enterExecuted = true;
}
if ( this.scene.hasDraw )
{
this.scene.oScene.draw();
}
}
// This will dispatch .mousePressed() to the
// current scene .mousePressed() method
this.mousePressed = function()
{
if ( this.scene == null )
return;
if ( this.scene.hasMousePressed )
{
this.scene.oScene.mousePressed();
}
}
// This will dispatch .mouseClicked() to the
// current scene .mouseClicked() method
this.mouseClicked = function()
{
if ( this.scene == null )
return;
if ( this.scene.hasMouseClicked )
{
this.scene.oScene.mouseClicked();
}
}
// This will dispatch .keyPressed() to the
// current scene .keyPressed() method
this.keyPressed = function()
{
if ( this.scene == null )
return;
if ( this.scene.hasKeyPressed )
{
this.scene.oScene.keyPressed();
}
}
// This will dispatch .windowResized() to the
// current scene .windowResized() method
this.windowResized = function()
{
if ( this.scene == null )
return;
if ( this.scene.hasWindowResized )
{
this.scene.oScene.windowResized();
}
}
}