Julian-Vos / asset-library-MapCamera2D-4

https://github.com/Julian-Vos/godot-scripts/tree/master
MIT License
10 stars 2 forks source link

Add an option to enforce remaining in the camera limits #4

Closed gturri closed 1 month ago

gturri commented 2 months ago

First of all, thanks for open sourcing that code. I just spent a couple hours trying to code my own Camera2D and then I found out your script which does much more cool stuff that what i was aiming for.

This pull request adds one feature that I was also looking for, and I think it would be a cool addition to your code.

Context

Godot Camera2D node has the attributes limit_left, limit_right, limit_top and limit_bottom. Those attributes can be configured to make sure that Godot won't move the camera out of it. But if I have a tiny scene and if I reduce the zoom a lot, I may reach a point where my whole scene becomes smaller than what the Camera2D displays, which means that the Camera2D shows some "void". (or, it's also possible to use those limit to display only a small part of the scene at first and to change those limit once the player performed some actions. In this case if the zoom becomes too small then the player can already see some of the not-yet-unlocked area)

What this commit does

This commit adds an option that prevent this behavior.

By default this new option is deactivated (so this commit keeps the existing behavior). And when this option is activated it ensures zoom the zoom cannot be smaller than this computed min_zoom.

gturri commented 2 months ago

Here is a screenshot to make it clearer: here my scene is the small island in the upper right corner and when I zoom out too much then I can see stuff outside of this scene (ie: all the area that appear in grey)

When this new option is enabled, it's not possible to zoom out too much anymore.

(sure, the zoom_min option you already added could be used to prevent this, but having code that can compute it for us dynamically prevents us from having to compute it ourselves)

Screenshot PR-camera

Julian-Vos commented 2 months ago

Hello, thank you for the great suggestion and your pull request! I'll be only holiday soon, so I'll have a better look afterwards. I had thought about it, but I use macOS natural scrolling which requires an inverse zoom factor, min and max (edit: I was incorrect, it only requires an inverse factor). That's an edge case though, and in your solution it's optional anyway. Alternatives are a function for computing min zoom that would have to be called by the user, or enforcing it but in the setter itself. Still, your solution is probably the best one.

Julian-Vos commented 1 month ago

Hey, I've just pushed a commit that implements this, Asset Library v1.2.0 pending approval! It's based on your PR, but it enables the exported property zoom_limited by default, and it's a bit more involved: zooming stops exactly at the limits (in your PR you could go over them slightly) and is updated automatically on viewport size change :)

gturri commented 1 month ago

Great, thanks! 👍