forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScaleMinMax.js
More file actions
168 lines (149 loc) · 4.88 KB
/
Copy pathScaleMinMax.js
File metadata and controls
168 lines (149 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The ScaleMinMax component allows a Game Object to limit how far it can be scaled by its parent.
*
* @class
*/
Phaser.Component.ScaleMinMax = function () {};
Phaser.Component.ScaleMinMax.prototype = {
/**
* The callback that will apply any scale limiting to the worldTransform.
* @property {function} transformCallback
*/
transformCallback: null,
/**
* The context under which `transformCallback` is called.
* @property {object} transformCallbackContext
*/
transformCallbackContext: this,
/**
* The minimum scale this Game Object will scale down to.
*
* It allows you to prevent a parent from scaling this Game Object lower than the given value.
*
* Set it to `null` to remove the limit.
* @property {Phaser.Point} scaleMin
*/
scaleMin: null,
/**
* The maximum scale this Game Object will scale up to.
*
* It allows you to prevent a parent from scaling this Game Object higher than the given value.
*
* Set it to `null` to remove the limit.
* @property {Phaser.Point} scaleMax
*/
scaleMax: null,
/**
* Adjust scaling limits, if set, to this Game Object.
*
* @method
* @private
* @param {PIXI.Matrix} wt - The updated worldTransform matrix.
*/
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 Game Object will scale based on its parent.
*
* For example if this Game Object 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, as the parents scale is lower than the minimum scale this Game Object should adhere to.
*
* By setting these values you can carefully control how Game Objects 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 Game Object.scaleMin directly,
* or pass `null` for the `maxX` and `maxY` parameters.
*
* Call `setScaleMinMax(null)` to clear all previously set values.
*
* @method
* @param {number|null} minX - The minimum horizontal scale value this Game Object can scale down to.
* @param {number|null} minY - The minimum vertical scale value this Game Object can scale down to.
* @param {number|null} maxX - The maximum horizontal scale value this Game Object can scale up to.
* @param {number|null} maxY - The maximum vertical scale value this Game Object can scale up to.
*/
setScaleMinMax: function (minX, minY, maxX, maxY) {
if (minY === undefined)
{
// 1 parameter, set all to it
minY = maxX = maxY = minX;
}
else if (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);
}
}
if (this.scaleMin === null)
{
this.transformCallback = null;
}
else
{
this.transformCallback = this.checkTransform;
this.transformCallbackContext = this;
}
}
};