/
Frame.js
851 lines (721 loc) · 21.3 KB
/
Frame.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var Clamp = require('../math/Clamp');
var Extend = require('../utils/object/Extend');
/**
* @classdesc
* A Frame is a section of a Texture.
*
* @class Frame
* @memberof Phaser.Textures
* @constructor
* @since 3.0.0
*
* @param {Phaser.Textures.Texture} texture - The Texture this Frame is a part of.
* @param {(number|string)} name - The name of this Frame. The name is unique within the Texture.
* @param {number} sourceIndex - The index of the TextureSource that this Frame is a part of.
* @param {number} x - The x coordinate of the top-left of this Frame.
* @param {number} y - The y coordinate of the top-left of this Frame.
* @param {number} width - The width of this Frame.
* @param {number} height - The height of this Frame.
*/
var Frame = new Class({
initialize:
function Frame (texture, name, sourceIndex, x, y, width, height)
{
/**
* The Texture this Frame is a part of.
*
* @name Phaser.Textures.Frame#texture
* @type {Phaser.Textures.Texture}
* @since 3.0.0
*/
this.texture = texture;
/**
* The name of this Frame.
* The name is unique within the Texture.
*
* @name Phaser.Textures.Frame#name
* @type {string}
* @since 3.0.0
*/
this.name = name;
/**
* The TextureSource this Frame is part of.
*
* @name Phaser.Textures.Frame#source
* @type {Phaser.Textures.TextureSource}
* @since 3.0.0
*/
this.source = texture.source[sourceIndex];
/**
* The index of the TextureSource in the Texture sources array.
*
* @name Phaser.Textures.Frame#sourceIndex
* @type {number}
* @since 3.0.0
*/
this.sourceIndex = sourceIndex;
/**
* A reference to the Texture Source WebGL Texture that this Frame is using.
*
* @name Phaser.Textures.Frame#glTexture
* @type {?WebGLTexture}
* @default null
* @since 3.11.0
*/
this.glTexture = this.source.glTexture;
/**
* X position within the source image to cut from.
*
* @name Phaser.Textures.Frame#cutX
* @type {number}
* @since 3.0.0
*/
this.cutX;
/**
* Y position within the source image to cut from.
*
* @name Phaser.Textures.Frame#cutY
* @type {number}
* @since 3.0.0
*/
this.cutY;
/**
* The width of the area in the source image to cut.
*
* @name Phaser.Textures.Frame#cutWidth
* @type {number}
* @since 3.0.0
*/
this.cutWidth;
/**
* The height of the area in the source image to cut.
*
* @name Phaser.Textures.Frame#cutHeight
* @type {number}
* @since 3.0.0
*/
this.cutHeight;
/**
* The X rendering offset of this Frame, taking trim into account.
*
* @name Phaser.Textures.Frame#x
* @type {number}
* @default 0
* @since 3.0.0
*/
this.x = 0;
/**
* The Y rendering offset of this Frame, taking trim into account.
*
* @name Phaser.Textures.Frame#y
* @type {number}
* @default 0
* @since 3.0.0
*/
this.y = 0;
/**
* The rendering width of this Frame, taking trim into account.
*
* @name Phaser.Textures.Frame#width
* @type {number}
* @since 3.0.0
*/
this.width;
/**
* The rendering height of this Frame, taking trim into account.
*
* @name Phaser.Textures.Frame#height
* @type {number}
* @since 3.0.0
*/
this.height;
/**
* Half the width, floored.
* Precalculated for the renderer.
*
* @name Phaser.Textures.Frame#halfWidth
* @type {number}
* @since 3.0.0
*/
this.halfWidth;
/**
* Half the height, floored.
* Precalculated for the renderer.
*
* @name Phaser.Textures.Frame#halfHeight
* @type {number}
* @since 3.0.0
*/
this.halfHeight;
/**
* The x center of this frame, floored.
*
* @name Phaser.Textures.Frame#centerX
* @type {number}
* @since 3.0.0
*/
this.centerX;
/**
* The y center of this frame, floored.
*
* @name Phaser.Textures.Frame#centerY
* @type {number}
* @since 3.0.0
*/
this.centerY;
/**
* The horizontal pivot point of this Frame.
*
* @name Phaser.Textures.Frame#pivotX
* @type {number}
* @default 0
* @since 3.0.0
*/
this.pivotX = 0;
/**
* The vertical pivot point of this Frame.
*
* @name Phaser.Textures.Frame#pivotY
* @type {number}
* @default 0
* @since 3.0.0
*/
this.pivotY = 0;
/**
* Does this Frame have a custom pivot point?
*
* @name Phaser.Textures.Frame#customPivot
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.customPivot = false;
/**
* **CURRENTLY UNSUPPORTED**
*
* 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.
*
* @name Phaser.Textures.Frame#rotated
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.rotated = false;
/**
* Over-rides the Renderer setting.
* -1 = use Renderer Setting
* 0 = No rounding
* 1 = Round
*
* @name Phaser.Textures.Frame#autoRound
* @type {number}
* @default -1
* @since 3.0.0
*/
this.autoRound = -1;
/**
* Any Frame specific custom data can be stored here.
*
* @name Phaser.Textures.Frame#customData
* @type {object}
* @since 3.0.0
*/
this.customData = {};
/**
* WebGL UV u0 value.
*
* @name Phaser.Textures.Frame#u0
* @type {number}
* @default 0
* @since 3.11.0
*/
this.u0 = 0;
/**
* WebGL UV v0 value.
*
* @name Phaser.Textures.Frame#v0
* @type {number}
* @default 0
* @since 3.11.0
*/
this.v0 = 0;
/**
* WebGL UV u1 value.
*
* @name Phaser.Textures.Frame#u1
* @type {number}
* @default 0
* @since 3.11.0
*/
this.u1 = 0;
/**
* WebGL UV v1 value.
*
* @name Phaser.Textures.Frame#v1
* @type {number}
* @default 0
* @since 3.11.0
*/
this.v1 = 0;
/**
* The un-modified source frame, trim and UV data.
*
* @name Phaser.Textures.Frame#data
* @type {object}
* @private
* @since 3.0.0
*/
this.data = {
cut: {
x: 0,
y: 0,
w: 0,
h: 0,
r: 0,
b: 0
},
trim: false,
sourceSize: {
w: 0,
h: 0
},
spriteSourceSize: {
x: 0,
y: 0,
w: 0,
h: 0,
r: 0,
b: 0
},
radius: 0,
drawImage: {
x: 0,
y: 0,
width: 0,
height: 0
}
};
this.setSize(width, height, x, y);
},
/**
* Sets the width, height, x and y of this Frame.
*
* This is called automatically by the constructor
* and should rarely be changed on-the-fly.
*
* @method Phaser.Textures.Frame#setSize
* @since 3.7.0
*
* @param {number} width - The width of the frame before being trimmed.
* @param {number} height - The height of the frame before being trimmed.
* @param {number} [x=0] - The x coordinate of the top-left of this Frame.
* @param {number} [y=0] - The y coordinate of the top-left of this Frame.
*
* @return {Phaser.Textures.Frame} This Frame object.
*/
setSize: function (width, height, x, y)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
this.cutX = x;
this.cutY = y;
this.cutWidth = width;
this.cutHeight = height;
this.width = width;
this.height = height;
this.halfWidth = Math.floor(width * 0.5);
this.halfHeight = Math.floor(height * 0.5);
this.centerX = Math.floor(width / 2);
this.centerY = Math.floor(height / 2);
var data = this.data;
var cut = data.cut;
cut.x = x;
cut.y = y;
cut.w = width;
cut.h = height;
cut.r = x + width;
cut.b = y + height;
data.sourceSize.w = width;
data.sourceSize.h = height;
data.spriteSourceSize.w = width;
data.spriteSourceSize.h = height;
data.radius = 0.5 * Math.sqrt(width * width + height * height);
var drawImage = data.drawImage;
drawImage.x = x;
drawImage.y = y;
drawImage.width = width;
drawImage.height = height;
return this.updateUVs();
},
/**
* If the frame was trimmed when added to the Texture Atlas, this records the trim and source data.
*
* @method Phaser.Textures.Frame#setTrim
* @since 3.0.0
*
* @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.
*
* @return {Phaser.Textures.Frame} This Frame object.
*/
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;
ss.r = destX + destWidth;
ss.b = destY + 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();
},
/**
* Takes a crop data object and, based on the rectangular region given, calculates the
* required UV coordinates in order to crop this Frame for WebGL and Canvas rendering.
*
* This is called directly by the Game Object Texture Components `setCrop` method.
* Please use that method to crop a Game Object.
*
* @method Phaser.Textures.Frame#setCropUVs
* @since 3.11.0
*
* @param {object} crop - The crop data object. This is the `GameObject._crop` property.
* @param {number} x - The x coordinate to start the crop from. Cannot be negative or exceed the Frame width.
* @param {number} y - The y coordinate to start the crop from. Cannot be negative or exceed the Frame height.
* @param {number} width - The width of the crop rectangle. Cannot exceed the Frame width.
* @param {number} height - The height of the crop rectangle. Cannot exceed the Frame height.
* @param {boolean} flipX - Does the parent Game Object have flipX set?
* @param {boolean} flipY - Does the parent Game Object have flipY set?
*
* @return {object} The updated crop data object.
*/
setCropUVs: function (crop, x, y, width, height, flipX, flipY)
{
// Clamp the input values
var cx = this.cutX;
var cy = this.cutY;
var cw = this.cutWidth;
var ch = this.cutHeight;
var rw = this.realWidth;
var rh = this.realHeight;
x = Clamp(x, 0, rw);
y = Clamp(y, 0, rh);
width = Clamp(width, 0, rw - x);
height = Clamp(height, 0, rh - y);
var ox = cx + x;
var oy = cy + y;
var ow = width;
var oh = height;
var data = this.data;
if (data.trim)
{
var ss = data.spriteSourceSize;
// Need to check for intersection between the cut area and the crop area
// If there is none, we set UV to be empty, otherwise set it to be the intersection area
width = Clamp(width, 0, cw - x);
height = Clamp(height, 0, ch - y);
var cropRight = x + width;
var cropBottom = y + height;
var intersects = !(ss.r < x || ss.b < y || ss.x > cropRight || ss.y > cropBottom);
if (intersects)
{
var ix = Math.max(ss.x, x);
var iy = Math.max(ss.y, y);
var iw = Math.min(ss.r, cropRight) - ix;
var ih = Math.min(ss.b, cropBottom) - iy;
ow = iw;
oh = ih;
if (flipX)
{
ox = cx + (cw - (ix - ss.x) - iw);
}
else
{
ox = cx + (ix - ss.x);
}
if (flipY)
{
oy = cy + (ch - (iy - ss.y) - ih);
}
else
{
oy = cy + (iy - ss.y);
}
x = ix;
y = iy;
width = iw;
height = ih;
}
else
{
ox = 0;
oy = 0;
ow = 0;
oh = 0;
}
}
else
{
if (flipX)
{
ox = cx + (cw - x - width);
}
if (flipY)
{
oy = cy + (ch - y - height);
}
}
var tw = this.source.width;
var th = this.source.height;
// Map the given coordinates into UV space, clamping to the 0-1 range.
crop.u0 = Math.max(0, ox / tw);
crop.v0 = Math.max(0, oy / th);
crop.u1 = Math.min(1, (ox + ow) / tw);
crop.v1 = Math.min(1, (oy + oh) / th);
crop.x = x;
crop.y = y;
crop.cx = ox;
crop.cy = oy;
crop.cw = ow;
crop.ch = oh;
crop.width = width;
crop.height = height;
crop.flipX = flipX;
crop.flipY = flipY;
return crop;
},
/**
* Takes a crop data object and recalculates the UVs based on the dimensions inside the crop object.
* Called automatically by `setFrame`.
*
* @method Phaser.Textures.Frame#updateCropUVs
* @since 3.11.0
*
* @param {object} crop - The crop data object. This is the `GameObject._crop` property.
* @param {boolean} flipX - Does the parent Game Object have flipX set?
* @param {boolean} flipY - Does the parent Game Object have flipY set?
*
* @return {object} The updated crop data object.
*/
updateCropUVs: function (crop, flipX, flipY)
{
return this.setCropUVs(crop, crop.x, crop.y, crop.width, crop.height, flipX, flipY);
},
/**
* Directly sets the canvas and WebGL UV data for this frame.
*
* Use this if you need to override the values that are generated automatically
* when the Frame is created.
*
* @method Phaser.Textures.Frame#setUVs
* @since 3.50.0
*
* @param {number} width - Width of this frame for the Canvas data.
* @param {number} height - Height of this frame for the Canvas data.
* @param {number} u0 - UV u0 value.
* @param {number} v0 - UV v0 value.
* @param {number} u1 - UV u1 value.
* @param {number} v1 - UV v1 value.
*
* @return {Phaser.Textures.Frame} This Frame object.
*/
setUVs: function (width, height, u0, v0, u1, v1)
{
// Canvas data
var cd = this.data.drawImage;
cd.width = width;
cd.height = height;
// WebGL data
this.u0 = u0;
this.v0 = v0;
this.u1 = u1;
this.v1 = v1;
return this;
},
/**
* Updates the internal WebGL UV cache and the drawImage cache.
*
* @method Phaser.Textures.Frame#updateUVs
* @since 3.0.0
*
* @return {Phaser.Textures.Frame} This Frame object.
*/
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.width = cw;
cd.height = ch;
// WebGL data
var tw = this.source.width;
var th = this.source.height;
this.u0 = cx / tw;
this.v0 = cy / th;
this.u1 = (cx + cw) / tw;
this.v1 = (cy + ch) / th;
return this;
},
/**
* Updates the internal WebGL UV cache.
*
* @method Phaser.Textures.Frame#updateUVsInverted
* @since 3.0.0
*
* @return {Phaser.Textures.Frame} This Frame object.
*/
updateUVsInverted: function ()
{
var tw = this.source.width;
var th = this.source.height;
this.u0 = (this.cutX + this.cutHeight) / tw;
this.v0 = this.cutY / th;
this.u1 = this.cutX / tw;
this.v1 = (this.cutY + this.cutWidth) / th;
return this;
},
/**
* Clones this Frame into a new Frame object.
*
* @method Phaser.Textures.Frame#clone
* @since 3.0.0
*
* @return {Phaser.Textures.Frame} A clone of this Frame.
*/
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;
},
/**
* Destroys this Frame by nulling its reference to the parent Texture and and data objects.
*
* @method Phaser.Textures.Frame#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.source = null;
this.texture = null;
this.glTexture = null;
this.customData = null;
this.data = null;
},
/**
* The width of the Frame in its un-trimmed, un-padded state, as prepared in the art package,
* before being packed.
*
* @name Phaser.Textures.Frame#realWidth
* @type {number}
* @readonly
* @since 3.0.0
*/
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.Textures.Frame#realHeight
* @type {number}
* @readonly
* @since 3.0.0
*/
realHeight: {
get: function ()
{
return this.data.sourceSize.h;
}
},
/**
* The radius of the Frame (derived from sqrt(w * w + h * h) / 2)
*
* @name Phaser.Textures.Frame#radius
* @type {number}
* @readonly
* @since 3.0.0
*/
radius: {
get: function ()
{
return this.data.radius;
}
},
/**
* Is the Frame trimmed or not?
*
* @name Phaser.Textures.Frame#trimmed
* @type {boolean}
* @readonly
* @since 3.0.0
*/
trimmed: {
get: function ()
{
return this.data.trim;
}
},
/**
* The Canvas drawImage data object.
*
* @name Phaser.Textures.Frame#canvasData
* @type {object}
* @readonly
* @since 3.0.0
*/
canvasData: {
get: function ()
{
return this.data.drawImage;
}
}
});
module.exports = Frame;