diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index d718fe1a89..ae2542ae4d 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -275,7 +275,14 @@ class FlxSprite extends FlxObject * Set to `null` to discard graphic frame clipping. */ public var clipRect(default, set):FlxRect; - + + /** + * If true, clipRect will behave as it did in flixel 4 and earlier. + * Where the clipRect will scale up proportionally with the sprite. + * @since 5.0.0 + */ + public var clipRectIgnoreScale(default, set):Bool = false; + /** * GLSL shader for this sprite. Only works with OpenFL Next or WebGL. * Avoid changing it frequently as this is a costly operation. @@ -1403,7 +1410,29 @@ class FlxSprite extends FlxObject if (clipRect != null) { - _frame = frame.clipTo(clipRect, _frame); + if (clipRectIgnoreScale) + _frame = frame.clipTo(clipRect, _frame); + else + { + //translate clipRect's world coorinates to graphical cooridinates + var rect = FlxRect.get(); + var point = FlxPoint.get(); + + point.set(x + clipRect.x, y + clipRect.y); + transformClipRectPoint(point); + rect.x = point.x; + rect.y = point.y; + + point.set(x + clipRect.right, y + clipRect.bottom); + transformClipRectPoint(point); + rect.right = point.x; + rect.bottom = point.y; + + _frame = frame.clipTo(rect, _frame); + + point.put(); + rect.put(); + } } else { @@ -1412,6 +1441,20 @@ class FlxSprite extends FlxObject return frame; } + + /** + * Copied from transformWorldToPixelsSimple with angle and offset ignored. + */ + function transformClipRectPoint(worldPoint:FlxPoint) + { + worldPoint.subtract(x, y); + // result.addPoint(offset); + worldPoint.subtractPoint(origin); + worldPoint.scale(1 / scale.x, 1 / scale.y); + // can't clip an angled rect. + // result.degrees -= angle; + worldPoint.addPoint(origin); + } @:noCompletion function set_facing(Direction:FlxDirectionFlags):FlxDirectionFlags @@ -1517,6 +1560,12 @@ class FlxSprite extends FlxObject return rect; } + @:noCompletion + function set_clipRectIgnoreScale(value:Bool):Bool + { + return this.clipRectIgnoreScale = value; + } + /** * Frames setter. Used by `loadGraphic` methods, but you can load generated frames yourself * (this should be even faster since engine doesn't need to do bunch of additional stuff). diff --git a/flixel/group/FlxSpriteGroup.hx b/flixel/group/FlxSpriteGroup.hx index 2b83d0eb94..8a5800e34e 100644 --- a/flixel/group/FlxSpriteGroup.hx +++ b/flixel/group/FlxSpriteGroup.hx @@ -796,6 +796,14 @@ class FlxTypedSpriteGroup extends FlxSprite transformChildren(clipRectTransform, rect); return super.set_clipRect(rect); } + + + override function set_clipRectIgnoreScale(value:Bool):Bool + { + if (exists && clipRectIgnoreScale != value) + transformChildren(clipRectIgnoreScaleTransform, value); + return super.set_clipRectIgnoreScale(value); + } override function set_pixelPerfectRender(Value:Bool):Bool { @@ -1010,6 +1018,9 @@ class FlxTypedSpriteGroup extends FlxSprite inline function flipXTransform(Sprite:FlxSprite, FlipX:Bool) Sprite.flipX = FlipX; + inline function clipRectIgnoreScaleTransform(sprite:FlxSprite, value:Bool) + sprite.clipRectIgnoreScale = value; + inline function flipYTransform(Sprite:FlxSprite, FlipY:Bool) Sprite.flipY = FlipY;