Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Viewport method to get automatically computed 2D stretch transform #80965

Merged

Conversation

Calinou
Copy link
Member

@Calinou Calinou commented Aug 24, 2023

Viewport.get_stretch_transform() returns the automatically computed 2D stretch transform. Combined with Transform2D.get_scale(), this is useful when using the canvas_items stretch mode in a project.

There are many situations where knowing this factor is useful:

  • Divide Camera2D zoom to keep the size of the 2D game world identical regardless of the 2D scale factor (so that UI elements can still be scaled).
  • Make certain controls always drawn at 1:1 scale (e.g. for the crosshair in a FPS). This is done by dividing the Control node's scale by the scale factor.

Testing project: test_scale_factor_2.zip

Preview

Top displays a GDScript implementation as shown in the proposal. Bottom displays the new implementation.

Screenshot_20230824_182726

@Calinou Calinou added this to the 4.x milestone Aug 24, 2023
@Calinou Calinou requested review from a team as code owners August 24, 2023 16:33
@Calinou Calinou force-pushed the viewport-add-get-stretch-scale-factor branch from 3ba9917 to a08fcec Compare August 24, 2023 16:34
scene/main/viewport.cpp Outdated Show resolved Hide resolved
@Calinou Calinou force-pushed the viewport-add-get-stretch-scale-factor branch 2 times, most recently from 91fdbfd to b0ce67a Compare June 10, 2024 18:04
@Calinou Calinou force-pushed the viewport-add-get-stretch-scale-factor branch from b0ce67a to 56e0ee6 Compare June 10, 2024 18:51
@Calinou Calinou force-pushed the viewport-add-get-stretch-scale-factor branch from 56e0ee6 to 6f3203c Compare October 2, 2024 00:14
doc/classes/Viewport.xml Outdated Show resolved Hide resolved
@akien-mga akien-mga modified the milestones: 4.x, 4.4 Oct 2, 2024
@Calinou Calinou force-pushed the viewport-add-get-stretch-scale-factor branch from 6f3203c to 41d1342 Compare October 28, 2024 17:14
Copy link
Member

@Geometror Geometror left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the adjusted documentation I think this should be good to go.

`Viewport.get_stretch_transform()` returns the automatically computed
2D stretch transform. Combined with `Transform2D.get_scale()`, this is
useful when using the `canvas_items` stretch mode in a project.

There are many situations where knowing this factor is useful:

- Divide Camera2D zoom to keep the size of the 2D game world identical
  regardless of the 2D scale factor (so that UI elements can still be scaled).
- Make certain controls always drawn at 1:1 scale
  (e.g. for the crosshair in a FPS). This is done by dividing the Control
  node's scale by the scale factor.
@Calinou Calinou force-pushed the viewport-add-get-stretch-scale-factor branch from 41d1342 to 0cf99cf Compare November 19, 2024 22:11
@Calinou Calinou changed the title Add a Viewport method to get automatically computed 2D scale factor Add a Viewport method to get automatically computed 2D stretch transform Nov 19, 2024
@Calinou
Copy link
Member Author

Calinou commented Nov 19, 2024

I've force-pushed a commit that amends the commit message to describe more accurately what it does now (expose the stretch transform, not just the scale).

Copy link
Contributor

@Sauermann Sauermann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation looks correct.

@Repiteo Repiteo merged commit 9c300a6 into godotengine:master Nov 27, 2024
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Nov 27, 2024

Thanks!

@Calinou Calinou deleted the viewport-add-get-stretch-scale-factor branch November 28, 2024 02:54
@akien-mga akien-mga changed the title Add a Viewport method to get automatically computed 2D stretch transform Add a Viewport method to get automatically compute 2D stretch transform Dec 5, 2024
@akien-mga akien-mga changed the title Add a Viewport method to get automatically compute 2D stretch transform Add a Viewport method to get automatically computed 2D stretch transform Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a Window method to return the automatically computed 2D scale factor
5 participants