AffineTransform in app cutting off image when made smaller

We have an app whose MainComponent uses an AffineTransform to scale the contents of its child Component to match the window size as the user resizes the window. We’re using this code to do that:

    compHolder->setTopLeftPosition(0, 0);
    float scaleFactorX = (float)getWidth() / 960.f;
    float scaleFactorY = (float)getHeight() / 560.f;
    float scaleFactor = (scaleFactorX >= scaleFactorY) ? scaleFactorX : scaleFactorY;
    compHolder->setTransform(juce::AffineTransform::scale(scaleFactor));

This keeps the width-to-height ratio, setting the scale factor to the largest size possible, given the X and Y values that the mouse drag causes to be set. This almost works, but if the user scales the window smaller than the default, then it starts to slowly eat away at the bottom of the window. I added the cast to float just in case it was using integer math and truncating, but that didn’t fix it.

Any idea why an AffineTransform doesn’t scale the contents of my “compHolder” Component quite right when scaleFactor is much less than 1? (My only solution so far to prevent this is to set a minimum size such that it doesn’t ever get small enough to cut off the stuff at the bottom of that Component.)

Try to use the juce::RectanglePlacement class and use its function getTransformToFit() instead of your own code:

https://docs.juce.com/master/classRectanglePlacement.html

That looks promising, thanks!

Forgot to mention: use the “fillDestination” mode to keep the aspect ratio.

Almost perfect. The fillDestination cuts off a little both top and bottom, though. Using the default (which is the same as centred), it adds small bars left and right, because the main window is using setFixedAspectRatio(), but it’s really the MainComponent that needs to maintain that aspect ratio, not the DocumentWindow that contains it. But I don’t see a way to easily tell the DocumentWindow to maintain its content at the same fixed aspect ratio.

For now, I just set the background color of the DocumentWindow to be the same color as the content’s background color, so that the extra small bands to the left and right are at least the same color.

Is there an easy way to tell the DocumentWindow to maintain it’s contents at a fixed aspect ratio?

Found the solution in this post. Works great on Mac. Haven’t tested PC yet, though.

That worked on Mac, not so well on PC. I got it to work on PC by grabbing the top/left of the bounds passed in (after subtracting the border), setting the top/left to 0,0, calling the base class checkBounds, then setting the top/left to what it was before checking bounds, and then adding back the border.

I don’t get why that does not cause the top/left to always be what it was before, but it works on both Mac and PC. :person_shrugging:

      border_.subtractFrom(bounds);
      juce::Point<int> delta = bounds.getTopLeft();
      bounds.setPosition(0.0, 0.0);
      ComponentBoundsConstrainer::checkBounds(bounds, previous, limits,
                                              stretching_top, stretching_left,
                                              stretching_bottom, stretching_right);
      bounds.setPosition(delta);
      border_.addTo(bounds);