Skip to content

Commit d629788

Browse files
committed
Added Sprite.setScaleMinMax for testing.
1 parent e5810f1 commit d629788

2 files changed

Lines changed: 231 additions & 0 deletions

File tree

src/gameobjects/Image.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ Phaser.Image = function (game, x, y, key, frame) {
6969

7070
PIXI.Sprite.call(this, PIXI.TextureCache['__default']);
7171

72+
this.transformCallback = this.checkTransform;
73+
this.transformCallbackContext = this;
74+
7275
this.position.set(x, y);
7376

7477
/**
@@ -627,6 +630,114 @@ Phaser.Image.prototype.bringToTop = function() {
627630

628631
};
629632

633+
/**
634+
* Adjust scaling limits, if set, to this Image.
635+
*
636+
* @method Phaser.Image#checkTransform
637+
* @private
638+
* @param {PIXI.Matrix} wt - The updated worldTransform matrix.
639+
*/
640+
Phaser.Image.prototype.checkTransform = function (wt) {
641+
642+
if (this.scaleMin)
643+
{
644+
if (wt.a < this.scaleMin.x)
645+
{
646+
wt.a = this.scaleMin.x;
647+
}
648+
649+
if (wt.d < this.scaleMin.y)
650+
{
651+
wt.d = this.scaleMin.y;
652+
}
653+
}
654+
655+
if (this.scaleMax)
656+
{
657+
if (wt.a > this.scaleMax.x)
658+
{
659+
wt.a = this.scaleMax.x;
660+
}
661+
662+
if (wt.d > this.scaleMax.y)
663+
{
664+
wt.d = this.scaleMax.y;
665+
}
666+
}
667+
668+
};
669+
670+
/**
671+
* Sets the scaleMin and scaleMax values in one call.
672+
* These values are used to limit how far this Image will scale (either up or down) based on its parent.
673+
* 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.
674+
* By using these values you can carefully control how Images deal with responsive scaling.
675+
*
676+
* If only one parameter is given then that value will be used for both scaleMin and scaleMax:
677+
* setScaleMinMax(1) = scaleMin.x, scaleMin.y, scaleMax.x and scaleMax.y all = 1
678+
*
679+
* If only two parameters are given the first is set as scaleMin.x and y and the second as scaleMax.x and y:
680+
* setScaleMinMax(0.5, 2) = scaleMin.x and y = 0.5 and scaleMax.x and y = 2
681+
*
682+
* 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.
683+
*
684+
* Call setScaleMinMax(null) to clear both the scaleMin and scaleMax values.
685+
*
686+
* @method Phaser.Image#setScaleMinMax
687+
* @memberof Phaser.Image
688+
* @param {number|null} minX - The minimum horizontal scale value this Image can scale down to.
689+
* @param {number|null} minY - The minimum vertical scale value this Image can scale down to.
690+
* @param {number|null} maxX - The maximum horizontal scale value this Image can scale up to.
691+
* @param {number|null} maxY - The maximum vertical scale value this Image can scale up to.
692+
*/
693+
Phaser.Image.prototype.setScaleMinMax = function (minX, minY, maxX, maxY) {
694+
695+
if (typeof minY === 'undefined')
696+
{
697+
// 1 parameter, set all to it
698+
minY = maxX = maxY = minX;
699+
}
700+
else if (typeof maxX === 'undefined')
701+
{
702+
// 2 parameters, the first is min, the second max
703+
maxX = maxY = minY;
704+
minY = minX;
705+
}
706+
707+
if (minX === null)
708+
{
709+
this.scaleMin = null;
710+
}
711+
else
712+
{
713+
if (this.scaleMin)
714+
{
715+
this.scaleMin.set(minX, minY);
716+
}
717+
else
718+
{
719+
this.scaleMin = new Phaser.Point(minX, minY);
720+
}
721+
}
722+
723+
if (maxX === null)
724+
{
725+
this.scaleMax = null;
726+
}
727+
else
728+
{
729+
if (this.scaleMax)
730+
{
731+
this.scaleMax.set(maxX, maxY);
732+
}
733+
else
734+
{
735+
this.scaleMax = new Phaser.Point(maxX, maxY);
736+
}
737+
}
738+
739+
};
740+
630741
/**
631742
* 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.
632743
* 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.

src/gameobjects/Sprite.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ Phaser.Sprite = function (game, x, y, key, frame) {
6666

6767
PIXI.Sprite.call(this, PIXI.TextureCache['__default']);
6868

69+
this.transformCallback = this.checkTransform;
70+
this.transformCallbackContext = this;
71+
6972
this.position.set(x, y);
7073

7174
/**
@@ -152,6 +155,16 @@ Phaser.Sprite = function (game, x, y, key, frame) {
152155
*/
153156
this.cropRect = null;
154157

158+
/**
159+
* @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.
160+
*/
161+
this.scaleMin = null;
162+
163+
/**
164+
* @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.
165+
*/
166+
this.scaleMax = null;
167+
155168
/**
156169
* A small internal cache:
157170
*
@@ -842,6 +855,113 @@ Phaser.Sprite.prototype.overlap = function (displayObject) {
842855

843856
};
844857

858+
/**
859+
* Adjust scaling limits, if set, to this Sprite.
860+
*
861+
* @method Phaser.Sprite#checkTransform
862+
* @private
863+
* @param {PIXI.Matrix} wt - The updated worldTransform matrix.
864+
*/
865+
Phaser.Sprite.prototype.checkTransform = function (wt) {
866+
867+
if (this.scaleMin)
868+
{
869+
if (wt.a < this.scaleMin.x)
870+
{
871+
wt.a = this.scaleMin.x;
872+
}
873+
874+
if (wt.d < this.scaleMin.y)
875+
{
876+
wt.d = this.scaleMin.y;
877+
}
878+
}
879+
880+
if (this.scaleMax)
881+
{
882+
if (wt.a > this.scaleMax.x)
883+
{
884+
wt.a = this.scaleMax.x;
885+
}
886+
887+
if (wt.d > this.scaleMax.y)
888+
{
889+
wt.d = this.scaleMax.y;
890+
}
891+
}
892+
893+
};
894+
895+
/**
896+
* Sets the scaleMin and scaleMax values. These values are used to limit how far this Sprite will scale based on its parent.
897+
* 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.
898+
* By using these values you can carefully control how Sprites deal with responsive scaling.
899+
*
900+
* If only one parameter is given then that value will be used for both scaleMin and scaleMax:
901+
* setScaleMinMax(1) = scaleMin.x, scaleMin.y, scaleMax.x and scaleMax.y all = 1
902+
*
903+
* If only two parameters are given the first is set as scaleMin.x and y and the second as scaleMax.x and y:
904+
* setScaleMinMax(0.5, 2) = scaleMin.x and y = 0.5 and scaleMax.x and y = 2
905+
*
906+
* 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.
907+
*
908+
* Call setScaleMinMax(null) to clear both the scaleMin and scaleMax values.
909+
*
910+
* @method Phaser.Sprite#setScaleMinMax
911+
* @memberof Phaser.Sprite
912+
* @param {number|null} minX - The minimum horizontal scale value this Sprite can scale down to.
913+
* @param {number|null} minY - The minimum vertical scale value this Sprite can scale down to.
914+
* @param {number|null} maxX - The maximum horizontal scale value this Sprite can scale up to.
915+
* @param {number|null} maxY - The maximum vertical scale value this Sprite can scale up to.
916+
*/
917+
Phaser.Sprite.prototype.setScaleMinMax = function (minX, minY, maxX, maxY) {
918+
919+
if (typeof minY === 'undefined')
920+
{
921+
// 1 parameter, set all to it
922+
minY = maxX = maxY = minX;
923+
}
924+
else if (typeof maxX === 'undefined')
925+
{
926+
// 2 parameters, the first is min, the second max
927+
maxX = maxY = minY;
928+
minY = minX;
929+
}
930+
931+
if (minX === null)
932+
{
933+
this.scaleMin = null;
934+
}
935+
else
936+
{
937+
if (this.scaleMin)
938+
{
939+
this.scaleMin.set(minX, minY);
940+
}
941+
else
942+
{
943+
this.scaleMin = new Phaser.Point(minX, minY);
944+
}
945+
}
946+
947+
if (maxX === null)
948+
{
949+
this.scaleMax = null;
950+
}
951+
else
952+
{
953+
if (this.scaleMax)
954+
{
955+
this.scaleMax.set(maxX, maxY);
956+
}
957+
else
958+
{
959+
this.scaleMax = new Phaser.Point(maxX, maxY);
960+
}
961+
}
962+
963+
};
964+
845965
/**
846966
* 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.
847967
* 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.

0 commit comments

Comments
 (0)