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