mirror of
https://github.com/photonstorm/phaser
synced 2025-02-17 06:28:30 +00:00
Updated Container methods, renamed renderers, added Factory.
This commit is contained in:
parent
d0cb0f2457
commit
680ce51842
5 changed files with 121 additions and 317 deletions
|
@ -1,53 +0,0 @@
|
|||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @author Mat Groves (@Doormat23)
|
||||
* @copyright 2016 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
|
||||
Phaser.Renderer.Canvas.GameObjects.Container = {
|
||||
|
||||
TYPES: [
|
||||
Phaser.Group.prototype,
|
||||
PIXI.DisplayObjectContainer.prototype
|
||||
],
|
||||
|
||||
render: function (renderer, src)
|
||||
{
|
||||
if (src.visible === false || src.alpha === 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (src._cacheAsBitmap)
|
||||
{
|
||||
return this.renderCachedSprite(renderer, src);
|
||||
}
|
||||
|
||||
if (src._mask)
|
||||
{
|
||||
renderer.pushMask(src._mask);
|
||||
}
|
||||
|
||||
for (var i = 0; i < src.children.length; i++)
|
||||
{
|
||||
var child = src.children[i];
|
||||
|
||||
child.render(renderer, child);
|
||||
}
|
||||
|
||||
if (src._mask)
|
||||
{
|
||||
renderer.popMask();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
renderCachedSprite: function (renderer, src)
|
||||
{
|
||||
// TODO
|
||||
return src;
|
||||
}
|
||||
|
||||
};
|
|
@ -3,7 +3,11 @@
|
|||
* @copyright 2016 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
Phaser.GameObject.Container = function () {
|
||||
Phaser.GameObject.Container = function (game, parent, x, y)
|
||||
{
|
||||
Phaser.Component.BaseTransform.call(this, x, y);
|
||||
|
||||
this.game = game;
|
||||
|
||||
/**
|
||||
* @property {number} type - The const type of this object.
|
||||
|
@ -11,279 +15,33 @@ Phaser.GameObject.Container = function () {
|
|||
*/
|
||||
this.type = Phaser.CONTAINER;
|
||||
|
||||
/**
|
||||
* If `ignoreChildInput` is `false` it will allow this objects _children_ to be considered as valid for Input events.
|
||||
*
|
||||
* If this property is `true` then the children will _not_ be considered as valid for Input events.
|
||||
*
|
||||
* Note that this property isn't recursive: only immediate children are influenced, it doesn't scan further down.
|
||||
* @property {boolean} ignoreChildInput
|
||||
* @default
|
||||
*/
|
||||
this.ignoreChildInput = false;
|
||||
this.parent = parent;
|
||||
|
||||
this.children = new Phaser.Component.Children(this);
|
||||
|
||||
// PIXI.Sprite.call(this, Phaser.Cache.DEFAULT);
|
||||
this.data = new Phaser.Component.Data(this);
|
||||
|
||||
// Phaser.Component.Core.init.call(this, game, x, y, key, frame);
|
||||
// Temporary for now?
|
||||
this.visible = true;
|
||||
this.alpha = 1;
|
||||
this.blendMode = Phaser.blendModes.NORMAL;
|
||||
this.scaleMode = Phaser.scaleModes.DEFAULT;
|
||||
this.exists = true;
|
||||
|
||||
};
|
||||
|
||||
Phaser.GameObject.Container.prototype = Object.create(PIXI.Sprite.prototype);
|
||||
Phaser.GameObject.Container.prototype = Object.create(Phaser.Component.BaseTransform.prototype);
|
||||
Phaser.GameObject.Container.prototype.constructor = Phaser.GameObject.Container;
|
||||
|
||||
/*
|
||||
* Updates the transform on all children of this container for rendering
|
||||
*
|
||||
* @method updateTransform
|
||||
* @private
|
||||
*/
|
||||
PIXI.DisplayObjectContainer.prototype.updateTransform = function () {
|
||||
|
||||
if (!this.visible)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.displayObjectUpdateTransform();
|
||||
|
||||
if (this._cacheAsBitmap)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < this.children.length; i++)
|
||||
{
|
||||
this.children[i].updateTransform();
|
||||
}
|
||||
|
||||
Phaser.GameObject.Container.prototype.preUpdate = function ()
|
||||
{
|
||||
// this.transform.update();
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the global bounds of the displayObjectContainer as a rectangle. The bounds calculation takes all visible children into consideration.
|
||||
*
|
||||
* @method getBounds
|
||||
* @param {PIXI.DisplayObject|PIXI.Matrix} [targetCoordinateSpace] Returns a rectangle that defines the area of the display object relative to the coordinate system of the targetCoordinateSpace object.
|
||||
* @return {Rectangle} The rectangular bounding area
|
||||
*/
|
||||
PIXI.DisplayObjectContainer.prototype.getBounds = function (targetCoordinateSpace) {
|
||||
|
||||
var isTargetCoordinateSpaceDisplayObject = (targetCoordinateSpace && targetCoordinateSpace instanceof PIXI.DisplayObject);
|
||||
var isTargetCoordinateSpaceThisOrParent = true;
|
||||
|
||||
if (!isTargetCoordinateSpaceDisplayObject)
|
||||
{
|
||||
targetCoordinateSpace = this;
|
||||
}
|
||||
else if (targetCoordinateSpace instanceof PIXI.DisplayObjectContainer)
|
||||
{
|
||||
isTargetCoordinateSpaceThisOrParent = targetCoordinateSpace.contains(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
isTargetCoordinateSpaceThisOrParent = false;
|
||||
}
|
||||
|
||||
var i;
|
||||
|
||||
if (isTargetCoordinateSpaceDisplayObject)
|
||||
{
|
||||
var matrixCache = targetCoordinateSpace.worldTransform;
|
||||
|
||||
targetCoordinateSpace.worldTransform = Phaser.identityMatrix;
|
||||
|
||||
for (i = 0; i < targetCoordinateSpace.children.length; i++)
|
||||
{
|
||||
targetCoordinateSpace.children[i].updateTransform();
|
||||
}
|
||||
}
|
||||
|
||||
var minX = Infinity;
|
||||
var minY = Infinity;
|
||||
|
||||
var maxX = -Infinity;
|
||||
var maxY = -Infinity;
|
||||
|
||||
var childBounds;
|
||||
var childMaxX;
|
||||
var childMaxY;
|
||||
|
||||
var childVisible = false;
|
||||
|
||||
for (i = 0; i < this.children.length; i++)
|
||||
{
|
||||
var child = this.children[i];
|
||||
|
||||
if (!child.visible)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
childVisible = true;
|
||||
|
||||
childBounds = this.children[i].getBounds();
|
||||
|
||||
minX = (minX < childBounds.x) ? minX : childBounds.x;
|
||||
minY = (minY < childBounds.y) ? minY : childBounds.y;
|
||||
|
||||
childMaxX = childBounds.width + childBounds.x;
|
||||
childMaxY = childBounds.height + childBounds.y;
|
||||
|
||||
maxX = (maxX > childMaxX) ? maxX : childMaxX;
|
||||
maxY = (maxY > childMaxY) ? maxY : childMaxY;
|
||||
}
|
||||
|
||||
var bounds = this._bounds;
|
||||
|
||||
if (!childVisible)
|
||||
{
|
||||
bounds = new Phaser.Rectangle();
|
||||
|
||||
var w0 = bounds.x;
|
||||
var w1 = bounds.width + bounds.x;
|
||||
|
||||
var h0 = bounds.y;
|
||||
var h1 = bounds.height + bounds.y;
|
||||
|
||||
var worldTransform = this.worldTransform;
|
||||
|
||||
var a = worldTransform.a;
|
||||
var b = worldTransform.b;
|
||||
var c = worldTransform.c;
|
||||
var d = worldTransform.d;
|
||||
var tx = worldTransform.tx;
|
||||
var ty = worldTransform.ty;
|
||||
|
||||
var x1 = a * w1 + c * h1 + tx;
|
||||
var y1 = d * h1 + b * w1 + ty;
|
||||
|
||||
var x2 = a * w0 + c * h1 + tx;
|
||||
var y2 = d * h1 + b * w0 + ty;
|
||||
|
||||
var x3 = a * w0 + c * h0 + tx;
|
||||
var y3 = d * h0 + b * w0 + ty;
|
||||
|
||||
var x4 = a * w1 + c * h0 + tx;
|
||||
var y4 = d * h0 + b * w1 + ty;
|
||||
|
||||
maxX = x1;
|
||||
maxY = y1;
|
||||
|
||||
minX = x1;
|
||||
minY = y1;
|
||||
|
||||
minX = x2 < minX ? x2 : minX;
|
||||
minX = x3 < minX ? x3 : minX;
|
||||
minX = x4 < minX ? x4 : minX;
|
||||
|
||||
minY = y2 < minY ? y2 : minY;
|
||||
minY = y3 < minY ? y3 : minY;
|
||||
minY = y4 < minY ? y4 : minY;
|
||||
|
||||
maxX = x2 > maxX ? x2 : maxX;
|
||||
maxX = x3 > maxX ? x3 : maxX;
|
||||
maxX = x4 > maxX ? x4 : maxX;
|
||||
|
||||
maxY = y2 > maxY ? y2 : maxY;
|
||||
maxY = y3 > maxY ? y3 : maxY;
|
||||
maxY = y4 > maxY ? y4 : maxY;
|
||||
}
|
||||
|
||||
bounds.x = minX;
|
||||
bounds.y = minY;
|
||||
bounds.width = maxX - minX;
|
||||
bounds.height = maxY - minY;
|
||||
|
||||
if (isTargetCoordinateSpaceDisplayObject)
|
||||
{
|
||||
targetCoordinateSpace.worldTransform = matrixCache;
|
||||
|
||||
for (i = 0; i < targetCoordinateSpace.children.length; i++)
|
||||
{
|
||||
targetCoordinateSpace.children[i].updateTransform();
|
||||
}
|
||||
}
|
||||
|
||||
if (!isTargetCoordinateSpaceThisOrParent)
|
||||
{
|
||||
var targetCoordinateSpaceBounds = targetCoordinateSpace.getBounds();
|
||||
|
||||
bounds.x -= targetCoordinateSpaceBounds.x;
|
||||
bounds.y -= targetCoordinateSpaceBounds.y;
|
||||
}
|
||||
|
||||
return bounds;
|
||||
|
||||
Phaser.GameObject.Container.prototype.update = function ()
|
||||
{
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the non-global local bounds of the displayObjectContainer as a rectangle without any transformations. The calculation takes all visible children into consideration.
|
||||
*
|
||||
* @method getLocalBounds
|
||||
* @return {Rectangle} The rectangular bounding area
|
||||
*/
|
||||
PIXI.DisplayObjectContainer.prototype.getLocalBounds = function () {
|
||||
|
||||
return this.getBounds(this);
|
||||
|
||||
Phaser.GameObject.Container.prototype.postUpdate = function ()
|
||||
{
|
||||
};
|
||||
|
||||
/**
|
||||
* The width of the displayObjectContainer, setting this will actually modify the scale to achieve the value set
|
||||
*
|
||||
* @property width
|
||||
* @type Number
|
||||
*/
|
||||
Object.defineProperty(PIXI.DisplayObjectContainer.prototype, 'width', {
|
||||
|
||||
get: function() {
|
||||
return this.getLocalBounds().width * this.scale.x;
|
||||
},
|
||||
|
||||
set: function(value) {
|
||||
|
||||
var width = this.getLocalBounds().width;
|
||||
|
||||
if (width !== 0)
|
||||
{
|
||||
this.scale.x = value / width;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.scale.x = 1;
|
||||
}
|
||||
|
||||
this._width = value;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* The height of the displayObjectContainer, setting this will actually modify the scale to achieve the value set
|
||||
*
|
||||
* @property height
|
||||
* @type Number
|
||||
*/
|
||||
Object.defineProperty(PIXI.DisplayObjectContainer.prototype, 'height', {
|
||||
|
||||
get: function() {
|
||||
return this.getLocalBounds().height * this.scale.y;
|
||||
},
|
||||
|
||||
set: function(value) {
|
||||
|
||||
var height = this.getLocalBounds().height;
|
||||
|
||||
if (height !== 0)
|
||||
{
|
||||
this.scale.y = value / height;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.scale.y = 1;
|
||||
}
|
||||
|
||||
this._height = value;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
|
81
src/gameobjects/container/ContainerCanvasRenderer.js
Normal file
81
src/gameobjects/container/ContainerCanvasRenderer.js
Normal file
|
@ -0,0 +1,81 @@
|
|||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @author Mat Groves (@Doormat23)
|
||||
* @copyright 2016 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
|
||||
Phaser.Renderer.Canvas.GameObjects.Container = {
|
||||
|
||||
TYPES: [
|
||||
Phaser.GameObject.Container.prototype
|
||||
],
|
||||
|
||||
render: function (renderer, src)
|
||||
{
|
||||
// Skip rendering?
|
||||
|
||||
if (src.visible === false || src.alpha === 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Blend Mode
|
||||
|
||||
if (src.blendMode !== renderer.currentBlendMode)
|
||||
{
|
||||
renderer.currentBlendMode = src.blendMode;
|
||||
renderer.context.globalCompositeOperation = renderer.blendModes[renderer.currentBlendMode];
|
||||
}
|
||||
|
||||
// Alpha (World Alpha?)
|
||||
|
||||
if (src.alpha !== renderer.context.globalAlpha)
|
||||
{
|
||||
renderer.context.globalAlpha = src.alpha;
|
||||
}
|
||||
|
||||
// Smoothing (should this be a Game Object, or Frame / Texture level property?)
|
||||
|
||||
if (src.scaleMode !== renderer.currentScaleMode)
|
||||
{
|
||||
renderer.currentScaleMode = src.scaleMode;
|
||||
renderer.context[renderer.smoothProperty] = (src.scaleMode === Phaser.scaleModes.LINEAR);
|
||||
}
|
||||
|
||||
/*
|
||||
if (src._cacheAsBitmap)
|
||||
{
|
||||
return this.renderCachedSprite(renderer, src);
|
||||
}
|
||||
|
||||
if (src._mask)
|
||||
{
|
||||
renderer.pushMask(src._mask);
|
||||
}
|
||||
*/
|
||||
|
||||
for (var i = 0; i < src.children.list.length; i++)
|
||||
{
|
||||
var child = src.children.list[i];
|
||||
|
||||
child.render(renderer, child);
|
||||
}
|
||||
|
||||
/*
|
||||
if (src._mask)
|
||||
{
|
||||
renderer.popMask();
|
||||
}
|
||||
*/
|
||||
|
||||
},
|
||||
|
||||
renderCachedSprite: function (renderer, src)
|
||||
{
|
||||
// TODO
|
||||
return src;
|
||||
}
|
||||
|
||||
};
|
18
src/gameobjects/container/ContainerFactory.js
Normal file
18
src/gameobjects/container/ContainerFactory.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2016 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
Phaser.GameObject.Container.FACTORY_KEY = 'container';
|
||||
|
||||
Phaser.GameObject.Container.FACTORY_ADD = function (parent, x, y)
|
||||
{
|
||||
// return parent.children.add(new Phaser.GameObject.Container(this.game, parent, x, y));
|
||||
return new Phaser.GameObject.Container(this.game, parent, x, y);
|
||||
};
|
||||
|
||||
Phaser.GameObject.Container.FACTORY_MAKE = function (x, y)
|
||||
{
|
||||
return new Phaser.GameObject.Container(this.game, parent, x, y);
|
||||
};
|
Loading…
Add table
Reference in a new issue