From 0fa96505d6d763564aec5280540444704940b26f Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 24 Sep 2022 12:20:28 -0500 Subject: [PATCH 01/19] getPixelAt and getPixelAtScreen --- flixel/FlxSprite.hx | 64 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 0178ecf3e6..3d271e8dc7 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -988,29 +988,73 @@ class FlxSprite extends FlxObject } /** - * Checks to see if a point in 2D world space overlaps this `FlxSprite` object's current displayed pixels. - * This check is ALWAYS made in screen space, and always takes `scrollFactor` into account. + * Checks to see if a point in 2D world space overlaps this `FlxSprite` object's current + * displayed pixels. This check is ALWAYS made in screen space, and always takes `scrollFactor` + * into account. * * @param point The point in world space you want to check. * @param mask Used in the pixel hit test to determine what counts as solid. - * @param camera Specify which game camera you want. If `null`, it will just grab the first global camera. + * @param camera Specify which game camera you want. If `null`, it will just grab the + * first global camera. * @return Whether or not the point overlaps this object. */ public function pixelsOverlapPoint(point:FlxPoint, mask:Int = 0xFF, ?camera:FlxCamera):Bool { - transformWorldToPixels(point, camera, _point); - + var pixelColor = getPixelAt(point); + + if (pixelColor != null) + return pixelColor.alpha * alpha >= mask; + // point is outside of the graphic + return false; + } + + /** + * Determines which of this sprite's pixels are at the specified world coordinate, if any. + * + * @param worldPoint The point in world space + * @param camera Specify which game camera you want. If `null`, it will just grab the + * first global camera. + * @return a `FlxColor`, if the point is in the sprite's graphic, otherwise `null` is returned. + * @since 5.0.0 + */ + public function getPixelAt(worldPoint:FlxPoint, ?camera:FlxCamera):Null + { + transformWorldToPixels(worldPoint, camera, _point); + + // point is inside the graphic if (_point.x >= 0 && _point.x <= frameWidth && _point.y >= 0 && _point.y <= frameHeight) { var frameData:BitmapData = updateFramePixels(); - var pixelColor:FlxColor = frameData.getPixel32(Std.int(_point.x), Std.int(_point.y)); - return pixelColor.alpha * alpha >= mask; + return frameData.getPixel32(Std.int(_point.x), Std.int(_point.y)); } - - return false; + + return null; } - + + /** + * Determines which of this sprite's pixels are at the specified screen coordinate, if any. + * + * @param screenPoint The point in screen space + * @param camera Specify which game camera you want. If `null`, it will just grab the + * first global camera. + * @return a `FlxColor`, if the point is in the sprite's graphic, otherwise `null` is returned. + * @since 5.0.0 + */ + public function getPixelAtScreen(screenPoint:FlxPoint, ?camera:FlxCamera):Null + { + transformWorldToPixels(screenPoint, camera, _point); + + // point is inside the graphic + if (_point.x >= 0 && _point.x <= frameWidth && _point.y >= 0 && _point.y <= frameHeight) + { + var frameData:BitmapData = updateFramePixels(); + return frameData.getPixel32(Std.int(_point.x), Std.int(_point.y)); + } + + return null; + } + /** * Converts the point from world coordinates to this sprite's pixel coordinates where (0,0) * is the top left of the graphic. From 0b0cb6c4150eaf13c9fad285fc2f397222cbc3ea Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 24 Sep 2022 12:22:51 -0500 Subject: [PATCH 02/19] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50549a92b0..16fcbe7ab0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ ([#2566](https://github.com/HaxeFlixel/flixel/pull/2566)) ([#2584](https://github.com/HaxeFlixel/flixel/pull/2584)) - `FlxAnimation` - added `loopPoint` to allow looping to a frame other than the starting frame ([#2621](https://github.com/HaxeFlixel/flixel/pull/2621)) +- `FlxSprite` - added `getPixelAt` and `getPixelAtScreen` ([#2640](https://github.com/HaxeFlixel/flixel/pull/2640)) #### Bugfixes: From d6303005251c9f3f2ca376ac9544059145fd1527 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 30 Sep 2022 15:41:24 -0500 Subject: [PATCH 03/19] optional args --- flixel/FlxSprite.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 3d271e8dc7..aae91eda3f 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -1063,7 +1063,7 @@ class FlxSprite extends FlxObject * @param camera The camera, used to compare screen coordinates * @param result Optional arg for the returning point */ - function transformWorldToPixels(point:FlxPoint, camera:FlxCamera, result:FlxPoint):FlxPoint + function transformWorldToPixels(point:FlxPoint, ?camera:FlxCamera, result:FlxPoint):FlxPoint { if (camera == null) camera = FlxG.camera; @@ -1081,7 +1081,7 @@ class FlxSprite extends FlxObject * @param camera The camera * @param result Optional arg for the returning point */ - function transformScreenToPixels(point:FlxPoint, camera:FlxCamera, result:FlxPoint):FlxPoint + function transformScreenToPixels(point:FlxPoint, ?camera:FlxCamera, result:FlxPoint):FlxPoint { if (camera == null) camera = FlxG.camera; From c7649dad3947f04e92a89d47608da18b73732490 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 30 Sep 2022 22:28:27 -0500 Subject: [PATCH 04/19] convert cliprect to world coordinates --- flixel/FlxSprite.hx | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index aae91eda3f..ad4442b571 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -269,7 +269,11 @@ class FlxSprite extends FlxObject * Set to `null` to discard graphic frame clipping. */ public var clipRect(default, set):FlxRect; - + public var clipRectWorldCoords = true; + + @:noCompletion + var _clipRect:FlxRect = FlxRect.get(); + /** * GLSL shader for this sprite. Only works with OpenFL Next or WebGL. * Avoid changing it frequently as this is a costly operation. @@ -1376,7 +1380,31 @@ class FlxSprite extends FlxObject if (clipRect != null) { - _frame = frame.clipTo(clipRect, _frame); + if (clipRectWorldCoords) + { + var world = FlxPoint.get(); + var local = FlxPoint.get(); + + world.set(x + clipRect.x, y + clipRect.y); + transformWorldToPixels(world, local);//TODO: make new util that ignores camera + _clipRect.x = local.x; + _clipRect.y = local.y; + _clipRect.setPosition(local.x, local.y); + + world.set(x + clipRect.right, y + clipRect.bottom); + transformWorldToPixels(world, local); + _clipRect.right = local.x; + _clipRect.bottom = local.y; + + world.put(); + local.put(); + } + else + { + _clipRect.copyFrom(clipRect); + } + + _frame = frame.clipTo(_clipRect, _frame); } else { From 544781ab4f55e8b611b70c1589cb2da1188c25a0 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 30 Sep 2022 22:44:52 -0500 Subject: [PATCH 05/19] docs and readibility --- flixel/FlxObject.hx | 37 +++++++++++++++++++++---------------- flixel/FlxSprite.hx | 17 +++++++---------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/flixel/FlxObject.hx b/flixel/FlxObject.hx index a261cd9c74..968b8d17fb 100644 --- a/flixel/FlxObject.hx +++ b/flixel/FlxObject.hx @@ -1069,34 +1069,39 @@ class FlxObject extends FlxBasic } /** - * Call this function to figure out the on-screen position of the object. + * Returns the screen position of this object. * - * @param point Takes a `FlxPoint` object and assigns the post-scrolled X and Y values of this object to it. - * @param camera Specify which game camera you want. - * If `null`, it will just grab the first global camera. - * @return The Point you passed in, or a new Point if you didn't pass one, - * containing the screen X and Y position of this object. + * @param result Optional arg for the returning point + * @param camera The desired "screen" coordinate space. If `null`, `FlxG.camera` is used. + * @return The screen position of this object. */ - public function getScreenPosition(?point:FlxPoint, ?camera:FlxCamera):FlxPoint + public function getScreenPosition(?result:FlxPoint, ?camera:FlxCamera):FlxPoint { - if (point == null) - point = FlxPoint.get(); + if (result == null) + result = FlxPoint.get(); if (camera == null) camera = FlxG.camera; - point.set(x, y); + result.set(x, y); if (pixelPerfectPosition) - point.floor(); + result.floor(); - return point.subtract(camera.scroll.x * scrollFactor.x, camera.scroll.y * scrollFactor.y); + return result.subtract(camera.scroll.x * scrollFactor.x, camera.scroll.y * scrollFactor.y); } - public function getPosition(?point:FlxPoint):FlxPoint + /** + * Returns the world position of this object. + * + * @param result Optional arg for the returning point. + * @return The world position of this object. + */ + public function getPosition(?result:FlxPoint):FlxPoint { - if (point == null) - point = FlxPoint.get(); - return point.set(x, y); + if (result == null) + result = FlxPoint.get(); + + return result.set(x, y); } /** diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index aae91eda3f..e57c102a29 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -1063,7 +1063,7 @@ class FlxSprite extends FlxObject * @param camera The camera, used to compare screen coordinates * @param result Optional arg for the returning point */ - function transformWorldToPixels(point:FlxPoint, ?camera:FlxCamera, result:FlxPoint):FlxPoint + function transformWorldToPixels(point:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint { if (camera == null) camera = FlxG.camera; @@ -1081,16 +1081,13 @@ class FlxSprite extends FlxObject * @param camera The camera * @param result Optional arg for the returning point */ - function transformScreenToPixels(point:FlxPoint, ?camera:FlxCamera, result:FlxPoint):FlxPoint + function transformScreenToPixels(point:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint { if (camera == null) camera = FlxG.camera; - - result = getScreenPosition(result, camera); - - FlxG.watch.addQuick("mouse", point); - FlxG.watch.addQuick("card", result); - + + getScreenPosition(result, camera); + result.subtract(point.x, point.y); result.negate(); result.addPoint(offset); @@ -1098,9 +1095,9 @@ class FlxSprite extends FlxObject result.scale(1 / scale.x, 1 / scale.y); result.degrees -= angle; result.addPoint(origin); - + point.putWeak(); - + return result; } From d1dbcba618f3c36367aad0c99baa55ab0da55e6c Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 30 Sep 2022 22:48:02 -0500 Subject: [PATCH 06/19] remove duplicate null camera setters --- flixel/FlxSprite.hx | 6 ------ 1 file changed, 6 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index e57c102a29..d668ed67b2 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -1065,9 +1065,6 @@ class FlxSprite extends FlxObject */ function transformWorldToPixels(point:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint { - if (camera == null) - camera = FlxG.camera; - var screenPoint = FlxPoint.weak(point.x - camera.scroll.x, point.y - camera.scroll.y); point.putWeak(); return transformScreenToPixels(screenPoint, camera, result); @@ -1083,9 +1080,6 @@ class FlxSprite extends FlxObject */ function transformScreenToPixels(point:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint { - if (camera == null) - camera = FlxG.camera; - getScreenPosition(result, camera); result.subtract(point.x, point.y); From 5330f631265cbc950c2f3543257549de1629cb01 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 30 Sep 2022 22:53:39 -0500 Subject: [PATCH 07/19] add back camera setter --- flixel/FlxSprite.hx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index d668ed67b2..e81f73dd6c 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -1065,6 +1065,9 @@ class FlxSprite extends FlxObject */ function transformWorldToPixels(point:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint { + if (camera == null) + camera = FlxG.camera; + var screenPoint = FlxPoint.weak(point.x - camera.scroll.x, point.y - camera.scroll.y); point.putWeak(); return transformScreenToPixels(screenPoint, camera, result); From 07318dbb96d85e17d1be28e0a49f708675f8ffd3 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 30 Sep 2022 22:56:27 -0500 Subject: [PATCH 08/19] more docs --- flixel/FlxSprite.hx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index e81f73dd6c..ad84664647 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -1059,8 +1059,8 @@ class FlxSprite extends FlxObject * Converts the point from world coordinates to this sprite's pixel coordinates where (0,0) * is the top left of the graphic. * - * @param point The world coordinates - * @param camera The camera, used to compare screen coordinates + * @param point The world coordinates. + * @param camera The camera, used for `scrollFactor`. If `null`, `FlxG.camera` is used. * @param result Optional arg for the returning point */ function transformWorldToPixels(point:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint @@ -1078,7 +1078,7 @@ class FlxSprite extends FlxObject * is the top left of the graphic. * * @param point The screen coordinates - * @param camera The camera + * @param camera The desired "screen" coordinate space. If `null`, `FlxG.camera` is used. * @param result Optional arg for the returning point */ function transformScreenToPixels(point:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint From be4dcaa81d96d9a794854ee55440ded5fa3d7edf Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 30 Sep 2022 23:52:26 -0500 Subject: [PATCH 09/19] finalize and make public --- flixel/FlxSprite.hx | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index ad84664647..e31a85a0b5 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -1063,15 +1063,40 @@ class FlxSprite extends FlxObject * @param camera The camera, used for `scrollFactor`. If `null`, `FlxG.camera` is used. * @param result Optional arg for the returning point */ - function transformWorldToPixels(point:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint + public function transformWorldToPixels(worldPoint:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint { if (camera == null) camera = FlxG.camera; - var screenPoint = FlxPoint.weak(point.x - camera.scroll.x, point.y - camera.scroll.y); - point.putWeak(); + var screenPoint = FlxPoint.weak(worldPoint.x - camera.scroll.x, worldPoint.y - camera.scroll.y); + worldPoint.putWeak(); return transformScreenToPixels(screenPoint, camera, result); } + + /** + * Converts the point from world coordinates to this sprite's pixel coordinates where (0,0) + * is the top left of the graphic. Same as `worldToPixels` but never uses a camera, + * therefore `scrollFactor` is ignored + * + * @param point The world coordinates. + * @param result Optional arg for the returning point + */ + public function transformWorldToPixelsSimple(worldPoint:FlxPoint, ?result:FlxPoint):FlxPoint + { + result = getPosition(result); + + result.subtract(worldPoint.x, worldPoint.y); + result.negate(); + result.addPoint(offset); + result.subtractPoint(origin); + result.scale(1 / scale.x, 1 / scale.y); + result.degrees -= angle; + result.addPoint(origin); + + worldPoint.putWeak(); + + return result; + } /** * Converts the point from screen coordinates to this sprite's pixel coordinates where (0,0) @@ -1081,11 +1106,11 @@ class FlxSprite extends FlxObject * @param camera The desired "screen" coordinate space. If `null`, `FlxG.camera` is used. * @param result Optional arg for the returning point */ - function transformScreenToPixels(point:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint + public function transformScreenToPixels(screenPoint:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint { - getScreenPosition(result, camera); + result = getScreenPosition(result, camera); - result.subtract(point.x, point.y); + result.subtract(screenPoint.x, screenPoint.y); result.negate(); result.addPoint(offset); result.subtractPoint(origin); @@ -1093,7 +1118,7 @@ class FlxSprite extends FlxObject result.degrees -= angle; result.addPoint(origin); - point.putWeak(); + screenPoint.putWeak(); return result; } From 6f61024a176114590ce0d4e12faa82b7bf9841b8 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 1 Oct 2022 00:05:28 -0500 Subject: [PATCH 10/19] ignore angle with clipRect --- flixel/FlxSprite.hx | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 983255ac9a..a58f216aeb 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -1405,13 +1405,13 @@ class FlxSprite extends FlxObject var local = FlxPoint.get(); world.set(x + clipRect.x, y + clipRect.y); - transformWorldToPixels(world, local);//TODO: make new util that ignores camera + transformClipRectPoint(world, local); _clipRect.x = local.x; _clipRect.y = local.y; _clipRect.setPosition(local.x, local.y); world.set(x + clipRect.right, y + clipRect.bottom); - transformWorldToPixels(world, local); + transformClipRectPoint(world, local); _clipRect.right = local.x; _clipRect.bottom = local.y; @@ -1432,6 +1432,26 @@ class FlxSprite extends FlxObject return frame; } + + /** + * Copied from transformWorldToPixelsSimple with angle removed + */ + function transformClipRectPoint(worldPoint:FlxPoint, ?result:FlxPoint) + { + result = getPosition(result); + + result.subtract(worldPoint.x, worldPoint.y); + result.negate(); + result.addPoint(offset); + result.subtractPoint(origin); + result.scale(1 / scale.x, 1 / scale.y); + // result.degrees -= angle; + result.addPoint(origin); + + worldPoint.putWeak(); + + return result; + } @:noCompletion function set_facing(Direction:FlxDirectionFlags):FlxDirectionFlags From 09769bac9997b622453087e1029bcd76bda81a16 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 1 Oct 2022 00:23:52 -0500 Subject: [PATCH 11/19] finalize var name, remove cache var --- flixel/FlxSprite.hx | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index a58f216aeb..7fdc160082 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -269,10 +269,13 @@ class FlxSprite extends FlxObject * Set to `null` to discard graphic frame clipping. */ public var clipRect(default, set):FlxRect; - public var clipRectWorldCoords = true; - @:noCompletion - var _clipRect:FlxRect = FlxRect.get(); + /** + * 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 legacyClipRectMode = false; /** * GLSL shader for this sprite. Only works with OpenFL Next or WebGL. @@ -1399,31 +1402,31 @@ class FlxSprite extends FlxObject if (clipRect != null) { - if (clipRectWorldCoords) + if (legacyClipRectMode) + _frame = frame.clipTo(clipRect, _frame); + else { + var rect = FlxRect.get(); var world = FlxPoint.get(); var local = FlxPoint.get(); world.set(x + clipRect.x, y + clipRect.y); transformClipRectPoint(world, local); - _clipRect.x = local.x; - _clipRect.y = local.y; - _clipRect.setPosition(local.x, local.y); + rect.x = local.x; + rect.y = local.y; + rect.setPosition(local.x, local.y); world.set(x + clipRect.right, y + clipRect.bottom); transformClipRectPoint(world, local); - _clipRect.right = local.x; - _clipRect.bottom = local.y; + rect.right = local.x; + rect.bottom = local.y; + + _frame = frame.clipTo(rect, _frame); world.put(); local.put(); + rect.put(); } - else - { - _clipRect.copyFrom(clipRect); - } - - _frame = frame.clipTo(_clipRect, _frame); } else { From b615f48f58323e5639b7a547384527510a382899 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 1 Oct 2022 00:32:16 -0500 Subject: [PATCH 12/19] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50514037f7..718d03c5ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,7 @@ ([#2584](https://github.com/HaxeFlixel/flixel/pull/2584)) - `FlxAnimation` - added `loopPoint` to allow looping to a frame other than the starting frame ([#2621](https://github.com/HaxeFlixel/flixel/pull/2621)) - `FlxSound` - added `pitch` to alter the playback speed ([#2564](https://github.com/HaxeFlixel/flixel/pull/2564)) -- `FlxSprite` - added `getPixelAt` and `getPixelAtScreen` ([#2640](https://github.com/HaxeFlixel/flixel/pull/2640)) +- `FlxSprite` - added `getPixelAt`, `getPixelAtScreen`, `transformWorldToPixels` and `transformScreenToPixels` ([#2640](https://github.com/HaxeFlixel/flixel/pull/2640)) #### Bugfixes: From be5c517324e3ffbc2aa8eb5a8ca2860244051162 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 1 Oct 2022 00:37:35 -0500 Subject: [PATCH 13/19] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50514037f7..718d03c5ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,7 @@ ([#2584](https://github.com/HaxeFlixel/flixel/pull/2584)) - `FlxAnimation` - added `loopPoint` to allow looping to a frame other than the starting frame ([#2621](https://github.com/HaxeFlixel/flixel/pull/2621)) - `FlxSound` - added `pitch` to alter the playback speed ([#2564](https://github.com/HaxeFlixel/flixel/pull/2564)) -- `FlxSprite` - added `getPixelAt` and `getPixelAtScreen` ([#2640](https://github.com/HaxeFlixel/flixel/pull/2640)) +- `FlxSprite` - added `getPixelAt`, `getPixelAtScreen`, `transformWorldToPixels` and `transformScreenToPixels` ([#2640](https://github.com/HaxeFlixel/flixel/pull/2640)) #### Bugfixes: From 03e738eaba417d9cc10b1b8c8a281861d0781225 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 1 Oct 2022 01:16:28 -0500 Subject: [PATCH 14/19] more docs --- flixel/FlxSprite.hx | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index e31a85a0b5..3c48e7e517 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -988,14 +988,13 @@ class FlxSprite extends FlxObject } /** - * Checks to see if a point in 2D world space overlaps this `FlxSprite` object's current - * displayed pixels. This check is ALWAYS made in screen space, and always takes `scrollFactor` - * into account. + * Checks to see if a point in 2D world space overlaps this `FlxSprite` object's + * current displayed pixels. This check is ALWAYS made in screen space, and + * factors in `scale`, `angle`, `offset`, `origin`, and `scrollFactor`. * * @param point The point in world space you want to check. * @param mask Used in the pixel hit test to determine what counts as solid. - * @param camera Specify which game camera you want. If `null`, it will just grab the - * first global camera. + * @param camera The desired "screen" coordinate space. If `null`, `FlxG.camera` is used. * @return Whether or not the point overlaps this object. */ public function pixelsOverlapPoint(point:FlxPoint, mask:Int = 0xFF, ?camera:FlxCamera):Bool @@ -1011,10 +1010,10 @@ class FlxSprite extends FlxObject /** * Determines which of this sprite's pixels are at the specified world coordinate, if any. + * Factors in `scale`, `angle`, `offset`, `origin`, and `scrollFactor`. * * @param worldPoint The point in world space - * @param camera Specify which game camera you want. If `null`, it will just grab the - * first global camera. + * @param camera The camera, used for `scrollFactor`. If `null`, `FlxG.camera` is used. * @return a `FlxColor`, if the point is in the sprite's graphic, otherwise `null` is returned. * @since 5.0.0 */ @@ -1034,10 +1033,10 @@ class FlxSprite extends FlxObject /** * Determines which of this sprite's pixels are at the specified screen coordinate, if any. + * Factors in `scale`, `angle`, `offset`, `origin`, and `scrollFactor`. * * @param screenPoint The point in screen space - * @param camera Specify which game camera you want. If `null`, it will just grab the - * first global camera. + * @param camera The desired "screen" coordinate space. If `null`, `FlxG.camera` is used. * @return a `FlxColor`, if the point is in the sprite's graphic, otherwise `null` is returned. * @since 5.0.0 */ @@ -1058,6 +1057,7 @@ class FlxSprite extends FlxObject /** * Converts the point from world coordinates to this sprite's pixel coordinates where (0,0) * is the top left of the graphic. + * Factors in `scale`, `angle`, `offset`, `origin`, and `scrollFactor`. * * @param point The world coordinates. * @param camera The camera, used for `scrollFactor`. If `null`, `FlxG.camera` is used. @@ -1101,6 +1101,7 @@ class FlxSprite extends FlxObject /** * Converts the point from screen coordinates to this sprite's pixel coordinates where (0,0) * is the top left of the graphic. + * Factors in `scale`, `angle`, `offset`, `origin`, and `scrollFactor`. * * @param point The screen coordinates * @param camera The desired "screen" coordinate space. If `null`, `FlxG.camera` is used. @@ -1202,7 +1203,7 @@ class FlxSprite extends FlxObject * Check and see if this object is currently on screen. Differs from `FlxObject`'s implementation * in that it takes the actual graphic into account, not just the hitbox or bounding box or whatever. * - * @param Camera Specify which game camera you want. If `null`, it will just grab the first global camera. + * @param Camera Specify which game camera you want. If `null`, `FlxG.camera` is used. * @return Whether the object is on screen or not. */ override public function isOnScreen(?camera:FlxCamera):Bool From adc47b877ee584e228bf30ad822171132920bbc9 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 1 Oct 2022 01:21:02 -0500 Subject: [PATCH 15/19] MORE DOCS! --- flixel/FlxSprite.hx | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 3c48e7e517..62a4b89f28 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -992,14 +992,14 @@ class FlxSprite extends FlxObject * current displayed pixels. This check is ALWAYS made in screen space, and * factors in `scale`, `angle`, `offset`, `origin`, and `scrollFactor`. * - * @param point The point in world space you want to check. - * @param mask Used in the pixel hit test to determine what counts as solid. - * @param camera The desired "screen" coordinate space. If `null`, `FlxG.camera` is used. + * @param worldPoint point in world space you want to check. + * @param mask Used in the pixel hit test to determine what counts as solid. + * @param camera The desired "screen" coordinate space. If `null`, `FlxG.camera` is used. * @return Whether or not the point overlaps this object. */ - public function pixelsOverlapPoint(point:FlxPoint, mask:Int = 0xFF, ?camera:FlxCamera):Bool + public function pixelsOverlapPoint(worldPoint:FlxPoint, mask:Int = 0xFF, ?camera:FlxCamera):Bool { - var pixelColor = getPixelAt(point); + var pixelColor = getPixelAt(worldPoint); if (pixelColor != null) return pixelColor.alpha * alpha >= mask; @@ -1059,9 +1059,9 @@ class FlxSprite extends FlxObject * is the top left of the graphic. * Factors in `scale`, `angle`, `offset`, `origin`, and `scrollFactor`. * - * @param point The world coordinates. - * @param camera The camera, used for `scrollFactor`. If `null`, `FlxG.camera` is used. - * @param result Optional arg for the returning point + * @param worldPoint The world coordinates. + * @param camera The camera, used for `scrollFactor`. If `null`, `FlxG.camera` is used. + * @param result Optional arg for the returning point */ public function transformWorldToPixels(worldPoint:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint { @@ -1078,8 +1078,8 @@ class FlxSprite extends FlxObject * is the top left of the graphic. Same as `worldToPixels` but never uses a camera, * therefore `scrollFactor` is ignored * - * @param point The world coordinates. - * @param result Optional arg for the returning point + * @param worldPoint The world coordinates. + * @param result Optional arg for the returning point */ public function transformWorldToPixelsSimple(worldPoint:FlxPoint, ?result:FlxPoint):FlxPoint { @@ -1103,9 +1103,9 @@ class FlxSprite extends FlxObject * is the top left of the graphic. * Factors in `scale`, `angle`, `offset`, `origin`, and `scrollFactor`. * - * @param point The screen coordinates - * @param camera The desired "screen" coordinate space. If `null`, `FlxG.camera` is used. - * @param result Optional arg for the returning point + * @param screenPoint The screen coordinates + * @param camera The desired "screen" coordinate space. If `null`, `FlxG.camera` is used. + * @param result Optional arg for the returning point */ public function transformScreenToPixels(screenPoint:FlxPoint, ?camera:FlxCamera, ?result:FlxPoint):FlxPoint { From c1db3a06e580b3c0d27e11d28c486eacbced50e8 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 1 Oct 2022 01:26:37 -0500 Subject: [PATCH 16/19] fix error --- flixel/FlxSprite.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 62a4b89f28..014814d937 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -1042,7 +1042,7 @@ class FlxSprite extends FlxObject */ public function getPixelAtScreen(screenPoint:FlxPoint, ?camera:FlxCamera):Null { - transformWorldToPixels(screenPoint, camera, _point); + transformScreenToPixels(screenPoint, camera, _point); // point is inside the graphic if (_point.x >= 0 && _point.x <= frameWidth && _point.y >= 0 && _point.y <= frameHeight) From 926acf8486d48d327fb794d71d33f2f7c0fdabde Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 1 Oct 2022 11:56:00 -0500 Subject: [PATCH 17/19] docs --- flixel/FlxSprite.hx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index e4dfe5456c..5854713755 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -1407,6 +1407,7 @@ class FlxSprite extends FlxObject _frame = frame.clipTo(clipRect, _frame); else { + //translate clipRect's world coorinates to graphical cooridinates var rect = FlxRect.get(); var world = FlxPoint.get(); var local = FlxPoint.get(); @@ -1438,7 +1439,8 @@ class FlxSprite extends FlxObject } /** - * Copied from transformWorldToPixelsSimple with angle removed + * Copied from transformWorldToPixelsSimple with angle ignored, + * because we can't clip an angled rect. */ function transformClipRectPoint(worldPoint:FlxPoint, ?result:FlxPoint) { From 28426151fdcf59fd562c490aaaaf59fdbe3c4c66 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sun, 2 Oct 2022 14:55:50 -0500 Subject: [PATCH 18/19] rename and simplify --- flixel/FlxSprite.hx | 50 ++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 5854713755..6a256766d0 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -275,7 +275,7 @@ class FlxSprite extends FlxObject * Where the clipRect will scale up proportionally with the sprite. * @since 5.0.0 */ - public var legacyClipRectMode = false; + public var clipRectIgnoreScale = false; /** * GLSL shader for this sprite. Only works with OpenFL Next or WebGL. @@ -1403,30 +1403,27 @@ class FlxSprite extends FlxObject if (clipRect != null) { - if (legacyClipRectMode) + if (clipRectIgnoreScale) _frame = frame.clipTo(clipRect, _frame); else { //translate clipRect's world coorinates to graphical cooridinates var rect = FlxRect.get(); - var world = FlxPoint.get(); - var local = FlxPoint.get(); + var point = FlxPoint.get(); - world.set(x + clipRect.x, y + clipRect.y); - transformClipRectPoint(world, local); - rect.x = local.x; - rect.y = local.y; - rect.setPosition(local.x, local.y); + point.set(x + clipRect.x, y + clipRect.y); + transformClipRectPoint(point); + rect.x = point.x; + rect.y = point.y; - world.set(x + clipRect.right, y + clipRect.bottom); - transformClipRectPoint(world, local); - rect.right = local.x; - rect.bottom = local.y; + point.set(x + clipRect.right, y + clipRect.bottom); + transformClipRectPoint(point); + rect.right = point.x; + rect.bottom = point.y; _frame = frame.clipTo(rect, _frame); - world.put(); - local.put(); + point.put(); rect.put(); } } @@ -1439,24 +1436,17 @@ class FlxSprite extends FlxObject } /** - * Copied from transformWorldToPixelsSimple with angle ignored, - * because we can't clip an angled rect. + * Copied from transformWorldToPixelsSimple with angle and offset ignored. */ - function transformClipRectPoint(worldPoint:FlxPoint, ?result:FlxPoint) + function transformClipRectPoint(worldPoint:FlxPoint) { - result = getPosition(result); - - result.subtract(worldPoint.x, worldPoint.y); - result.negate(); - result.addPoint(offset); - result.subtractPoint(origin); - result.scale(1 / scale.x, 1 / scale.y); + 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; - result.addPoint(origin); - - worldPoint.putWeak(); - - return result; + worldPoint.addPoint(origin); } @:noCompletion From 7ce2689e97bad5692e85ad1a9e4b75c21e43c0f8 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 8 Oct 2022 09:31:12 -0500 Subject: [PATCH 19/19] add setter --- flixel/FlxSprite.hx | 8 +++++++- flixel/group/FlxSpriteGroup.hx | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/flixel/FlxSprite.hx b/flixel/FlxSprite.hx index 6a256766d0..1d95de86a8 100644 --- a/flixel/FlxSprite.hx +++ b/flixel/FlxSprite.hx @@ -275,7 +275,7 @@ class FlxSprite extends FlxObject * Where the clipRect will scale up proportionally with the sprite. * @since 5.0.0 */ - public var clipRectIgnoreScale = false; + public var clipRectIgnoreScale(default, set):Bool = false; /** * GLSL shader for this sprite. Only works with OpenFL Next or WebGL. @@ -1553,6 +1553,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). diff --git a/flixel/group/FlxSpriteGroup.hx b/flixel/group/FlxSpriteGroup.hx index c0965d03db..cf39c2661b 100644 --- a/flixel/group/FlxSpriteGroup.hx +++ b/flixel/group/FlxSpriteGroup.hx @@ -796,6 +796,14 @@ class FlxTypedSpriteGroup 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 { @@ -918,6 +926,9 @@ class FlxTypedSpriteGroup 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;