From d6297884e6bdd0cde49f97ed289e57ae7fec6dc4 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Sat, 8 Nov 2014 13:28:15 +0000 Subject: [PATCH] Added Sprite.setScaleMinMax for testing. --- src/gameobjects/Image.js | 111 +++++++++++++++++++++++++++++++++++ src/gameobjects/Sprite.js | 120 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 231 insertions(+) diff --git a/src/gameobjects/Image.js b/src/gameobjects/Image.js index 65e2f62f6..a7ef0c99e 100644 --- a/src/gameobjects/Image.js +++ b/src/gameobjects/Image.js @@ -69,6 +69,9 @@ Phaser.Image = function (game, x, y, key, frame) { PIXI.Sprite.call(this, PIXI.TextureCache['__default']); + this.transformCallback = this.checkTransform; + this.transformCallbackContext = this; + this.position.set(x, y); /** @@ -627,6 +630,114 @@ Phaser.Image.prototype.bringToTop = function() { }; +/** + * Adjust scaling limits, if set, to this Image. + * + * @method Phaser.Image#checkTransform + * @private + * @param {PIXI.Matrix} wt - The updated worldTransform matrix. + */ +Phaser.Image.prototype.checkTransform = function (wt) { + + if (this.scaleMin) + { + if (wt.a < this.scaleMin.x) + { + wt.a = this.scaleMin.x; + } + + if (wt.d < this.scaleMin.y) + { + wt.d = this.scaleMin.y; + } + } + + if (this.scaleMax) + { + if (wt.a > this.scaleMax.x) + { + wt.a = this.scaleMax.x; + } + + if (wt.d > this.scaleMax.y) + { + wt.d = this.scaleMax.y; + } + } + +}; + +/** + * Sets the scaleMin and scaleMax values in one call. + * These values are used to limit how far this Image will scale (either up or down) based on its parent. + * For example if this Image has a minScale value of 1 and its parent has a scale value of 0.5, the 0.5 will be ignored and the scale value of 1 will be used. + * By using these values you can carefully control how Images deal with responsive scaling. + * + * If only one parameter is given then that value will be used for both scaleMin and scaleMax: + * setScaleMinMax(1) = scaleMin.x, scaleMin.y, scaleMax.x and scaleMax.y all = 1 + * + * If only two parameters are given the first is set as scaleMin.x and y and the second as scaleMax.x and y: + * setScaleMinMax(0.5, 2) = scaleMin.x and y = 0.5 and scaleMax.x and y = 2 + * + * If you wish to set scaleMin with different values for x and y then either modify Image.scaleMin directly, or pass `null` for the maxX and maxY parameters. + * + * Call setScaleMinMax(null) to clear both the scaleMin and scaleMax values. + * + * @method Phaser.Image#setScaleMinMax + * @memberof Phaser.Image + * @param {number|null} minX - The minimum horizontal scale value this Image can scale down to. + * @param {number|null} minY - The minimum vertical scale value this Image can scale down to. + * @param {number|null} maxX - The maximum horizontal scale value this Image can scale up to. + * @param {number|null} maxY - The maximum vertical scale value this Image can scale up to. + */ +Phaser.Image.prototype.setScaleMinMax = function (minX, minY, maxX, maxY) { + + if (typeof minY === 'undefined') + { + // 1 parameter, set all to it + minY = maxX = maxY = minX; + } + else if (typeof maxX === 'undefined') + { + // 2 parameters, the first is min, the second max + maxX = maxY = minY; + minY = minX; + } + + if (minX === null) + { + this.scaleMin = null; + } + else + { + if (this.scaleMin) + { + this.scaleMin.set(minX, minY); + } + else + { + this.scaleMin = new Phaser.Point(minX, minY); + } + } + + if (maxX === null) + { + this.scaleMax = null; + } + else + { + if (this.scaleMax) + { + this.scaleMax.set(maxX, maxY); + } + else + { + this.scaleMax = new Phaser.Point(maxX, maxY); + } + } + +}; + /** * Indicates the rotation of the Image, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation. * Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement player.angle = 450 is the same as player.angle = 90. diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 912133490..51c4c358d 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -66,6 +66,9 @@ Phaser.Sprite = function (game, x, y, key, frame) { PIXI.Sprite.call(this, PIXI.TextureCache['__default']); + this.transformCallback = this.checkTransform; + this.transformCallbackContext = this; + this.position.set(x, y); /** @@ -152,6 +155,16 @@ Phaser.Sprite = function (game, x, y, key, frame) { */ this.cropRect = null; + /** + * @property {Phaser.Point} scaleMin - Set the minimum scale this Sprite will scale down to. Prevents a parent from scaling this Sprite lower than the given value. Set to `null` to remove. + */ + this.scaleMin = null; + + /** + * @property {Phaser.Point} scaleMax - Set the maximum scale this Sprite will scale up to. Prevents a parent from scaling this Sprite higher than the given value. Set to `null` to remove. + */ + this.scaleMax = null; + /** * A small internal cache: * @@ -842,6 +855,113 @@ Phaser.Sprite.prototype.overlap = function (displayObject) { }; +/** + * Adjust scaling limits, if set, to this Sprite. + * + * @method Phaser.Sprite#checkTransform + * @private + * @param {PIXI.Matrix} wt - The updated worldTransform matrix. + */ +Phaser.Sprite.prototype.checkTransform = function (wt) { + + if (this.scaleMin) + { + if (wt.a < this.scaleMin.x) + { + wt.a = this.scaleMin.x; + } + + if (wt.d < this.scaleMin.y) + { + wt.d = this.scaleMin.y; + } + } + + if (this.scaleMax) + { + if (wt.a > this.scaleMax.x) + { + wt.a = this.scaleMax.x; + } + + if (wt.d > this.scaleMax.y) + { + wt.d = this.scaleMax.y; + } + } + +}; + +/** + * Sets the scaleMin and scaleMax values. These values are used to limit how far this Sprite will scale based on its parent. + * For example if this Sprite has a minScale value of 1 and its parent has a scale value of 0.5, the 0.5 will be ignored and the scale value of 1 will be used. + * By using these values you can carefully control how Sprites deal with responsive scaling. + * + * If only one parameter is given then that value will be used for both scaleMin and scaleMax: + * setScaleMinMax(1) = scaleMin.x, scaleMin.y, scaleMax.x and scaleMax.y all = 1 + * + * If only two parameters are given the first is set as scaleMin.x and y and the second as scaleMax.x and y: + * setScaleMinMax(0.5, 2) = scaleMin.x and y = 0.5 and scaleMax.x and y = 2 + * + * If you wish to set scaleMin with different values for x and y then either modify Sprite.scaleMin directly, or pass `null` for the maxX and maxY parameters. + * + * Call setScaleMinMax(null) to clear both the scaleMin and scaleMax values. + * + * @method Phaser.Sprite#setScaleMinMax + * @memberof Phaser.Sprite + * @param {number|null} minX - The minimum horizontal scale value this Sprite can scale down to. + * @param {number|null} minY - The minimum vertical scale value this Sprite can scale down to. + * @param {number|null} maxX - The maximum horizontal scale value this Sprite can scale up to. + * @param {number|null} maxY - The maximum vertical scale value this Sprite can scale up to. + */ +Phaser.Sprite.prototype.setScaleMinMax = function (minX, minY, maxX, maxY) { + + if (typeof minY === 'undefined') + { + // 1 parameter, set all to it + minY = maxX = maxY = minX; + } + else if (typeof maxX === 'undefined') + { + // 2 parameters, the first is min, the second max + maxX = maxY = minY; + minY = minX; + } + + if (minX === null) + { + this.scaleMin = null; + } + else + { + if (this.scaleMin) + { + this.scaleMin.set(minX, minY); + } + else + { + this.scaleMin = new Phaser.Point(minX, minY); + } + } + + if (maxX === null) + { + this.scaleMax = null; + } + else + { + if (this.scaleMax) + { + this.scaleMax.set(maxX, maxY); + } + else + { + this.scaleMax = new Phaser.Point(maxX, maxY); + } + } + +}; + /** * Indicates the rotation of the Sprite, in degrees, from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation. * Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement player.angle = 450 is the same as player.angle = 90.