@openglobus/og 0.13.2 → 0.13.5

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.
@@ -1,347 +1,349 @@
1
- /**
2
- * @module og/entity/Label
3
- */
4
-
5
- "use strict";
6
-
7
- import * as utils from "../utils/shared.js";
8
- import { BaseBillboard } from "./BaseBillboard.js";
9
- import { Vec4 } from "../math/Vec4.js";
10
- import { LOCK_FREE, LOCK_UPDATE } from "./LabelWorker.js";
11
-
12
- const ALIGN = {
13
- RIGHT: 0,
14
- LEFT: 1,
15
- CENTER: 2
16
- };
17
-
18
- /**
19
- * Text align options.
20
- * @readonly
21
- * @enum {number}
22
- */
23
- const STR2ALIGN = {
24
- left: ALIGN.LEFT,
25
- right: ALIGN.RIGHT,
26
- center: ALIGN.CENTER
27
- };
28
-
29
- /**
30
- * Billboard text label.
31
- * @class
32
- * @extends {BaseBillboard}
33
- * @param {Object} [options] - Label options:
34
- * @param {Vec3|Array.<number>} [options.position] - Billboard spatial position.
35
- * @param {number} [options.rotation] - Screen angle rotaion.
36
- * @param {Vec4|string|Array.<number>} [options.color] - Billboard color.
37
- * @param {Vec3|Array.<number>} [options.alignedAxis] - Billboard aligned vector.
38
- * @param {Vec3|Array.<number>} [options.offset] - Billboard center screen offset.
39
- * @param {boolean} [options.visibility] - Visibility.
40
- * @param {string} [options.text] - Text string.
41
- * @param {string} [options.face] - HTML5 font face.
42
- * @param {number} [options.size] - Font size in pixels.
43
- * @param {string} [options.style] - HTML5 font style. Example 'normal', 'italic'.
44
- * @param {string} [options.weight] - HTML5 font weight. Example 'normal', 'bold'.
45
- * @param {number} [options.outline] - Text outline size. 0 - no outline, 1 - maximum outline. Default 0.58.
46
- * @param {Vec4|string|Array.<number>} [options.outlineColor] - Outline color.
47
- * @param {Label.ALIGN} [options.align] - Text horizontal align: "left", "right" and "center".
48
- */
49
- class Label extends BaseBillboard {
50
- constructor(options) {
51
- super(options);
52
-
53
- options = options || {};
54
-
55
- /**
56
- * Label text string.
57
- * @private
58
- * @type {string}
59
- */
60
- this._text = options.text;
61
-
62
- /**
63
- * HTML5 font face.
64
- * @private
65
- * @type {string}
66
- */
67
- this._face = utils.defaultString(options.face, "arial");
68
-
69
- /**
70
- * Font size in pixels.
71
- * @private
72
- * @type {number}
73
- */
74
- this._size = options.size || 24;
75
-
76
- /**
77
- * Label outline.
78
- * @private
79
- * @type {number}
80
- */
81
- this._outline = options.outline != undefined ? options.outline : 0.0;
82
-
83
- /**
84
- * Label outline color.
85
- * @private
86
- * @type {Vec4}
87
- */
88
- this._outlineColor = utils.createColorRGBA(
89
- options.outlineColor,
90
- new Vec4(0.0, 0.0, 0.0, 1.0)
91
- );
92
-
93
- /**
94
- * Text horizontal align: "left", "right" and "center".
95
- * @private
96
- * @type {Label.ALIGN}
97
- */
98
- this._align = options.align
99
- ? STR2ALIGN[options.align.trim().toLowerCase()] || ALIGN.RIGHT
100
- : ALIGN.RIGHT;
101
-
102
- /**
103
- * Label font atlas index.
104
- * @private
105
- * @type {number}
106
- */
107
- this._fontIndex = 0;
108
-
109
- /**
110
- * Font atlas pointer.
111
- * @private
112
- * @type {utils.FontAtlas}
113
- */
114
- this._fontAtlas = null;
115
- }
116
-
117
- /**
118
- * Sets lablel text.
119
- * @public
120
- * @param {string} text - Text string.
121
- * It can't be bigger than maximum labelHandler _maxLetters value.
122
- */
123
- setText(text) {
124
- this._text = text.toString();
125
- if (this._isReady && this._handler) {
126
- this._handler.setText(this._handlerIndex, text, this._fontIndex, this._align);
127
- }
128
- }
129
-
130
- /**
131
- * Gets current text string.
132
- * @public
133
- * @returns {string}
134
- */
135
- getText() {
136
- return this._text;
137
- }
138
-
139
- /**
140
- * Sets label text align. Could be center, left or right. Left is default.
141
- * @public
142
- * @param {Label.ALIGN} align - Text align.
143
- */
144
- setAlign(align) {
145
- this._align = STR2ALIGN[align.trim().toLowerCase()];
146
- if (this._isReady && this._handler) {
147
- this._handler.setText(this._handlerIndex, this._text, this._fontIndex, this._align);
148
- } else if (this._lockId !== LOCK_FREE) {
149
- this._lockId = LOCK_UPDATE;
150
- }
151
- }
152
-
153
- /**
154
- * Gets label text current alignment.
155
- * @public
156
- * @returns {Label.ALIGN}
157
- */
158
- getAlign() {
159
- return this._align;
160
- }
161
-
162
- /**
163
- * Sets font face family.
164
- * @public
165
- * @param {string} face - Font face family.
166
- */
167
- setFace(face) {
168
- this._face = face.trim().toLowerCase();
169
- this.update();
170
- }
171
-
172
- /**
173
- * Gets current font face.
174
- * @public
175
- * @returns {string}
176
- */
177
- getFace() {
178
- return this._face;
179
- }
180
-
181
- /**
182
- * Sets label font size in pixels.
183
- * @public
184
- * @param {number} size - Label size in pixels.
185
- */
186
- setSize(size) {
187
- if (size !== this._size) {
188
- this._size = size;
189
- if (this._isReady && this._handler) {
190
- this._handler.setSizeArr(this._handlerIndex, size);
191
- } else if (this._lockId !== LOCK_FREE) {
192
- this._lockId = LOCK_UPDATE;
193
- }
194
- }
195
- }
196
-
197
- /**
198
- * Gets label size in pixels.
199
- * @public
200
- * @returns {number}
201
- */
202
- getSize() {
203
- return this._size;
204
- }
205
-
206
- /**
207
- * Sets text outline border size. Where 0 - is no outline and 1 - is the maximum outline size.
208
- * @public
209
- * @param {number} outline - Text outline size.
210
- */
211
- setOutline(outline) {
212
- this._outline = outline;
213
- if (this._isReady && this._handler) {
214
- this._handler.setOutlineArr(this._handlerIndex, outline);
215
- } else if (this._lockId !== LOCK_FREE) {
216
- this._lockId = LOCK_UPDATE;
217
- }
218
- }
219
-
220
- /**
221
- * Gets text current outline size.
222
- * @public
223
- * @returns {number}
224
- */
225
- getOutline() {
226
- return this._outline;
227
- }
228
-
229
- /**
230
- * Sets label opacity.
231
- * @public
232
- * @param {number} a - Label opacity.
233
- */
234
- setOpacity(a) {
235
- super.setOpacity(a);
236
- this.setOutlineOpacity(a);
237
- }
238
-
239
- /**
240
- * Sets text outline color.
241
- * @public
242
- * @param {number} r - Red.
243
- * @param {number} g - Green.
244
- * @param {number} b - Blue.
245
- * @param {number} a - Alpha.
246
- */
247
- setOutlineColor(r, g, b, a) {
248
- if (a !== this._outlineColor.w || r !== this._outlineColor.x || g !== this._outlineColor.y || b !== this._outlineColor.z) {
249
- this._outlineColor.x = r;
250
- this._outlineColor.y = g;
251
- this._outlineColor.z = b;
252
- this._outlineColor.w = a;
253
- if (this._isReady && this._handler) {
254
- this._handler.setOutlineColorArr(this._handlerIndex, this._outlineColor);
255
- } else if (this._lockId !== LOCK_FREE) {
256
- this._lockId = LOCK_UPDATE;
257
- }
258
- }
259
- }
260
-
261
- /**
262
- * Sets text outline color.
263
- * @public
264
- * @param {Vec4} rgba - Color vector.
265
- */
266
- setOutlineColor4v(rgba) {
267
- this.setOutlineColor(rgba.x, rgba.y, rgba.z, rgba.w);
268
- }
269
-
270
- /**
271
- * Sets text outline color HTML string.
272
- * @public
273
- * @param {string} color - HTML string color.
274
- */
275
- setOutlineColorHTML(color) {
276
- this.setOutlineColor4v(utils.htmlColorToRgba(color));
277
- }
278
-
279
- /**
280
- * Gets outline color vector.
281
- * @public
282
- * @returns {Vec4}
283
- */
284
- getOutlineColor() {
285
- return this._outlineColor;
286
- }
287
-
288
- /**
289
- * Sets outline opacity. Actually outline color alpha value.
290
- * @public
291
- * @param {number} opacity - Outline opacity.
292
- */
293
- setOutlineOpacity(opacity) {
294
- if (opacity !== this._outlineColor.w) {
295
- this._outlineColor.w = opacity;
296
- if (this._isReady && this._handler) {
297
- this._handler.setOutlineColorArr(this._handlerIndex, this._outlineColor);
298
- } else if (this._lockId !== LOCK_FREE) {
299
- this._lockId = LOCK_UPDATE;
300
- }
301
- }
302
- }
303
-
304
- /**
305
- * Gets outline opacity value.
306
- * @public
307
- * @returns {number}
308
- */
309
- getOutlineOpacity() {
310
- return this._outlineColor.w;
311
- }
312
-
313
- /**
314
- * Updates label parameters.
315
- * @public
316
- */
317
- async update() {
318
- if (this._fontAtlas) {
319
- const fontIndex = await this._fontAtlas.getFontIndex(this._face);
320
- this._applyFontIndex(fontIndex);
321
- }
322
- }
323
-
324
- _applyFontIndex(fontIndex) {
325
- this._fontIndex = fontIndex;
326
- if (this._isReady && this._handler) {
327
- this._handler.setFontIndexArr(this._handlerIndex, this._fontIndex);
328
- this._handler.setText(this._handlerIndex, this._text, this._fontIndex, this._align);
329
- } else if (this._lockId !== LOCK_FREE) {
330
- this._lockId = LOCK_UPDATE;
331
- }
332
- }
333
-
334
- /**
335
- * Assigns font atlas and update.
336
- * @public
337
- * @param {utils.FontAtlas} fontAtlas - Font atlas.
338
- */
339
- assignFontAtlas(fontAtlas) {
340
- if (!this._fontAtlas) {
341
- this._fontAtlas = fontAtlas;
342
- }
343
- this.update();
344
- }
345
- }
346
-
1
+ /**
2
+ * @module og/entity/Label
3
+ */
4
+
5
+ "use strict";
6
+
7
+ import * as utils from "../utils/shared.js";
8
+ import { BaseBillboard } from "./BaseBillboard.js";
9
+ import { Vec4 } from "../math/Vec4.js";
10
+ import { LOCK_FREE, LOCK_UPDATE } from "./LabelWorker.js";
11
+
12
+ const ALIGN = {
13
+ RIGHT: 0,
14
+ LEFT: 1,
15
+ CENTER: 2
16
+ };
17
+
18
+ /**
19
+ * Text align options.
20
+ * @readonly
21
+ * @enum {number}
22
+ */
23
+ const STR2ALIGN = {
24
+ left: ALIGN.LEFT,
25
+ right: ALIGN.RIGHT,
26
+ center: ALIGN.CENTER
27
+ };
28
+
29
+ /**
30
+ * Billboard text label.
31
+ * @class
32
+ * @extends {BaseBillboard}
33
+ * @param {Object} [options] - Label options:
34
+ * @param {Vec3|Array.<number>} [options.position] - Billboard spatial position.
35
+ * @param {number} [options.rotation] - Screen angle rotaion.
36
+ * @param {Vec4|string|Array.<number>} [options.color] - Billboard color.
37
+ * @param {Vec3|Array.<number>} [options.alignedAxis] - Billboard aligned vector.
38
+ * @param {Vec3|Array.<number>} [options.offset] - Billboard center screen offset.
39
+ * @param {boolean} [options.visibility] - Visibility.
40
+ * @param {string} [options.text] - Text string.
41
+ * @param {string} [options.face] - HTML5 font face.
42
+ * @param {number} [options.size] - Font size in pixels.
43
+ * @param {string} [options.style] - HTML5 font style. Example 'normal', 'italic'.
44
+ * @param {string} [options.weight] - HTML5 font weight. Example 'normal', 'bold'.
45
+ * @param {number} [options.outline] - Text outline size. 0 - no outline, 1 - maximum outline. Default 0.58.
46
+ * @param {Vec4|string|Array.<number>} [options.outlineColor] - Outline color.
47
+ * @param {Label.ALIGN} [options.align] - Text horizontal align: "left", "right" and "center".
48
+ */
49
+ class Label extends BaseBillboard {
50
+ constructor(options) {
51
+ super(options);
52
+
53
+ options = options || {};
54
+
55
+ /**
56
+ * Label text string.
57
+ * @private
58
+ * @type {string}
59
+ */
60
+ this._text = options.text;
61
+
62
+ /**
63
+ * HTML5 font face.
64
+ * @private
65
+ * @type {string}
66
+ */
67
+ this._face = utils.defaultString(options.face, "arial");
68
+
69
+ /**
70
+ * Font size in pixels.
71
+ * @private
72
+ * @type {number}
73
+ */
74
+ this._size = options.size || 24;
75
+
76
+ /**
77
+ * Label outline.
78
+ * @private
79
+ * @type {number}
80
+ */
81
+ this._outline = options.outline != undefined ? options.outline : 0.0;
82
+
83
+ /**
84
+ * Label outline color.
85
+ * @private
86
+ * @type {Vec4}
87
+ */
88
+ this._outlineColor = utils.createColorRGBA(
89
+ options.outlineColor,
90
+ new Vec4(0.0, 0.0, 0.0, 1.0)
91
+ );
92
+
93
+ /**
94
+ * Text horizontal align: "left", "right" and "center".
95
+ * @private
96
+ * @type {Label.ALIGN}
97
+ */
98
+ this._align = options.align
99
+ ? STR2ALIGN[options.align.trim().toLowerCase()] || ALIGN.RIGHT
100
+ : ALIGN.RIGHT;
101
+
102
+ /**
103
+ * Label font atlas index.
104
+ * @private
105
+ * @type {number}
106
+ */
107
+ this._fontIndex = 0;
108
+
109
+ /**
110
+ * Font atlas pointer.
111
+ * @private
112
+ * @type {utils.FontAtlas}
113
+ */
114
+ this._fontAtlas = null;
115
+
116
+ this._isRTL = options.isRTL || false;
117
+ }
118
+
119
+ /**
120
+ * Sets lablel text.
121
+ * @public
122
+ * @param {string} text - Text string.
123
+ * It can't be bigger than maximum labelHandler _maxLetters value.
124
+ */
125
+ setText(text) {
126
+ this._text = text.toString();
127
+ if (this._isReady && this._handler) {
128
+ this._handler.setText(this._handlerIndex, text, this._fontIndex, this._align, this._isRTL);
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Gets current text string.
134
+ * @public
135
+ * @returns {string}
136
+ */
137
+ getText() {
138
+ return this._text;
139
+ }
140
+
141
+ /**
142
+ * Sets label text align. Could be center, left or right. Left is default.
143
+ * @public
144
+ * @param {Label.ALIGN} align - Text align.
145
+ */
146
+ setAlign(align) {
147
+ this._align = STR2ALIGN[align.trim().toLowerCase()];
148
+ if (this._isReady && this._handler) {
149
+ this._handler.setText(this._handlerIndex, this._text, this._fontIndex, this._align, this._isRTL);
150
+ } else if (this._lockId !== LOCK_FREE) {
151
+ this._lockId = LOCK_UPDATE;
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Gets label text current alignment.
157
+ * @public
158
+ * @returns {Label.ALIGN}
159
+ */
160
+ getAlign() {
161
+ return this._align;
162
+ }
163
+
164
+ /**
165
+ * Sets font face family.
166
+ * @public
167
+ * @param {string} face - Font face family.
168
+ */
169
+ setFace(face) {
170
+ this._face = face.trim().toLowerCase();
171
+ this.update();
172
+ }
173
+
174
+ /**
175
+ * Gets current font face.
176
+ * @public
177
+ * @returns {string}
178
+ */
179
+ getFace() {
180
+ return this._face;
181
+ }
182
+
183
+ /**
184
+ * Sets label font size in pixels.
185
+ * @public
186
+ * @param {number} size - Label size in pixels.
187
+ */
188
+ setSize(size) {
189
+ if (size !== this._size) {
190
+ this._size = size;
191
+ if (this._isReady && this._handler) {
192
+ this._handler.setSizeArr(this._handlerIndex, size);
193
+ } else if (this._lockId !== LOCK_FREE) {
194
+ this._lockId = LOCK_UPDATE;
195
+ }
196
+ }
197
+ }
198
+
199
+ /**
200
+ * Gets label size in pixels.
201
+ * @public
202
+ * @returns {number}
203
+ */
204
+ getSize() {
205
+ return this._size;
206
+ }
207
+
208
+ /**
209
+ * Sets text outline border size. Where 0 - is no outline and 1 - is the maximum outline size.
210
+ * @public
211
+ * @param {number} outline - Text outline size.
212
+ */
213
+ setOutline(outline) {
214
+ this._outline = outline;
215
+ if (this._isReady && this._handler) {
216
+ this._handler.setOutlineArr(this._handlerIndex, outline);
217
+ } else if (this._lockId !== LOCK_FREE) {
218
+ this._lockId = LOCK_UPDATE;
219
+ }
220
+ }
221
+
222
+ /**
223
+ * Gets text current outline size.
224
+ * @public
225
+ * @returns {number}
226
+ */
227
+ getOutline() {
228
+ return this._outline;
229
+ }
230
+
231
+ /**
232
+ * Sets label opacity.
233
+ * @public
234
+ * @param {number} a - Label opacity.
235
+ */
236
+ setOpacity(a) {
237
+ super.setOpacity(a);
238
+ this.setOutlineOpacity(a);
239
+ }
240
+
241
+ /**
242
+ * Sets text outline color.
243
+ * @public
244
+ * @param {number} r - Red.
245
+ * @param {number} g - Green.
246
+ * @param {number} b - Blue.
247
+ * @param {number} a - Alpha.
248
+ */
249
+ setOutlineColor(r, g, b, a) {
250
+ if (a !== this._outlineColor.w || r !== this._outlineColor.x || g !== this._outlineColor.y || b !== this._outlineColor.z) {
251
+ this._outlineColor.x = r;
252
+ this._outlineColor.y = g;
253
+ this._outlineColor.z = b;
254
+ this._outlineColor.w = a;
255
+ if (this._isReady && this._handler) {
256
+ this._handler.setOutlineColorArr(this._handlerIndex, this._outlineColor);
257
+ } else if (this._lockId !== LOCK_FREE) {
258
+ this._lockId = LOCK_UPDATE;
259
+ }
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Sets text outline color.
265
+ * @public
266
+ * @param {Vec4} rgba - Color vector.
267
+ */
268
+ setOutlineColor4v(rgba) {
269
+ this.setOutlineColor(rgba.x, rgba.y, rgba.z, rgba.w);
270
+ }
271
+
272
+ /**
273
+ * Sets text outline color HTML string.
274
+ * @public
275
+ * @param {string} color - HTML string color.
276
+ */
277
+ setOutlineColorHTML(color) {
278
+ this.setOutlineColor4v(utils.htmlColorToRgba(color));
279
+ }
280
+
281
+ /**
282
+ * Gets outline color vector.
283
+ * @public
284
+ * @returns {Vec4}
285
+ */
286
+ getOutlineColor() {
287
+ return this._outlineColor;
288
+ }
289
+
290
+ /**
291
+ * Sets outline opacity. Actually outline color alpha value.
292
+ * @public
293
+ * @param {number} opacity - Outline opacity.
294
+ */
295
+ setOutlineOpacity(opacity) {
296
+ if (opacity !== this._outlineColor.w) {
297
+ this._outlineColor.w = opacity;
298
+ if (this._isReady && this._handler) {
299
+ this._handler.setOutlineColorArr(this._handlerIndex, this._outlineColor);
300
+ } else if (this._lockId !== LOCK_FREE) {
301
+ this._lockId = LOCK_UPDATE;
302
+ }
303
+ }
304
+ }
305
+
306
+ /**
307
+ * Gets outline opacity value.
308
+ * @public
309
+ * @returns {number}
310
+ */
311
+ getOutlineOpacity() {
312
+ return this._outlineColor.w;
313
+ }
314
+
315
+ /**
316
+ * Updates label parameters.
317
+ * @public
318
+ */
319
+ async update() {
320
+ if (this._fontAtlas) {
321
+ const fontIndex = await this._fontAtlas.getFontIndex(this._face);
322
+ this._applyFontIndex(fontIndex);
323
+ }
324
+ }
325
+
326
+ _applyFontIndex(fontIndex) {
327
+ this._fontIndex = fontIndex;
328
+ if (this._isReady && this._handler) {
329
+ this._handler.setFontIndexArr(this._handlerIndex, this._fontIndex);
330
+ this._handler.setText(this._handlerIndex, this._text, this._fontIndex, this._align, this._isRTL);
331
+ } else if (this._lockId !== LOCK_FREE) {
332
+ this._lockId = LOCK_UPDATE;
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Assigns font atlas and update.
338
+ * @public
339
+ * @param {utils.FontAtlas} fontAtlas - Font atlas.
340
+ */
341
+ assignFontAtlas(fontAtlas) {
342
+ if (!this._fontAtlas) {
343
+ this._fontAtlas = fontAtlas;
344
+ }
345
+ this.update();
346
+ }
347
+ }
348
+
347
349
  export { Label, ALIGN };