Scaling a widget to the size of the screen.

Hello, I’m trying to scale a widget to exactly 0.9 times the size of the screen.

I found some code to get the dpi, then I made a function which to me seems like it should give the correct values. But I wouldn’t be here if it did. :slight_smile:

.cpp

float UMiscBlueprintNodes::GetUMG_DPI_Scale() {

	FVector2D viewportSize;
	GEngine->GameViewport->GetViewportSize(viewportSize);

	int32 X = FGenericPlatformMath::FloorToInt(viewportSize.X);
	int32 Y = FGenericPlatformMath::FloorToInt(viewportSize.Y);

	return GetDefault<UUserInterfaceSettings>(UUserInterfaceSettings::StaticClass())->GetDPIScaleBasedOnSize(FIntPoint(X, Y));
}

FVector2D UMiscBlueprintNodes::GetViewportResScaledByDPI(float ExtraScaleAmount) {
	float DPI_Scale = GetUMG_DPI_Scale();

	FVector2D viewportSize;
	GEngine->GameViewport->GetViewportSize(viewportSize);

	if (DPI_Scale < 1.0) {
		DPI_Scale = abs(DPI_Scale - 1.0) + 1;
	}
	else {
		DPI_Scale = abs(DPI_Scale - 2.0);
	}

	return ExtraScaleAmount * (viewportSize * DPI_Scale);
}

The dpi scaling seems to be throwing off what to give a widget to size it properly manually.

Thanks!

Since it looks like you’re creating functions for use in Blueprint anyway, why not just use the built-in Blueprint nodes for this purpose?

Link: Get Viewport Size node
Link: Get Viewport Scale node

To get the viewport’s actual size as it is has been scaled by the DPI, just divide the size by the scale.

Thanks. Yeah I used those as my first attempt and got inconsistent results depending on the platform I pushed it to. So I went to C++. The results in c++ are consistent at least but either were correct as far as scaling a widget to the size of the screen.

Very odd. Guess I’m stumped along with you.