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

FlxSpriteGroup clipRect support #2051

Merged
merged 4 commits into from
Apr 27, 2017
Merged
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
16 changes: 16 additions & 0 deletions flixel/group/FlxSpriteGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -280,6 +281,8 @@ class FlxTypedSpriteGroup<T:FlxSprite> 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This null check now seems unnecessary, since clipRectTransform() can handle null?

Copy link
Member Author

@MSGhero MSGhero Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that's some leftover code from when I was testing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no, that would override the child's existing clip. The transform sets the child's clip based on the parent's, so you'd be setting null instead of leaving the existing one alone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're right!

Great thing to unit-test. ;)

}

/**
Expand Down Expand Up @@ -763,6 +766,13 @@ class FlxTypedSpriteGroup<T:FlxSprite> 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)
Expand Down Expand Up @@ -879,6 +889,12 @@ class FlxTypedSpriteGroup<T:FlxSprite> 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);
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/src/flixel/group/FlxSpriteGroupTest.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flixel.group;

import flixel.FlxSprite;
import flixel.math.FlxRect;
import massive.munit.Assert;

class FlxSpriteGroupTest extends FlxTest
Expand Down Expand Up @@ -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
}
}