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

Clip rect scale #2643

Open
wants to merge 24 commits into
base: dev
Choose a base branch
from
Open
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
53 changes: 51 additions & 2 deletions flixel/FlxSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +279 to +284
Copy link
Member Author

Choose a reason for hiding this comment

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

TODO:

  • keep legacy behavior, make this an opt-in feature
  • come up with a better name maybe scaleClipRect or something


/**
* GLSL shader for this sprite. Only works with OpenFL Next or WebGL.
* Avoid changing it frequently as this is a costly operation.
Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -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).
Expand Down
11 changes: 11 additions & 0 deletions flixel/group/FlxSpriteGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,14 @@ class FlxTypedSpriteGroup<T:FlxSprite> 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
{
Expand Down Expand Up @@ -1010,6 +1018,9 @@ class FlxTypedSpriteGroup<T:FlxSprite> 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;

Expand Down