Skip to content

Commit

Permalink
FlxObject: fix overlapsPoint() if overlap is at x or y = 0 (#1818)
Browse files Browse the repository at this point in the history
* The top and left edges of checked objects were not registering as overlapping, when they should be.  (Consider the example of a single pixel at 0,0: the former could would never register an overlap with the mouse, always looking for your position to be > 0 and < 1.  Even though it's a float, it still gets updated to integer values when you move the mouse.)

May seem trivial, but when you have a button at the edge of a full-screen game, it becomes important.

* Fix test by moving button out from under the cursor.

* Add unit test for #1818.  Also make the existing unit test remove from the state afterwards so we don't double-add, while leaving the existing tests independent of having been add()ed.
  • Loading branch information
IBwWG authored and Gama11 committed Apr 21, 2016
1 parent 5ea4826 commit 6395483
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions flixel/FlxObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ class FlxObject extends FlxBasic
{
if (!InScreenSpace)
{
return (point.x > x) && (point.x < x + width) && (point.y > y) && (point.y < y + height);
return (point.x >= x) && (point.x < x + width) && (point.y >= y) && (point.y < y + height);
}

if (Camera == null)
Expand All @@ -792,7 +792,7 @@ class FlxObject extends FlxBasic
var yPos:Float = point.y - Camera.scroll.y;
getScreenPosition(_point, Camera);
point.putWeak();
return (xPos > _point.x) && (xPos < _point.x + width) && (yPos > _point.y) && (yPos < _point.y + height);
return (xPos >= _point.x) && (xPos < _point.x + width) && (yPos >= _point.y) && (yPos < _point.y + height);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/src/flixel/ui/FlxButtonTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,23 @@ class FlxButtonTest extends FlxTest
{
setAndAssertText(null);
}

@Test // #1818
function testHighlightStatusInUpperLeftCorner()
{
FlxG.state.add(button);

button.setPosition();
step(1);
Assert.areEqual(FlxButton.HIGHLIGHT, button.status);

FlxG.state.remove(button);
}

@Test // #1365
function testTriggerAnimationOnce()
{
button.x = 1; // put it slightly to the right of the cursor so that it isn't highlighted by default
button.animation.add("normal", [for (i in 0...4) 0], 30, false);
FlxG.state.add(button);
step(2);
Expand All @@ -80,6 +93,7 @@ class FlxButtonTest extends FlxTest
step(10);
Assert.areEqual("normal", button.animation.curAnim.name);
Assert.areEqual(true, button.animation.finished);
FlxG.state.remove(button);
}

function setAndAssertText(text:String)
Expand Down

0 comments on commit 6395483

Please sign in to comment.