Skip to content

Commit 3f4decd

Browse files
committed
First pass at adding rotated atlas frame support in to the Canvas renderer.
1 parent 6fd15ee commit 3f4decd

5 files changed

Lines changed: 84 additions & 43 deletions

File tree

src/animation/AnimationParser.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ Phaser.AnimationParser = {
138138
frames[i].spriteSourceSize.h
139139
);
140140
}
141+
142+
if (frames[i].rotated)
143+
{
144+
newFrame.rotated = true;
145+
}
141146
}
142147

143148
return data;
@@ -251,6 +256,11 @@ Phaser.AnimationParser = {
251256
);
252257
}
253258

259+
if (frames[key].rotated)
260+
{
261+
newFrame.rotated = true;
262+
}
263+
254264
i++;
255265
}
256266

src/animation/Frame.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Phaser.Frame = function (index, x, y, width, height, name) {
6464
this.distance = Phaser.Math.distance(0, 0, width, height);
6565

6666
/**
67-
* @property {boolean} rotated - Rotated? (not yet implemented)
67+
* @property {boolean} rotated - Is the frame rotated in the source texture?
6868
* @default
6969
*/
7070
this.rotated = false;

src/gameobjects/components/LoadTexture.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ Phaser.Component.LoadTexture.prototype = {
190190
this.texture.trim = null;
191191
}
192192

193+
if (frame.rotated)
194+
{
195+
this.texture.rotated = true;
196+
}
197+
193198
if (this.cropRect)
194199
{
195200
this.updateCrop();

src/pixi/display/Sprite.js

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -448,62 +448,86 @@ PIXI.Sprite.prototype._renderCanvas = function(renderSession, matrix)
448448
}
449449

450450
// Ignore null sources
451-
if (this.texture.valid)
451+
if (!this.texture.valid)
452452
{
453-
var resolution = this.texture.baseTexture.resolution / renderSession.resolution;
454-
455-
renderSession.context.globalAlpha = this.worldAlpha;
453+
// Update the children and leave
454+
for (var i = 0; i < this.children.length; i++)
455+
{
456+
this.children[i]._renderCanvas(renderSession);
457+
}
456458

457-
// If smoothingEnabled is supported and we need to change the smoothing property for this texture
458-
if (renderSession.smoothProperty && renderSession.scaleMode !== this.texture.baseTexture.scaleMode)
459+
if (this._mask)
459460
{
460-
renderSession.scaleMode = this.texture.baseTexture.scaleMode;
461-
renderSession.context[renderSession.smoothProperty] = (renderSession.scaleMode === PIXI.scaleModes.LINEAR);
461+
renderSession.maskManager.popMask(renderSession);
462462
}
463463

464-
// If the texture is trimmed we offset by the trim x/y, otherwise we use the frame dimensions
465-
var dx = (this.texture.trim) ? this.texture.trim.x - this.anchor.x * this.texture.trim.width : this.anchor.x * -this.texture.frame.width;
466-
var dy = (this.texture.trim) ? this.texture.trim.y - this.anchor.y * this.texture.trim.height : this.anchor.y * -this.texture.frame.height;
464+
return;
465+
}
467466

468-
var tx = (wt.tx * renderSession.resolution) + renderSession.shakeX;
469-
var ty = (wt.ty * renderSession.resolution) + renderSession.shakeY;
467+
var resolution = this.texture.baseTexture.resolution / renderSession.resolution;
470468

471-
// Allow for pixel rounding
472-
if (renderSession.roundPixels)
473-
{
474-
renderSession.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx | 0, ty | 0);
475-
dx |= 0;
476-
dy |= 0;
477-
}
478-
else
479-
{
480-
renderSession.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx, ty);
481-
}
469+
renderSession.context.globalAlpha = this.worldAlpha;
482470

483-
var cw = this.texture.crop.width;
484-
var ch = this.texture.crop.height;
471+
// If smoothingEnabled is supported and we need to change the smoothing property for this texture
472+
if (renderSession.smoothProperty && renderSession.scaleMode !== this.texture.baseTexture.scaleMode)
473+
{
474+
renderSession.scaleMode = this.texture.baseTexture.scaleMode;
475+
renderSession.context[renderSession.smoothProperty] = (renderSession.scaleMode === PIXI.scaleModes.LINEAR);
476+
}
485477

486-
dx /= resolution;
487-
dy /= resolution;
478+
// If the texture is trimmed we offset by the trim x/y, otherwise we use the frame dimensions
479+
var dx = (this.texture.trim) ? this.texture.trim.x - this.anchor.x * this.texture.trim.width : this.anchor.x * -this.texture.frame.width;
480+
var dy = (this.texture.trim) ? this.texture.trim.y - this.anchor.y * this.texture.trim.height : this.anchor.y * -this.texture.frame.height;
488481

489-
if (this.tint !== 0xFFFFFF)
490-
{
491-
if (this.texture.requiresReTint || this.cachedTint !== this.tint)
492-
{
493-
this.tintedTexture = PIXI.CanvasTinter.getTintedTexture(this, this.tint);
482+
var tx = (wt.tx * renderSession.resolution) + renderSession.shakeX;
483+
var ty = (wt.ty * renderSession.resolution) + renderSession.shakeY;
494484

495-
this.cachedTint = this.tint;
496-
this.texture.requiresReTint = false;
497-
}
485+
// Allow for pixel rounding
486+
if (renderSession.roundPixels)
487+
{
488+
renderSession.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx | 0, ty | 0);
489+
dx |= 0;
490+
dy |= 0;
491+
}
492+
else
493+
{
494+
renderSession.context.setTransform(wt.a, wt.b, wt.c, wt.d, tx, ty);
495+
}
498496

499-
renderSession.context.drawImage(this.tintedTexture, 0, 0, cw, ch, dx, dy, cw / resolution, ch / resolution);
500-
}
501-
else
497+
var cw = this.texture.crop.width;
498+
var ch = this.texture.crop.height;
499+
500+
if (this.texture.rotated)
501+
{
502+
// 90 degree coordinate rotation
503+
cw = ch;
504+
ch = this.texture.crop.width;
505+
506+
renderSession.context.translate(0, cw);
507+
renderSession.context.rotate(-1.5707963267948966);
508+
}
509+
510+
dx /= resolution;
511+
dy /= resolution;
512+
513+
if (this.tint !== 0xFFFFFF)
514+
{
515+
if (this.texture.requiresReTint || this.cachedTint !== this.tint)
502516
{
503-
var cx = this.texture.crop.x;
504-
var cy = this.texture.crop.y;
505-
renderSession.context.drawImage(this.texture.baseTexture.source, cx, cy, cw, ch, dx, dy, cw / resolution, ch / resolution);
517+
this.tintedTexture = PIXI.CanvasTinter.getTintedTexture(this, this.tint);
518+
519+
this.cachedTint = this.tint;
520+
this.texture.requiresReTint = false;
506521
}
522+
523+
renderSession.context.drawImage(this.tintedTexture, 0, 0, cw, ch, dx, dy, cw / resolution, ch / resolution);
524+
}
525+
else
526+
{
527+
var cx = this.texture.crop.x;
528+
var cy = this.texture.crop.y;
529+
530+
renderSession.context.drawImage(this.texture.baseTexture.source, cx, cy, cw, ch, dx, dy, cw / resolution, ch / resolution);
507531
}
508532

509533
for (var i = 0; i < this.children.length; i++)

src/pixi/textures/Texture.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ PIXI.Texture = function(baseTexture, frame, crop, trim)
134134
*/
135135
this.crop = crop || new PIXI.Rectangle(0, 0, 1, 1);
136136

137+
this.rotated = false;
138+
137139
if (baseTexture.hasLoaded)
138140
{
139141
if (this.noFrame) frame = new PIXI.Rectangle(0, 0, baseTexture.width, baseTexture.height);

0 commit comments

Comments
 (0)