Skip to content
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
29 changes: 21 additions & 8 deletions flixel/graphics/frames/FlxFrame.hx
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,8 @@ class FlxFrame implements IFlxDestroyable
cacheFrameMatrix();
}

updateUV();

frameRect.put();
return this;
}
Expand Down Expand Up @@ -717,15 +719,21 @@ class FlxFrame implements IFlxDestroyable

function set_frame(value:FlxRect):FlxRect
{
if (value != null)
{
if (uv == null)
uv = FlxUVRect.get();

uv.set(value.x / parent.width, value.y / parent.height, value.right / parent.width, value.bottom / parent.height);
}
frame = value;
updateUV();

return value;
}

function updateUV()
{
if (frame == null)
return;

if (uv == null)
uv = FlxUVRect.get();

return frame = value;
uv.setFromFrameRect(frame, parent);
}
}

Expand Down Expand Up @@ -779,6 +787,11 @@ abstract FlxUVRect(FlxRect) from FlxRect to flixel.util.FlxPool.IFlxPooled
this.set(l, t, r, b);
}

public inline function setFromFrameRect(frame:FlxRect, parent:FlxGraphic)
{
this.set(frame.x / parent.width, frame.y / parent.height, frame.right / parent.width, frame.bottom / parent.height);
}

public inline function copyTo(uv:FlxUVRect)
{
uv.set(left, top, right, bottom);
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/src/flixel/graphics/frames/FlxFrameTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,17 @@ class FlxFrameTest extends FlxTest
FlxAssert.areNear(uvRect.right, rect.width);
FlxAssert.areNear(uvRect.bottom, rect.height);
}


@Test
function testClipFrame()
{
final frame = createFrames("clip", 100, 100, 1, 1)[0];
FlxAssert.rectsNear(cast frame.uv, FlxRect.get(0, 0, 1, 1));

frame.clip(FlxRect.get(10, 10, 80, 80));
FlxAssert.rectsNear(cast frame.uv, FlxRect.get(0.1, 0.1, 0.1 + 0.8, 0.1 + 0.8));
}

function createFrames(name:String, width = 100, height = 100, cols = 10, rows = 10, buffer = 0):Array<FlxFrame>
{
final sprite = new FlxSprite(0, 0);
Expand All @@ -132,6 +142,7 @@ class FlxFrameTest extends FlxTest

return sprite.frames.frames;
}

function createFrame(name:String):FlxFrame
{
var frame = new FlxFrame(null);
Expand Down