HaxeFlixel/flixel-addons

[Question] Why is FlxGridOverlay restricted to Int dimensions?

neillrobson opened this issue · 3 comments

I recently ran into a situation where I needed a grid sprite with gridlines that lined up exactly with the sprite edges on all four sides. (I knew in advance that I would need fractional cell dimensions, and that the grid lines would become blurry in certain cases.)

I thought that FlxGridOverlay could serve that purpose, but unfortunately I found that I could not pass in float values to the utility.

Curious, I glanced at the source code, and could not find any reason why this restriction was in place. The CellWidth and CellHeight arguments are used here, but the Rectangle constructor can (I believe) take float arguments.

I can easily re-implement the functionality I need outside of this library, but I am still interested to hear about the reasoning behind this decision. Thanks!

I doubt there's any particular reason for this. But who knows, this code is quite old and ported from AS3 (Flixel Power Tools).

Ultimately, the Rectangle object is being passed to openfl.BitmapData.fillRect, where it will ignore the float representation of any values and calculate the fill on an integer basis. So even though the Rectangle can take a float, it's driving a fundamental integer operation underneath. It's admittedly weird for the API to use a float-based rectangle for an integer operation, but that's what we inherited here from the Flash API.

If you want to draw grid lines with fuzzy floating-point based edges, you'll need a different method.

Does that help explain it?

Yep! That explanation definitely makes sense. I found that simply copying the method out of FlxGridOverlay and changing the argument types to Float worked for my purposes. (Admittedly, I wasn't concerned about fuzzy or precise edges: I just wanted the ability to pass in decimal values and have the underlying API figure out the best solution.)

I'll close the issue for now. Hopefully it helps anyone else coming along with a similar curiosity.