diff --git a/flixel/group/FlxSpriteGroup.hx b/flixel/group/FlxSpriteGroup.hx index a33d81d1f8..1639753183 100644 --- a/flixel/group/FlxSpriteGroup.hx +++ b/flixel/group/FlxSpriteGroup.hx @@ -10,6 +10,7 @@ import flixel.group.FlxGroup.FlxTypedGroup; import flixel.group.FlxGroup.FlxTypedGroupIterator; import flixel.math.FlxMath; import flixel.math.FlxPoint; +import flixel.math.FlxRect; import flixel.system.FlxAssets.FlxGraphicAsset; import flixel.util.FlxColor; import flixel.util.FlxDestroyUtil; @@ -280,6 +281,8 @@ class FlxTypedSpriteGroup extends FlxSprite sprite.alpha *= alpha; sprite.scrollFactor.copyFrom(scrollFactor); sprite.cameras = _cameras; // _cameras instead of cameras because get_cameras() will not return null + + if (clipRect != null) clipRectTransform(sprite, clipRect); } /** @@ -763,6 +766,13 @@ class FlxTypedSpriteGroup extends FlxSprite return blend = Value; } + override function set_clipRect(rect:FlxRect):FlxRect + { + if (exists) + transformChildren(clipRectTransform, rect); + return super.set_clipRect(rect); + } + override private function set_pixelPerfectRender(Value:Bool):Bool { if (exists && pixelPerfectRender != Value) @@ -879,6 +889,12 @@ class FlxTypedSpriteGroup extends FlxSprite private inline function originTransform(Sprite:FlxSprite, Origin:FlxPoint) Sprite.origin.copyFrom(Origin); private inline function scaleTransform(Sprite:FlxSprite, Scale:FlxPoint) Sprite.scale.copyFrom(Scale); private inline function scrollFactorTransform(Sprite:FlxSprite, ScrollFactor:FlxPoint) Sprite.scrollFactor.copyFrom(ScrollFactor); + + private inline function clipRectTransform(Sprite:FlxSprite, ClipRect:FlxRect) + { + if (ClipRect == null) Sprite.clipRect = null; + else Sprite.clipRect = FlxRect.get(ClipRect.x - Sprite.x + x, ClipRect.y - Sprite.y + y, ClipRect.width, ClipRect.height); + } // Functions for the FlxCallbackPoint private inline function offsetCallback(Offset:FlxPoint) transformChildren(offsetTransform, Offset); diff --git a/tests/unit/src/flixel/group/FlxSpriteGroupTest.hx b/tests/unit/src/flixel/group/FlxSpriteGroupTest.hx index 4a57da754f..4eb99267ce 100644 --- a/tests/unit/src/flixel/group/FlxSpriteGroupTest.hx +++ b/tests/unit/src/flixel/group/FlxSpriteGroupTest.hx @@ -1,6 +1,7 @@ package flixel.group; import flixel.FlxSprite; +import flixel.math.FlxRect; import massive.munit.Assert; class FlxSpriteGroupTest extends FlxTest @@ -51,4 +52,28 @@ class FlxSpriteGroupTest extends FlxTest Assert.areEqual(group.length, group.countLiving()); Assert.areEqual(0, group.countDead()); } + + @Test // 2051 + function testClipRect() + { + var rect = FlxRect.get(10, 10, 50, 50); + group.x = group.y = 50; + + var child = group.members[0]; + child.x = child.y = 100; + + group.clipRect = rect; + + Assert.isTrue(child.clipRect.equals(FlxRect.weak( -40, -40, 50, 50))); // child.clipRect should be set + + var group2 = new FlxSpriteGroup(); + group2.add(child); + + Assert.isTrue(child.clipRect.equals(FlxRect.weak( -40, -40, 50, 50))); // child.clipRect should not be overridden by null + + group2.x = group2.y = 50; // child gets offset to 150,150 + group2.clipRect = FlxRect.get(20, 20, 50, 50); + + Assert.isTrue(child.clipRect.equals(FlxRect.weak( -80, -80, 50, 50))); // child.clipRect should be overridden + } } \ No newline at end of file