How to get the displayed size of a stretched texture in TextureRect?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AsP

Assume having a TextureRect with some texture and arbitrary properties regarding expand and stretch_mode.

How do you easily get the size at which the texture is actually displayed without manually handling every possible case of the stretch mode?

I know I can get:

  • TextureRect.rect_size and rect_scale (inherited from Control) for the bounding rectangle
  • TextureRect.texture.get_size() for the image size in pixels

I’m confused. Why is rect_size not enough?

exuin | 2021-04-08 14:51

TextureRect.rect_scale * TextureRect.texture.get_size()
Edit: yeah, I just realized, this solution was for the custom stretch mode I wrote which uses the scaling property.

Since, if you scale a TextureRect, using TextureRect.rect_size() will only return the set width and height but not the actual displayed size. To get around this, I had to get the scale property and the image texture’s size manually to get the actual displayed size. But I digress.

bloodsign | 2021-04-08 17:06

Hmm, what if the texture’s scale is 1 but expand is on?

exuin | 2021-04-08 17:31

rect_size may be controlled from elsewhere (container, script) and may be larger than the texture.

AsP | 2021-04-08 21:39

expand or stretch_mode don’t affect the rect_scale property, so this approach doesn’t work

AsP | 2021-04-08 21:41

:bust_in_silhouette: Reply From: exuin

Unfortunately I don’t think this is possible - there are too any stretch modes. You’ll probably have to code for each one specifically.