0 votes

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
Godot version 3.2.3
in Engine by (24 points)

I'm confused. Why is rect_size not enough?

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.

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

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

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

1 Answer

0 votes

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

by (7,038 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.