forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.js
More file actions
134 lines (106 loc) · 4.13 KB
/
system.js
File metadata and controls
134 lines (106 loc) · 4.13 KB
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
import { IndexedList } from '../../../core/indexed-list.js';
import { Vec2 } from '../../../math/vec2.js';
import { Component } from '../component.js';
import { ComponentSystem } from '../system.js';
import { ScreenComponent } from './component.js';
import { ScreenComponentData } from './data.js';
const _schema = ['enabled'];
/**
* @class
* @name ScreenComponentSystem
* @augments ComponentSystem
* @classdesc Manages creation of {@link ScreenComponent}s.
* @description Create a new ScreenComponentSystem.
* @param {Application} app - The application.
*/
class ScreenComponentSystem extends ComponentSystem {
constructor(app) {
super(app);
this.id = 'screen';
this.ComponentType = ScreenComponent;
this.DataType = ScreenComponentData;
this.schema = _schema;
this.windowResolution = new Vec2();
// queue of callbacks
this._drawOrderSyncQueue = new IndexedList();
this.app.graphicsDevice.on("resizecanvas", this._onResize, this);
ComponentSystem.bind('update', this._onUpdate, this);
this.on('beforeremove', this.onRemoveComponent, this);
}
initializeComponentData(component, data, properties) {
if (data.priority !== undefined) component.priority = data.priority;
if (data.screenSpace !== undefined) component.screenSpace = data.screenSpace;
component.cull = component.screenSpace;
if (data.scaleMode !== undefined) component.scaleMode = data.scaleMode;
if (data.scaleBlend !== undefined) component.scaleBlend = data.scaleBlend;
if (data.resolution !== undefined) {
if (data.resolution instanceof Vec2) {
component._resolution.copy(data.resolution);
} else {
component._resolution.set(data.resolution[0], data.resolution[1]);
}
component.resolution = component._resolution;
}
if (data.referenceResolution !== undefined) {
if (data.referenceResolution instanceof Vec2) {
component._referenceResolution.copy(data.referenceResolution);
} else {
component._referenceResolution.set(data.referenceResolution[0], data.referenceResolution[1]);
}
component.referenceResolution = component._referenceResolution;
}
// queue up a draw order sync
component.syncDrawOrder();
super.initializeComponentData(component, data, properties);
}
destroy() {
this.off();
this.app.graphicsDevice.off("resizecanvas", this._onResize, this);
}
_onUpdate(dt) {
var components = this.store;
for (var id in components) {
if (components[id].entity.screen.update) components[id].entity.screen.update(dt);
}
}
_onResize(width, height) {
this.windowResolution.x = width;
this.windowResolution.y = height;
}
cloneComponent(entity, clone) {
var screen = entity.screen;
return this.addComponent(clone, {
enabled: screen.enabled,
screenSpace: screen.screenSpace,
scaleMode: screen.scaleMode,
resolution: screen.resolution.clone(),
referenceResolution: screen.referenceResolution.clone()
});
}
onRemoveComponent(entity, component) {
component.onRemove();
}
processDrawOrderSyncQueue() {
var list = this._drawOrderSyncQueue.list();
for (var i = 0; i < list.length; i++) {
var item = list[i];
item.callback.call(item.scope);
}
this._drawOrderSyncQueue.clear();
}
queueDrawOrderSync(id, fn, scope) {
// first queued sync this frame
// attach an event listener
if (!this._drawOrderSyncQueue.list().length) {
this.app.once('prerender', this.processDrawOrderSyncQueue, this);
}
if (!this._drawOrderSyncQueue.has(id)) {
this._drawOrderSyncQueue.push(id, {
callback: fn,
scope: scope
});
}
}
}
Component._buildAccessors(ScreenComponent.prototype, _schema);
export { ScreenComponentSystem };