phaser/src/gameobjects/tilesprite/TileSprite.js

295 lines
8.4 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2017-10-11 16:05:59 +00:00
var CanvasPool = require('../../display/canvas/CanvasPool');
2017-04-25 22:09:13 +00:00
var Class = require('../../utils/Class');
var Components = require('../components');
var CONST = require('../../const');
var GameObject = require('../GameObject');
var GetPowerOfTwo = require('../../math/pow2/GetPowerOfTwo');
2017-04-25 22:09:13 +00:00
var TileSpriteRender = require('./TileSpriteRender');
2018-02-06 22:56:27 +00:00
/**
2018-02-07 15:27:21 +00:00
* @classdesc
2018-02-06 22:56:27 +00:00
* [description]
*
* @class TileSprite
* @extends Phaser.GameObjects.GameObject
* @memberOf Phaser.GameObjects
* @constructor
* @since 3.0.0
*
* @extends Phaser.GameObjects.Components.Alpha
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.Flip
* @extends Phaser.GameObjects.Components.GetBounds
* @extends Phaser.GameObjects.Components.Origin
* @extends Phaser.GameObjects.Components.Pipeline
* @extends Phaser.GameObjects.Components.ScaleMode
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Size
* @extends Phaser.GameObjects.Components.Texture
* @extends Phaser.GameObjects.Components.Tint
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.Visible
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {number} x - The horizontal position of this Game Object in the world.
* @param {number} y - The vertical position of this Game Object in the world.
* @param {number} width - The width of the Game Object.
* @param {number} height - The height of the Game Object.
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
2018-03-20 14:56:31 +00:00
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
2018-02-06 22:56:27 +00:00
*/
2017-04-25 22:09:13 +00:00
var TileSprite = new Class({
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.Depth,
2017-04-25 22:09:13 +00:00
Components.Flip,
Components.GetBounds,
Components.Origin,
2018-01-29 21:46:48 +00:00
Components.Pipeline,
2017-04-25 22:09:13 +00:00
Components.ScaleMode,
2017-07-04 11:36:19 +00:00
Components.ScrollFactor,
2017-04-25 22:09:13 +00:00
Components.Size,
Components.Texture,
2017-07-04 11:36:19 +00:00
Components.Tint,
2017-04-25 22:09:13 +00:00
Components.Transform,
Components.Visible,
TileSpriteRender
],
initialize:
function TileSprite (scene, x, y, width, height, texture, frame)
2017-04-25 22:09:13 +00:00
{
2018-01-25 00:15:51 +00:00
var renderer = scene.sys.game.renderer;
GameObject.call(this, scene, 'TileSprite');
2017-04-25 22:09:13 +00:00
2018-02-06 22:56:27 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.TileSprite#tilePositionX
* @type {number}
* @default 0
* @since 3.0.0
*/
2017-04-25 22:09:13 +00:00
this.tilePositionX = 0;
2018-02-06 22:56:27 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.TileSprite#tilePositionY
* @type {number}
* @default 0
* @since 3.0.0
*/
2017-04-25 22:09:13 +00:00
this.tilePositionY = 0;
2018-02-06 22:56:27 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.TileSprite#dirty
* @type {boolean}
* @default true
* @since 3.0.0
*/
2017-05-02 20:57:21 +00:00
this.dirty = true;
2018-02-06 22:56:27 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.TileSprite#tileTexture
2018-03-19 11:54:31 +00:00
* @type {?WebGLTexture}
2018-02-06 22:56:27 +00:00
* @default null
* @since 3.0.0
*/
2017-05-02 20:57:21 +00:00
this.tileTexture = null;
2018-02-06 22:56:27 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.TileSprite#renderer
2018-03-20 14:56:31 +00:00
* @type {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)}
2018-02-06 22:56:27 +00:00
* @since 3.0.0
*/
2018-01-25 00:15:51 +00:00
this.renderer = renderer;
2017-04-25 22:09:13 +00:00
this.setTexture(texture, frame);
this.setPosition(x, y);
2017-05-02 20:57:21 +00:00
this.setSize(width, height);
2018-02-09 15:23:26 +00:00
this.setOriginFromFrame();
2018-01-29 21:46:48 +00:00
this.initPipeline('TextureTintPipeline');
2017-05-02 20:57:21 +00:00
2018-02-06 22:56:27 +00:00
/**
* The next power of two value from the width of the Frame.
*
* @name Phaser.GameObjects.TileSprite#potWidth
* @type {integer}
* @since 3.0.0
*/
this.potWidth = GetPowerOfTwo(this.frame.width);
2018-02-06 22:56:27 +00:00
/**
* The next power of two value from the height of the Frame.
*
* @name Phaser.GameObjects.TileSprite#potHeight
* @type {integer}
* @since 3.0.0
*/
this.potHeight = GetPowerOfTwo(this.frame.height);
2018-02-06 22:56:27 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.TileSprite#canvasPattern
* @type {?CanvasPattern}
* @default null
* @since 3.0.0
*/
2017-05-04 00:14:14 +00:00
this.canvasPattern = null;
2018-02-06 22:56:27 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.TileSprite#canvasBuffer
* @type {HTMLCanvasElement}
* @since 3.0.0
*/
this.canvasBuffer = CanvasPool.create2D(this, this.potWidth, this.potHeight);
2018-02-06 22:56:27 +00:00
/**
* [description]
*
* @name Phaser.GameObjects.TileSprite#canvasBufferCtx
* @type {CanvasRenderingContext2D}
* @since 3.0.0
*/
2017-05-02 20:57:21 +00:00
this.canvasBufferCtx = this.canvasBuffer.getContext('2d');
this.oldFrame = null;
2017-05-02 20:57:21 +00:00
this.updateTileTexture();
if (scene.sys.game.config.renderType === CONST.WEBGL)
2018-02-16 18:17:51 +00:00
{
scene.sys.game.renderer.onContextRestored(function (renderer)
{
var gl = renderer.gl;
this.tileTexture = null;
this.dirty = true;
2018-03-12 14:52:35 +00:00
this.tileTexture = renderer.createTexture2D(0, gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT, gl.RGBA, this.canvasBuffer, this.potWidth, this.potHeight);
}, this);
}
},
/**
* Sets {@link Phaser.GameObjects.TileSprite#tilePositionX} and {@link Phaser.GameObjects.TileSprite#tilePositionY}.
*
* @method Phaser.GameObjects.TileSprite#setTilePosition
2018-03-12 14:23:20 +00:00
* @since 3.3.0
*
* @param {number} [x] - The x position of this sprite's tiling texture.
* @param {number} [y] - The y position of this sprite's tiling texture.
*
* @return {Phaser.GameObjects.TileSprite} This Tile Sprite instance.
*/
setTilePosition: function (x, y)
{
if (x !== undefined)
{
this.tilePositionX = x;
}
if (y !== undefined)
{
this.tilePositionY = y;
}
return this;
},
2018-02-06 22:56:27 +00:00
/**
* [description]
*
* @method Phaser.GameObjects.TileSprite#updateTileTexture
* @since 3.0.0
*/
2017-06-02 16:08:22 +00:00
updateTileTexture: function ()
{
2018-03-05 01:45:28 +00:00
if (!this.dirty && this.oldFrame === this.frame)
2017-06-02 16:08:22 +00:00
{
2017-05-02 20:57:21 +00:00
return;
2017-06-02 16:08:22 +00:00
}
2017-05-02 20:57:21 +00:00
this.oldFrame = this.frame;
this.canvasBufferCtx.clearRect(0, 0, this.canvasBuffer.width, this.canvasBuffer.height);
2018-03-05 01:45:28 +00:00
2018-01-25 00:15:51 +00:00
if (this.renderer.gl)
2017-05-02 20:57:21 +00:00
{
this.canvasBufferCtx.drawImage(
this.frame.source.image,
this.frame.cutX, this.frame.cutY,
this.frame.cutWidth, this.frame.cutHeight,
0, 0,
this.potWidth, this.potHeight
);
2018-01-25 00:15:51 +00:00
this.tileTexture = this.renderer.canvasToTexture(this.canvasBuffer, this.tileTexture, (this.tileTexture === null), this.scaleMode);
2017-05-02 20:57:21 +00:00
}
2017-05-04 00:14:14 +00:00
else
{
this.canvasBuffer.width = this.frame.cutWidth;
this.canvasBuffer.height = this.frame.cutHeight;
this.canvasBufferCtx.drawImage(
this.frame.source.image,
this.frame.cutX, this.frame.cutY,
this.frame.cutWidth, this.frame.cutHeight,
0, 0,
this.frame.cutWidth, this.frame.cutHeight
);
2017-05-04 00:14:14 +00:00
this.canvasPattern = this.canvasBufferCtx.createPattern(this.canvasBuffer, 'repeat');
}
2017-06-02 16:08:22 +00:00
2017-05-02 20:57:21 +00:00
this.dirty = false;
2017-05-04 00:28:49 +00:00
},
2018-02-06 22:56:27 +00:00
/**
* [description]
*
* @method Phaser.GameObjects.TileSprite#destroy
* @since 3.0.0
*/
2017-06-02 16:08:22 +00:00
destroy: function ()
2017-05-04 00:28:49 +00:00
{
2017-06-02 16:08:22 +00:00
if (this.renderer)
2017-05-04 00:28:49 +00:00
{
2018-01-25 00:15:51 +00:00
this.renderer.deleteTexture(this.tileTexture);
2017-05-04 00:28:49 +00:00
}
2017-06-02 16:08:22 +00:00
2017-05-04 00:28:49 +00:00
CanvasPool.remove(this.canvasBuffer);
2017-06-02 16:08:22 +00:00
2017-05-04 00:28:49 +00:00
this.canvasPattern = null;
this.canvasBufferCtx = null;
this.canvasBuffer = null;
2017-06-02 16:08:22 +00:00
2017-05-04 00:28:49 +00:00
this.renderer = null;
this.visible = false;
2017-04-25 22:09:13 +00:00
}
});
module.exports = TileSprite;