From 6bc2146987570b3a501fa47953d4ae6b0c32261b Mon Sep 17 00:00:00 2001 From: MSGhero Date: Sat, 15 Apr 2017 18:17:16 -0400 Subject: [PATCH 1/4] Clip rects 4 sprite groups --- flixel/group/FlxSpriteGroup.hx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/flixel/group/FlxSpriteGroup.hx b/flixel/group/FlxSpriteGroup.hx index a33d81d1f8..d8ebcfc8e7 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,9 @@ 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 +767,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 +890,7 @@ 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) 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); From 8f265a5baff416aefc1c2ebfe06e1829bc9e3a0e Mon Sep 17 00:00:00 2001 From: MSGhero Date: Tue, 18 Apr 2017 01:30:19 -0400 Subject: [PATCH 2/4] Null fix --- flixel/group/FlxSpriteGroup.hx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/flixel/group/FlxSpriteGroup.hx b/flixel/group/FlxSpriteGroup.hx index d8ebcfc8e7..5b2e6a8fd2 100644 --- a/flixel/group/FlxSpriteGroup.hx +++ b/flixel/group/FlxSpriteGroup.hx @@ -282,8 +282,8 @@ class FlxTypedSpriteGroup extends FlxSprite sprite.scrollFactor.copyFrom(scrollFactor); sprite.cameras = _cameras; // _cameras instead of cameras because get_cameras() will not return null - if (clipRect != null) - clipRectTransform(sprite, clipRect); + if (clipRect != null) clipRectTransform(sprite, clipRect); + else sprite.clipRect = null; } /** @@ -769,7 +769,7 @@ class FlxTypedSpriteGroup extends FlxSprite override function set_clipRect(rect:FlxRect):FlxRect { - if (exists) + if (exists) transformChildren(clipRectTransform, rect); return super.set_clipRect(rect); } @@ -890,7 +890,10 @@ 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) Sprite.clipRect = FlxRect.get(ClipRect.x - Sprite.x + x, ClipRect.y - Sprite.y + y, ClipRect.width, ClipRect.height); + 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); From 9800d98a6bd523201c90179cf79dafea0d3ad58c Mon Sep 17 00:00:00 2001 From: MSGhero Date: Wed, 26 Apr 2017 12:17:42 -0400 Subject: [PATCH 3/4] Fix formatting + remove unnecessary line --- flixel/group/FlxSpriteGroup.hx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/flixel/group/FlxSpriteGroup.hx b/flixel/group/FlxSpriteGroup.hx index 5b2e6a8fd2..1639753183 100644 --- a/flixel/group/FlxSpriteGroup.hx +++ b/flixel/group/FlxSpriteGroup.hx @@ -283,7 +283,6 @@ class FlxTypedSpriteGroup extends FlxSprite sprite.cameras = _cameras; // _cameras instead of cameras because get_cameras() will not return null if (clipRect != null) clipRectTransform(sprite, clipRect); - else sprite.clipRect = null; } /** @@ -890,7 +889,9 @@ 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) { + + 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); } From 99aa5652bdb52d0bbd2bc37bad137ab1300b47f5 Mon Sep 17 00:00:00 2001 From: MSGhero Date: Wed, 26 Apr 2017 17:05:47 -0400 Subject: [PATCH 4/4] Unit tests for FSG clipRect --- .../src/flixel/group/FlxSpriteGroupTest.hx | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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