Keep sprite2D same width or height with different images

In the code that I’m running, I want to display an image of the users choice, but it needs to fit on screen correctly.

I want to use something like this layout (not finalized but it will look similar)


The blue areas will have UI elements and the green area is reserved for the image

When a chosen image is to large, it needs to be scaled down dynamically so it fits correctly on screen.
Depending on it’s size, this means that either the vertical, or horizontal white borders are the guide to scaling, and change when the image is a certain amount taller then it is wide.

As much as the image can, it needs to rake up the green area, it’s even allowed to scale up when it is smaller then it.

the image is saved as

global_origin_image

in Globals.gd

what is the best way to handle this?
The colored areas have been made by scaling, is it smarter to use normal sized images if width measurements are needed?

Never mind, I got it

this works for me

func _scale_correctly():
var prioritize_width = _prioritize_width()
var tf = 1
if (prioritize_width):
tf = $backGround/green.texture.get_width() * $backGround/green.scale.x / float(Globals.global_origin_image.get_width())
else:
tf = $backGround/green.texture.get_height() * $backGround/green.scale.y / float(Globals.global_origin_image.get_height())
$picture.scale = Vector2(tf, tf)

func _prioritize_width():
var proportion_background = float($backGround/green.texture.get_width() * $backGround/green.scale.x) / float($backGround/green.texture.get_height() * $backGround/green.scale.y)
var proportions_input = float(Globals.global_origin_image.get_width() / float(Globals.global_origin_image.get_height()))
print(str(proportion_background))
print(str(proportions_input))
if (proportions_input >= proportion_background):
return true
else:
return false

Only need to make sure $picture is located at the exact center of $background/green

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.