forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.js
More file actions
219 lines (183 loc) · 8.1 KB
/
component.js
File metadata and controls
219 lines (183 loc) · 8.1 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
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
pc.extend(pc, function () {
/**
* @enum pc.SCALEMODE
* @name pc.SCALEMODE_NONE
* @description Always use the application's resolution as the resolution for the {@link pc.ScreenComponent}.
*/
pc.SCALEMODE_NONE = "none";
/**
* @enum pc.SCALEMODE
* @name pc.SCALEMODE_BLEND
* @description Scale the {@link pc.ScreenComponent} when the application's resolution is different than the ScreenComponent's referenceResolution.
*/
pc.SCALEMODE_BLEND = "blend";
/**
* @component
* @name pc.ScreenComponent
* @description Create a new ScreenComponent
* @class A ScreenComponent enables the Entity to render child {@link pc.ElementComponent}s using anchors and positions in the ScreenComponent's space.
* @param {pc.ScreenComponentSystem} system The ComponentSystem that created this Component
* @param {pc.Entity} entity The Entity that this Component is attached to.
* @extends pc.Component
* @property {Boolean} screenSpace If true then the ScreenComponent will render its child {@link pc.ElementComponent}s in screen space instead of world space. Enable this to create 2D user interfaces.
* @property {String} scaleMode Can either be {@link pc.SCALEMODE_NONE} or {@link pc.SCALEMODE_BLEND}. See the description of referenceResolution for more information.
* @property {Number} scaleBlend A value between 0 and 1 that is used when scaleMode is equal to {@link pc.SCALEMODE_BLEND}. Scales the ScreenComponent with width as a reference (when value is 0), the height as a reference (when value is 1) or anything in between.
* @property {pc.Vec2} resolution The width and height of the ScreenComponent. When screenSpace is true the resolution will always be equal to {@link pc.GraphicsDevice#width} x {@link pc.GraphicsDevice#height}.
* @property {pc.Vec2} referenceResolution The resolution that the ScreenComponent is designed for. This is only taken into account when screenSpace is true and scaleMode is {@link pc.SCALEMODE_BLEND}. If the actual resolution is different then the ScreenComponent will be scaled according to the scaleBlend value.
*/
var ScreenComponent = function ScreenComponent (system, entity) {
this._resolution = new pc.Vec2(640, 320);
this._referenceResolution = new pc.Vec2(640,320);
this._scaleMode = pc.SCALEMODE_NONE;
this.scale = 1;
this._scaleBlend = 0.5;
this._screenSpace = false;
this._screenMatrix = new pc.Mat4();
system.app.graphicsDevice.on("resizecanvas", this._onResize, this);
};
ScreenComponent = pc.inherits(ScreenComponent, pc.Component);
var _transform = new pc.Mat4();
pc.extend(ScreenComponent.prototype, {
/**
* @function
* @name pc.ScreenComponent#syncDrawOrder
* @description Set the drawOrder of each child {@link pc.ElementComponent}
* so that ElementComponents which are last in the hierarchy are rendered on top.
*/
syncDrawOrder: function () {
var i = 1;
var recurse = function (e) {
if (e.element) {
e.element.drawOrder = i++;
}
var children = e.getChildren();
for (var j = 0; j < children.length; j++) {
recurse(children[j]);
}
};
recurse(this.entity);
},
_calcProjectionMatrix: function () {
var left;
var right;
var bottom;
var top;
var near = 1;
var far = -1;
var w = this._resolution.x / this.scale;
var h = this._resolution.y / this.scale;
left = 0;
right = w;
bottom = -h;
top = 0;
this._screenMatrix.setOrtho(left, right, bottom, top, near, far);
if (!this._screenSpace) {
_transform.setScale(0.5*w, 0.5*h, 1);
this._screenMatrix.mul2(_transform, this._screenMatrix);
}
},
_updateScale: function () {
this.scale = this._calcScale(this._resolution, this.referenceResolution);
},
_calcScale: function (resolution, referenceResolution) {
// Using log of scale values
// This produces a nicer outcome where if you have a xscale = 2 and yscale = 0.5
// the combined scale is 1 for an even blend
var lx = Math.log2(resolution.x / referenceResolution.x);
var ly = Math.log2(resolution.y / referenceResolution.y);
return Math.pow(2, (lx*(1-this._scaleBlend) + ly*this._scaleBlend));
},
_onResize: function (width, height) {
if (this._screenSpace) {
this._resolution.set(width, height);
this.resolution = this._resolution; // force update
}
},
onRemove: function () {
this.system.app.graphicsDevice.off("resizecanvas", this._onResize, this);
}
});
Object.defineProperty(ScreenComponent.prototype, "resolution", {
set: function (value) {
if (!this._screenSpace) {
this._resolution.set(value.x, value.y);
} else {
// ignore input when using screenspace.
this._resolution.set(this.system.app.graphicsDevice.width, this.system.app.graphicsDevice.height);
}
this._updateScale();
this._calcProjectionMatrix();
if (! this.entity._dirtyLocal)
this.entity._dirtify(true);
this.fire("set:resolution", this._resolution);
},
get: function () {
return this._resolution;
}
});
Object.defineProperty(ScreenComponent.prototype, "referenceResolution", {
set: function (value) {
this._referenceResolution.set(value.x, value.y);
this._updateScale();
this._calcProjectionMatrix();
if (! this.entity._dirtyLocal)
this.entity._dirtify(true);
this.fire("set:referenceresolution", this._resolution);
},
get: function () {
if (this._scaleMode === pc.SCALEMODE_NONE) {
return this._resolution;
} else {
return this._referenceResolution;
}
}
});
Object.defineProperty(ScreenComponent.prototype, "screenSpace", {
set: function (value) {
this._screenSpace = value;
if (this._screenSpace) {
this._resolution.set(this.system.app.graphicsDevice.width, this.system.app.graphicsDevice.height);
}
this.resolution = this._resolution; // force update either way
if (! this.entity._dirtyLocal)
this.entity._dirtify(true);
this.fire('set:screenspace', this._screenSpace);
},
get: function () {
return this._screenSpace;
}
});
Object.defineProperty(ScreenComponent.prototype, "scaleMode", {
set: function (value) {
if (value !== pc.SCALEMODE_NONE && value !== pc.SCALEMODE_BLEND) {
value = pc.SCALEMODE_NONE;
}
// world space screens do not support scale modes
if (!this._screenSpace && value !== pc.SCALEMODE_NONE) {
value = pc.SCALEMODE_NONE;
}
this._scaleMode = value;
this.resolution = this._resolution; // force update
this.fire("set:scalemode", this._scaleMode);
},
get: function () {
return this._scaleMode;
}
});
Object.defineProperty(ScreenComponent.prototype, "scaleBlend", {
set: function (value) {
this._scaleBlend = value;
this._updateScale();
this._calcProjectionMatrix();
if (! this.entity._dirtyLocal)
this.entity._dirtify(true);
this.fire("set:scaleblend", this._scaleBlend);
},
get: function () {
return this._scaleBlend;
}
});
return {
ScreenComponent: ScreenComponent
};
}());