Skip to content

Commit

Permalink
cliprect scale
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesisfeline authored Feb 18, 2024
2 parents 40a25c8 + efa0706 commit d052e47
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
53 changes: 51 additions & 2 deletions flixel/FlxSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,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. Avoid changing it frequently as this is a costly operation.
* @since 4.1.0
Expand Down Expand Up @@ -1418,7 +1425,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 @@ -1427,6 +1456,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 @@ -1533,6 +1576,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 @@ -818,6 +818,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 @@ -1040,6 +1048,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

0 comments on commit d052e47

Please sign in to comment.