-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Size.js
189 lines (165 loc) · 5.29 KB
/
Size.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Provides methods used for getting and setting the size of a Game Object.
*
* @namespace Phaser.GameObjects.Components.Size
* @since 3.0.0
*/
var Size = {
/**
* A property indicating that a Game Object has this component.
*
* @name Phaser.GameObjects.Components.Size#_sizeComponent
* @type {boolean}
* @private
* @default true
* @since 3.2.0
*/
_sizeComponent: true,
/**
* The native (un-scaled) width of this Game Object.
*
* Changing this value will not change the size that the Game Object is rendered in-game.
* For that you need to either set the scale of the Game Object (`setScale`) or use
* the `displayWidth` property.
*
* @name Phaser.GameObjects.Components.Size#width
* @type {number}
* @since 3.0.0
*/
width: 0,
/**
* The native (un-scaled) height of this Game Object.
*
* Changing this value will not change the size that the Game Object is rendered in-game.
* For that you need to either set the scale of the Game Object (`setScale`) or use
* the `displayHeight` property.
*
* @name Phaser.GameObjects.Components.Size#height
* @type {number}
* @since 3.0.0
*/
height: 0,
/**
* The displayed width of this Game Object.
*
* This value takes into account the scale factor.
*
* Setting this value will adjust the Game Object's scale property.
*
* @name Phaser.GameObjects.Components.Size#displayWidth
* @type {number}
* @since 3.0.0
*/
displayWidth: {
get: function ()
{
return Math.abs(this.scaleX * this.frame.realWidth);
},
set: function (value)
{
this.scaleX = value / this.frame.realWidth;
}
},
/**
* The displayed height of this Game Object.
*
* This value takes into account the scale factor.
*
* Setting this value will adjust the Game Object's scale property.
*
* @name Phaser.GameObjects.Components.Size#displayHeight
* @type {number}
* @since 3.0.0
*/
displayHeight: {
get: function ()
{
return Math.abs(this.scaleY * this.frame.realHeight);
},
set: function (value)
{
this.scaleY = value / this.frame.realHeight;
}
},
/**
* Sets the size of this Game Object to be that of the given Frame.
*
* This will not change the size that the Game Object is rendered in-game.
* For that you need to either set the scale of the Game Object (`setScale`) or call the
* `setDisplaySize` method, which is the same thing as changing the scale but allows you
* to do so by giving pixel values.
*
* If you have enabled this Game Object for input, changing the size will _not_ change the
* size of the hit area. To do this you should adjust the `input.hitArea` object directly.
*
* @method Phaser.GameObjects.Components.Size#setSizeToFrame
* @since 3.0.0
*
* @param {Phaser.Textures.Frame|boolean} [frame] - The frame to base the size of this Game Object on.
*
* @return {this} This Game Object instance.
*/
setSizeToFrame: function (frame)
{
if (!frame) { frame = this.frame; }
this.width = frame.realWidth;
this.height = frame.realHeight;
var input = this.input;
if (input && !input.customHitArea)
{
input.hitArea.width = this.width;
input.hitArea.height = this.height;
}
return this;
},
/**
* Sets the internal size of this Game Object, as used for frame or physics body creation.
*
* This will not change the size that the Game Object is rendered in-game.
* For that you need to either set the scale of the Game Object (`setScale`) or call the
* `setDisplaySize` method, which is the same thing as changing the scale but allows you
* to do so by giving pixel values.
*
* If you have enabled this Game Object for input, changing the size will _not_ change the
* size of the hit area. To do this you should adjust the `input.hitArea` object directly.
*
* @method Phaser.GameObjects.Components.Size#setSize
* @since 3.0.0
*
* @param {number} width - The width of this Game Object.
* @param {number} height - The height of this Game Object.
*
* @return {this} This Game Object instance.
*/
setSize: function (width, height)
{
this.width = width;
this.height = height;
return this;
},
/**
* Sets the display size of this Game Object.
*
* Calling this will adjust the scale.
*
* @method Phaser.GameObjects.Components.Size#setDisplaySize
* @since 3.0.0
*
* @param {number} width - The width of this Game Object.
* @param {number} height - The height of this Game Object.
*
* @return {this} This Game Object instance.
*/
setDisplaySize: function (width, height)
{
this.displayWidth = width;
this.displayHeight = height;
return this;
}
};
module.exports = Size;