forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrame.js
More file actions
399 lines (315 loc) · 9.35 KB
/
Copy pathFrame.js
File metadata and controls
399 lines (315 loc) · 9.35 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
var Class = require('../utils/Class');
var Extend = require('../utils/object/Extend');
/**
* A Frame is a section of a Texture.
*/
var Frame = new Class({
initialize:
function Frame (texture, name, sourceIndex, x, y, width, height)
{
/**
* @property {Phaser.Texture} texture - The Texture this frame belongs to.
*/
this.texture = texture;
/**
* @property {string} name - The name of this frame within the Texture.
*/
this.name = name;
this.source = texture.source[sourceIndex];
this.sourceIndex = sourceIndex;
/**
* @property {number} cutX - X position within the source image to cut from.
*/
this.cutX = x;
/**
* @property {number} cutY - Y position within the source image to cut from.
*/
this.cutY = y;
/**
* @property {number} cutWidth - The width of the area in the source image to cut.
*/
this.cutWidth = width;
/**
* @property {number} cutHeight - The height of the area in the source image to cut.
*/
this.cutHeight = height;
/**
* @property {number} x - The X rendering offset of this Frame, taking trim into account.
*/
this.x = 0;
/**
* @property {number} y - The Y rendering offset of this Frame, taking trim into account.
*/
this.y = 0;
/**
* @property {number} width - The rendering width of this Frame, taking trim into account.
*/
this.width = width;
/**
* @property {number} height - The rendering height of this Frame, taking trim into account.
*/
this.height = height;
// The half sizes of this frame (to save in constant calculations in the renderer)
this.halfWidth = Math.floor(width * 0.5);
this.halfHeight = Math.floor(height * 0.5);
/**
* @property {number} width - The rendering width of this Frame, taking trim into account.
*/
this.centerX = Math.floor(width / 2);
/**
* @property {number} height - The rendering height of this Frame, taking trim into account.
*/
this.centerY = Math.floor(height / 2);
/**
* @property {number} pivotX - The horizontal pivot point of this Frame.
*/
this.pivotX = 0;
/**
* @property {number} height - The vertical pivot point of this Frame.
*/
this.pivotY = 0;
/**
* Is this frame is rotated or not in the Texture?
* Rotation allows you to use rotated frames in texture atlas packing.
* It has nothing to do with Sprite rotation.
*
* @property {boolean} rotated
* @default
*/
this.rotated = false;
// Over-rides the Renderer setting? -1 = use Renderer Setting, 0 = No rounding, 1 = Round
this.autoRound = -1;
// Any Frame specific custom data can be stored here
this.customData = {};
/**
* The un-modified source frame, trim and UV data.
*
* @private
* @property {object} data
*/
this.data = {
cut: {
x: x,
y: y,
w: width,
h: height,
r: x + width,
b: y + height
},
trim: false,
sourceSize: {
w: width,
h: height
},
spriteSourceSize: {
x: 0,
y: 0,
w: width,
h: height
},
uvs: {
x0: 0,
y0: 0,
x1: 0,
y1: 0,
x2: 0,
y2: 0,
x3: 0,
y3: 0
},
radius: 0.5 * Math.sqrt(width * width + height * height),
drawImage: {
sx: x,
sy: y,
sWidth: width,
sHeight: height,
dWidth: width,
dHeight: height
}
};
this.updateUVs();
},
/**
* If the frame was trimmed when added to the Texture Atlas, this records the trim and source data.
*
* @method Phaser.TextureFrame#setTrim
* @param {number} actualWidth - The width of the frame before being trimmed.
* @param {number} actualHeight - The height of the frame before being trimmed.
* @param {number} destX - The destination X position of the trimmed frame for display.
* @param {number} destY - The destination Y position of the trimmed frame for display.
* @param {number} destWidth - The destination width of the trimmed frame for display.
* @param {number} destHeight - The destination height of the trimmed frame for display.
*/
setTrim: function (actualWidth, actualHeight, destX, destY, destWidth, destHeight)
{
var data = this.data;
var ss = data.spriteSourceSize;
// Store actual values
data.trim = true;
data.sourceSize.w = actualWidth;
data.sourceSize.h = actualHeight;
ss.x = destX;
ss.y = destY;
ss.w = destWidth;
ss.h = destHeight;
// Adjust properties
this.x = destX;
this.y = destY;
this.width = destWidth;
this.height = destHeight;
this.halfWidth = destWidth * 0.5;
this.halfHeight = destHeight * 0.5;
this.centerX = Math.floor(destWidth / 2);
this.centerY = Math.floor(destHeight / 2);
return this.updateUVs();
},
/**
* Updates the internal WebGL UV cache and the drawImage cache.
*
* @method updateUVs
* @private
*/
updateUVs: function ()
{
var cx = this.cutX;
var cy = this.cutY;
var cw = this.cutWidth;
var ch = this.cutHeight;
// Canvas data
var cd = this.data.drawImage;
cd.sWidth = cw;
cd.sHeight = ch;
cd.dWidth = cw;
cd.dHeight = ch;
// WebGL data
var tw = this.source.width;
var th = this.source.height;
var uvs = this.data.uvs;
uvs.x0 = cx / tw;
uvs.y0 = cy / th;
uvs.x1 = cx / tw;
uvs.y1 = (cy + ch) / th;
uvs.x2 = (cx + cw) / tw;
uvs.y2 = (cy + ch) / th;
uvs.x3 = (cx + cw) / tw;
uvs.y3 = cy / th;
return this;
},
/**
* Updates the internal WebGL UV cache.
*
* @method updateUVsInverted
* @private
*/
updateUVsInverted: function ()
{
var tw = this.source.width;
var th = this.source.height;
var uvs = this.data.uvs;
uvs.x3 = (this.cutX + this.cutHeight) / tw;
uvs.y3 = (this.cutY + this.cutWidth) / th;
uvs.x2 = this.cutX / tw;
uvs.y2 = (this.cutY + this.cutWidth) / th;
uvs.x1 = this.cutX / tw;
uvs.y1 = this.cutY / th;
uvs.x0 = (this.cutX + this.cutHeight) / tw;
uvs.y0 = this.cutY / th;
return this;
},
clone: function ()
{
var clone = new Frame(this.texture, this.name, this.sourceIndex);
clone.cutX = this.cutX;
clone.cutY = this.cutY;
clone.cutWidth = this.cutWidth;
clone.cutHeight = this.cutHeight;
clone.x = this.x;
clone.y = this.y;
clone.width = this.width;
clone.height = this.height;
clone.halfWidth = this.halfWidth;
clone.halfHeight = this.halfHeight;
clone.centerX = this.centerX;
clone.centerY = this.centerY;
clone.rotated = this.rotated;
clone.data = Extend(true, clone.data, this.data);
clone.updateUVs();
return clone;
},
destroy: function ()
{
},
/**
* The width of the Frame in its un-trimmed, un-padded state, as prepared in the art package,
* before being packed.
*
* @name Phaser.TextureFrame#realWidth
* @property {any} realWidth
*/
realWidth: {
get: function ()
{
return this.data.sourceSize.w;
}
},
/**
* The height of the Frame in its un-trimmed, un-padded state, as prepared in the art package,
* before being packed.
*
* @name Phaser.TextureFrame#realHeight
* @property {any} realHeight
*/
realHeight: {
get: function ()
{
return this.data.sourceSize.h;
}
},
/**
* UVs
*
* @name Phaser.TextureFrame#uvs
* @property {Object} uvs
*/
uvs: {
get: function ()
{
return this.data.uvs;
}
},
/**
* The radius of the Frame (derived from sqrt(w * w + h * h) / 2)
* @name Phaser.TextureFrame#radius
* @property {number} radius
*/
radius: {
get: function ()
{
return this.data.radius;
}
},
/**
* Is the Frame trimmed?
* @name Phaser.TextureFrame#trimmed
* @property {boolean} trimmed
*/
trimmed: {
get: function ()
{
return this.data.trim;
}
},
/**
* Canvas Draw Image data
*
* @name Phaser.TextureFrame#canvasData
* @property {Object} canvasData
*/
canvasData: {
get: function ()
{
return this.data.drawImage;
}
}
});
module.exports = Frame;