Skip to content

Commit 054ceaf

Browse files
committed
Added jsdocs to Container and made add support arrays
1 parent 4f6239d commit 054ceaf

6 files changed

Lines changed: 218 additions & 31 deletions

File tree

Lines changed: 119 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @author Felipe Alfonso <@bitnenfer>
4+
* @copyright 2018 Photon Storm Ltd.
5+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
6+
*/
7+
18
var Class = require('../../utils/Class');
29
var Components = require('../components');
310
var GameObject = require('../GameObject');
411
var Render = require('./ContainerRender');
12+
var Vector2 = require('../../math/Vector2');
513

14+
/**
15+
* @classdesc
16+
* A Container Game Object.
17+
*
18+
* @class Container
19+
* @extends Phaser.GameObjects.GameObject
20+
* @memberOf Phaser.GameObjects
21+
* @constructor
22+
* @since 3.4.0
23+
*
24+
* @extends Phaser.GameObjects.Components.BlendMode
25+
* @extends Phaser.GameObjects.Components.Transform
26+
*
27+
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
28+
* @param {number} x - The horizontal position of this Game Object in the world.
29+
* @param {number} y - The vertical position of this Game Object in the world.
30+
*/
631
var Container = new Class({
732

833
Extends: GameObject,
@@ -18,62 +43,139 @@ var Container = new Class({
1843
function Container (scene, x, y)
1944
{
2045
GameObject.call(this, scene, 'Container');
46+
47+
/**
48+
* [description]
49+
*
50+
* @name Phaser.GameObjects.Container#parentContainer
51+
* @type {Phaser.GameObjects.Container}
52+
* @since 3.4.0
53+
*/
2154
this.parentContainer = null;
55+
56+
/**
57+
* [description]
58+
*
59+
* @name Phaser.GameObjects.Container#children
60+
* @type {Phaser.GameObjects.GameObject[]}
61+
* @since 3.4.0
62+
*/
2263
this.children = [];
64+
2365
this.setPosition(x, y);
66+
67+
/**
68+
* [description]
69+
*
70+
* @name Phaser.GameObjects.Container#localTransform
71+
* @type {Phaser.GameObjects.Components.TransformMatrix}
72+
* @since 3.4.0
73+
*/
2474
this.localTransform = new Components.TransformMatrix();
75+
76+
/**
77+
* [description]
78+
*
79+
* @name Phaser.GameObjects.Container#tempTransformMatrix
80+
* @type {Phaser.GameObjects.Components.TransformMatrix}
81+
* @since 3.4.0
82+
*/
2583
this.tempTransformMatrix = new Components.TransformMatrix();
2684
},
2785

86+
/**
87+
* Adds the given Game Object, or array of Game Objects, to this Container.
88+
*
89+
* @method Phaser.GameObjects.Container#add
90+
* @since 3.4.0
91+
*
92+
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[])} gameObject - The Game Object, or array of Game Objects, to add to this Container.
93+
*
94+
* @return {Phaser.GameObjects.Container} This Container instance.
95+
*/
2896
add: function (gameObject)
2997
{
30-
if (gameObject.type === 'Container')
98+
if (!Array.isArray(gameObject))
3199
{
32-
gameObject.parentContainer = this;
100+
gameObject = [ gameObject ];
33101
}
34-
if (this.children.indexOf(gameObject) < 0)
102+
103+
for (var i = 0; i < gameObject.length; i++)
35104
{
36-
this.children.push(gameObject);
105+
var entry = gameObject[i];
106+
107+
if (entry.type === 'Container')
108+
{
109+
entry.parentContainer = this;
110+
}
111+
112+
if (this.children.indexOf(entry) === -1)
113+
{
114+
this.children.push(entry);
115+
}
37116
}
117+
38118
return this;
39119
},
40120

121+
/**
122+
* Removes a Game Object from this Container.
123+
*
124+
* @method Phaser.GameObjects.Container#remove
125+
* @since 3.4.0
126+
*
127+
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object to remove from this Container.
128+
*
129+
* @return {Phaser.GameObjects.Container} This Container instance.
130+
*/
41131
remove: function (gameObject)
42132
{
43133
var index = this.children.indexOf(gameObject);
44-
if (index >= 0)
134+
135+
if (index !== -1)
45136
{
46137
if (gameObject.type === 'Container')
47138
{
48139
gameObject.parentContainer = null;
49140
}
141+
50142
this.children.splice(index, 1);
51143
}
144+
52145
return this;
53146
},
54147

55-
pointToContainer: function (pointSrc, pointDst)
148+
/**
149+
* Takes a Point-like object, such as a Vector2, Geom.Point or object with public x and y properties,
150+
* and transforms it into the space of this Container, then returns it in the output object.
151+
*
152+
* @method Phaser.GameObjects.Container#pointToContainer
153+
* @since 3.4.0
154+
*
155+
* @param {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} source - The Source Point to be transformed.
156+
* @param {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} [output] - A destination object to store the transformed point in. If none given a Vector2 will be created and returned.
157+
*
158+
* @return {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} The transformed point.
159+
*/
160+
pointToContainer: function (source, output)
56161
{
57-
var parent = this.parentContainer;
58-
var tempMatrix = this.tempTransformMatrix;
59-
60-
if (pointDst === undefined)
61-
{
62-
pointDst = { x: 0, y: 0 };
63-
}
162+
if (output === undefined) { output = new Vector2(); }
64163

65-
if (parent !== null)
164+
if (this.parentContainer)
66165
{
67-
parent.pointToContainer(pointSrc, pointDst);
166+
this.parentContainer.pointToContainer(source, output);
68167
}
69168

169+
var tempMatrix = this.tempTransformMatrix;
170+
70171
tempMatrix.loadIdentity();
71172
tempMatrix.applyITRS(this.x, this.y, this.rotation, this.scaleX, this.scaleY);
72173
tempMatrix.invert();
73-
tempMatrix.transformPoint(pointSrc.x, pointSrc.y, pointDst);
174+
tempMatrix.transformPoint(source.x, source.y, output);
74175

75-
return pointDst;
176+
return output;
76177
}
178+
77179
});
78180

79181
module.exports = Container;

src/gameobjects/container/ContainerCanvasRenderer.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @author Felipe Alfonso <@bitnenfer>
4+
* @copyright 2018 Photon Storm Ltd.
5+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
6+
*/
7+
18
var GameObject = require('../GameObject');
29

10+
/**
11+
* Renders this Game Object with the Canvas Renderer to the given Camera.
12+
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
13+
* This method should not be called directly. It is a utility function of the Render module.
14+
*
15+
* @method Phaser.GameObjects.Container#renderCanvas
16+
* @since 3.4.0
17+
* @private
18+
*
19+
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer.
20+
* @param {Phaser.GameObjects.Container} container - The Game Object being rendered in this call.
21+
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
22+
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
23+
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
24+
*/
325
var ContainerCanvasRenderer = function (renderer, container, interpolationPercentage, camera, parentMatrix)
426
{
527
if (GameObject.RENDER_MASK !== container.renderFlags || (container.cameraFilter > 0 && (container.cameraFilter & camera._id)))
Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1-
var GetAdvancedValue = require('../../utils/object/GetAdvancedValue');
2-
var GameObjectCreator = require('../GameObjectCreator');
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @author Felipe Alfonso <@bitnenfer>
4+
* @copyright 2018 Photon Storm Ltd.
5+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
6+
*/
7+
38
var Container = require('./Container');
9+
var GameObjectCreator = require('../GameObjectCreator');
10+
var GetAdvancedValue = require('../../utils/object/GetAdvancedValue');
11+
var GetFastValue = require('../../utils/object/GetFastValue');
412

13+
/**
14+
* Creates a new Container Game Object and returns it.
15+
*
16+
* Note: This method will only be available if the Container Game Object has been built into Phaser.
17+
*
18+
* @method Phaser.GameObjects.GameObjectCreator#container
19+
* @since 3.4.0
20+
*
21+
* @param {object} config - [description]
22+
*
23+
* @return {Phaser.GameObjects.Container} The Game Object that was created.
24+
*/
525
GameObjectCreator.register('container', function (config)
626
{
7-
var x = GetAdvancedValue(config, 'x', 0.0);
8-
var y = GetAdvancedValue(config, 'y', 0.0);
9-
var add = GetAdvancedValue(config, 'add', true);
27+
var x = GetAdvancedValue(config, 'x', 0);
28+
var y = GetAdvancedValue(config, 'y', 0);
29+
var add = GetFastValue(config, 'add', true);
30+
1031
var container = new Container(this.scene, x, y);
1132

12-
if (add)
13-
{
14-
this.scene.sys.displayList.add(container);
15-
}
33+
BuildGameObject(this.scene, container, config);
1634

1735
return container;
1836
});
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @author Felipe Alfonso <@bitnenfer>
4+
* @copyright 2018 Photon Storm Ltd.
5+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
6+
*/
7+
18
var Container = require('./Container');
29
var GameObjectFactory = require('../GameObjectFactory');
310

11+
/**
12+
* Creates a new Container Game Object and adds it to the Scene.
13+
*
14+
* Note: This method will only be available if the Container Game Object has been built into Phaser.
15+
*
16+
* @method Phaser.GameObjects.GameObjectFactory#container
17+
* @since 3.4.0
18+
*
19+
* @param {number} x - The horizontal position of this Game Object in the world.
20+
* @param {number} y - The vertical position of this Game Object in the world.
21+
*
22+
* @return {Phaser.GameObjects.Container} The Game Object that was created.
23+
*/
424
GameObjectFactory.register('container', function (x, y)
525
{
6-
var container = new Container(this.scene, x, y);
7-
8-
this.displayList.add(container);
9-
10-
return container;
26+
return this.displayList.add(new Container(this.scene, x, y));
1127
});

src/gameobjects/container/ContainerRender.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @author Felipe Alfonso <@bitnenfer>
4+
* @copyright 2018 Photon Storm Ltd.
5+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
6+
*/
7+
18
var renderWebGL = require('../../utils/NOOP');
29
var renderCanvas = require('../../utils/NOOP');
310

src/gameobjects/container/ContainerWebGLRenderer.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @author Felipe Alfonso <@bitnenfer>
4+
* @copyright 2018 Photon Storm Ltd.
5+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
6+
*/
7+
18
var GameObject = require('../GameObject');
29

10+
/**
11+
* Renders this Game Object with the WebGL Renderer to the given Camera.
12+
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
13+
* This method should not be called directly. It is a utility function of the Render module.
14+
*
15+
* @method Phaser.GameObjects.Container#renderWebGL
16+
* @since 3.4.0
17+
* @private
18+
*
19+
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
20+
* @param {Phaser.GameObjects.Container} container - The Game Object being rendered in this call.
21+
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
22+
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
23+
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
24+
*/
325
var ContainerWebGLRenderer = function (renderer, container, interpolationPercentage, camera, parentMatrix)
426
{
527
if (GameObject.RENDER_MASK !== container.renderFlags || (container.cameraFilter > 0 && (container.cameraFilter & camera._id)))

0 commit comments

Comments
 (0)