Skip to content

Commit 2d676b0

Browse files
committed
Working through crop issues.
1 parent 77d5be1 commit 2d676b0

4 files changed

Lines changed: 137 additions & 27 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Calling addToWorld() would previously not check the _toRemove array, which could
166166
* Group.bringToTop (and consequently Sprite.bringToTop) no longer removes the child from the InputManager if enabled (thanks @BinaryMoon, fix #928)
167167
* Group.sendToBack (and consequently Sprite.sendToBack) no longer removes the child from the InputManager if enabled.
168168
* When adding a new Animation to a Sprite it would incorrectly reset the current Sprite frame to the first frame of the animation sequence, it is now left un-touched until you call `play` on the animation.
169+
* Tween.from now returns a reference to the tweened object in the same way that Tween.to does (thanks @b-ely, fix #976)
169170

170171

171172

src/gameobjects/Sprite.js

Lines changed: 132 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,17 @@ Phaser.Sprite = function (game, x, y, key, frame) {
171171
* 12 = texture height
172172
* 13 = texture spriteSourceSizeX
173173
* 14 = texture spriteSourceSizeY
174-
* 15 = trim x
175-
* 16 = trim y
174+
* 15 = calculated trim x
175+
* 16 = calculated trim y
176+
* 17 = trim actualWidth
177+
* 18 = trim actualHeight
176178
* @property {Array} _cache
177179
* @private
178180
*/
179-
this._cache = [ 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
181+
this._cache = [ 0, 0, 0, 0, 1,
182+
0, 1, 0, 0, 0,
183+
0, 0, 0, 0, 0,
184+
0, 0, 0, 0 ];
180185

181186
/**
182187
* @property {Phaser.Rectangle} _bounds - Internal cache var.
@@ -409,8 +414,8 @@ Phaser.Sprite.prototype.resetFrame = function() {
409414
{
410415
this.texture.trim.x = this._cache[13];
411416
this.texture.trim.y = this._cache[14];
412-
this.texture.trim.width = this.texture.frame.width;
413-
this.texture.trim.height = this.texture.frame.height;
417+
this.texture.trim.width = this._cache[17];
418+
this.texture.trim.height = this._cache[18];
414419
}
415420

416421
if (this.game.renderType === Phaser.WEBGL)
@@ -436,33 +441,27 @@ Phaser.Sprite.prototype.setFrame = function(frame) {
436441
this._cache[12] = frame.height;
437442
this._cache[13] = frame.spriteSourceSizeX;
438443
this._cache[14] = frame.spriteSourceSizeY;
444+
this._cache[17] = frame.sourceSizeW;
445+
this._cache[18] = frame.sourceSizeH;
439446

440447
this.texture.frame.x = frame.x;
441448
this.texture.frame.y = frame.y;
442449
this.texture.frame.width = frame.width;
443450
this.texture.frame.height = frame.height;
444451

445-
// frame x/y/w/h = the exact rect to copy out of the image
446-
// spriteSourceSize w/h always = frame.w/h but spriteSourceSize.x/y = where it should be placed on rendering (offset from top left of sourceSize)
447-
// sourceSize = the actual frame size width and height
448-
449-
// "filename": "razor_rain_vertical_10",
450-
// "frame": {"x":367,"y":656,"w":50,"h":28},
451-
// "rotated": false,
452-
// "trimmed": true,
453-
// "spriteSourceSize": {"x":62,"y":0,"w":50,"h":28},
454-
// "sourceSize": {"w":174,"h":360}
455-
456-
// 152 var actualSize = frameData[i].sourceSize;
457-
// 153: var realSize = frameData[i].spriteSourceSize;
458-
// 154
459-
// 155: texture.trim = new PIXI.Rectangle(realSize.x, realSize.y, actualSize.w, actualSize.h);
460-
461-
462452
if (frame.trimmed)
463453
{
464-
// this.texture.trim = { x: frame.spriteSourceSizeX, y: frame.spriteSourceSizeY, width: frame.width, height: frame.height };
465-
this.texture.trim = { x: frame.spriteSourceSizeX, y: frame.spriteSourceSizeY, width: frame.sourceSizeW, height: frame.sourceSizeH };
454+
if (this.texture.trim)
455+
{
456+
this.texture.trim.x = frame.spriteSourceSizeX;
457+
this.texture.trim.y = frame.spriteSourceSizeY;
458+
this.texture.trim.width = frame.sourceSizeW;
459+
this.texture.trim.height = frame.sourceSizeH;
460+
}
461+
else
462+
{
463+
this.texture.trim = { x: frame.spriteSourceSizeX, y: frame.spriteSourceSizeY, width: frame.sourceSizeW, height: frame.sourceSizeH };
464+
}
466465
}
467466

468467
if (this.game.renderType === Phaser.WEBGL)
@@ -493,6 +492,111 @@ Phaser.Sprite.prototype.updateCrop = function() {
493492

494493
if (this.texture.trim)
495494
{
495+
/*
496+
var frame = this.texture.frame;
497+
498+
var trim = texture.trim;
499+
500+
RENDER NON-TRIMMED FRAME, only difference is the x/y coords it renders at
501+
502+
context.drawImage(this.texture.baseTexture.source,
503+
frame.x,
504+
frame.y,
505+
frame.width,
506+
frame.height,
507+
(this.anchor.x) * -frame.width,
508+
(this.anchor.y) * -frame.height,
509+
frame.width,
510+
frame.height);
511+
512+
"filename": "razor_rain_vertical_23",
513+
"frame": {"x":175,"y":1444,"w":174,"h":360}, <- the actual area of the PNG to cut
514+
"trimmed": false,
515+
"spriteSourceSize": {"x":0,"y":0,"w":174,"h":360}, <- the offset applied to the Sprite x/y coords to draw the frame to
516+
"sourceSize": {"w":174,"h":360} <- the frame size
517+
518+
This frame will render at:
519+
520+
(this.anchor.x) * -frame.width
521+
0 * -174 = 0
522+
523+
Remember - the setTransform has already offset the render by the Sprite.x/y value inherited from the display list
524+
525+
RENDER A TRIMMED FRAME
526+
527+
context.drawImage(this.texture.baseTexture.source,
528+
frame.x,
529+
frame.y,
530+
frame.width,
531+
frame.height,
532+
trim.x - this.anchor.x * trim.width,
533+
trim.y - this.anchor.y * trim.height,
534+
frame.width,
535+
frame.height);
536+
537+
"filename": "razor_rain_vertical_23",
538+
"frame": {"x":445,"y":245,"w":94,"h":282}, <- the actual area of the PNG to cut
539+
"trimmed": true,
540+
"spriteSourceSize": {"x":45,"y":0,"w":94,"h":282}, <- the offset applied to the Sprite x/y coords to draw the frame to AND the frame width/height
541+
"sourceSize": {"w":174,"h":360} <- the frame size
542+
543+
This frame will render at:
544+
545+
trim.x - this.anchor.x * trim.width spriteSourceSize.x - this.anchor.x * sourceSize.w
546+
45 - 0 * 174 = 45
547+
548+
trim.y - this.anchor.y * trim.height spriteSourceSize.y - this.anchor.y * sourceSize.h
549+
0 - 0 * 360 = 0
550+
551+
worldTransform uses texture.frame.width / height to work out offset from x/y
552+
553+
*/
554+
555+
//
556+
557+
var tx = this._cache[9] - this._cache[13];
558+
var ty = this._cache[10] - this._cache[14];
559+
560+
var r1 = new Phaser.Rectangle(this._cache[13], this._cache[14], this._cache[17], this._cache[18]);
561+
var r2 = new Phaser.Rectangle(this.cropRect.x + tx, this.cropRect.y + ty, this.cropRect.width, this.cropRect.height);
562+
var area = Phaser.Rectangle.intersection(r1, r2);
563+
564+
// This will cut the cropped area instead of the whole frame
565+
566+
this.texture.frame.x = this._cache[9] + area.x;
567+
this.texture.frame.y = this._cache[10] + area.y;
568+
this.texture.frame.width = area.width;
569+
this.texture.frame.height = area.height;
570+
571+
this.texture.trim.width = area.width;
572+
this.texture.trim.height = area.height;
573+
574+
// But we still need to adjust the trim values
575+
576+
577+
578+
// this.texture.frame.x = this._cache[9] + this.cropRect.x;
579+
// this.texture.frame.y = this._cache[10] + this.cropRect.y;
580+
// this.texture.frame.width = this.cropRect.width;
581+
// this.texture.frame.height = this.cropRect.height;
582+
583+
// this.texture.trim.width = this.cropRect.width;
584+
// this.texture.trim.height = this.cropRect.height;
585+
586+
587+
// this._cache[9] = frame.x;
588+
// this._cache[10] = frame.y;
589+
// this._cache[11] = frame.width;
590+
// this._cache[12] = frame.height;
591+
// this._cache[13] = frame.spriteSourceSizeX;
592+
// this._cache[14] = frame.spriteSourceSizeY;
593+
// this._cache[15] = trim calc x
594+
// this._cache[16] = trim calc y
595+
// this._cache[17] = frame.sourceSizeW;
596+
// this._cache[18] = frame.sourceSizeH;
597+
598+
599+
/*
496600
this._cache[15] = this._cache[9] + this.cropRect.x - this._cache[13];
497601
498602
if (this._cache[15] < this._cache[9])
@@ -526,10 +630,12 @@ Phaser.Sprite.prototype.updateCrop = function() {
526630
this.texture.frame.width = this.cropRect.width - this._cache[15];
527631
this.texture.frame.height = this.cropRect.height - this._cache[16];
528632
529-
this.texture.trim.x = this._cache[15];
530-
this.texture.trim.y = this._cache[16];
633+
this.texture.trim.x = this._cache[13];
634+
this.texture.trim.y = this._cache[14];
635+
// Double-check we need this
531636
this.texture.trim.width = this.cropRect.width;
532637
this.texture.trim.height = this.cropRect.height;
638+
*/
533639
}
534640
else
535641
{
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
"src/physics/arcade/World.js",
3+
"src/physics/arcade/Body.js"
4+
]

tasks/manifests/phaser.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
"src/utils/Color.js",
9090

9191
"src/physics/Physics.js",
92-
9392
"src/physics/arcade/World.js",
9493
"src/physics/arcade/Body.js",
9594

0 commit comments

Comments
 (0)