Skip to content

Commit

Permalink
account for scale, origin, offset and pixelPerfectPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
Geokureli committed Apr 23, 2024
1 parent 4178602 commit 87ce045
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
11 changes: 10 additions & 1 deletion flixel/FlxSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,16 @@ class FlxSprite extends FlxObject
{
if (point == null)
point = FlxPoint.get();
return point.set(x + frameWidth * 0.5 * scale.x, y + frameHeight * 0.5 * scale.y);

point.set(x, y);
if (pixelPerfectPosition)
point.floor();

_scaledOrigin.set(origin.x * scale.x, origin.y * scale.y);
point.x += origin.x - offset.x - _scaledOrigin.x + frameWidth * 0.5 * scale.x;
point.y += origin.y - offset.y - _scaledOrigin.y + frameHeight * 0.5 * scale.y;

return point;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/src/flixel/FlxSpriteTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import openfl.display.BitmapData;
import flixel.animation.FlxAnimation;
import flixel.graphics.atlas.FlxAtlas;
import flixel.math.FlxRect;
import flixel.math.FlxPoint;
import flixel.text.FlxText;
import flixel.util.FlxColor;
import massive.munit.Assert;
Expand Down Expand Up @@ -328,4 +329,44 @@ class FlxSpriteTest extends FlxTest

expected.put();
}

@Test
function testGetGraphicMidpoint()
{
final full:SimplePoint = [sprite1.frameWidth, sprite1.frameHeight];
final mid:SimplePoint = [full.x / 2, full.y / 2];
final zero:SimplePoint = [0, 0];
assertGraphicMidpoint({ pos:[0, 5], size:full, origin:mid, offset:zero});
assertGraphicMidpoint({ pos:[0, 5], size:full, origin:full, offset:zero});
assertGraphicMidpoint({ pos:[0, 5], size:[10, 10], origin:mid, offset:zero});
assertGraphicMidpoint({ pos:[0, 5], size:[50, 50], origin:mid, offset:[1, 3]});
assertGraphicMidpoint({ pos:[0, 5], size:[50, 50], origin:zero, offset:zero});
assertGraphicMidpoint({ pos:[0, 5], size:[50, 50], origin:full, offset:[50, 60]});
assertGraphicMidpoint({ pos:[0, 5], size:[50, 100], origin:[10, 20], offset:[-50, 60]});
}

function assertGraphicMidpoint(orientation:Orientation, ?pos:PosInfos)
{
sprite1.x = orientation.pos.x;
sprite1.y = orientation.pos.y;
sprite1.setGraphicSize(orientation.size.x, orientation.size.y);
sprite1.offset.set(orientation.offset.x, orientation.offset.y);
sprite1.origin.set(orientation.origin.x, orientation.origin.y);
final actual = sprite1.getGraphicMidpoint(FlxPoint.weak());

// check against getScreenBounds
final rect = sprite1.getScreenBounds(FlxRect.weak());
FlxAssert.areNear(rect.x + 0.5 * rect.width, actual.x, 0.001, pos);
FlxAssert.areNear(rect.y + 0.5 * rect.height, actual.y, 0.001, pos);
}
}

abstract SimplePoint(Array<Float>) from Array<Float>
{
public var x(get, never):Float;
inline function get_x() return this[0];

public var y(get, never):Float;
inline function get_y() return this[1];
}
typedef Orientation = { pos:SimplePoint, size:SimplePoint, offset:SimplePoint, origin:SimplePoint }

0 comments on commit 87ce045

Please sign in to comment.