Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FlxSprite.skew #3241

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion flixel/FlxSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ class FlxSprite extends FlxObject
*/
public var scale(default, null):FlxPoint;

/**
* An `FlxMatrix` that is concatenated to the internal matrix.
* Use this if you want to make unique changes to the rendering of `this` sprite.
* @since 5.9.0
*/
public var transform(default, null):FlxMatrix;

/**
* Blending modes, just like Photoshop or whatever, e.g. "multiply", "screen", etc.
*/
Expand Down Expand Up @@ -393,6 +400,7 @@ class FlxSprite extends FlxObject
offset = FlxPoint.get();
origin = FlxPoint.get();
scale = FlxPoint.get(1, 1);
transform = new FlxMatrix();
_halfSize = FlxPoint.get();
_matrix = new FlxMatrix();
colorTransform = new ColorTransform();
Expand All @@ -418,6 +426,7 @@ class FlxSprite extends FlxObject
offset = FlxDestroyUtil.put(offset);
origin = FlxDestroyUtil.put(origin);
scale = FlxDestroyUtil.put(scale);
transform = null;
_halfSize = FlxDestroyUtil.put(_halfSize);
_scaledOrigin = FlxDestroyUtil.put(_scaledOrigin);

Expand Down Expand Up @@ -856,6 +865,8 @@ class FlxSprite extends FlxObject
_matrix.rotateWithTrig(_cosAngle, _sinAngle);
}

_matrix.concat(transform);

getScreenPosition(_point, camera).subtractPoint(offset);
_point.add(origin.x, origin.y);
_matrix.translate(_point.x, _point.y);
Expand Down Expand Up @@ -1285,7 +1296,7 @@ class FlxSprite extends FlxObject
if (FlxG.renderTile)
return false;

return isSimpleRenderBlit(camera);
return isSimpleRenderBlit(camera) && transform.isIdentity();
}

/**
Expand Down Expand Up @@ -1704,6 +1715,7 @@ interface IFlxSprite extends IFlxBasic
var offset(default, null):FlxPoint;
var origin(default, null):FlxPoint;
var scale(default, null):FlxPoint;
var transform(default, null):FlxMatrix;
var velocity(default, null):FlxPoint;
var maxVelocity(default, null):FlxPoint;
var acceleration(default, null):FlxPoint;
Expand Down
31 changes: 31 additions & 0 deletions flixel/math/FlxMatrix.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,37 @@ import openfl.geom.Matrix;
*/
class FlxMatrix extends Matrix
{
public inline function isIdentity():Bool
{
return a == 1 && b == 0 && c == 0 && d == 1 && tx == 0 && ty == 0;
}

/**
* Skews `this` matrix, in radians.
* @param skewX Horizontal skew in radians.
* @param skewY Vertical skew in radians.
* @return `this` skewed matrix.
*/
public inline function skewRadians(skewX:Float, skewY:Float):FlxMatrix
{
b = Math.tan(skewY);

c = Math.tan(skewX);

return this;
}

/**
* Skews `this` matrix, in degrees.
* @param skewY Horizontal skew in degrees.
* @param skewX Vertical skew in degrees.
* @return `this` skewed matrix.
*/
public inline function skewDegrees(skewX:Float, skewY:Float):FlxMatrix
{
return skewRadians(skewY * FlxAngle.TO_RAD, skewX * FlxAngle.TO_RAD);
}

/**
* Rotates this matrix, but takes the values of sine and cosine,
* so it might be useful when you rotate multiple matrices by the same angle
Expand Down
2 changes: 2 additions & 0 deletions flixel/text/FlxText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,8 @@ class FlxText extends FlxSprite
if (angle != 0)
_matrix.rotateWithTrig(_cosAngle, _sinAngle);
}

_matrix.concat(transform);

// same as super but checks _graphicOffset
getScreenPosition(_point, camera).subtractPoint(offset).subtractPoint(_graphicOffset);
Expand Down
Loading