@awayfl/awayfl-player 0.2.26 → 0.2.31

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.
@@ -23,206 +23,288 @@ function fullSerializer(obj: any) {
23
23
  const OBJECT_FIELDS = ['id','visible', 'index', 'assetType:type', 'name'];
24
24
 
25
25
  export class AVMDebug {
26
- constructor(public player: AVMStage) {
27
-
28
- registerDebugMethod(this._dirObjectByIds.bind(this), {
29
- name: "dirObjectByIds",
30
- description:"Export selected object to console",
31
- declaration: [{name: 'ids', type: "object"}]
32
- });
33
-
34
- registerDebugMethod(this._applyPropsByIds.bind(this), {
35
- name: "applyPropsByIds",
36
- description:"Apply propertyes by node ids",
37
- declaration: [{name: 'ids', type: "object"}, {name:'object', type:'object'}]
38
- });
39
-
40
- registerDebugMethod(this._removeObjectByIds.bind(this), {
41
- name: "removeObjectByIds",
42
- description:"Remove object from sceen tree",
43
- declaration: [{name: 'ids', type: "object"}]
44
- });
45
-
46
- registerDebugMethod(this._getInfo.bind(this), {
47
- name: "getInfo",
48
- description:"Get file info for app",
49
- declaration: [{name:"return", type:"object"}]
50
- });
51
-
52
- registerDebugMethod(this._getSceneTree.bind(this), {
53
- name: "getNodeTree",
54
- description:"Get sceen tree of app",
55
- declaration: [
56
- {name:"return", type:"object"},
57
- {name:"flat", type:"boolean"},
58
- {name:"from", type:"number"},
59
- {name:"rect", type:"object"}
60
- ]
61
- });
62
-
63
- registerDebugMethod(this._getStageCanvas.bind(this), {
64
- name: "getStageCanvas",
65
- description:"Get canvas attahed to stage",
66
- declaration: []
67
- });
68
-
69
- //@ts-ignore
70
- window._AWAY_DEBUG_PLAYER_ = this;
71
- }
72
-
73
- public onAvmInit(version: number) {
74
-
75
- //@ts-ignore
76
- window._AWAY_DEBUG_STORAGE = version === 1 ? SOavm1 : SOavm2;
77
- }
78
-
79
- private _selectNode(ids: number[]): DisplayObject {
80
- let node = this.player.root as any;
81
-
82
- for(let i of ids) {
83
- node = node._children.find((e) => e.id === i);
84
- if(!node) {
85
- break;
86
- }
87
- }
88
-
89
- if(!node){
90
- throw new Error("Node not found");
91
- }
92
-
93
- return node;
94
- }
95
-
96
- private _getStageCanvas() {
97
- return this.player.view.stage.container;
98
- }
99
-
100
- private _dirObjectByIds(ids: number[]){
101
- console.dir(this._selectNode(ids));
102
- }
103
-
104
- private _getNodeBounds(node: DisplayObject) {
105
- const view = this.player.view;
106
-
107
- let box;
108
- const pool = AVMStage.instance && (<any>AVMStage.instance()).pool
109
- if (pool) {
110
- //@ts-ignore
111
- const partition = pool.getNode(node).partition;
112
- const picker = PickGroup.getInstance(view).getBoundsPicker(partition);
113
-
114
- //@ts-ignore
115
- box = picker.getBoxBounds(pool.getNode(this.player.root, true, true));
26
+ private _rafState: 'stop' | 'next' | 'play' = 'play';
27
+ private _defaultRaf: any = self.requestAnimationFrame;
28
+ private _requestedCallbacks: FrameRequestCallback [] = [];
29
+
30
+ constructor(public player: AVMStage) {
31
+
32
+ registerDebugMethod(this._dirObjectByIds.bind(this), {
33
+ name: "dirObjectByIds",
34
+ description:"Export selected object to console",
35
+ declaration: [{name: 'ids', type: "object"}]
36
+ });
37
+
38
+ registerDebugMethod(this._applyPropsByIds.bind(this), {
39
+ name: "applyPropsByIds",
40
+ description:"Apply propertyes by node ids",
41
+ declaration: [{name: 'ids', type: "object"}, {name:'object', type:'object'}]
42
+ });
43
+
44
+ registerDebugMethod(this._removeObjectByIds.bind(this), {
45
+ name: "removeObjectByIds",
46
+ description:"Remove object from sceen tree",
47
+ declaration: [{name: 'ids', type: "object"}]
48
+ });
49
+
50
+ registerDebugMethod(this._getInfo.bind(this), {
51
+ name: "getInfo",
52
+ description:"Get file info for app",
53
+ declaration: [{name:"return", type:"object"}]
54
+ });
55
+
56
+ registerDebugMethod(this._getSceneTree.bind(this), {
57
+ name: "getNodeTree",
58
+ description:"Get sceen tree of app",
59
+ declaration: [
60
+ {name:"return", type:"object"},
61
+ {name:"flat", type:"boolean"},
62
+ {name:"from", type:"number"},
63
+ {name:"rect", type:"object"}
64
+ ]
65
+ });
66
+
67
+ registerDebugMethod(this._getStageCanvas.bind(this), {
68
+ name: "getStageCanvas",
69
+ description:"Get canvas attahed to stage",
70
+ declaration: []
71
+ });
72
+
73
+ registerDebugMethod(this._setRAFState.bind(this), {
74
+ name: "setRAFState",
75
+ description: "Changed RAF state",
76
+ declaration: [
77
+ {name:'return', type: 'string'},
78
+ {name:'state', type: 'string'}
79
+ ]
80
+ });
81
+
82
+ registerDebugMethod(this._getRAFState.bind(this), {
83
+ name: "getRAFState",
84
+ description: "Changed RAF state",
85
+ declaration: [
86
+ {name:'return', type: 'string'},
87
+ {name:'state', type: 'string'}
88
+ ]
89
+ });
90
+
91
+ this._mokedRaf = this._mokedRaf.bind(this);
92
+
93
+ //@ts-ignore
94
+ window._AWAY_DEBUG_PLAYER_ = this;
95
+ }
96
+
97
+ public onAvmInit(version: number) {
98
+
99
+ //@ts-ignore
100
+ window._AWAY_DEBUG_STORAGE = version === 1 ? SOavm1 : SOavm2;
101
+ }
102
+
103
+ private _mokedRaf(callback: FrameRequestCallback) {
104
+ if (this._requestedCallbacks.indexOf(callback) !== -1) return;
105
+
106
+ this._requestedCallbacks.push(callback);
107
+ return 0;
108
+ }
109
+
110
+ private _setRAFState(state: 'stop' | 'next' | 'play'): 'stop' | 'next' | 'play' {
111
+ if (!state) return this._rafState;
112
+ if (state === this._rafState)
113
+ return;
114
+
115
+ if(state === 'next' && this._rafState === 'stop') {
116
+ const time = performance.now();
117
+ const callbacks = this._requestedCallbacks.slice();
118
+
119
+ this._rafState = 'next';
120
+ this._requestedCallbacks.length = 0;
121
+
122
+ callbacks.forEach((e) => e && e(time));
123
+
124
+ return this._rafState = 'stop';
125
+ }
126
+
127
+ if (state === 'stop') {
128
+ this._requestedCallbacks.length = 0;
129
+ self.requestAnimationFrame = this._mokedRaf;
130
+
131
+ return this._rafState = 'stop';
132
+ }
133
+
134
+ if (state === 'play') {
135
+ const time = performance.now();
136
+ const callbacks = this._requestedCallbacks.slice();
137
+
138
+ this._rafState = 'play';
139
+ this._requestedCallbacks.length = 0;
140
+
141
+ self.requestAnimationFrame = this._defaultRaf;
142
+
143
+ callbacks.forEach((e) => e && e(time));
144
+ }
145
+
146
+ return this._rafState;
147
+ }
148
+
149
+ private _getRAFState(): 'stop' | 'next' | 'play' {
150
+ return this._rafState;
151
+ }
152
+
153
+ private _selectNode(ids: number[]): DisplayObject {
154
+ let node = this.player.root as any;
155
+
156
+ for(let i of ids) {
157
+ node = node._children.find((e) => e.id === i);
158
+ if(!node) {
159
+ break;
160
+ }
161
+ }
162
+
163
+ if(!node){
164
+ throw new Error("Node not found");
165
+ }
166
+
167
+ return node;
168
+ }
169
+
170
+ private _getStageCanvas() {
171
+ return this.player.view.stage.container;
172
+ }
173
+
174
+ private _dirObjectByIds(ids: number[]) {
175
+ const node = this._selectNode(ids);
176
+ //@ts-ignore
177
+ const exposeID = window._lastTempNode = window._lastTempNode || 1;
178
+ //@ts-ignore
179
+ window._lastTempNode++;
180
+
181
+ window['tempNode' + exposeID] = node
182
+ console.log('tempNode' + exposeID, '=');
183
+ console.dir(node);
184
+ }
185
+
186
+ private _getNodeBounds(node: DisplayObject) {
187
+ const view = this.player.view;
188
+
189
+ let box;
190
+ const pool = AVMStage.instance && (<any>AVMStage.instance()).pool
191
+ if (pool) {
192
+ //@ts-ignore
193
+ const partition = pool.getNode(node).partition;
194
+ const picker = PickGroup.getInstance().getBoundsPicker(partition);
195
+
196
+ //@ts-ignore
197
+ box = picker.getBoxBounds(pool.getNode(this.player.root), true, true);
116
198
 
117
- } else {
118
- //@ts-ignore
119
- box = PickGroup.getInstance(view).getBoundsPicker(node.partition).getBoxBounds(this.player.root);
120
- }
121
-
122
- if (!box)
123
- return null;
124
-
125
- const sx = view.width / this.player.stageWidth;
126
- const sy = view.height / this.player.stageHeight;
127
-
128
- //console.log("DisplayObject:getRect not yet implemented");FromBounds
129
- return {
130
- x: box.x * sx,
131
- y: box.y * sy,
132
- width: box.width * sx,
133
- height: box.height * sy
134
- };
135
- }
136
-
137
- private _traverse(node: any, req = false, rect = false, visibleOnly = false) {
138
-
139
- const ret = {
140
- parentId: node.parent ? node.parent.id : -1,
141
- children: null,
142
- rect: null,
143
- }
144
-
145
- for(let name of OBJECT_FIELDS) {
146
- const sub = name.split(":");
147
- if(sub.length > 1) {
148
- ret[sub[1]] = node[sub[0]];
149
- } else{
150
- ret[name] = node[name];
151
- }
152
- }
153
-
154
- ret["globalVisible"] =
155
- node.parent ? (node.parent.visible && node.visible) : node.visible;
156
-
157
- if(rect) {
158
- ret.rect = this._getNodeBounds(node)
159
- }
160
-
161
- if(req) {
162
-
163
- ret.children = [];
164
- for(let c of node._children) {
165
- if(visibleOnly && c.visible || !visibleOnly){
166
- ret.children.push(this._traverse(c, req, rect, visibleOnly));
167
- }
168
- }
169
- }
170
-
171
- return ret;
172
- }
173
-
174
- private _removeObjectByIds(ids: number[]) {
175
- const node = this._selectNode(ids);
176
-
177
- node.parent.removeChild(node);
178
- }
179
-
180
- private _applyPropsByIds(ids: number[], object: any) {
181
- const node = this._selectNode(ids);
182
-
183
- Object.assign(node, object);
184
- }
185
-
186
- private _getSceneTree(params: {flat?: boolean, from?: number, rect?: boolean, visibleOnly?: boolean})
187
- private _getSceneTree(flat?: boolean, from?: number, rect?: boolean)
188
-
189
- private _getSceneTree(params: any, fromArg?: number, rectArg?: boolean) {
190
- if(typeof params !== 'object') {
191
- params = {
192
- flat: params || false,
193
- from: fromArg || 0,
194
- rect: rectArg || false,
195
- visibleOnly: false
196
- }
197
- }
198
-
199
- const {
200
- flat = false,
201
- from = 0,
202
- rect = false,
203
- visibleOnly = false
204
- } = params;
205
-
206
- const tree = [];
207
- //@ts-ignore
208
- const q: any[] = this.player.root._children.slice();
209
-
210
- while(true) {
211
- const node = q.pop();
212
-
213
- if(!node) {
214
- break;
215
- }
216
-
217
- tree.push(this._traverse(node, !flat, rect, visibleOnly));
218
-
219
- if(flat) {
220
- q.push.apply(q, node._children.reverse().filter(e => (e.visible && visibleOnly || !visibleOnly)));
221
- }
222
- }
223
-
224
- return tree;
225
- }
199
+ } else {
200
+ //@ts-ignore
201
+ box = PickGroup.getInstance().getBoundsPicker(node.partition).getBoxBounds(this.player.root);
202
+ }
203
+
204
+ if (!box)
205
+ return null;
206
+
207
+ const sx = view.width / this.player.stageWidth;
208
+ const sy = view.height / this.player.stageHeight;
209
+
210
+ //console.log("DisplayObject:getRect not yet implemented");FromBounds
211
+ return {
212
+ x: box.x * sx,
213
+ y: box.y * sy,
214
+ width: box.width * sx,
215
+ height: box.height * sy
216
+ };
217
+ }
218
+
219
+ private _traverse(node: any, req = false, rect = false, visibleOnly = false) {
220
+
221
+ const ret = {
222
+ parentId: node.parent ? node.parent.id : -1,
223
+ children: null,
224
+ rect: null,
225
+ }
226
+
227
+ for(let name of OBJECT_FIELDS) {
228
+ const sub = name.split(":");
229
+ if(sub.length > 1) {
230
+ ret[sub[1]] = node[sub[0]];
231
+ } else{
232
+ ret[name] = node[name];
233
+ }
234
+ }
235
+
236
+ ret["globalVisible"] =
237
+ node.parent ? (node.parent.visible && node.visible) : node.visible;
238
+
239
+ if(rect) {
240
+ ret.rect = this._getNodeBounds(node)
241
+ }
242
+
243
+ if(req) {
244
+
245
+ ret.children = [];
246
+ for(let c of node._children) {
247
+ if(visibleOnly && c.visible || !visibleOnly){
248
+ ret.children.push(this._traverse(c, req, rect, visibleOnly));
249
+ }
250
+ }
251
+ }
252
+
253
+ return ret;
254
+ }
255
+
256
+ private _removeObjectByIds(ids: number[]) {
257
+ const node = this._selectNode(ids);
258
+
259
+ node.parent.removeChild(node);
260
+ }
261
+
262
+ private _applyPropsByIds(ids: number[], object: any) {
263
+ const node = this._selectNode(ids);
264
+
265
+ Object.assign(node, object);
266
+ }
267
+
268
+ private _getSceneTree(params: {flat?: boolean, from?: number, rect?: boolean, visibleOnly?: boolean})
269
+ private _getSceneTree(flat?: boolean, from?: number, rect?: boolean)
270
+
271
+ private _getSceneTree(params: any, fromArg?: number, rectArg?: boolean) {
272
+ if(typeof params !== 'object') {
273
+ params = {
274
+ flat: params || false,
275
+ from: fromArg || 0,
276
+ rect: rectArg || false,
277
+ visibleOnly: false
278
+ }
279
+ }
280
+
281
+ const {
282
+ flat = false,
283
+ from = 0,
284
+ rect = false,
285
+ visibleOnly = false
286
+ } = params;
287
+
288
+ const tree = [];
289
+ //@ts-ignore
290
+ const q: any[] = this.player.root._children.slice();
291
+
292
+ while(true) {
293
+ const node = q.pop();
294
+
295
+ if(!node) {
296
+ break;
297
+ }
298
+
299
+ tree.push(this._traverse(node, !flat, rect, visibleOnly));
300
+
301
+ if(flat) {
302
+ q.push.apply(q, node._children.reverse().filter(e => (e.visible && visibleOnly || !visibleOnly)));
303
+ }
304
+ }
305
+
306
+ return tree;
307
+ }
226
308
 
227
309
  private _getInfo() {
228
310
  const player = <any>this.player;