phaser/wip/core/FlexGrid.js

328 lines
10 KiB
JavaScript
Raw Normal View History

2014-09-05 05:01:48 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2016-04-04 21:15:01 +00:00
* @copyright 2016 Photon Storm Ltd.
2014-09-05 05:01:48 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
2014-09-09 11:47:39 +00:00
* WARNING: This is an EXPERIMENTAL class. The API will change significantly in the coming versions and is incomplete.
* Please try to avoid using in production games with a long time to build.
* This is also why the documentation is incomplete.
*
* FlexGrid is a a responsive grid manager that works in conjunction with the ScaleManager RESIZE scaling mode and FlexLayers
* to provide for game object positioning in a responsive manner.
2014-09-05 05:01:48 +00:00
*
* @class Phaser.FlexGrid
* @constructor
* @param {Phaser.ScaleManager} manager - The ScaleManager.
2014-09-09 11:47:39 +00:00
* @param {number} width - The width of the game.
* @param {number} height - The height of the game.
2014-09-05 05:01:48 +00:00
*/
Phaser.FlexGrid = function (manager, width, height) {
/**
* @property {Phaser.Game} game - A reference to the currently running Game.
*/
this.game = manager.game;
/**
2015-02-05 05:13:15 +00:00
* @property {Phaser.ScaleManager} manager - A reference to the ScaleManager.
2014-09-05 05:01:48 +00:00
*/
this.manager = manager;
// The perfect dimensions on which everything else is based
this.width = width;
this.height = height;
2014-09-24 05:52:22 +00:00
this.boundsCustom = new Phaser.Rectangle(0, 0, width, height);
this.boundsFluid = new Phaser.Rectangle(0, 0, width, height);
2014-09-05 15:19:32 +00:00
this.boundsFull = new Phaser.Rectangle(0, 0, width, height);
this.boundsNone = new Phaser.Rectangle(0, 0, width, height);
2014-09-05 05:01:48 +00:00
/**
* @property {Phaser.Point} position -
* @readonly
*/
2014-09-24 05:52:22 +00:00
this.positionCustom = new Phaser.Point(0, 0);
this.positionFluid = new Phaser.Point(0, 0);
this.positionFull = new Phaser.Point(0, 0);
this.positionNone = new Phaser.Point(0, 0);
2014-09-05 05:01:48 +00:00
/**
* @property {Phaser.Point} scaleFactor - The scale factor based on the game dimensions vs. the scaled dimensions.
* @readonly
*/
2014-09-24 05:52:22 +00:00
this.scaleCustom = new Phaser.Point(1, 1);
this.scaleFluid = new Phaser.Point(1, 1);
2014-09-05 15:19:32 +00:00
this.scaleFluidInversed = new Phaser.Point(1, 1);
this.scaleFull = new Phaser.Point(1, 1);
this.scaleNone = new Phaser.Point(1, 1);
2014-09-05 05:01:48 +00:00
2014-09-24 05:52:22 +00:00
this.customWidth = 0;
this.customHeight = 0;
this.customOffsetX = 0;
this.customOffsetY = 0;
2014-09-05 05:01:48 +00:00
this.ratioH = width / height;
this.ratioV = height / width;
this.multiplier = 0;
this.layers = [];
};
Phaser.FlexGrid.prototype = {
2014-09-09 11:47:39 +00:00
/**
* Sets the core game size. This resets the w/h parameters and bounds.
*
2014-11-25 00:20:41 +00:00
* @method Phaser.FlexGrid#setSize
2014-09-09 11:47:39 +00:00
* @param {number} width - The new dimensions.
* @param {number} height - The new dimensions.
*/
2014-09-05 05:01:48 +00:00
setSize: function (width, height) {
// These are locked and don't change until setSize is called again
this.width = width;
this.height = height;
this.ratioH = width / height;
this.ratioV = height / width;
this.scaleNone = new Phaser.Point(1, 1);
this.boundsNone.width = this.width;
this.boundsNone.height = this.height;
2014-09-05 05:01:48 +00:00
this.refresh();
},
// Need ability to create your own layers with custom scaling, etc.
2014-09-24 05:52:22 +00:00
/**
* A custom layer is centered on the game and maintains its aspect ratio as it scales up and down.
*
2014-11-25 00:20:41 +00:00
* @method Phaser.FlexGrid#createCustomLayer
2014-09-24 05:52:22 +00:00
* @param {number} width - Width of this layer in pixels.
* @param {number} height - Height of this layer in pixels.
* @param {PIXI.DisplayObject[]} [children] - An array of children that are used to populate the FlexLayer.
2014-09-24 05:52:22 +00:00
* @return {Phaser.FlexLayer} The Layer object.
*/
createCustomLayer: function (width, height, children, addToWorld) {
if (addToWorld === undefined) { addToWorld = true; }
2014-09-24 05:52:22 +00:00
this.customWidth = width;
this.customHeight = height;
this.boundsCustom.width = width;
this.boundsCustom.height = height;
var layer = new Phaser.FlexLayer(this, this.positionCustom, this.boundsCustom, this.scaleCustom);
if (addToWorld)
{
this.game.world.add(layer);
}
this.layers.push(layer);
if (typeof children !== 'undefined' && typeof children !== null)
{
layer.addMultiple(children);
}
return layer;
},
2014-09-05 14:46:21 +00:00
/**
* A fluid layer is centered on the game and maintains its aspect ratio as it scales up and down.
*
2014-11-25 00:20:41 +00:00
* @method Phaser.FlexGrid#createFluidLayer
2014-09-09 11:47:39 +00:00
* @param {array} [children] - An array of children that are used to populate the FlexLayer.
2014-09-05 14:46:21 +00:00
* @return {Phaser.FlexLayer} The Layer object.
*/
createFluidLayer: function (children, addToWorld) {
if (addToWorld === undefined) { addToWorld = true; }
2014-09-05 14:46:21 +00:00
var layer = new Phaser.FlexLayer(this, this.positionFluid, this.boundsFluid, this.scaleFluid);
if (addToWorld)
{
this.game.world.add(layer);
}
this.layers.push(layer);
if (typeof children !== 'undefined' && typeof children !== null)
2014-09-05 14:46:21 +00:00
{
layer.addMultiple(children);
}
return layer;
},
2014-09-05 14:46:21 +00:00
/**
* A full layer is placed at 0,0 and extends to the full size of the game. Children are scaled according to the fluid ratios.
*
2014-11-25 00:20:41 +00:00
* @method Phaser.FlexGrid#createFullLayer
2014-09-09 11:47:39 +00:00
* @param {array} [children] - An array of children that are used to populate the FlexLayer.
2014-09-05 14:46:21 +00:00
* @return {Phaser.FlexLayer} The Layer object.
*/
createFullLayer: function (children) {
2014-09-05 14:46:21 +00:00
var layer = new Phaser.FlexLayer(this, this.positionFull, this.boundsFull, this.scaleFluid);
2014-09-05 14:46:21 +00:00
this.game.world.add(layer);
this.layers.push(layer);
2014-09-05 14:46:21 +00:00
if (typeof children !== 'undefined')
{
layer.addMultiple(children);
}
return layer;
},
2014-09-05 14:46:21 +00:00
/**
* A fixed layer is centered on the game and is the size of the required dimensions and is never scaled.
*
2014-11-25 00:20:41 +00:00
* @method Phaser.FlexGrid#createFixedLayer
* @param {PIXI.DisplayObject[]} [children] - An array of children that are used to populate the FlexLayer.
2014-09-05 14:46:21 +00:00
* @return {Phaser.FlexLayer} The Layer object.
*/
createFixedLayer: function (children) {
2014-09-05 05:01:48 +00:00
2014-09-05 14:46:21 +00:00
var layer = new Phaser.FlexLayer(this, this.positionNone, this.boundsNone, this.scaleNone);
2014-09-05 05:01:48 +00:00
2014-09-05 14:46:21 +00:00
this.game.world.add(layer);
2014-09-05 05:01:48 +00:00
this.layers.push(layer);
2014-09-05 14:46:21 +00:00
if (typeof children !== 'undefined')
{
layer.addMultiple(children);
}
2014-09-05 05:01:48 +00:00
return layer;
},
2014-09-09 11:47:39 +00:00
/**
* Resets the layer children references
*
2014-11-25 00:20:41 +00:00
* @method Phaser.FlexGrid#reset
2014-09-09 11:47:39 +00:00
*/
reset: function () {
var i = this.layers.length;
while (i--)
{
if (!this.layers[i].persist)
{
// Remove references to this class
this.layers[i].position = null;
this.layers[i].scale = null;
this.layers.slice(i, 1);
}
}
},
2014-09-09 11:47:39 +00:00
/**
* Called when the game container changes dimensions.
*
2014-11-25 00:20:41 +00:00
* @method Phaser.FlexGrid#onResize
2014-09-09 11:47:39 +00:00
* @param {number} width - The new width of the game container.
* @param {number} height - The new height of the game container.
*/
2014-09-05 05:01:48 +00:00
onResize: function (width, height) {
2014-11-08 13:25:14 +00:00
this.ratioH = width / height;
this.ratioV = height / width;
this.refresh(width, height);
2014-09-05 05:01:48 +00:00
},
2014-09-09 11:47:39 +00:00
/**
* Updates all internal vars such as the bounds and scale values.
*
2014-11-25 00:20:41 +00:00
* @method Phaser.FlexGrid#refresh
2014-09-09 11:47:39 +00:00
*/
2014-09-05 05:01:48 +00:00
refresh: function () {
this.multiplier = Math.min((this.manager.height / this.height), (this.manager.width / this.width));
this.boundsFluid.width = Math.round(this.width * this.multiplier);
this.boundsFluid.height = Math.round(this.height * this.multiplier);
2014-09-05 05:01:48 +00:00
this.scaleFluid.set(this.boundsFluid.width / this.width, this.boundsFluid.height / this.height);
2014-09-05 15:19:32 +00:00
this.scaleFluidInversed.set(this.width / this.boundsFluid.width, this.height / this.boundsFluid.height);
this.scaleFull.set(this.boundsFull.width / this.width, this.boundsFull.height / this.height);
2014-11-04 23:57:14 +00:00
this.boundsFull.width = Math.round(this.manager.width * this.scaleFluidInversed.x);
this.boundsFull.height = Math.round(this.manager.height * this.scaleFluidInversed.y);
2014-09-05 05:01:48 +00:00
this.boundsFluid.centerOn(this.manager.bounds.centerX, this.manager.bounds.centerY);
this.boundsNone.centerOn(this.manager.bounds.centerX, this.manager.bounds.centerY);
2014-09-05 05:01:48 +00:00
this.positionFluid.set(this.boundsFluid.x, this.boundsFluid.y);
this.positionNone.set(this.boundsNone.x, this.boundsNone.y);
2014-09-05 05:01:48 +00:00
2014-11-08 13:25:14 +00:00
},
2014-11-04 23:57:14 +00:00
2014-11-25 00:20:41 +00:00
/**
* Fits a sprites width to the bounds.
*
* @method Phaser.FlexGrid#fitSprite
* @param {Phaser.Sprite} sprite - The Sprite to fit.
*/
2014-11-08 13:25:14 +00:00
fitSprite: function (sprite) {
2014-09-24 05:52:22 +00:00
2014-11-08 13:25:14 +00:00
this.manager.scaleSprite(sprite);
2014-09-24 05:52:22 +00:00
2014-11-08 13:25:14 +00:00
sprite.x = this.manager.bounds.centerX;
sprite.y = this.manager.bounds.centerY;
2014-09-24 05:52:22 +00:00
2014-09-05 05:01:48 +00:00
},
2014-09-09 11:47:39 +00:00
/**
* Call in the render function to output the bounds rects.
*
2014-11-25 00:20:41 +00:00
* @method Phaser.FlexGrid#debug
2014-09-09 11:47:39 +00:00
*/
2014-09-05 05:01:48 +00:00
debug: function () {
2014-09-05 14:46:21 +00:00
// for (var i = 0; i < this.layers.length; i++)
// {
// this.layers[i].debug();
// }
2014-09-24 05:52:22 +00:00
// this.game.debug.text(this.boundsFull.width + ' x ' + this.boundsFull.height, this.boundsFull.x + 4, this.boundsFull.y + 16);
// this.game.debug.geom(this.boundsFull, 'rgba(0,0,255,0.9', false);
2014-09-05 14:46:21 +00:00
this.game.debug.text(this.boundsFluid.width + ' x ' + this.boundsFluid.height, this.boundsFluid.x + 4, this.boundsFluid.y + 16);
this.game.debug.geom(this.boundsFluid, 'rgba(255,0,0,0.9', false);
2014-09-05 05:01:48 +00:00
// this.game.debug.text(this.boundsNone.width + ' x ' + this.boundsNone.height, this.boundsNone.x + 4, this.boundsNone.y + 16);
// this.game.debug.geom(this.boundsNone, 'rgba(0,255,0,0.9', false);
2014-09-24 05:52:22 +00:00
// this.game.debug.text(this.boundsCustom.width + ' x ' + this.boundsCustom.height, this.boundsCustom.x + 4, this.boundsCustom.y + 16);
// this.game.debug.geom(this.boundsCustom, 'rgba(255,255,0,0.9', false);
}
2014-09-05 05:01:48 +00:00
};
Phaser.FlexGrid.prototype.constructor = Phaser.FlexGrid;