Skip to content

Commit

Permalink
FlxMouseEventManager: check objects for camera overlap (HaxeFlixel#1964)
Browse files Browse the repository at this point in the history
* FlxMouseEventManager: check camera for overlap before checking object.

* Update FlxMouseEventManager.hx

* Update FlxPointer.hx

* Update FlxPointer.hx
  • Loading branch information
bendmorris authored and Aurel300 committed Apr 17, 2018
1 parent f420957 commit 76eda07
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
9 changes: 9 additions & 0 deletions flixel/FlxCamera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,15 @@ class FlxCamera extends FlxBasic
updateFlashOffset();
setScale(scaleX, scaleY);
}

/**
* Checks whether this camera contains a given point or rectangle, in
* screen coordinates.
*/
public inline function containsPoint(point:FlxPoint, width:Float = 0, height:Float = 0):Bool
{
return (point.x + width > 0) && (point.x < this.width) && (point.y + height > 0) && (point.y < this.height);
}

private function set_followLerp(Value:Float):Float
{
Expand Down
2 changes: 1 addition & 1 deletion flixel/FlxObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ class FlxObject extends FlxBasic
Camera = FlxG.camera;
}
getScreenPosition(_point, Camera);
return (_point.x + width > 0) && (_point.x < Camera.width) && (_point.y + height > 0) && (_point.y < Camera.height);
return Camera.containsPoint(_point, width, height);
}

/**
Expand Down
23 changes: 22 additions & 1 deletion flixel/input/FlxPointer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ class FlxPointer
return point;
}

/**
* Fetch the screen position of the pointer relative to given camera's viewport.
*
* @param Camera If unspecified, first/main global camera is used instead.
* @param point An existing point object to store the results (if you don't want a new one created).
* @return The touch point's location relative to camera's viewport.
*/
public function getPositionInCameraView(?Camera:FlxCamera, ?point:FlxPoint):FlxPoint
{
if (Camera == null)
Camera = FlxG.camera;

if (point == null)
point = FlxPoint.get();

point.x = (_globalScreenX - Camera.x) / Camera.zoom;
point.y = (_globalScreenY - Camera.y) / Camera.zoom;

return point;
}

/**
* Returns a FlxPoint with this input's x and y.
*/
Expand Down Expand Up @@ -147,4 +168,4 @@ class FlxPointer
x = Std.int(_cachedPoint.x);
y = Std.int(_cachedPoint.y);
}
}
}
26 changes: 17 additions & 9 deletions flixel/input/mouse/FlxMouseEventManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class FlxMouseEventManager extends FlxBasic
}

/**
* Removes all registerd objects from the registry.
* Removes all registered objects from the registry.
*/
public static function removeAll():Void
{
Expand Down Expand Up @@ -443,22 +443,30 @@ class FlxMouseEventManager extends FlxBasic
for (camera in Register.object.cameras)
{
#if FLX_MOUSE
_point = FlxG.mouse.getWorldPosition(camera, _point);

if (checkOverlapWithPoint(Register, _point, camera))
_point = FlxG.mouse.getPositionInCameraView(camera, _point);
if (camera.containsPoint(_point))
{
return true;
_point = FlxG.mouse.getWorldPosition(camera, _point);

if (checkOverlapWithPoint(Register, _point, camera))
{
return true;
}
}
#end

#if FLX_TOUCH
for (touch in FlxG.touches.list)
{
_point = touch.getWorldPosition(camera, _point);

if (checkOverlapWithPoint(Register, _point, camera))
_point = touch.getPositionInCameraView(camera, _point);
if (camera.containsPoint(_point))
{
return true;
_point = touch.getWorldPosition(camera, _point);

if (checkOverlapWithPoint(Register, _point, camera))
{
return true;
}
}
}
#end
Expand Down

0 comments on commit 76eda07

Please sign in to comment.