From 69efae8d181cb58895eab9c9b0b4d9834540e3d7 Mon Sep 17 00:00:00 2001 From: DetectiveBaldi <86160807+DetectiveBaldi@users.noreply.github.com> Date: Tue, 20 Aug 2024 18:19:44 -0400 Subject: [PATCH 1/5] Update FlxSprite.hx --- flixel/FlxSprite.hx | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 8fc653620a..58bef3d056 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -251,6 +251,8 @@ class FlxSprite extends FlxObject */ public var scale(default, null):FlxPoint; + public var skew(default, null):FlxPoint; + /** * Blending modes, just like Photoshop or whatever, e.g. "multiply", "screen", etc. */ @@ -343,6 +345,9 @@ class FlxSprite extends FlxObject @:noCompletion var _scaledOrigin:FlxPoint; + @:noCompletion + var _skewMatrix:FlxMatrix; + /** * These vars are being used for rendering in some of `FlxSprite` subclasses (`FlxTileblock`, `FlxBar`, * and `FlxBitmapText`) and for checks if the sprite is in camera's view. @@ -393,10 +398,12 @@ class FlxSprite extends FlxObject offset = FlxPoint.get(); origin = FlxPoint.get(); scale = FlxPoint.get(1, 1); + skew = FlxPoint.get(); _halfSize = FlxPoint.get(); _matrix = new FlxMatrix(); colorTransform = new ColorTransform(); _scaledOrigin = new FlxPoint(); + _skewMatrix = new FlxMatrix(); } /** @@ -418,8 +425,10 @@ class FlxSprite extends FlxObject offset = FlxDestroyUtil.put(offset); origin = FlxDestroyUtil.put(origin); scale = FlxDestroyUtil.put(scale); + skew = FlxDestroyUtil.put(skew); _halfSize = FlxDestroyUtil.put(_halfSize); _scaledOrigin = FlxDestroyUtil.put(_scaledOrigin); + _skewMatrix = null; framePixels = FlxDestroyUtil.dispose(framePixels); @@ -856,6 +865,17 @@ class FlxSprite extends FlxObject _matrix.rotateWithTrig(_cosAngle, _sinAngle); } + _skewMatrix.identity(); + + if (skew.x != 0.0 || skew.y != 0.0) + { + _skewMatrix.b = Math.tan(skew.y * FlxAngle.TO_RAD); + + _skewMatrix.c = Math.tan(skew.x * FlxAngle.TO_RAD); + } + + _matrix.concat(_skewMatrix); + getScreenPosition(_point, camera).subtractPoint(offset); _point.add(origin.x, origin.y); _matrix.translate(_point.x, _point.y); @@ -1285,7 +1305,7 @@ class FlxSprite extends FlxObject if (FlxG.renderTile) return false; - return isSimpleRenderBlit(camera); + return isSimpleRenderBlit(camera) && (skew.x == 0) && (skew.y == 0); } /** From 14bbec58927889e6cb18538164aab0dc34085172 Mon Sep 17 00:00:00 2001 From: DetectiveBaldi <86160807+DetectiveBaldi@users.noreply.github.com> Date: Wed, 21 Aug 2024 19:07:09 -0400 Subject: [PATCH 2/5] implement skew method to FlxMatrix --- flixel/FlxSprite.hx | 17 +++++------------ flixel/math/FlxMatrix.hx | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 58bef3d056..a9abe0db47 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -251,6 +251,9 @@ class FlxSprite extends FlxObject */ public var scale(default, null):FlxPoint; + /** + * Changes the skew transformation applied to `this` sprite, in degrees. + */ public var skew(default, null):FlxPoint; /** @@ -345,9 +348,6 @@ class FlxSprite extends FlxObject @:noCompletion var _scaledOrigin:FlxPoint; - @:noCompletion - var _skewMatrix:FlxMatrix; - /** * These vars are being used for rendering in some of `FlxSprite` subclasses (`FlxTileblock`, `FlxBar`, * and `FlxBitmapText`) and for checks if the sprite is in camera's view. @@ -403,7 +403,6 @@ class FlxSprite extends FlxObject _matrix = new FlxMatrix(); colorTransform = new ColorTransform(); _scaledOrigin = new FlxPoint(); - _skewMatrix = new FlxMatrix(); } /** @@ -428,7 +427,6 @@ class FlxSprite extends FlxObject skew = FlxDestroyUtil.put(skew); _halfSize = FlxDestroyUtil.put(_halfSize); _scaledOrigin = FlxDestroyUtil.put(_scaledOrigin); - _skewMatrix = null; framePixels = FlxDestroyUtil.dispose(framePixels); @@ -865,16 +863,10 @@ class FlxSprite extends FlxObject _matrix.rotateWithTrig(_cosAngle, _sinAngle); } - _skewMatrix.identity(); - if (skew.x != 0.0 || skew.y != 0.0) { - _skewMatrix.b = Math.tan(skew.y * FlxAngle.TO_RAD); - - _skewMatrix.c = Math.tan(skew.x * FlxAngle.TO_RAD); + _matrix.skew(skew.x * FlxAngle.TO_RAD, skew.y * FlxAngle.TO_RAD); } - - _matrix.concat(_skewMatrix); getScreenPosition(_point, camera).subtractPoint(offset); _point.add(origin.x, origin.y); @@ -1724,6 +1716,7 @@ interface IFlxSprite extends IFlxBasic var offset(default, null):FlxPoint; var origin(default, null):FlxPoint; var scale(default, null):FlxPoint; + var skew(default, null):FlxPoint; var velocity(default, null):FlxPoint; var maxVelocity(default, null):FlxPoint; var acceleration(default, null):FlxPoint; diff --git a/flixel/math/FlxMatrix.hx b/flixel/math/FlxMatrix.hx index 26c3b7c881..5cf49e7e4e 100644 --- a/flixel/math/FlxMatrix.hx +++ b/flixel/math/FlxMatrix.hx @@ -9,6 +9,29 @@ import openfl.geom.Matrix; */ class FlxMatrix extends Matrix { + /** + * Changes the skew transformation applied to `this` matrix, in radians. + * @param x Horizontal skew in radians. + * @param y Vertical skew in radians. + * @return `this` skewed matrix. + */ + public inline function skew(x:Float = 0.0, y:Float = 0.0):FlxMatrix + { + var a1:Float = a + b * x; + b = a * y + b; + a = a1; + + var c1:Float = c + d * x; + d = c * y + d; + c = c1; + + var tx1:Float = tx + ty * x; + ty = tx * y + ty; + tx = tx1; + + return this; + } + /** * 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 From 7f39b371fb7d2bf913b2f64d9e4cd48c2d4630f8 Mon Sep 17 00:00:00 2001 From: DetectiveBaldi <86160807+DetectiveBaldi@users.noreply.github.com> Date: Sat, 24 Aug 2024 08:56:47 -0400 Subject: [PATCH 3/5] skewing on text --- flixel/text/FlxText.hx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/flixel/text/FlxText.hx b/flixel/text/FlxText.hx index 3378887100..e81363968f 100644 --- a/flixel/text/FlxText.hx +++ b/flixel/text/FlxText.hx @@ -6,6 +6,7 @@ import flixel.graphics.FlxGraphic; import flixel.graphics.atlas.FlxAtlas; import flixel.graphics.atlas.FlxNode; import flixel.graphics.frames.FlxFramesCollection; +import flixel.math.FlxAngle; import flixel.math.FlxMath; import flixel.math.FlxPoint; import flixel.math.FlxRect; @@ -1056,6 +1057,11 @@ class FlxText extends FlxSprite if (angle != 0) _matrix.rotateWithTrig(_cosAngle, _sinAngle); } + + if (skew.x != 0.0 || skew.y != 0.0) + { + _matrix.skew(skew.x * FlxAngle.TO_RAD, skew.y * FlxAngle.TO_RAD); + } // same as super but checks _graphicOffset getScreenPosition(_point, camera).subtractPoint(offset).subtractPoint(_graphicOffset); From 6fcc973220e79c343de608c73d7f0a74253c11e8 Mon Sep 17 00:00:00 2001 From: DetectiveBaldi <86160807+DetectiveBaldi@users.noreply.github.com> Date: Mon, 26 Aug 2024 16:24:39 -0400 Subject: [PATCH 4/5] FlxSprite.transform + related FlxMatrix functions --- flixel/FlxSprite.hx | 19 +++++++++---------- flixel/math/FlxMatrix.hx | 40 ++++++++++++++++++++++++---------------- flixel/text/FlxText.hx | 6 +----- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index a9abe0db47..542c03145a 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -252,9 +252,11 @@ class FlxSprite extends FlxObject public var scale(default, null):FlxPoint; /** - * Changes the skew transformation applied to `this` sprite, in degrees. + * 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 skew(default, null):FlxPoint; + public var transform(default, null):FlxMatrix; /** * Blending modes, just like Photoshop or whatever, e.g. "multiply", "screen", etc. @@ -398,7 +400,7 @@ class FlxSprite extends FlxObject offset = FlxPoint.get(); origin = FlxPoint.get(); scale = FlxPoint.get(1, 1); - skew = FlxPoint.get(); + transform = new FlxMatrix(); _halfSize = FlxPoint.get(); _matrix = new FlxMatrix(); colorTransform = new ColorTransform(); @@ -424,7 +426,7 @@ class FlxSprite extends FlxObject offset = FlxDestroyUtil.put(offset); origin = FlxDestroyUtil.put(origin); scale = FlxDestroyUtil.put(scale); - skew = FlxDestroyUtil.put(skew); + transform = null; _halfSize = FlxDestroyUtil.put(_halfSize); _scaledOrigin = FlxDestroyUtil.put(_scaledOrigin); @@ -863,10 +865,7 @@ class FlxSprite extends FlxObject _matrix.rotateWithTrig(_cosAngle, _sinAngle); } - if (skew.x != 0.0 || skew.y != 0.0) - { - _matrix.skew(skew.x * FlxAngle.TO_RAD, skew.y * FlxAngle.TO_RAD); - } + _matrix.concat(transform); getScreenPosition(_point, camera).subtractPoint(offset); _point.add(origin.x, origin.y); @@ -1297,7 +1296,7 @@ class FlxSprite extends FlxObject if (FlxG.renderTile) return false; - return isSimpleRenderBlit(camera) && (skew.x == 0) && (skew.y == 0); + return isSimpleRenderBlit(camera) && transform.isIdentity(); } /** @@ -1716,7 +1715,7 @@ interface IFlxSprite extends IFlxBasic var offset(default, null):FlxPoint; var origin(default, null):FlxPoint; var scale(default, null):FlxPoint; - var skew(default, null):FlxPoint; + var transform(default, null):FlxMatrix; var velocity(default, null):FlxPoint; var maxVelocity(default, null):FlxPoint; var acceleration(default, null):FlxPoint; diff --git a/flixel/math/FlxMatrix.hx b/flixel/math/FlxMatrix.hx index 5cf49e7e4e..3970e4c2a7 100644 --- a/flixel/math/FlxMatrix.hx +++ b/flixel/math/FlxMatrix.hx @@ -9,29 +9,37 @@ import openfl.geom.Matrix; */ class FlxMatrix extends Matrix { + public inline function isIdentity():Bool + { + return equals(Matrix.__identity); + } + /** - * Changes the skew transformation applied to `this` matrix, in radians. - * @param x Horizontal skew in radians. - * @param y Vertical skew in radians. + * Skews `this` matrix, in radians. + * @param skewX Horizontal skew in radians. + * @param skewY Vertical skew in radians. * @return `this` skewed matrix. */ - public inline function skew(x:Float = 0.0, y:Float = 0.0):FlxMatrix + public inline function skewRadians(skewX:Float, skewY:Float):FlxMatrix { - var a1:Float = a + b * x; - b = a * y + b; - a = a1; - - var c1:Float = c + d * x; - d = c * y + d; - c = c1; - - var tx1:Float = tx + ty * x; - ty = tx * y + ty; - tx = tx1; - + 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 diff --git a/flixel/text/FlxText.hx b/flixel/text/FlxText.hx index e81363968f..7c63b8adf3 100644 --- a/flixel/text/FlxText.hx +++ b/flixel/text/FlxText.hx @@ -6,7 +6,6 @@ import flixel.graphics.FlxGraphic; import flixel.graphics.atlas.FlxAtlas; import flixel.graphics.atlas.FlxNode; import flixel.graphics.frames.FlxFramesCollection; -import flixel.math.FlxAngle; import flixel.math.FlxMath; import flixel.math.FlxPoint; import flixel.math.FlxRect; @@ -1058,10 +1057,7 @@ class FlxText extends FlxSprite _matrix.rotateWithTrig(_cosAngle, _sinAngle); } - if (skew.x != 0.0 || skew.y != 0.0) - { - _matrix.skew(skew.x * FlxAngle.TO_RAD, skew.y * FlxAngle.TO_RAD); - } + _matrix.concat(transform); // same as super but checks _graphicOffset getScreenPosition(_point, camera).subtractPoint(offset).subtractPoint(_graphicOffset); From 78e5d7a1741eb19be556e9af7c417fc3cde23b17 Mon Sep 17 00:00:00 2001 From: DetectiveBaldi <86160807+DetectiveBaldi@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:01:12 -0400 Subject: [PATCH 5/5] fix flash (?) --- flixel/math/FlxMatrix.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flixel/math/FlxMatrix.hx b/flixel/math/FlxMatrix.hx index 3970e4c2a7..c70d1f6798 100644 --- a/flixel/math/FlxMatrix.hx +++ b/flixel/math/FlxMatrix.hx @@ -11,7 +11,7 @@ class FlxMatrix extends Matrix { public inline function isIdentity():Bool { - return equals(Matrix.__identity); + return a == 1 && b == 0 && c == 0 && d == 1 && tx == 0 && ty == 0; } /**