Skip to content

Commit

Permalink
Merge pull request #1591 from larsiusprime/frameflip
Browse files Browse the repository at this point in the history
Add support for flipped FlxFrames
  • Loading branch information
Beeblerox committed Aug 31, 2015
2 parents 24920cb + 2209936 commit 426c9b6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
4 changes: 3 additions & 1 deletion flixel/FlxSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,9 @@ class FlxSprite extends FlxObject
{
if (_frame != null && dirty)
{
if (!flipX && !flipY && _frame.type == FlxFrameType.REGULAR)
var doFlipX = flipX != _frame.flipX;
var doFlipY = flipY != _frame.flipY;
if (!doFlipX && !doFlipY && _frame.type == FlxFrameType.REGULAR)
{
framePixels = _frame.paint(framePixels, _flashPointZero, false, true);
}
Expand Down
8 changes: 6 additions & 2 deletions flixel/graphics/frames/FlxAtlasFrames.hx
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ class FlxAtlasFrames extends FlxFramesCollection
var data:Fast = new haxe.xml.Fast(Xml.parse(Description).firstElement());

var angle:Int;
var flipX:Bool;
var flipY:Bool;
var name:String;
var trimmed:Bool;
var rotated:Bool;
Expand All @@ -255,7 +257,9 @@ class FlxAtlasFrames extends FlxFramesCollection
{
name = texture.att.name;
trimmed = texture.has.frameX;
rotated = (texture.has.rotated && texture.att.rotated == "true") ? true : false;
rotated = (texture.has.rotated && texture.att.rotated == "true");
flipX = (texture.has.flipX && texture.att.flipX == "true");
flipY = (texture.has.flipY && texture.att.flipY == "true");

rect = new FlxRect(Std.parseFloat(texture.att.x), Std.parseFloat(texture.att.y), Std.parseFloat(texture.att.width), Std.parseFloat(texture.att.height));

Expand All @@ -278,7 +282,7 @@ class FlxAtlasFrames extends FlxFramesCollection
sourceSize.set(size.height, size.width);
}

frames.addAtlasFrame(rect, sourceSize, offset, name, angle);
frames.addAtlasFrame(rect, sourceSize, offset, name, angle, flipX, flipY);
}

return frames;
Expand Down
22 changes: 18 additions & 4 deletions flixel/graphics/frames/FlxFrame.hx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class FlxFrame implements IFlxDestroyable
*/
public var angle:FlxFrameAngle;

public var flipX:Bool;
public var flipY:Bool;

/**
* Original (uncropped) image size.
*/
Expand All @@ -74,10 +77,13 @@ class FlxFrame implements IFlxDestroyable
private var blitMatrix:Vector<Float>;

@:allow(flixel)
private function new(parent:FlxGraphic, angle:FlxFrameAngle = FlxFrameAngle.ANGLE_0)
private function new(parent:FlxGraphic, angle:FlxFrameAngle = FlxFrameAngle.ANGLE_0, flipX:Bool = false, flipY:Bool = false)
{
this.parent = parent;
this.angle = angle;
this.flipX = flipX;
this.flipY = flipY;

type = FlxFrameType.REGULAR;

sourceSize = FlxPoint.get();
Expand Down Expand Up @@ -229,12 +235,15 @@ class FlxFrame implements IFlxDestroyable
mat.tx = tileMatrix[4];
mat.ty = tileMatrix[5];

if (rotation == FlxFrameAngle.ANGLE_0 && !flipX && !flipY)
var doFlipX = flipX != this.flipX;
var doFlipY = flipY != this.flipY;

if (rotation == FlxFrameAngle.ANGLE_0 && !doFlipX && !doFlipY)
{
return mat;
}

return rotateAndFlip(mat, rotation, flipX, flipY);
return rotateAndFlip(mat, rotation, doFlipX, doFlipY);
#end
}

Expand Down Expand Up @@ -324,8 +333,11 @@ class FlxFrame implements IFlxDestroyable
return bmd;
}

var doFlipX = flipX != this.flipX;
var doFlipY = flipY != this.flipY;

var matrix:FlxMatrix = FlxMatrix.matrix;
prepareTransformedBlitMatrix(matrix, rotation, flipX, flipY);
prepareTransformedBlitMatrix(matrix, rotation, doFlipX, doFlipY);
matrix.translate(point.x, point.y);
var rect:Rectangle = getDrawFrameRect(matrix);
bmd.draw(parent.bitmap, matrix, null, null, rect);
Expand Down Expand Up @@ -609,6 +621,8 @@ class FlxFrame implements IFlxDestroyable
}

clone.offset.copyFrom(offset);
clone.flipX = flipX;
clone.flipY = flipY;
clone.sourceSize.copyFrom(sourceSize);
clone.frame = FlxRect.get().copyFrom(frame);
clone.type = type;
Expand Down
6 changes: 4 additions & 2 deletions flixel/graphics/frames/FlxFramesCollection.hx
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,18 @@ class FlxFramesCollection implements IFlxDestroyable
* @param offset how frame region is located on original frame image (offset from top left corner of original image)
* @param name name for this frame (name of packed image file)
* @param angle rotation of packed image (can be 0, 90, -90).
* @param flipX if packed image should be horizontally flipped
* @param flipY if packed iamge should be vertically flipped
* @return Newly created and added frame object.
*/
public function addAtlasFrame(frame:FlxRect, sourceSize:FlxPoint, offset:FlxPoint, name:String = null, angle:FlxFrameAngle = 0):FlxFrame
public function addAtlasFrame(frame:FlxRect, sourceSize:FlxPoint, offset:FlxPoint, name:String = null, angle:FlxFrameAngle = 0, flipX:Bool=false, flipY:Bool=false):FlxFrame
{
if (name != null && framesHash.exists(name))
{
return framesHash.get(name);
}

var texFrame:FlxFrame = new FlxFrame(parent, angle);
var texFrame:FlxFrame = new FlxFrame(parent, angle, flipX, flipY);
texFrame.name = name;
texFrame.sourceSize.set(sourceSize.x, sourceSize.y);
texFrame.offset.set(offset.x, offset.y);
Expand Down

0 comments on commit 426c9b6

Please sign in to comment.