@eva/plugin-renderer-text 2.0.1-beta.4 → 2.0.1-beta.40

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.
@@ -2,7 +2,7 @@ function _extends() { return _extends = Object.assign ? Object.assign.bind() : f
2
2
  globalThis.EVA = globalThis.EVA || {};
3
3
  globalThis.EVA.plugin = globalThis.EVA.plugin || {};
4
4
  globalThis.EVA.plugin.renderer = globalThis.EVA.plugin.renderer || {};
5
- var _EVA_IIFE_text = function (exports, pixi_js, eva_js, pluginRenderer, rendererAdapter) {
5
+ var _EVA_IIFE_text = function (exports, eva_js, pixi_js, pluginRenderer, rendererAdapter) {
6
6
  'use strict';
7
7
  function __decorate(decorators, target, key, desc) {
8
8
  var c = arguments.length,
@@ -55,6 +55,25 @@ var _EVA_IIFE_text = function (exports, pixi_js, eva_js, pluginRenderer, rendere
55
55
  prop.type = type;
56
56
  };
57
57
  }
58
+ class BitmapText extends eva_js.Component {
59
+ constructor() {
60
+ super(...arguments);
61
+ this.text = '';
62
+ this.style = {};
63
+ }
64
+ init(obj) {
65
+ this.style = _extends({
66
+ fontSize: 24,
67
+ fill: '#000000',
68
+ fontFamily: 'Arial'
69
+ }, obj === null || obj === void 0 ? void 0 : obj.style);
70
+ if (obj) {
71
+ this.text = obj.text;
72
+ }
73
+ }
74
+ }
75
+ BitmapText.componentName = 'BitmapText';
76
+ __decorate([type('string')], BitmapText.prototype, "text", void 0);
58
77
  class Text$2 extends eva_js.Component {
59
78
  constructor() {
60
79
  super(...arguments);
@@ -81,6 +100,30 @@ var _EVA_IIFE_text = function (exports, pixi_js, eva_js, pluginRenderer, rendere
81
100
  }
82
101
  Text$2.componentName = 'Text';
83
102
  __decorate([type('string')], Text$2.prototype, "text", void 0);
103
+ class HTMLText extends eva_js.Component {
104
+ constructor() {
105
+ super(...arguments);
106
+ this.text = '';
107
+ this.style = {};
108
+ this.textureStyle = {};
109
+ }
110
+ init(obj) {
111
+ this.style = _extends({
112
+ fontSize: 24,
113
+ fill: '#000000',
114
+ fontFamily: 'Arial'
115
+ }, obj === null || obj === void 0 ? void 0 : obj.style);
116
+ this.textureStyle = _extends({
117
+ scaleMode: 'linear',
118
+ resolution: window.devicePixelRatio || 1
119
+ }, obj === null || obj === void 0 ? void 0 : obj.textureStyle);
120
+ if (obj) {
121
+ this.text = obj.text;
122
+ }
123
+ }
124
+ }
125
+ HTMLText.componentName = 'HTMLText';
126
+ __decorate([type('string')], HTMLText.prototype, "text", void 0);
84
127
  let Text = class Text extends pluginRenderer.Renderer {
85
128
  constructor() {
86
129
  super(...arguments);
@@ -93,15 +136,18 @@ var _EVA_IIFE_text = function (exports, pixi_js, eva_js, pluginRenderer, rendere
93
136
  }
94
137
  componentChanged(changed) {
95
138
  return __awaiter(this, void 0, void 0, function* () {
96
- if (changed.componentName !== 'Text') return;
139
+ const isText = changed.componentName === 'Text';
140
+ const isHTMLText = changed.componentName === 'HTMLText';
141
+ const isBitmapText = changed.componentName === 'BitmapText';
142
+ if (!isText && !isHTMLText && !isBitmapText) return;
97
143
  if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
98
- const component = changed.component;
99
- const text = new rendererAdapter.Text(component.text, component.style);
100
- this.containerManager.getContainer(changed.gameObject.id).addChildAt(text, 0);
101
- this.texts[changed.gameObject.id] = {
102
- text,
103
- component: changed.component
104
- };
144
+ if (isText) {
145
+ yield this.addTextComponent(changed);
146
+ } else if (isHTMLText) {
147
+ yield this.addHTMLTextComponent(changed);
148
+ } else {
149
+ yield this.addBitmapTextComponent(changed);
150
+ }
105
151
  this.setSize(changed);
106
152
  } else if (changed.type === eva_js.OBSERVER_TYPE.REMOVE) {
107
153
  this.containerManager.getContainer(changed.gameObject.id).removeChild(this.texts[changed.gameObject.id].text);
@@ -111,19 +157,179 @@ var _EVA_IIFE_text = function (exports, pixi_js, eva_js, pluginRenderer, rendere
111
157
  delete this.texts[changed.gameObject.id];
112
158
  } else {
113
159
  this.change(changed);
160
+ const component = changed.component;
161
+ if (changed.prop.prop[0] === 'style' && component.style && component.style.fontFamily) {
162
+ const {
163
+ text
164
+ } = this.texts[changed.gameObject.id];
165
+ yield this.waitForFontResource(text, changed, component.style.fontFamily);
166
+ }
114
167
  this.setSize(changed);
115
168
  }
116
169
  });
117
170
  }
171
+ addTextComponent(changed) {
172
+ return __awaiter(this, void 0, void 0, function* () {
173
+ const component = changed.component;
174
+ const styleWithoutFont = _extends({}, component.style);
175
+ const fontFamily = styleWithoutFont.fontFamily;
176
+ delete styleWithoutFont.fontFamily;
177
+ const initialText = fontFamily ? '' : component.text;
178
+ const text = new rendererAdapter.Text(initialText, styleWithoutFont);
179
+ this.containerManager.getContainer(changed.gameObject.id).addChildAt(text, 0);
180
+ this.texts[changed.gameObject.id] = {
181
+ text,
182
+ component
183
+ };
184
+ if (fontFamily) {
185
+ yield this.waitForFontResource(text, changed, fontFamily);
186
+ }
187
+ });
188
+ }
189
+ addHTMLTextComponent(changed) {
190
+ return __awaiter(this, void 0, void 0, function* () {
191
+ const component = changed.component;
192
+ const styleWithoutFont = _extends({}, component.style);
193
+ const fontFamily = styleWithoutFont.fontFamily;
194
+ delete styleWithoutFont.fontFamily;
195
+ const initialText = fontFamily ? '' : component.text;
196
+ const htmlText = new rendererAdapter.HTMLText(_extends({
197
+ text: initialText,
198
+ style: styleWithoutFont
199
+ }, component.textureStyle && {
200
+ textureStyle: component.textureStyle
201
+ }));
202
+ this.containerManager.getContainer(changed.gameObject.id).addChildAt(htmlText, 0);
203
+ this.texts[changed.gameObject.id] = {
204
+ text: htmlText,
205
+ component
206
+ };
207
+ if (fontFamily) {
208
+ yield this.waitForFontResource(htmlText, changed, fontFamily);
209
+ }
210
+ });
211
+ }
212
+ addBitmapTextComponent(changed) {
213
+ return __awaiter(this, void 0, void 0, function* () {
214
+ const component = changed.component;
215
+ const styleWithoutFont = _extends({}, component.style);
216
+ const fontFamily = styleWithoutFont.fontFamily;
217
+ delete styleWithoutFont.fontFamily;
218
+ const initialText = fontFamily ? '' : component.text;
219
+ const bitmapText = new rendererAdapter.BitmapText(initialText, styleWithoutFont);
220
+ this.containerManager.getContainer(changed.gameObject.id).addChildAt(bitmapText, 0);
221
+ this.texts[changed.gameObject.id] = {
222
+ text: bitmapText,
223
+ component
224
+ };
225
+ if (fontFamily) {
226
+ yield this.waitForFontResource(bitmapText, changed, fontFamily);
227
+ }
228
+ });
229
+ }
230
+ waitForFontResource(text, changed, fontFamily) {
231
+ return __awaiter(this, void 0, void 0, function* () {
232
+ if (!fontFamily) {
233
+ return;
234
+ }
235
+ try {
236
+ const fontName = Array.isArray(fontFamily) ? fontFamily[0] : fontFamily;
237
+ const asyncId = this.increaseAsyncId(changed.gameObject.id);
238
+ yield eva_js.resource.getResource(fontName);
239
+ if (!this.validateAsyncId(changed.gameObject.id, asyncId)) return;
240
+ const component = this.texts[changed.gameObject.id].component;
241
+ text.style.fontFamily = fontFamily;
242
+ text.text = component.text;
243
+ } catch (error) {
244
+ console.warn(`字体资源 ${fontFamily} 加载失败:`, error);
245
+ }
246
+ });
247
+ }
248
+ processStyle(style) {
249
+ const processed = _extends({}, style);
250
+ if (processed.strokeThickness) {
251
+ const color = processed.stroke;
252
+ processed.stroke = {
253
+ color,
254
+ width: processed.strokeThickness
255
+ };
256
+ delete processed.strokeThickness;
257
+ }
258
+ if (processed.dropShadow) {
259
+ const dropShadowConfig = {};
260
+ if (processed.dropShadowColor != null) {
261
+ dropShadowConfig.color = processed.dropShadowColor;
262
+ }
263
+ if (processed.dropShadowDistance != null) {
264
+ dropShadowConfig.distance = processed.dropShadowDistance;
265
+ }
266
+ if (processed.dropShadowAngle != null) {
267
+ dropShadowConfig.angle = processed.dropShadowAngle;
268
+ }
269
+ if (processed.dropShadowAlpha != null) {
270
+ dropShadowConfig.alpha = processed.dropShadowAlpha;
271
+ }
272
+ if (processed.dropShadowBlur != null) {
273
+ dropShadowConfig.blur = processed.dropShadowBlur;
274
+ }
275
+ processed.dropShadow = dropShadowConfig;
276
+ delete processed.dropShadowColor;
277
+ delete processed.dropShadowDistance;
278
+ delete processed.dropShadowAngle;
279
+ delete processed.dropShadowAlpha;
280
+ delete processed.dropShadowBlur;
281
+ }
282
+ if (Array.isArray(processed.fill)) {
283
+ processed.fill = processed.fill[0];
284
+ }
285
+ if (Array.isArray(processed.fillGradientStops)) {
286
+ let fontSize;
287
+ if (processed.fontSize == null) {
288
+ fontSize = pixi_js.TextStyle.defaultTextStyle.fontSize;
289
+ } else if (typeof processed.fontSize === 'string') {
290
+ fontSize = parseInt(processed.fontSize, 10);
291
+ } else {
292
+ fontSize = processed.fontSize;
293
+ }
294
+ const gradientFill = new pixi_js.FillGradient(0, 0, 0, fontSize * 1.7);
295
+ const fills = processed.fillGradientStops.map(color => pixi_js.Color.shared.setValue(color).toNumber());
296
+ fills.forEach((number, index) => {
297
+ const ratio = index / (fills.length - 1);
298
+ gradientFill.addColorStop(ratio, number);
299
+ });
300
+ processed.fill = {
301
+ fill: gradientFill
302
+ };
303
+ delete processed.fillGradientStops;
304
+ }
305
+ return processed;
306
+ }
118
307
  change(changed) {
119
308
  const {
120
309
  text,
121
310
  component
122
311
  } = this.texts[changed.gameObject.id];
312
+ const isHTMLText = changed.componentName === 'HTMLText';
123
313
  if (changed.prop.prop[0] === 'text') {
124
314
  text.text = component.text;
125
315
  } else if (changed.prop.prop[0] === 'style') {
126
- _extends(text.style, changed.component.style);
316
+ const processedStyle = this.processStyle(component.style);
317
+ _extends(text.style, processedStyle);
318
+ } else if (changed.prop.prop[0] === 'textureStyle' && isHTMLText) {
319
+ const htmlComponent = component;
320
+ const container = this.containerManager.getContainer(changed.gameObject.id);
321
+ const index = container.getChildIndex(text);
322
+ container.removeChild(text);
323
+ text.destroy({
324
+ children: true
325
+ });
326
+ const newText = new rendererAdapter.HTMLText({
327
+ text: htmlComponent.text,
328
+ style: htmlComponent.style,
329
+ textureStyle: htmlComponent.textureStyle
330
+ });
331
+ container.addChildAt(newText, index);
332
+ this.texts[changed.gameObject.id].text = newText;
127
333
  }
128
334
  }
129
335
  setSize(changed) {
@@ -131,8 +337,12 @@ var _EVA_IIFE_text = function (exports, pixi_js, eva_js, pluginRenderer, rendere
131
337
  transform
132
338
  } = changed.gameObject;
133
339
  if (!transform) return;
134
- transform.size.width = this.texts[changed.gameObject.id].text.width;
135
- transform.size.height = this.texts[changed.gameObject.id].text.height;
340
+ const {
341
+ text
342
+ } = this.texts[changed.gameObject.id];
343
+ const size = text.getSize();
344
+ transform.size.width = size.width;
345
+ transform.size.height = size.height;
136
346
  }
137
347
  };
138
348
  Text.systemName = 'Text';
@@ -140,14 +350,27 @@ var _EVA_IIFE_text = function (exports, pixi_js, eva_js, pluginRenderer, rendere
140
350
  Text: ['text', {
141
351
  prop: ['style'],
142
352
  deep: true
353
+ }],
354
+ HTMLText: ['text', {
355
+ prop: ['style'],
356
+ deep: true
357
+ }, {
358
+ prop: ['textureStyle'],
359
+ deep: true
360
+ }],
361
+ BitmapText: ['text', {
362
+ prop: ['style'],
363
+ deep: true
143
364
  }]
144
365
  })], Text);
145
366
  var Text$1 = Text;
367
+ exports.BitmapText = BitmapText;
368
+ exports.HTMLText = HTMLText;
146
369
  exports.Text = Text$2;
147
370
  exports.TextSystem = Text$1;
148
371
  Object.defineProperty(exports, '__esModule', {
149
372
  value: true
150
373
  });
151
374
  return exports;
152
- }({}, PIXI, EVA, EVA.plugin.renderer, EVA.rendererAdapter);
375
+ }({}, EVA, PIXI, EVA.plugin.renderer, EVA.rendererAdapter);
153
376
  globalThis.EVA.plugin.renderer.text = globalThis.EVA.plugin.renderer.text || _EVA_IIFE_text;