/
Origin.js
197 lines (171 loc) · 5.49 KB
/
Origin.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
190
191
192
193
194
195
196
197
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* Provides methods used for getting and setting the origin of a Game Object.
* Values are normalized, given in the range 0 to 1.
* Display values contain the calculated pixel values.
* Should be applied as a mixin and not used directly.
*
* @namespace Phaser.GameObjects.Components.Origin
* @since 3.0.0
*/
var Origin = {
/**
* A property indicating that a Game Object has this component.
*
* @name Phaser.GameObjects.Components.Origin#_originComponent
* @type {boolean}
* @private
* @default true
* @since 3.2.0
*/
_originComponent: true,
/**
* The horizontal origin of this Game Object.
* The origin maps the relationship between the size and position of the Game Object.
* The default value is 0.5, meaning all Game Objects are positioned based on their center.
* Setting the value to 0 means the position now relates to the left of the Game Object.
*
* @name Phaser.GameObjects.Components.Origin#originX
* @type {number}
* @default 0.5
* @since 3.0.0
*/
originX: 0.5,
/**
* The vertical origin of this Game Object.
* The origin maps the relationship between the size and position of the Game Object.
* The default value is 0.5, meaning all Game Objects are positioned based on their center.
* Setting the value to 0 means the position now relates to the top of the Game Object.
*
* @name Phaser.GameObjects.Components.Origin#originY
* @type {number}
* @default 0.5
* @since 3.0.0
*/
originY: 0.5,
// private + read only
_displayOriginX: 0,
_displayOriginY: 0,
/**
* The horizontal display origin of this Game Object.
* The origin is a normalized value between 0 and 1.
* The displayOrigin is a pixel value, based on the size of the Game Object combined with the origin.
*
* @name Phaser.GameObjects.Components.Origin#displayOriginX
* @type {number}
* @since 3.0.0
*/
displayOriginX: {
get: function ()
{
return this._displayOriginX;
},
set: function (value)
{
this._displayOriginX = value;
this.originX = value / this.width;
}
},
/**
* The vertical display origin of this Game Object.
* The origin is a normalized value between 0 and 1.
* The displayOrigin is a pixel value, based on the size of the Game Object combined with the origin.
*
* @name Phaser.GameObjects.Components.Origin#displayOriginY
* @type {number}
* @since 3.0.0
*/
displayOriginY: {
get: function ()
{
return this._displayOriginY;
},
set: function (value)
{
this._displayOriginY = value;
this.originY = value / this.height;
}
},
/**
* Sets the origin of this Game Object.
*
* The values are given in the range 0 to 1.
*
* @method Phaser.GameObjects.Components.Origin#setOrigin
* @since 3.0.0
*
* @param {number} [x=0.5] - The horizontal origin value.
* @param {number} [y=x] - The vertical origin value. If not defined it will be set to the value of `x`.
*
* @return {this} This Game Object instance.
*/
setOrigin: function (x, y)
{
if (x === undefined) { x = 0.5; }
if (y === undefined) { y = x; }
this.originX = x;
this.originY = y;
return this.updateDisplayOrigin();
},
/**
* Sets the origin of this Game Object based on the Pivot values in its Frame.
*
* @method Phaser.GameObjects.Components.Origin#setOriginFromFrame
* @since 3.0.0
*
* @return {this} This Game Object instance.
*/
setOriginFromFrame: function ()
{
if (!this.frame || !this.frame.customPivot)
{
return this.setOrigin();
}
else
{
this.originX = this.frame.pivotX;
this.originY = this.frame.pivotY;
}
return this.updateDisplayOrigin();
},
/**
* Sets the display origin of this Game Object.
* The difference between this and setting the origin is that you can use pixel values for setting the display origin.
*
* @method Phaser.GameObjects.Components.Origin#setDisplayOrigin
* @since 3.0.0
*
* @param {number} [x=0] - The horizontal display origin value.
* @param {number} [y=x] - The vertical display origin value. If not defined it will be set to the value of `x`.
*
* @return {this} This Game Object instance.
*/
setDisplayOrigin: function (x, y)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = x; }
this.displayOriginX = x;
this.displayOriginY = y;
return this;
},
/**
* Updates the Display Origin cached values internally stored on this Game Object.
* You don't usually call this directly, but it is exposed for edge-cases where you may.
*
* @method Phaser.GameObjects.Components.Origin#updateDisplayOrigin
* @since 3.0.0
*
* @return {this} This Game Object instance.
*/
updateDisplayOrigin: function ()
{
this._displayOriginX = this.originX * this.width;
this._displayOriginY = this.originY * this.height;
return this;
}
};
module.exports = Origin;