Skip to content

Commit e91d95e

Browse files
committed
Text updated to use proper texture frame and now supports crop
1 parent 177e47c commit e91d95e

2 files changed

Lines changed: 57 additions & 21 deletions

File tree

src/gameobjects/text/static/Text.js

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var TextStyle = require('../TextStyle');
2929
* @extends Phaser.GameObjects.Components.Alpha
3030
* @extends Phaser.GameObjects.Components.BlendMode
3131
* @extends Phaser.GameObjects.Components.ComputedSize
32+
* @extends Phaser.GameObjects.Components.Crop
3233
* @extends Phaser.GameObjects.Components.Depth
3334
* @extends Phaser.GameObjects.Components.Flip
3435
* @extends Phaser.GameObjects.Components.GetBounds
@@ -55,6 +56,7 @@ var Text = new Class({
5556
Components.Alpha,
5657
Components.BlendMode,
5758
Components.ComputedSize,
59+
Components.Crop,
5860
Components.Depth,
5961
Components.Flip,
6062
Components.GetBounds,
@@ -78,6 +80,15 @@ var Text = new Class({
7880

7981
GameObject.call(this, scene, 'Text');
8082

83+
/**
84+
* The renderer in use by this Text object.
85+
*
86+
* @name Phaser.GameObjects.Text#renderer
87+
* @type {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)}
88+
* @since 3.12.0
89+
*/
90+
this.renderer = scene.sys.game.renderer;
91+
8192
this.setPosition(x, y);
8293
this.setOrigin(0, 0);
8394
this.initPipeline('TextureTintPipeline');
@@ -171,16 +182,6 @@ var Text = new Class({
171182
*/
172183
this.height = 1;
173184

174-
/**
175-
* The Canvas Texture that the text is rendered to for WebGL rendering.
176-
*
177-
* @name Phaser.GameObjects.Text#canvasTexture
178-
* @type {HTMLCanvasElement}
179-
* @default null
180-
* @since 3.0.0
181-
*/
182-
this.canvasTexture = null;
183-
184185
/**
185186
* Whether the text or its settings have changed and need updating.
186187
*
@@ -197,6 +198,34 @@ var Text = new Class({
197198
this.style.resolution = scene.sys.game.config.resolution;
198199
}
199200

201+
/**
202+
* The internal crop data object, as used by `setCrop` and passed to the `Frame.setCropUVs` method.
203+
*
204+
* @name Phaser.GameObjects.Text#_crop
205+
* @type {object}
206+
* @private
207+
* @since 3.12.0
208+
*/
209+
this._crop = this.resetCropObject();
210+
211+
// Create a Texture for this Text object
212+
this.texture = scene.sys.textures.addCanvas(null, this.canvas, true);
213+
214+
// Get the frame
215+
this.frame = this.texture.get();
216+
217+
// Set the resolution
218+
this.frame.source.resolution = this.style.resolution;
219+
220+
if (this.renderer && this.renderer.gl)
221+
{
222+
// Clear the default 1x1 glTexture, as we override it later
223+
224+
this.renderer.deleteTexture(this.frame.source.glTexture);
225+
226+
this.frame.source.glTexture = null;
227+
}
228+
200229
this.initRTL();
201230

202231
if (style && style.padding)
@@ -215,7 +244,6 @@ var Text = new Class({
215244
{
216245
scene.sys.game.renderer.onContextRestored(function ()
217246
{
218-
this.canvasTexture = null;
219247
this.dirty = true;
220248
}, this);
221249
}
@@ -1014,6 +1042,9 @@ var Text = new Class({
10141042
{
10151043
canvas.width = w;
10161044
canvas.height = h;
1045+
1046+
this.frame.setSize(w, h);
1047+
10171048
style.syncFont(canvas, context); // Resizing resets the context
10181049
}
10191050
else
@@ -1088,6 +1119,12 @@ var Text = new Class({
10881119

10891120
context.restore();
10901121

1122+
if (this.renderer.gl)
1123+
{
1124+
this.frame.source.glTexture = this.renderer.canvasToTexture(canvas, this.frame.source.glTexture);
1125+
this.frame.glTexture = this.frame.source.glTexture;
1126+
}
1127+
10911128
this.dirty = true;
10921129

10931130
return this;
@@ -1152,6 +1189,8 @@ var Text = new Class({
11521189
}
11531190

11541191
CanvasPool.remove(this.canvas);
1192+
1193+
this.texture.destroy();
11551194
}
11561195

11571196
});

src/gameobjects/text/static/TextWebGLRenderer.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,24 @@ var TextWebGLRenderer = function (renderer, src, interpolationPercentage, camera
2727
{
2828
return;
2929
}
30-
31-
if (src.dirty)
32-
{
33-
src.canvasTexture = renderer.canvasToTexture(src.canvas, src.canvasTexture);
34-
src.dirty = false;
35-
}
3630

31+
var frame = src.frame;
32+
var width = frame.width;
33+
var height = frame.height;
3734
var getTint = Utils.getTintAppendFloatAlpha;
3835

3936
this.pipeline.batchTexture(
4037
src,
41-
src.canvasTexture,
42-
src.canvasTexture.width, src.canvasTexture.height,
38+
frame.glTexture,
39+
width, height,
4340
src.x, src.y,
44-
src.canvasTexture.width / src.style.resolution, src.canvasTexture.height / src.style.resolution,
41+
width / src.style.resolution, height / src.style.resolution,
4542
src.scaleX, src.scaleY,
4643
src.rotation,
4744
src.flipX, src.flipY,
4845
src.scrollFactorX, src.scrollFactorY,
4946
src.displayOriginX, src.displayOriginY,
50-
0, 0, src.canvasTexture.width, src.canvasTexture.height,
47+
0, 0, width, height,
5148
getTint(src._tintTL, camera.alpha * src._alphaTL),
5249
getTint(src._tintTR, camera.alpha * src._alphaTR),
5350
getTint(src._tintBL, camera.alpha * src._alphaBL),

0 commit comments

Comments
 (0)