Skip to content

Commit 620c5a3

Browse files
committed
Added Graphics.setTexture and a clear texture command
1 parent 7acbbcf commit 620c5a3

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

src/gameobjects/graphics/Commands.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module.exports = {
2828
RESTORE: 15,
2929
TRANSLATE: 16,
3030
SCALE: 17,
31-
ROTATE: 18
31+
ROTATE: 18,
32+
SET_TEXTURE: 19,
33+
CLEAR_TEXTURE: 20
3234

3335
};

src/gameobjects/graphics/Graphics.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,62 @@ var Graphics = new Class({
314314
return this;
315315
},
316316

317+
/**
318+
* Sets the texture and frame this Graphics Object will use when texturing the shapes it renders.
319+
*
320+
* Textures are referenced by their string-based keys, as stored in the Texture Manager.
321+
*
322+
* Once set, all shapes will use this texture. Call this method with no arguments to clear a previously texture.
323+
*
324+
* The textures are not tiled. They are stretched to the dimensions of the shapes being rendered. For this reason,
325+
* it works best with seamless / tileable textures.
326+
*
327+
* The mode argument controls how the textures are combined with the fill colors. The default value (0) will
328+
* multiply the texture by the fill color. A value of 1 will use just the fill color, but the alpha data from the texture,
329+
* and a value of 2 will use just the texture and no fill color at all.
330+
*
331+
* @method Phaser.GameObjects.Graphics#setTexture
332+
* @since 3.12.0
333+
* @webglOnly
334+
*
335+
* @param {string} [key] - The key of the texture to be used, as stored in the Texture Manager. Leave blank to clear a previously set texture.
336+
* @param {(string|integer)} [frame] - The name or index of the frame within the Texture.
337+
* @param {number} [mode=0] - The texture tint mode. 0 is multiply, 1 is alpha only and 2 is texture only.
338+
*
339+
* @return {this} This Game Object.
340+
*/
341+
setTexture: function (key, frame, mode)
342+
{
343+
if (mode === undefined) { mode = 0; }
344+
345+
if (key === undefined)
346+
{
347+
this.commandBuffer.push(
348+
Commands.CLEAR_TEXTURE
349+
);
350+
}
351+
else
352+
{
353+
var textureFrame = this.scene.sys.textures.getFrame(key, frame);
354+
355+
if (textureFrame)
356+
{
357+
if (mode === 2)
358+
{
359+
mode = 3;
360+
}
361+
362+
this.commandBuffer.push(
363+
Commands.SET_TEXTURE,
364+
textureFrame,
365+
mode
366+
);
367+
}
368+
}
369+
370+
return this;
371+
},
372+
317373
/**
318374
* Start a new shape path.
319375
*

0 commit comments

Comments
 (0)