|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2018 Photon Storm Ltd. |
| 4 | + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var BitmapMask = require('../../display/mask/BitmapMask'); |
| 8 | +var GeometryMask = require('../../display/mask/GeometryMask'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Provides methods used for getting and setting the mask of a Game Object. |
| 12 | + * |
| 13 | + * @name Phaser.GameObjects.Components.Mask |
| 14 | + * @since 3.0.0 |
| 15 | + */ |
| 16 | + |
| 17 | +var Mask = { |
| 18 | + |
| 19 | + /** |
| 20 | + * The Mask this Game Object is using during render. |
| 21 | + * |
| 22 | + * @name Phaser.GameObjects.Components.Mask#mask |
| 23 | + * @type {Phaser.Display.Masks.BitmapMask|Phaser.Display.Masks.GeometryMask} |
| 24 | + * @since 3.0.0 |
| 25 | + */ |
| 26 | + mask: null, |
| 27 | + |
| 28 | + /** |
| 29 | + * Sets the mask that this Game Object will use to render with. |
| 30 | + * |
| 31 | + * The mask must have been previously created and can be either a |
| 32 | + * GeometryMask or a BitmapMask. |
| 33 | + * |
| 34 | + * Note: Bitmap Masks only work on WebGL. Geometry Masks work on both WebGL and Canvas. |
| 35 | + * |
| 36 | + * If a mask is already set on this Game Object it will be immediately replaced. |
| 37 | + * |
| 38 | + * @method Phaser.GameObjects.Components.Mask#setMask |
| 39 | + * @since 3.6.2 |
| 40 | + * |
| 41 | + * @param {Phaser.Display.Masks.BitmapMask|Phaser.Display.Masks.GeometryMask} mask - The mask this Game Object will use when rendering. |
| 42 | + * |
| 43 | + * @return {Phaser.GameObjects.GameObject} This Game Object instance. |
| 44 | + */ |
| 45 | + setMask: function (mask) |
| 46 | + { |
| 47 | + this.mask = mask; |
| 48 | + |
| 49 | + return this; |
| 50 | + }, |
| 51 | + |
| 52 | + /** |
| 53 | + * Clears the mask that this Game Object was using. |
| 54 | + * |
| 55 | + * @method Phaser.GameObjects.Components.Mask#clearMask |
| 56 | + * @since 3.6.2 |
| 57 | + * |
| 58 | + * @return {Phaser.GameObjects.GameObject} This Game Object instance. |
| 59 | + */ |
| 60 | + clearMask: function () |
| 61 | + { |
| 62 | + this.mask = null; |
| 63 | + |
| 64 | + return this; |
| 65 | + }, |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates and returns a Bitmap Mask. This mask can be used by any Game Object, |
| 69 | + * including this one. |
| 70 | + * |
| 71 | + * To create the mask you need to pass in a reference to a renderable Game Object. |
| 72 | + * A renderable Game Object is one that uses a texture to render with, such as an |
| 73 | + * Image, Sprite, Render Texture or BitmapText. |
| 74 | + * |
| 75 | + * If you do not provide a renderable object, and this Game Object has a texture, |
| 76 | + * it will use itself as the object. This means you can call this method to create |
| 77 | + * a Bitmap Mask from any renderable Game Object. |
| 78 | + * |
| 79 | + * @method Phaser.GameObjects.Components.Mask#createBitmapMask |
| 80 | + * @since 3.6.2 |
| 81 | + * |
| 82 | + * @param {Phaser.GameObjects.GameObject} [renderable] - A renderable Game Object that uses a texture, such as a Sprite. |
| 83 | + * |
| 84 | + * @return {Phaser.Display.Masks.BitmapMask} This Bitmap Mask that was created. |
| 85 | + */ |
| 86 | + createBitmapMask: function (renderable) |
| 87 | + { |
| 88 | + if (renderable === undefined && this.texture) |
| 89 | + { |
| 90 | + // eslint-disable-next-line consistent-this |
| 91 | + renderable = this; |
| 92 | + } |
| 93 | + |
| 94 | + return new BitmapMask(this.scene, renderable); |
| 95 | + }, |
| 96 | + |
| 97 | + /** |
| 98 | + * Creates and returns a Geometry Mask. This mask can be used by any Game Object, |
| 99 | + * including this one. |
| 100 | + * |
| 101 | + * To create the mask you need to pass in a reference to a Graphics Game Object. |
| 102 | + * |
| 103 | + * If you do not provide a graphics object, and this Game Object is an instance |
| 104 | + * of a Graphics object, then it will use itself to create the mask. |
| 105 | + * |
| 106 | + * This means you can call this method to create a Geometry Mask from any Graphics Game Object. |
| 107 | + * |
| 108 | + * @method Phaser.GameObjects.Components.Mask#createGeometryMask |
| 109 | + * @since 3.6.2 |
| 110 | + * |
| 111 | + * @param {Phaser.GameObjects.Graphics} [graphics] - A Graphics Game Object. The geometry within it will be used as the mask. |
| 112 | + * |
| 113 | + * @return {Phaser.Display.Masks.GeometryMask} This Geometry Mask that was created. |
| 114 | + */ |
| 115 | + createGeometryMask: function (graphics) |
| 116 | + { |
| 117 | + if (graphics === undefined && this.type === 'Graphics') |
| 118 | + { |
| 119 | + // eslint-disable-next-line consistent-this |
| 120 | + graphics = this; |
| 121 | + } |
| 122 | + |
| 123 | + return new GeometryMask(this.scene, graphics); |
| 124 | + } |
| 125 | + |
| 126 | +}; |
| 127 | + |
| 128 | +module.exports = Mask; |
0 commit comments