How to fit a Picture inside a panel?

Started by GetterSetter, 04 April 2024, 19:48:27


Hello, I would like to ask if there are any ways to fully fit the picture with the constant w/h-ratio inside the panel (see attachment: the purple border is the panel and the green rectangle is the picture) that are already implemented inside the library, or should I implement such things by myself? The main idea is to fit it dynamically (while the panel is being resized from (w0;h0) to (w1; h1)).


The most efficient way is to calculate it yourself on a resize event, but you can actually let TGUI do this automatically.

If you know the ratio is 4/3, you can use this code:
pic->setPosition("(&.size - size) / 2");
pic->setSize({"min(&.w, &.h * 4/3)", "min(&.h, &.w * 3/4)"});

Or if you don't know the ratio you can calculate it on runtime and change the above setSize call to the following (this code assumes the picture is added to the gui directly, but just replace "gui" with the parent of the picture if not):
pic->setSize({bindMin(bindWidth(gui), bindHeight(gui) * (pic->getSize().x / pic->getSize().y)),
              bindMin(bindHeight(gui), bindWidth(gui) * (pic->getSize().y / pic->getSize().x))});