@eva/plugin-renderer-text 2.0.1-beta.9 → 2.0.1
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/dist/EVA.plugin.renderer.text.js +236 -34
- package/dist/EVA.plugin.renderer.text.min.js +1 -1
- package/dist/plugin-renderer-text.cjs.js +383 -33
- package/dist/plugin-renderer-text.cjs.prod.js +2 -2
- package/dist/plugin-renderer-text.d.ts +233 -4
- package/dist/plugin-renderer-text.esm.js +384 -36
- package/package.json +5 -5
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var pixi_js = require('pixi.js');
|
|
6
5
|
var eva_js = require('@eva/eva.js');
|
|
7
6
|
var inspectorDecorator = require('@eva/inspector-decorator');
|
|
7
|
+
var pixi_js = require('pixi.js');
|
|
8
8
|
var pluginRenderer = require('@eva/plugin-renderer');
|
|
9
9
|
var rendererAdapter = require('@eva/renderer-adapter');
|
|
10
10
|
|
|
@@ -39,12 +39,104 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* 位图文本组件
|
|
44
|
+
*
|
|
45
|
+
* BitmapText 组件使用位图字体渲染文本,性能优于 Canvas 文本渲染。
|
|
46
|
+
* 适用于频繁更新的文本场景,如计分板、倒计时等。
|
|
47
|
+
*
|
|
48
|
+
* 支持两种字体模式:
|
|
49
|
+
* - 动态生成:指定 TextStyle,PixiJS 自动生成 bitmap font
|
|
50
|
+
* - 预加载字体:通过 BitmapFont.install() 预安装或从 .fnt 文件加载
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const score = new GameObject('score');
|
|
55
|
+
* score.addComponent(new BitmapText({
|
|
56
|
+
* text: 'Score: 0',
|
|
57
|
+
* style: {
|
|
58
|
+
* fontSize: 32,
|
|
59
|
+
* fill: '#ffffff',
|
|
60
|
+
* fontFamily: 'Arial'
|
|
61
|
+
* }
|
|
62
|
+
* }));
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
class BitmapText extends eva_js.Component {
|
|
66
|
+
constructor() {
|
|
67
|
+
super(...arguments);
|
|
68
|
+
this.text = '';
|
|
69
|
+
this.style = {};
|
|
70
|
+
}
|
|
71
|
+
init(obj) {
|
|
72
|
+
this.style = Object.assign({ fontSize: 24, fill: '#000000', fontFamily: 'Arial' }, obj === null || obj === void 0 ? void 0 : obj.style);
|
|
73
|
+
if (obj) {
|
|
74
|
+
this.text = obj.text;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
BitmapText.componentName = 'BitmapText';
|
|
79
|
+
__decorate([
|
|
80
|
+
inspectorDecorator.type('string')
|
|
81
|
+
], BitmapText.prototype, "text", void 0);
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 文本组件(基于 PixiJS Text)
|
|
85
|
+
*
|
|
86
|
+
* Text 组件用于渲染文本内容,支持丰富的文本样式配置。
|
|
87
|
+
* 它基于 PixiJS 的 Text 实现,支持字体、颜色、描边、阴影、对齐等多种样式。
|
|
88
|
+
*
|
|
89
|
+
* 主要特性:
|
|
90
|
+
* - 支持多种字体和字号
|
|
91
|
+
* - 支持文本颜色、渐变填充
|
|
92
|
+
* - 支持描边和投影效果
|
|
93
|
+
* - 支持文本对齐和换行
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* // 基础文本
|
|
98
|
+
* const label = new GameObject('label');
|
|
99
|
+
* label.addComponent(new Text({
|
|
100
|
+
* text: 'Hello EVA!',
|
|
101
|
+
* style: {
|
|
102
|
+
* fontSize: 32,
|
|
103
|
+
* fill: 0xffffff
|
|
104
|
+
* }
|
|
105
|
+
* }));
|
|
106
|
+
*
|
|
107
|
+
* // 带样式的文本
|
|
108
|
+
* label.addComponent(new Text({
|
|
109
|
+
* text: '得分: 9999',
|
|
110
|
+
* style: {
|
|
111
|
+
* fontFamily: 'Arial',
|
|
112
|
+
* fontSize: 48,
|
|
113
|
+
* fontWeight: 'bold',
|
|
114
|
+
* fill: ['#ff0000', '#ffff00'], // 渐变色
|
|
115
|
+
* stroke: '#000000',
|
|
116
|
+
* strokeThickness: 4,
|
|
117
|
+
* dropShadow: true,
|
|
118
|
+
* dropShadowDistance: 3
|
|
119
|
+
* }
|
|
120
|
+
* }));
|
|
121
|
+
* // 如需高清渲染,使用 Render 组件的 resolution 属性
|
|
122
|
+
* label.addComponent(new Render({ resolution: 2 }));
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
42
125
|
class Text$2 extends eva_js.Component {
|
|
43
126
|
constructor() {
|
|
44
127
|
super(...arguments);
|
|
128
|
+
/** 文本内容 */
|
|
45
129
|
this.text = '';
|
|
130
|
+
/** 文本样式配置 */
|
|
131
|
+
// @decorators.IDEProp 复杂编辑后续添加
|
|
46
132
|
this.style = {};
|
|
47
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* 初始化组件
|
|
136
|
+
* @param obj - 初始化参数
|
|
137
|
+
* @param obj.text - 文本内容
|
|
138
|
+
* @param obj.style - 文本样式
|
|
139
|
+
*/
|
|
48
140
|
init(obj) {
|
|
49
141
|
const style = new pixi_js.TextStyle({
|
|
50
142
|
fontSize: 20,
|
|
@@ -63,11 +155,104 @@ class Text$2 extends eva_js.Component {
|
|
|
63
155
|
}
|
|
64
156
|
}
|
|
65
157
|
}
|
|
158
|
+
/** 组件名称 */
|
|
66
159
|
Text$2.componentName = 'Text';
|
|
67
160
|
__decorate([
|
|
68
161
|
inspectorDecorator.type('string')
|
|
69
162
|
], Text$2.prototype, "text", void 0);
|
|
70
163
|
|
|
164
|
+
/**
|
|
165
|
+
* HTML 富文本组件
|
|
166
|
+
*
|
|
167
|
+
* HTMLText 组件支持渲染带有 HTML 标签的富文本内容。
|
|
168
|
+
* 可以在文本中使用 HTML 标签(如 `<b>`, `<i>`, `<span>` 等)来实现丰富的文本样式,
|
|
169
|
+
* 适用于聊天对话、新闻内容、富文本显示等需要多样式文本的场景。
|
|
170
|
+
*
|
|
171
|
+
* 支持的 HTML 标签:
|
|
172
|
+
* - `<b>` - 粗体
|
|
173
|
+
* - `<i>` - 斜体
|
|
174
|
+
* - `<span style="color:#ff0000">` - 自定义样式
|
|
175
|
+
* - `<br>` - 换行
|
|
176
|
+
* 以及更多标准 HTML 文本标签
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```typescript
|
|
180
|
+
* // 基础富文本
|
|
181
|
+
* const label = new GameObject('label');
|
|
182
|
+
* label.addComponent(new HTMLText({
|
|
183
|
+
* text: '这是<b>粗体</b>和<i>斜体</i>文本',
|
|
184
|
+
* style: {
|
|
185
|
+
* fontSize: 24,
|
|
186
|
+
* fill: '#000000',
|
|
187
|
+
* fontFamily: 'Arial'
|
|
188
|
+
* }
|
|
189
|
+
* }));
|
|
190
|
+
*
|
|
191
|
+
* // 带颜色的富文本
|
|
192
|
+
* label.addComponent(new HTMLText({
|
|
193
|
+
* text: '欢迎 <span style="color:#ff0000">玩家123</span> 加入游戏!',
|
|
194
|
+
* style: {
|
|
195
|
+
* fontSize: 20,
|
|
196
|
+
* wordWrap: true,
|
|
197
|
+
* wordWrapWidth: 300
|
|
198
|
+
* }
|
|
199
|
+
* }));
|
|
200
|
+
*
|
|
201
|
+
* // 自定义标签样式
|
|
202
|
+
* label.addComponent(new HTMLText({
|
|
203
|
+
* text: '获得 <gold>100</gold> 金币',
|
|
204
|
+
* style: {
|
|
205
|
+
* fontSize: 18,
|
|
206
|
+
* tagStyles: {
|
|
207
|
+
* gold: {
|
|
208
|
+
* fill: '#ffd700',
|
|
209
|
+
* fontWeight: 'bold'
|
|
210
|
+
* }
|
|
211
|
+
* }
|
|
212
|
+
* }
|
|
213
|
+
* }));
|
|
214
|
+
*
|
|
215
|
+
* // 高分辨率渲染(推荐使用 Render 组件的 resolution 属性)
|
|
216
|
+
* label.addComponent(new HTMLText({
|
|
217
|
+
* text: '高清文本',
|
|
218
|
+
* textureStyle: {
|
|
219
|
+
* scaleMode: 'linear'
|
|
220
|
+
* }
|
|
221
|
+
* }));
|
|
222
|
+
* label.addComponent(new Render({ resolution: 2 }));
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
class HTMLText extends eva_js.Component {
|
|
226
|
+
constructor() {
|
|
227
|
+
super(...arguments);
|
|
228
|
+
/** 富文本内容(支持 HTML 标签) */
|
|
229
|
+
this.text = '';
|
|
230
|
+
/** 文本样式配置 */
|
|
231
|
+
this.style = {};
|
|
232
|
+
/** 纹理渲染配置 */
|
|
233
|
+
this.textureStyle = {};
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* 初始化组件
|
|
237
|
+
* @param obj - 初始化参数
|
|
238
|
+
* @param obj.text - 富文本内容
|
|
239
|
+
* @param obj.style - 文本样式
|
|
240
|
+
* @param obj.textureStyle - 纹理配置
|
|
241
|
+
*/
|
|
242
|
+
init(obj) {
|
|
243
|
+
this.style = Object.assign({ fontSize: 24, fill: '#000000', fontFamily: 'Arial' }, obj === null || obj === void 0 ? void 0 : obj.style);
|
|
244
|
+
this.textureStyle = Object.assign({ scaleMode: 'linear', resolution: window.devicePixelRatio || 1 }, obj === null || obj === void 0 ? void 0 : obj.textureStyle);
|
|
245
|
+
if (obj) {
|
|
246
|
+
this.text = obj.text;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/** 组件名称 */
|
|
251
|
+
HTMLText.componentName = 'HTMLText';
|
|
252
|
+
__decorate([
|
|
253
|
+
inspectorDecorator.type('string')
|
|
254
|
+
], HTMLText.prototype, "text", void 0);
|
|
255
|
+
|
|
71
256
|
let Text = class Text extends pluginRenderer.Renderer {
|
|
72
257
|
constructor() {
|
|
73
258
|
super(...arguments);
|
|
@@ -80,16 +265,21 @@ let Text = class Text extends pluginRenderer.Renderer {
|
|
|
80
265
|
}
|
|
81
266
|
componentChanged(changed) {
|
|
82
267
|
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
-
|
|
268
|
+
const isText = changed.componentName === 'Text';
|
|
269
|
+
const isHTMLText = changed.componentName === 'HTMLText';
|
|
270
|
+
const isBitmapText = changed.componentName === 'BitmapText';
|
|
271
|
+
if (!isText && !isHTMLText && !isBitmapText)
|
|
84
272
|
return;
|
|
85
273
|
if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
274
|
+
if (isText) {
|
|
275
|
+
yield this.addTextComponent(changed);
|
|
276
|
+
}
|
|
277
|
+
else if (isHTMLText) {
|
|
278
|
+
yield this.addHTMLTextComponent(changed);
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
yield this.addBitmapTextComponent(changed);
|
|
282
|
+
}
|
|
93
283
|
this.setSize(changed);
|
|
94
284
|
}
|
|
95
285
|
else if (changed.type === eva_js.OBSERVER_TYPE.REMOVE) {
|
|
@@ -99,56 +289,216 @@ let Text = class Text extends pluginRenderer.Renderer {
|
|
|
99
289
|
}
|
|
100
290
|
else {
|
|
101
291
|
this.change(changed);
|
|
292
|
+
// 如果样式改变且涉及字体,也需要等待字体资源加载
|
|
293
|
+
const component = changed.component;
|
|
294
|
+
if (changed.prop.prop[0] === 'style' && component.style && component.style.fontFamily) {
|
|
295
|
+
const { text } = this.texts[changed.gameObject.id];
|
|
296
|
+
yield this.waitForFontResource(text, changed, component.style.fontFamily);
|
|
297
|
+
}
|
|
102
298
|
this.setSize(changed);
|
|
103
299
|
}
|
|
104
300
|
});
|
|
105
301
|
}
|
|
302
|
+
addTextComponent(changed) {
|
|
303
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
304
|
+
const component = changed.component;
|
|
305
|
+
// 创建文本样式副本,先不设置 fontFamily
|
|
306
|
+
const styleWithoutFont = Object.assign({}, component.style);
|
|
307
|
+
const fontFamily = styleWithoutFont.fontFamily;
|
|
308
|
+
delete styleWithoutFont.fontFamily;
|
|
309
|
+
const initialText = fontFamily ? '' : component.text;
|
|
310
|
+
const text = new rendererAdapter.Text(initialText, styleWithoutFont);
|
|
311
|
+
this.containerManager.getContainer(changed.gameObject.id).addChildAt(text, 0);
|
|
312
|
+
this.texts[changed.gameObject.id] = {
|
|
313
|
+
text,
|
|
314
|
+
component,
|
|
315
|
+
};
|
|
316
|
+
// 如果指定了字体资源,等待资源加载完成后设置 fontFamily
|
|
317
|
+
if (fontFamily) {
|
|
318
|
+
yield this.waitForFontResource(text, changed, fontFamily);
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
addHTMLTextComponent(changed) {
|
|
323
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
324
|
+
const component = changed.component;
|
|
325
|
+
// 创建样式副本,先不设置 fontFamily
|
|
326
|
+
const styleWithoutFont = Object.assign({}, component.style);
|
|
327
|
+
const fontFamily = styleWithoutFont.fontFamily;
|
|
328
|
+
delete styleWithoutFont.fontFamily;
|
|
329
|
+
const initialText = fontFamily ? '' : component.text;
|
|
330
|
+
const htmlText = new rendererAdapter.HTMLText(Object.assign({ text: initialText, style: styleWithoutFont }, (component.textureStyle && { textureStyle: component.textureStyle })));
|
|
331
|
+
this.containerManager.getContainer(changed.gameObject.id).addChildAt(htmlText, 0);
|
|
332
|
+
this.texts[changed.gameObject.id] = {
|
|
333
|
+
text: htmlText,
|
|
334
|
+
component,
|
|
335
|
+
};
|
|
336
|
+
// 如果指定了字体资源,等待资源加载完成后设置 fontFamily
|
|
337
|
+
if (fontFamily) {
|
|
338
|
+
yield this.waitForFontResource(htmlText, changed, fontFamily);
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
addBitmapTextComponent(changed) {
|
|
343
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
344
|
+
const component = changed.component;
|
|
345
|
+
// 创建样式副本,先不设置 fontFamily
|
|
346
|
+
const styleWithoutFont = Object.assign({}, component.style);
|
|
347
|
+
const fontFamily = styleWithoutFont.fontFamily;
|
|
348
|
+
delete styleWithoutFont.fontFamily;
|
|
349
|
+
const initialText = fontFamily ? '' : component.text;
|
|
350
|
+
const bitmapText = new rendererAdapter.BitmapText(initialText, styleWithoutFont);
|
|
351
|
+
this.containerManager.getContainer(changed.gameObject.id).addChildAt(bitmapText, 0);
|
|
352
|
+
this.texts[changed.gameObject.id] = {
|
|
353
|
+
text: bitmapText,
|
|
354
|
+
component,
|
|
355
|
+
};
|
|
356
|
+
// 如果指定了字体资源,等待资源加载完成后设置 fontFamily
|
|
357
|
+
if (fontFamily) {
|
|
358
|
+
yield this.waitForFontResource(bitmapText, changed, fontFamily);
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* 等待字体资源加载完成并更新文本
|
|
364
|
+
*/
|
|
365
|
+
waitForFontResource(text, changed, fontFamily) {
|
|
366
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
367
|
+
if (!fontFamily) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
try {
|
|
371
|
+
const fontName = Array.isArray(fontFamily) ? fontFamily[0] : fontFamily;
|
|
372
|
+
// 通过 resource 系统获取字体资源
|
|
373
|
+
const asyncId = this.increaseAsyncId(changed.gameObject.id);
|
|
374
|
+
yield eva_js.resource.getResource(fontName);
|
|
375
|
+
// 验证异步操作是否仍然有效(防止组件已被移除)
|
|
376
|
+
if (!this.validateAsyncId(changed.gameObject.id, asyncId))
|
|
377
|
+
return;
|
|
378
|
+
// 字体资源加载成功后,设置 fontFamily 并重新设置文本内容以触发重新渲染
|
|
379
|
+
const component = this.texts[changed.gameObject.id].component;
|
|
380
|
+
text.style.fontFamily = fontFamily;
|
|
381
|
+
text.text = component.text;
|
|
382
|
+
// 更新尺寸
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
console.warn(`字体资源 ${fontFamily} 加载失败:`, error);
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* 将 Eva.js 的样式格式转换为 PixiJS v8 格式
|
|
391
|
+
*/
|
|
392
|
+
processStyle(style) {
|
|
393
|
+
const processed = Object.assign({}, style);
|
|
394
|
+
// stroke + strokeThickness -> stroke: { color, width }
|
|
395
|
+
if (processed.strokeThickness) {
|
|
396
|
+
const color = processed.stroke;
|
|
397
|
+
processed.stroke = {
|
|
398
|
+
color,
|
|
399
|
+
width: processed.strokeThickness,
|
|
400
|
+
};
|
|
401
|
+
delete processed.strokeThickness;
|
|
402
|
+
}
|
|
403
|
+
// dropShadow* -> dropShadow: { color, distance, angle, alpha, blur }
|
|
404
|
+
if (processed.dropShadow) {
|
|
405
|
+
const dropShadowConfig = {};
|
|
406
|
+
if (processed.dropShadowColor != null) {
|
|
407
|
+
dropShadowConfig.color = processed.dropShadowColor;
|
|
408
|
+
}
|
|
409
|
+
if (processed.dropShadowDistance != null) {
|
|
410
|
+
dropShadowConfig.distance = processed.dropShadowDistance;
|
|
411
|
+
}
|
|
412
|
+
if (processed.dropShadowAngle != null) {
|
|
413
|
+
dropShadowConfig.angle = processed.dropShadowAngle;
|
|
414
|
+
}
|
|
415
|
+
if (processed.dropShadowAlpha != null) {
|
|
416
|
+
dropShadowConfig.alpha = processed.dropShadowAlpha;
|
|
417
|
+
}
|
|
418
|
+
if (processed.dropShadowBlur != null) {
|
|
419
|
+
dropShadowConfig.blur = processed.dropShadowBlur;
|
|
420
|
+
}
|
|
421
|
+
processed.dropShadow = dropShadowConfig;
|
|
422
|
+
delete processed.dropShadowColor;
|
|
423
|
+
delete processed.dropShadowDistance;
|
|
424
|
+
delete processed.dropShadowAngle;
|
|
425
|
+
delete processed.dropShadowAlpha;
|
|
426
|
+
delete processed.dropShadowBlur;
|
|
427
|
+
}
|
|
428
|
+
// fill 数组 -> 取第一个值 (deprecated)
|
|
429
|
+
if (Array.isArray(processed.fill)) {
|
|
430
|
+
processed.fill = processed.fill[0];
|
|
431
|
+
}
|
|
432
|
+
// fillGradientStops -> FillGradient 对象
|
|
433
|
+
if (Array.isArray(processed.fillGradientStops)) {
|
|
434
|
+
let fontSize;
|
|
435
|
+
if (processed.fontSize == null) {
|
|
436
|
+
fontSize = pixi_js.TextStyle.defaultTextStyle.fontSize;
|
|
437
|
+
}
|
|
438
|
+
else if (typeof processed.fontSize === 'string') {
|
|
439
|
+
fontSize = parseInt(processed.fontSize, 10);
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
fontSize = processed.fontSize;
|
|
443
|
+
}
|
|
444
|
+
const gradientFill = new pixi_js.FillGradient(0, 0, 0, fontSize * 1.7);
|
|
445
|
+
const fills = processed.fillGradientStops.map((color) => pixi_js.Color.shared.setValue(color).toNumber());
|
|
446
|
+
fills.forEach((number, index) => {
|
|
447
|
+
const ratio = index / (fills.length - 1);
|
|
448
|
+
gradientFill.addColorStop(ratio, number);
|
|
449
|
+
});
|
|
450
|
+
processed.fill = { fill: gradientFill };
|
|
451
|
+
delete processed.fillGradientStops;
|
|
452
|
+
}
|
|
453
|
+
return processed;
|
|
454
|
+
}
|
|
106
455
|
change(changed) {
|
|
107
456
|
const { text, component } = this.texts[changed.gameObject.id];
|
|
457
|
+
const isHTMLText = changed.componentName === 'HTMLText';
|
|
108
458
|
if (changed.prop.prop[0] === 'text') {
|
|
109
459
|
text.text = component.text;
|
|
110
460
|
}
|
|
111
461
|
else if (changed.prop.prop[0] === 'style') {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
asyncChangeTextStyle(text, textStyle) {
|
|
116
|
-
if (textStyle.fontFamily) {
|
|
117
|
-
const fontFamily = textStyle.fontFamily;
|
|
118
|
-
textStyle.fontFamily = '';
|
|
119
|
-
this.asyncUpdateFontFamily(text, fontFamily);
|
|
462
|
+
const processedStyle = this.processStyle(component.style);
|
|
463
|
+
Object.assign(text.style, processedStyle);
|
|
120
464
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
465
|
+
else if (changed.prop.prop[0] === 'textureStyle' && isHTMLText) {
|
|
466
|
+
// HTMLText 纹理样式变化需要重新创建
|
|
467
|
+
const htmlComponent = component;
|
|
468
|
+
const container = this.containerManager.getContainer(changed.gameObject.id);
|
|
469
|
+
const index = container.getChildIndex(text);
|
|
470
|
+
container.removeChild(text);
|
|
471
|
+
text.destroy({ children: true });
|
|
472
|
+
const newText = new rendererAdapter.HTMLText({
|
|
473
|
+
text: htmlComponent.text,
|
|
474
|
+
style: htmlComponent.style,
|
|
475
|
+
textureStyle: htmlComponent.textureStyle
|
|
476
|
+
});
|
|
477
|
+
container.addChildAt(newText, index);
|
|
478
|
+
this.texts[changed.gameObject.id].text = newText;
|
|
135
479
|
}
|
|
136
480
|
}
|
|
137
481
|
setSize(changed) {
|
|
138
482
|
const { transform } = changed.gameObject;
|
|
139
483
|
if (!transform)
|
|
140
484
|
return;
|
|
141
|
-
|
|
142
|
-
|
|
485
|
+
const { text } = this.texts[changed.gameObject.id];
|
|
486
|
+
const size = text.getSize();
|
|
487
|
+
transform.size.width = size.width;
|
|
488
|
+
transform.size.height = size.height;
|
|
143
489
|
}
|
|
144
490
|
};
|
|
145
491
|
Text.systemName = 'Text';
|
|
146
492
|
Text = __decorate([
|
|
147
493
|
eva_js.decorators.componentObserver({
|
|
148
494
|
Text: ['text', { prop: ['style'], deep: true }],
|
|
495
|
+
HTMLText: ['text', { prop: ['style'], deep: true }, { prop: ['textureStyle'], deep: true }],
|
|
496
|
+
BitmapText: ['text', { prop: ['style'], deep: true }],
|
|
149
497
|
})
|
|
150
498
|
], Text);
|
|
151
499
|
var Text$1 = Text;
|
|
152
500
|
|
|
501
|
+
exports.BitmapText = BitmapText;
|
|
502
|
+
exports.HTMLText = HTMLText;
|
|
153
503
|
exports.Text = Text$2;
|
|
154
504
|
exports.TextSystem = Text$1;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@eva/eva.js"),e=require("@eva/inspector-decorator"),o=require("pixi.js"),n=require("@eva/plugin-renderer"),i=require("@eva/renderer-adapter");
|
|
2
2
|
/*! *****************************************************************************
|
|
3
3
|
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
4
4
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
@@ -13,4 +13,4 @@ MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
|
13
13
|
See the Apache Version 2.0 License for specific language governing permissions
|
|
14
14
|
and limitations under the License.
|
|
15
15
|
***************************************************************************** */
|
|
16
|
-
function
|
|
16
|
+
function s(t,e,o,n){var i,s=arguments.length,r=s<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(t,e,o,n);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(r=(s<3?i(r):s>3?i(e,o,r):i(e,o))||r);return s>3&&r&&Object.defineProperty(e,o,r),r}function r(t,e,o,n){return new(o||(o=Promise))((function(i,s){function r(t){try{l(n.next(t))}catch(t){s(t)}}function a(t){try{l(n.throw(t))}catch(t){s(t)}}function l(t){t.done?i(t.value):new o((function(e){e(t.value)})).then(r,a)}l((n=n.apply(t,e||[])).next())}))}class a extends t.Component{constructor(){super(...arguments),this.text="",this.style={}}init(t){this.style=Object.assign({fontSize:24,fill:"#000000",fontFamily:"Arial"},null==t?void 0:t.style),t&&(this.text=t.text)}}a.componentName="BitmapText",s([e.type("string")],a.prototype,"text",void 0);class l extends t.Component{constructor(){super(...arguments),this.text="",this.style={}}init(t){const e=new o.TextStyle({fontSize:20}),n={};for(const t in e)0===t.indexOf("_")&&(n[t.substring(1)]=e[t]);delete n.styleKey,this.style=n,t&&(this.text=t.text,Object.assign(this.style,t.style))}}l.componentName="Text",s([e.type("string")],l.prototype,"text",void 0);class d extends t.Component{constructor(){super(...arguments),this.text="",this.style={},this.textureStyle={}}init(t){this.style=Object.assign({fontSize:24,fill:"#000000",fontFamily:"Arial"},null==t?void 0:t.style),this.textureStyle=Object.assign({scaleMode:"linear",resolution:window.devicePixelRatio||1},null==t?void 0:t.textureStyle),t&&(this.text=t.text)}}d.componentName="HTMLText",s([e.type("string")],d.prototype,"text",void 0);let c=class extends n.Renderer{constructor(){super(...arguments),this.name="Text",this.texts={}}init(){this.renderSystem=this.game.getSystem(n.RendererSystem),this.renderSystem.rendererManager.register(this)}componentChanged(e){return r(this,void 0,void 0,(function*(){const o="Text"===e.componentName,n="HTMLText"===e.componentName,i="BitmapText"===e.componentName;if(o||n||i)if(e.type===t.OBSERVER_TYPE.ADD)o?yield this.addTextComponent(e):n?yield this.addHTMLTextComponent(e):yield this.addBitmapTextComponent(e),this.setSize(e);else if(e.type===t.OBSERVER_TYPE.REMOVE)this.containerManager.getContainer(e.gameObject.id).removeChild(this.texts[e.gameObject.id].text),this.texts[e.gameObject.id].text.destroy({children:!0}),delete this.texts[e.gameObject.id];else{this.change(e);const t=e.component;if("style"===e.prop.prop[0]&&t.style&&t.style.fontFamily){const{text:o}=this.texts[e.gameObject.id];yield this.waitForFontResource(o,e,t.style.fontFamily)}this.setSize(e)}}))}addTextComponent(t){return r(this,void 0,void 0,(function*(){const e=t.component,o=Object.assign({},e.style),n=o.fontFamily;delete o.fontFamily;const s=n?"":e.text,r=new i.Text(s,o);this.containerManager.getContainer(t.gameObject.id).addChildAt(r,0),this.texts[t.gameObject.id]={text:r,component:e},n&&(yield this.waitForFontResource(r,t,n))}))}addHTMLTextComponent(t){return r(this,void 0,void 0,(function*(){const e=t.component,o=Object.assign({},e.style),n=o.fontFamily;delete o.fontFamily;const s=n?"":e.text,r=new i.HTMLText(Object.assign({text:s,style:o},e.textureStyle&&{textureStyle:e.textureStyle}));this.containerManager.getContainer(t.gameObject.id).addChildAt(r,0),this.texts[t.gameObject.id]={text:r,component:e},n&&(yield this.waitForFontResource(r,t,n))}))}addBitmapTextComponent(t){return r(this,void 0,void 0,(function*(){const e=t.component,o=Object.assign({},e.style),n=o.fontFamily;delete o.fontFamily;const s=n?"":e.text,r=new i.BitmapText(s,o);this.containerManager.getContainer(t.gameObject.id).addChildAt(r,0),this.texts[t.gameObject.id]={text:r,component:e},n&&(yield this.waitForFontResource(r,t,n))}))}waitForFontResource(e,o,n){return r(this,void 0,void 0,(function*(){if(n)try{const i=Array.isArray(n)?n[0]:n,s=this.increaseAsyncId(o.gameObject.id);if(yield t.resource.getResource(i),!this.validateAsyncId(o.gameObject.id,s))return;const r=this.texts[o.gameObject.id].component;e.style.fontFamily=n,e.text=r.text}catch(t){console.warn(`字体资源 ${n} 加载失败:`,t)}}))}processStyle(t){const e=Object.assign({},t);if(e.strokeThickness){const t=e.stroke;e.stroke={color:t,width:e.strokeThickness},delete e.strokeThickness}if(e.dropShadow){const t={};null!=e.dropShadowColor&&(t.color=e.dropShadowColor),null!=e.dropShadowDistance&&(t.distance=e.dropShadowDistance),null!=e.dropShadowAngle&&(t.angle=e.dropShadowAngle),null!=e.dropShadowAlpha&&(t.alpha=e.dropShadowAlpha),null!=e.dropShadowBlur&&(t.blur=e.dropShadowBlur),e.dropShadow=t,delete e.dropShadowColor,delete e.dropShadowDistance,delete e.dropShadowAngle,delete e.dropShadowAlpha,delete e.dropShadowBlur}if(Array.isArray(e.fill)&&(e.fill=e.fill[0]),Array.isArray(e.fillGradientStops)){let t;t=null==e.fontSize?o.TextStyle.defaultTextStyle.fontSize:"string"==typeof e.fontSize?parseInt(e.fontSize,10):e.fontSize;const n=new o.FillGradient(0,0,0,1.7*t),i=e.fillGradientStops.map((t=>o.Color.shared.setValue(t).toNumber()));i.forEach(((t,e)=>{const o=e/(i.length-1);n.addColorStop(o,t)})),e.fill={fill:n},delete e.fillGradientStops}return e}change(t){const{text:e,component:o}=this.texts[t.gameObject.id],n="HTMLText"===t.componentName;if("text"===t.prop.prop[0])e.text=o.text;else if("style"===t.prop.prop[0]){const t=this.processStyle(o.style);Object.assign(e.style,t)}else if("textureStyle"===t.prop.prop[0]&&n){const n=o,s=this.containerManager.getContainer(t.gameObject.id),r=s.getChildIndex(e);s.removeChild(e),e.destroy({children:!0});const a=new i.HTMLText({text:n.text,style:n.style,textureStyle:n.textureStyle});s.addChildAt(a,r),this.texts[t.gameObject.id].text=a}}setSize(t){const{transform:e}=t.gameObject;if(!e)return;const{text:o}=this.texts[t.gameObject.id],n=o.getSize();e.size.width=n.width,e.size.height=n.height}};c.systemName="Text",c=s([t.decorators.componentObserver({Text:["text",{prop:["style"],deep:!0}],HTMLText:["text",{prop:["style"],deep:!0},{prop:["textureStyle"],deep:!0}],BitmapText:["text",{prop:["style"],deep:!0}]})],c);var p=c;exports.BitmapText=a,exports.HTMLText=d,exports.Text=l,exports.TextSystem=p;
|