@vpmedia/phaser 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -3
- package/dist/phaser.cjs +1 -1
- package/dist/phaser.cjs.LICENSE.txt +1 -1
- package/dist/phaser.cjs.map +1 -1
- package/dist/phaser.js +1 -1
- package/dist/phaser.js.LICENSE.txt +1 -1
- package/dist/phaser.js.map +1 -1
- package/package.json +23 -17
- package/src/index.js +142 -0
- package/src/phaser/core/animation.js +355 -0
- package/src/phaser/core/animation_manager.js +238 -0
- package/src/phaser/core/animation_parser.js +133 -0
- package/src/phaser/core/array_set.js +107 -0
- package/src/phaser/core/cache.js +558 -0
- package/src/phaser/core/const.js +106 -0
- package/src/phaser/core/device.js +67 -0
- package/src/phaser/core/device_util.js +388 -0
- package/src/phaser/core/dom.js +207 -0
- package/src/phaser/core/event_manager.js +243 -0
- package/src/phaser/core/factory.js +74 -0
- package/src/phaser/core/frame.js +75 -0
- package/src/phaser/core/frame_data.js +84 -0
- package/src/phaser/core/frame_util.js +33 -0
- package/src/phaser/core/game.js +412 -0
- package/src/phaser/core/input.js +401 -0
- package/src/phaser/core/input_button.js +102 -0
- package/src/phaser/core/input_handler.js +687 -0
- package/src/phaser/core/input_mouse.js +289 -0
- package/src/phaser/core/input_mspointer.js +197 -0
- package/src/phaser/core/input_pointer.js +427 -0
- package/src/phaser/core/input_touch.js +157 -0
- package/src/phaser/core/loader.js +1057 -0
- package/src/phaser/core/loader_parser.js +109 -0
- package/src/phaser/core/raf.js +46 -0
- package/src/phaser/core/raf_fb.js +75 -0
- package/src/phaser/core/raf_to.js +34 -0
- package/src/phaser/core/scale_manager.js +806 -0
- package/src/phaser/core/scene.js +65 -0
- package/src/phaser/core/scene_manager.js +309 -0
- package/src/phaser/core/signal.js +175 -0
- package/src/phaser/core/signal_binding.js +69 -0
- package/src/phaser/core/sound.js +538 -0
- package/src/phaser/core/sound_manager.js +364 -0
- package/src/phaser/core/stage.js +108 -0
- package/src/phaser/core/time.js +203 -0
- package/src/phaser/core/timer.js +276 -0
- package/src/phaser/core/timer_event.js +21 -0
- package/src/phaser/core/tween.js +329 -0
- package/src/phaser/core/tween_data.js +258 -0
- package/src/phaser/core/tween_easing.js +341 -0
- package/src/phaser/core/tween_manager.js +185 -0
- package/src/phaser/core/world.js +18 -0
- package/src/phaser/display/bitmap_text.js +322 -0
- package/src/phaser/display/button.js +194 -0
- package/src/phaser/display/canvas/buffer.js +36 -0
- package/src/phaser/display/canvas/graphics.js +227 -0
- package/src/phaser/display/canvas/masker.js +39 -0
- package/src/phaser/display/canvas/pool.js +126 -0
- package/src/phaser/display/canvas/renderer.js +123 -0
- package/src/phaser/display/canvas/tinter.js +144 -0
- package/src/phaser/display/canvas/util.js +159 -0
- package/src/phaser/display/display_object.js +597 -0
- package/src/phaser/display/graphics.js +723 -0
- package/src/phaser/display/graphics_data.js +27 -0
- package/src/phaser/display/graphics_data_util.js +15 -0
- package/src/phaser/display/group.js +227 -0
- package/src/phaser/display/image.js +288 -0
- package/src/phaser/display/sprite_batch.js +15 -0
- package/src/phaser/display/sprite_util.js +250 -0
- package/src/phaser/display/text.js +1089 -0
- package/src/phaser/display/webgl/abstract_filter.js +25 -0
- package/src/phaser/display/webgl/base_texture.js +68 -0
- package/src/phaser/display/webgl/blend_manager.js +35 -0
- package/src/phaser/display/webgl/earcut.js +662 -0
- package/src/phaser/display/webgl/earcut_node.js +28 -0
- package/src/phaser/display/webgl/fast_sprite_batch.js +242 -0
- package/src/phaser/display/webgl/filter_manager.js +46 -0
- package/src/phaser/display/webgl/filter_texture.js +61 -0
- package/src/phaser/display/webgl/graphics.js +624 -0
- package/src/phaser/display/webgl/graphics_data.js +42 -0
- package/src/phaser/display/webgl/mask_manager.js +36 -0
- package/src/phaser/display/webgl/render_texture.js +81 -0
- package/src/phaser/display/webgl/renderer.js +234 -0
- package/src/phaser/display/webgl/shader/complex.js +74 -0
- package/src/phaser/display/webgl/shader/fast.js +97 -0
- package/src/phaser/display/webgl/shader/normal.js +225 -0
- package/src/phaser/display/webgl/shader/primitive.js +72 -0
- package/src/phaser/display/webgl/shader/strip.js +77 -0
- package/src/phaser/display/webgl/shader_manager.js +89 -0
- package/src/phaser/display/webgl/sprite_batch.js +320 -0
- package/src/phaser/display/webgl/stencil_manager.js +170 -0
- package/src/phaser/display/webgl/texture.js +117 -0
- package/src/phaser/display/webgl/texture_util.js +34 -0
- package/src/phaser/display/webgl/util.js +78 -0
- package/src/phaser/geom/circle.js +186 -0
- package/src/phaser/geom/ellipse.js +65 -0
- package/src/phaser/geom/line.js +190 -0
- package/src/phaser/geom/matrix.js +147 -0
- package/src/phaser/geom/point.js +164 -0
- package/src/phaser/geom/polygon.js +140 -0
- package/src/phaser/geom/rectangle.js +306 -0
- package/src/phaser/geom/rounded_rectangle.js +36 -0
- package/src/phaser/geom/util/circle.js +122 -0
- package/src/phaser/geom/util/ellipse.js +34 -0
- package/src/phaser/geom/util/line.js +135 -0
- package/src/phaser/geom/util/matrix.js +53 -0
- package/src/phaser/geom/util/point.js +296 -0
- package/src/phaser/geom/util/polygon.js +28 -0
- package/src/phaser/geom/util/rectangle.js +229 -0
- package/src/phaser/geom/util/rounded_rectangle.js +32 -0
- package/src/phaser/util/math.js +297 -0
- package/src/phaser/util/string.js +32 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Andras Csizmadia <andras@vpmedia.hu>
|
|
3
|
+
* @author Richard Davey <rich@photonstorm.com>
|
|
4
|
+
* @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
|
|
5
|
+
*/
|
|
6
|
+
export default class {
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
this.game = null;
|
|
10
|
+
this.key = '';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
init() {
|
|
14
|
+
// inherit
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
preload() {
|
|
18
|
+
// inherit
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
loadUpdate() {
|
|
22
|
+
// inherit
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
loadRender() {
|
|
26
|
+
// inherit
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
create() {
|
|
30
|
+
// inherit
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
update() {
|
|
34
|
+
// inherit
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
preRender() {
|
|
38
|
+
// inherit
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
render() {
|
|
42
|
+
// inherit
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
resize() {
|
|
46
|
+
// inherit
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
paused() {
|
|
50
|
+
// inherit
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
resumed() {
|
|
54
|
+
// inherit
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
pauseUpdate() {
|
|
58
|
+
// inherit
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
shutdown() {
|
|
62
|
+
// inherit
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Andras Csizmadia <andras@vpmedia.hu>
|
|
3
|
+
* @author Richard Davey <rich@photonstorm.com>
|
|
4
|
+
* @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
|
|
5
|
+
*/
|
|
6
|
+
import Scene from './scene';
|
|
7
|
+
import { RENDER_CANVAS } from './const';
|
|
8
|
+
|
|
9
|
+
export default class {
|
|
10
|
+
|
|
11
|
+
constructor(game, pendingState) {
|
|
12
|
+
this.game = game;
|
|
13
|
+
this.states = {};
|
|
14
|
+
this._pendingState = null;
|
|
15
|
+
if (typeof pendingState !== 'undefined' && pendingState !== null) {
|
|
16
|
+
this._pendingState = pendingState;
|
|
17
|
+
}
|
|
18
|
+
this._clearWorld = false;
|
|
19
|
+
this._clearCache = false;
|
|
20
|
+
this._created = false;
|
|
21
|
+
this._args = [];
|
|
22
|
+
this.current = '';
|
|
23
|
+
this.onInitCallback = null;
|
|
24
|
+
this.onPreloadCallback = null;
|
|
25
|
+
this.onCreateCallback = null;
|
|
26
|
+
this.onUpdateCallback = null;
|
|
27
|
+
this.onRenderCallback = null;
|
|
28
|
+
this.onResizeCallback = null;
|
|
29
|
+
this.onPreRenderCallback = null;
|
|
30
|
+
this.onLoadUpdateCallback = null;
|
|
31
|
+
this.onLoadRenderCallback = null;
|
|
32
|
+
this.onPausedCallback = null;
|
|
33
|
+
this.onResumedCallback = null;
|
|
34
|
+
this.onPauseUpdateCallback = null;
|
|
35
|
+
this.onShutDownCallback = null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
boot() {
|
|
39
|
+
if (this._pendingState !== null && typeof this._pendingState !== 'string') {
|
|
40
|
+
this.add('default', this._pendingState, true);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
add(key, state, autoStart = false) {
|
|
45
|
+
let newState = null;
|
|
46
|
+
if (state instanceof Scene) {
|
|
47
|
+
newState = state;
|
|
48
|
+
} else if (typeof state === 'object') {
|
|
49
|
+
newState = state;
|
|
50
|
+
newState.game = this.game;
|
|
51
|
+
} else if (typeof state === 'function') {
|
|
52
|
+
newState = new state(this.game);
|
|
53
|
+
}
|
|
54
|
+
this.states[key] = newState;
|
|
55
|
+
if (autoStart) {
|
|
56
|
+
if (this.game.isBooted) {
|
|
57
|
+
this.start(key);
|
|
58
|
+
} else {
|
|
59
|
+
this._pendingState = key;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return newState;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
remove(key) {
|
|
66
|
+
if (this.current === key) {
|
|
67
|
+
this.callbackContext = null;
|
|
68
|
+
this.onInitCallback = null;
|
|
69
|
+
this.onShutDownCallback = null;
|
|
70
|
+
this.onPreloadCallback = null;
|
|
71
|
+
this.onLoadRenderCallback = null;
|
|
72
|
+
this.onLoadUpdateCallback = null;
|
|
73
|
+
this.onCreateCallback = null;
|
|
74
|
+
this.onUpdateCallback = null;
|
|
75
|
+
this.onPreRenderCallback = null;
|
|
76
|
+
this.onRenderCallback = null;
|
|
77
|
+
this.onResizeCallback = null;
|
|
78
|
+
this.onPausedCallback = null;
|
|
79
|
+
this.onResumedCallback = null;
|
|
80
|
+
this.onPauseUpdateCallback = null;
|
|
81
|
+
}
|
|
82
|
+
delete this.states[key];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
start(key, clearWorld = true, clearCache = false, ...args) {
|
|
86
|
+
if (this.checkState(key)) {
|
|
87
|
+
// Place the state in the queue. It will be started the next time the game loop begins.
|
|
88
|
+
this._pendingState = key;
|
|
89
|
+
this._clearWorld = clearWorld;
|
|
90
|
+
this._clearCache = clearCache;
|
|
91
|
+
if (args && args.length > 0) {
|
|
92
|
+
this._args = args.slice();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
restart(clearWorld = true, clearCache = false, ...args) {
|
|
98
|
+
this._pendingState = this.current;
|
|
99
|
+
this._clearWorld = clearWorld;
|
|
100
|
+
this._clearCache = clearCache;
|
|
101
|
+
if (args && args.length > 0) {
|
|
102
|
+
this._args = args.slice();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
preUpdate() {
|
|
107
|
+
if (this._pendingState && this.game.isBooted) {
|
|
108
|
+
// var previousStateKey = this.current;
|
|
109
|
+
// Already got a state running?
|
|
110
|
+
this.clearCurrentState();
|
|
111
|
+
this.setCurrentState(this._pendingState);
|
|
112
|
+
this.game.world.x = 0;
|
|
113
|
+
this.game.world.y = 0;
|
|
114
|
+
if (this.current !== this._pendingState) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
this._pendingState = null;
|
|
118
|
+
// If StateManager.start has been called from the init of a State that ALSO has a preload, then
|
|
119
|
+
// onPreloadCallback will be set, but must be ignored
|
|
120
|
+
if (this.onPreloadCallback) {
|
|
121
|
+
this.game.load.reset(true);
|
|
122
|
+
this.onPreloadCallback.call(this.callbackContext, this.game);
|
|
123
|
+
// Is the loader empty?
|
|
124
|
+
if (this.game.load.totalQueuedFiles() === 0 && this.game.load.totalQueuedPacks() === 0) {
|
|
125
|
+
this.loadComplete();
|
|
126
|
+
} else {
|
|
127
|
+
// Start the loader going as we have something in the queue
|
|
128
|
+
this.game.load.start();
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
// No init? Then there was nothing to load either
|
|
132
|
+
this.loadComplete();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
clearCurrentState() {
|
|
138
|
+
if (this.current) {
|
|
139
|
+
if (this.onShutDownCallback) {
|
|
140
|
+
this.onShutDownCallback.call(this.callbackContext, this.game);
|
|
141
|
+
}
|
|
142
|
+
this.game.tweens.removeAll();
|
|
143
|
+
this.game.input.reset(true);
|
|
144
|
+
this.game.time.removeAll();
|
|
145
|
+
this.game.scale.reset(this._clearWorld);
|
|
146
|
+
if (this._clearWorld) {
|
|
147
|
+
this.game.world.destroy(true, true);
|
|
148
|
+
if (this._clearCache) {
|
|
149
|
+
this.game.cache.destroy();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
checkState(key) {
|
|
156
|
+
if (this.states[key]) {
|
|
157
|
+
if (this.states[key].preload || this.states[key].create || this.states[key].update || this.states[key].render) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
link(key) {
|
|
166
|
+
this.states[key].game = this.game;
|
|
167
|
+
this.states[key].key = key;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
unlink(key) {
|
|
171
|
+
if (this.states[key]) {
|
|
172
|
+
this.states[key].game = null;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
setCurrentState(key) {
|
|
177
|
+
this.callbackContext = this.states[key];
|
|
178
|
+
this.link(key);
|
|
179
|
+
// Used when the state is set as being the current active state
|
|
180
|
+
this.onInitCallback = this.callbackContext.init || this.dummy;
|
|
181
|
+
this.onPreloadCallback = this.callbackContext.preload || null;
|
|
182
|
+
this.onLoadRenderCallback = this.callbackContext.loadRender || null;
|
|
183
|
+
this.onLoadUpdateCallback = this.callbackContext.loadUpdate || null;
|
|
184
|
+
this.onCreateCallback = this.callbackContext.create || null;
|
|
185
|
+
this.onUpdateCallback = this.callbackContext.update || null;
|
|
186
|
+
this.onPreRenderCallback = this.callbackContext.preRender || null;
|
|
187
|
+
this.onRenderCallback = this.callbackContext.render || null;
|
|
188
|
+
this.onResizeCallback = this.callbackContext.resize || null;
|
|
189
|
+
this.onPausedCallback = this.callbackContext.paused || null;
|
|
190
|
+
this.onResumedCallback = this.callbackContext.resumed || null;
|
|
191
|
+
this.onPauseUpdateCallback = this.callbackContext.pauseUpdate || null;
|
|
192
|
+
this.onShutDownCallback = this.callbackContext.shutdown || this.dummy;
|
|
193
|
+
this.current = key;
|
|
194
|
+
this._created = false;
|
|
195
|
+
this.onInitCallback.apply(this.callbackContext, this._args);
|
|
196
|
+
if (key === this._pendingState) {
|
|
197
|
+
this._args = [];
|
|
198
|
+
}
|
|
199
|
+
this.game.isKickStart = true;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
getCurrentState() {
|
|
203
|
+
return this.states[this.current];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
loadComplete() {
|
|
207
|
+
if (this._created === false && this.onLoadUpdateCallback) {
|
|
208
|
+
this.onLoadUpdateCallback.call(this.callbackContext, this.game);
|
|
209
|
+
}
|
|
210
|
+
if (this._created === false && this.onCreateCallback) {
|
|
211
|
+
this._created = true;
|
|
212
|
+
this.onCreateCallback.call(this.callbackContext, this.game);
|
|
213
|
+
} else {
|
|
214
|
+
this._created = true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
pause() {
|
|
219
|
+
if (this._created && this.onPausedCallback) {
|
|
220
|
+
this.onPausedCallback.call(this.callbackContext, this.game);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
resume() {
|
|
225
|
+
if (this._created && this.onResumedCallback) {
|
|
226
|
+
this.onResumedCallback.call(this.callbackContext, this.game);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
update() {
|
|
231
|
+
if (this._created) {
|
|
232
|
+
if (this.onUpdateCallback) {
|
|
233
|
+
this.onUpdateCallback.call(this.callbackContext, this.game);
|
|
234
|
+
}
|
|
235
|
+
} else if (this.onLoadUpdateCallback) {
|
|
236
|
+
this.onLoadUpdateCallback.call(this.callbackContext, this.game);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
pauseUpdate() {
|
|
241
|
+
if (this._created) {
|
|
242
|
+
if (this.onPauseUpdateCallback) {
|
|
243
|
+
this.onPauseUpdateCallback.call(this.callbackContext, this.game);
|
|
244
|
+
}
|
|
245
|
+
} else if (this.onLoadUpdateCallback) {
|
|
246
|
+
this.onLoadUpdateCallback.call(this.callbackContext, this.game);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
preRender(elapsedTime) {
|
|
251
|
+
if (this._created && this.onPreRenderCallback) {
|
|
252
|
+
this.onPreRenderCallback.call(this.callbackContext, this.game, elapsedTime);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
resize(width, height) {
|
|
257
|
+
if (this.onResizeCallback) {
|
|
258
|
+
this.onResizeCallback.call(this.callbackContext, width, height);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
render() {
|
|
263
|
+
if (this._created) {
|
|
264
|
+
if (this.onRenderCallback) {
|
|
265
|
+
if (this.game.renderer.type === RENDER_CANVAS) {
|
|
266
|
+
this.game.context.save();
|
|
267
|
+
this.game.context.setTransform(1, 0, 0, 1, 0, 0);
|
|
268
|
+
this.onRenderCallback.call(this.callbackContext, this.game);
|
|
269
|
+
this.game.context.restore();
|
|
270
|
+
} else {
|
|
271
|
+
this.onRenderCallback.call(this.callbackContext, this.game);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
} else if (this.onLoadRenderCallback) {
|
|
275
|
+
this.onLoadRenderCallback.call(this.callbackContext, this.game);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
destroy() {
|
|
280
|
+
this._clearWorld = true;
|
|
281
|
+
this._clearCache = true;
|
|
282
|
+
this.clearCurrentState();
|
|
283
|
+
this.callbackContext = null;
|
|
284
|
+
this.onInitCallback = null;
|
|
285
|
+
this.onShutDownCallback = null;
|
|
286
|
+
this.onPreloadCallback = null;
|
|
287
|
+
this.onLoadRenderCallback = null;
|
|
288
|
+
this.onLoadUpdateCallback = null;
|
|
289
|
+
this.onCreateCallback = null;
|
|
290
|
+
this.onUpdateCallback = null;
|
|
291
|
+
this.onRenderCallback = null;
|
|
292
|
+
this.onPausedCallback = null;
|
|
293
|
+
this.onResumedCallback = null;
|
|
294
|
+
this.onPauseUpdateCallback = null;
|
|
295
|
+
this.game = null;
|
|
296
|
+
this.states = {};
|
|
297
|
+
this._pendingState = null;
|
|
298
|
+
this.current = '';
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
dummy() {
|
|
302
|
+
// pass
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
get created() {
|
|
306
|
+
return this._created;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Andras Csizmadia <andras@vpmedia.hu>
|
|
3
|
+
* @author Richard Davey <rich@photonstorm.com>
|
|
4
|
+
* @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
|
|
5
|
+
*/
|
|
6
|
+
import SignalBinding from './signal_binding';
|
|
7
|
+
|
|
8
|
+
export default class {
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
this._bindings = null;
|
|
12
|
+
this._prevParams = null;
|
|
13
|
+
this.memorize = false;
|
|
14
|
+
this._shouldPropagate = true;
|
|
15
|
+
this.active = true;
|
|
16
|
+
this._boundDispatch = false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
validateListener(listener, fnName) {
|
|
20
|
+
if (typeof listener !== 'function') {
|
|
21
|
+
throw new Error('Signal: listener is a required param of {fn}() and should be a Function.'.replace('{fn}', fnName));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
_registerListener(listener, isOnce, listenerContext, priority, args) {
|
|
26
|
+
const prevIndex = this._indexOfListener(listener, listenerContext);
|
|
27
|
+
let binding;
|
|
28
|
+
if (prevIndex !== -1) {
|
|
29
|
+
binding = this._bindings[prevIndex];
|
|
30
|
+
if (binding.isOnce() !== isOnce) {
|
|
31
|
+
throw new Error('You cannot add' + (isOnce ? '' : 'Once') + '() then add' + (!isOnce ? '' : 'Once') + '() the same listener without removing the relationship first.');
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
binding = new SignalBinding(this, listener, isOnce, listenerContext, priority, args);
|
|
35
|
+
this._addBinding(binding);
|
|
36
|
+
}
|
|
37
|
+
if (this.memorize && this._prevParams) {
|
|
38
|
+
binding.execute(this._prevParams);
|
|
39
|
+
}
|
|
40
|
+
return binding;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_addBinding(binding) {
|
|
44
|
+
if (!this._bindings) {
|
|
45
|
+
this._bindings = [];
|
|
46
|
+
}
|
|
47
|
+
// Simplified insertion sort
|
|
48
|
+
let n = this._bindings.length;
|
|
49
|
+
do {
|
|
50
|
+
n -= 1;
|
|
51
|
+
}
|
|
52
|
+
while (this._bindings[n] && binding._priority <= this._bindings[n]._priority);
|
|
53
|
+
this._bindings.splice(n + 1, 0, binding);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
_indexOfListener(listener, context = null) {
|
|
57
|
+
if (!this._bindings) {
|
|
58
|
+
return -1;
|
|
59
|
+
}
|
|
60
|
+
let n = this._bindings.length;
|
|
61
|
+
let cur;
|
|
62
|
+
while (n) {
|
|
63
|
+
n -= 1;
|
|
64
|
+
cur = this._bindings[n];
|
|
65
|
+
if (cur._listener === listener && cur.context === context) {
|
|
66
|
+
return n;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return -1;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
has(listener, context) {
|
|
73
|
+
return this._indexOfListener(listener, context) !== -1;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
add(listener, listenerContext, priority, ...args) {
|
|
77
|
+
this.validateListener(listener, 'add');
|
|
78
|
+
return this._registerListener(listener, false, listenerContext, priority, args);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
addOnce(listener, listenerContext, priority, ...args) {
|
|
82
|
+
this.validateListener(listener, 'addOnce');
|
|
83
|
+
return this._registerListener(listener, true, listenerContext, priority, args);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
remove(listener, context) {
|
|
87
|
+
this.validateListener(listener, 'remove');
|
|
88
|
+
const i = this._indexOfListener(listener, context);
|
|
89
|
+
if (i !== -1) {
|
|
90
|
+
// no reason to a SignalBinding exist if it isn't attached to a signal
|
|
91
|
+
this._bindings[i]._destroy();
|
|
92
|
+
this._bindings.splice(i, 1);
|
|
93
|
+
}
|
|
94
|
+
return listener;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
removeAll(context = null) {
|
|
98
|
+
if (!this._bindings) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
let n = this._bindings.length;
|
|
102
|
+
while (n) {
|
|
103
|
+
n -= 1;
|
|
104
|
+
if (context) {
|
|
105
|
+
if (this._bindings[n].context === context) {
|
|
106
|
+
this._bindings[n]._destroy();
|
|
107
|
+
this._bindings.splice(n, 1);
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
this._bindings[n]._destroy();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (!context) {
|
|
114
|
+
this._bindings.length = 0;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getNumListeners() {
|
|
119
|
+
return this._bindings ? this._bindings.length : 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
halt() {
|
|
123
|
+
this._shouldPropagate = false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
dispatch(...args) {
|
|
127
|
+
if (!this.active || !this._bindings) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const paramsArr = args.slice();
|
|
131
|
+
let n = this._bindings.length;
|
|
132
|
+
|
|
133
|
+
if (this.memorize) {
|
|
134
|
+
this._prevParams = paramsArr;
|
|
135
|
+
}
|
|
136
|
+
if (!n) {
|
|
137
|
+
// Should come after memorize
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const bindings = this._bindings.slice(); // clone array in case add/remove items during dispatch
|
|
141
|
+
this._shouldPropagate = true; // in case `halt` was called before dispatch or during the previous dispatch.
|
|
142
|
+
|
|
143
|
+
// execute all callbacks until end of the list or until a callback returns `false` or stops propagation
|
|
144
|
+
// reverse loop since listeners with higher priority will be added at the end of the list
|
|
145
|
+
do {
|
|
146
|
+
n -= 1;
|
|
147
|
+
}
|
|
148
|
+
while (bindings[n] && this._shouldPropagate && bindings[n].execute(paramsArr) !== false);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
forget() {
|
|
152
|
+
if (this._prevParams) {
|
|
153
|
+
this._prevParams = null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
dispose() {
|
|
158
|
+
this.removeAll();
|
|
159
|
+
this.forget();
|
|
160
|
+
this._bindings = null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
toString() {
|
|
164
|
+
return '[Signal active:' + this.active + ' numListeners:' + this.getNumListeners() + ']';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
get boundDispatch() {
|
|
168
|
+
const _this = this;
|
|
169
|
+
if (!this._boundDispatch) {
|
|
170
|
+
this._boundDispatch = (...rest) => _this.dispatch(...rest);
|
|
171
|
+
}
|
|
172
|
+
return this._boundDispatch;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Andras Csizmadia <andras@vpmedia.hu>
|
|
3
|
+
* @author Richard Davey <rich@photonstorm.com>
|
|
4
|
+
* @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export default class {
|
|
8
|
+
|
|
9
|
+
constructor(signal, listener, isOnce = false, listenerContext = null, priority = 0, args = null) {
|
|
10
|
+
this._signal = signal;
|
|
11
|
+
this._listener = listener;
|
|
12
|
+
this._args = args;
|
|
13
|
+
this._priority = priority;
|
|
14
|
+
this._isOnce = isOnce;
|
|
15
|
+
this.context = listenerContext;
|
|
16
|
+
this.callCount = 0;
|
|
17
|
+
this.active = true;
|
|
18
|
+
this.params = null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
execute(paramsArr) {
|
|
22
|
+
let handlerReturn;
|
|
23
|
+
let params;
|
|
24
|
+
if (this.active && !!this._listener) {
|
|
25
|
+
params = this.params ? this.params.concat(paramsArr) : paramsArr;
|
|
26
|
+
if (this._args) {
|
|
27
|
+
params = params.concat(this._args);
|
|
28
|
+
}
|
|
29
|
+
handlerReturn = this._listener.apply(this.context, params);
|
|
30
|
+
this.callCount += 1;
|
|
31
|
+
if (this._isOnce) {
|
|
32
|
+
this.detach();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return handlerReturn;
|
|
36
|
+
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
detach() {
|
|
40
|
+
return this.isBound() ? this._signal.remove(this._listener, this.context) : null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
isBound() {
|
|
44
|
+
return (!!this._signal && !!this._listener);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
isOnce() {
|
|
48
|
+
return this._isOnce;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getListener() {
|
|
52
|
+
return this._listener;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
getSignal() {
|
|
56
|
+
return this._signal;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
_destroy() {
|
|
60
|
+
delete this._signal;
|
|
61
|
+
delete this._listener;
|
|
62
|
+
delete this.context;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
toString() {
|
|
66
|
+
return '[SignalBinding isOnce:' + this._isOnce + ', isBound:' + this.isBound() + ', active:' + this.active + ']';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
}
|