set texture rect dimension

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

Hello,
i have a scene with a texture rect (whose texture is loaded time to time from pictures in a folder) and an “Expand” button.
I want it to have a pretty simple beahaviour:

  1. if the picture dimension is bigger than the screen, i want it to scale it down to fit the screen
  2. if the picture dimension is smaller and expand is pressed, i want the texture to be scaled up to fill the screen dimension
  3. If the picture dimension is smaller and not expanded, the texture remain had the same dimension of the screen
    4.I want the picture to always remain in the middle of the screen and to keep its aspect ratio.

Is there a way to do it with control nodes? ideally, the perfect code would control the expand and stretch_mode of the texture rect properties only.
I really cannot find a way to make it work easily

:bust_in_silhouette: Reply From: Andrea

right now, my best solution is to manually set position and dimension:

var maxs=max($TextureRect.texture.get_size().x, $TextureRect.texture.get_size().y)
	if maxs>screen_size.x or maxs>screen_size.y:
		$TextureRect.texture.rect_min_size=screen_size
		$TextureRect.texture.rect_position=Vector2(0,0)
		$TextureRect.texture.expand=true
		$TextureRect.texture.stretch_mode=6
	else:
		if expanded:
			$TextureRect.texture.rect_min_size=screen_size
			$TextureRect.texture.rect_position=Vector2(0,0)
			$TextureRect.texture.expand=true
			$TextureRect.texture.stretch_mode=6
		else:
			$TextureRect.texture.rect_min_size=Vector2(0,0)
			$TextureRect.texture.rect_position=screen_size/2
			$TextureRect.texture.expand=false
			$TextureRect.texture.stretch_mode=0