void UUserWidget::RemoveFromParent() { if ( FullScreenWidget.IsValid() ) { TSharedPtr<SWidget> WidgetHost = FullScreenWidget.Pin(); // If this is a game world add the widget to the current worlds viewport. UWorld* World = GetWorld(); if ( World && World->IsGameWorld() ) { if ( UGameViewportClient* ViewportClient = World->GetGameViewport() ) { TSharedRef<SWidget> WidgetHostRef = WidgetHost.ToSharedRef(); ViewportClient->RemoveViewportWidgetContent(WidgetHostRef); if ( ULocalPlayer* LocalPlayer = GetOwningLocalPlayer() ) { ViewportClient->RemoveViewportWidgetForPlayer(LocalPlayer, WidgetHostRef); } FWorldDelegates::LevelRemovedFromWorld.RemoveAll(this); } } } else { Super::RemoveFromParent(); } }
void UWidgetLayoutLibrary::RemoveAllWidgets(UObject* WorldContextObject) { UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject); if ( World && World->IsGameWorld() ) { if ( UGameViewportClient* ViewportClient = World->GetGameViewport() ) { ViewportClient->RemoveAllViewportWidgets(); } } }
void UUserWidget::AddToScreen(ULocalPlayer* Player, int32 ZOrder) { if ( !FullScreenWidget.IsValid() ) { // First create and initialize the variable so that users calling this function twice don't // attempt to add the widget to the viewport again. TSharedRef<SConstraintCanvas> FullScreenCanvas = SNew(SConstraintCanvas); FullScreenWidget = FullScreenCanvas; TSharedRef<SWidget> UserSlateWidget = TakeWidget(); FullScreenCanvas->AddSlot() .Offset(BIND_UOBJECT_ATTRIBUTE(FMargin, GetFullScreenOffset)) .Anchors(BIND_UOBJECT_ATTRIBUTE(FAnchors, GetViewportAnchors)) .Alignment(BIND_UOBJECT_ATTRIBUTE(FVector2D, GetFullScreenAlignment)) [ UserSlateWidget ]; // If this is a game world add the widget to the current worlds viewport. UWorld* World = GetWorld(); if ( World && World->IsGameWorld() ) { if ( UGameViewportClient* ViewportClient = World->GetGameViewport() ) { if ( Player ) { ViewportClient->AddViewportWidgetForPlayer(Player, FullScreenCanvas, ZOrder); } else { // We add 10 to the zorder when adding to the viewport to avoid // displaying below any built-in controls, like the virtual joysticks on mobile builds. ViewportClient->AddViewportWidgetContent(FullScreenCanvas, ZOrder + 10); } // Just in case we already hooked this delegate, remove the handler. FWorldDelegates::LevelRemovedFromWorld.RemoveAll(this); // Widgets added to the viewport are automatically removed if the persistent level is unloaded. FWorldDelegates::LevelRemovedFromWorld.AddUObject(this, &UUserWidget::OnLevelRemovedFromWorld); } } } else { FMessageLog("PIE").Warning(FText::Format(LOCTEXT("WidgetAlreadyOnScreen", "The widget '{0}' was already added to the screen."), FText::FromString(GetClass()->GetName()))); } }
FVector2D UWidgetLayoutLibrary::GetViewportSize(UObject* WorldContextObject) { UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject); if ( World && World->IsGameWorld() ) { if ( UGameViewportClient* ViewportClient = World->GetGameViewport() ) { FVector2D ViewportSize; ViewportClient->GetViewportSize(ViewportSize); return ViewportSize; } } return FVector2D(1, 1); }
float UWidgetLayoutLibrary::GetViewportScale(UObject* WorldContextObject) { UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject); if ( World && World->IsGameWorld() ) { if ( UGameViewportClient* ViewportClient = World->GetGameViewport() ) { FVector2D ViewportSize; ViewportClient->GetViewportSize(ViewportSize); return GetDefault<UUserInterfaceSettings>()->GetDPIScaleBasedOnSize(FIntPoint(ViewportSize.X, ViewportSize.Y)); } } return 1; }
void UTKMathFunctionLibrary::ConvertAnchorToAnchor(UObject* WorldContextObject, FAnchors CurrentAnchor, FMargin Offsets, FAnchors TargetAnchor, FMargin& ConvertedOffsets) { if (CurrentAnchor.Minimum == TargetAnchor.Minimum && CurrentAnchor.Maximum == TargetAnchor.Maximum) { ConvertedOffsets = Offsets; return; } FVector2D View = FVector2D(1, 1); UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject); if (World && World->IsGameWorld()) { if (UGameViewportClient* ViewportClient = World->GetGameViewport()) { ViewportClient->GetViewportSize(View); } } FMargin ZeroAnchorOffsets = Offsets; //Convert to 0,0 anchor first. if (CurrentAnchor.Minimum != FVector2D(0, 0) || CurrentAnchor.Maximum != FVector2D(0, 0)) { ZeroAnchorOffsets.Left = View.X * CurrentAnchor.Minimum.X + Offsets.Left; ZeroAnchorOffsets.Top = View.Y * CurrentAnchor.Minimum.Y + Offsets.Top; if (CurrentAnchor.Minimum.X != CurrentAnchor.Maximum.X) { ZeroAnchorOffsets.Right = View.X * CurrentAnchor.Maximum.X - (Offsets.Right + Offsets.Left); } if (CurrentAnchor.Minimum.Y != CurrentAnchor.Maximum.Y) { ZeroAnchorOffsets.Bottom = View.Y * CurrentAnchor.Maximum.Y - (Offsets.Bottom + Offsets.Top); } if (TargetAnchor.Minimum == FVector2D(0, 0) && TargetAnchor.Maximum == FVector2D(0, 0)) { ConvertedOffsets = ZeroAnchorOffsets; return; } } //Convert 0,0 anchor offsets to target anchor offsets. ConvertedOffsets.Left = (-View.X) * TargetAnchor.Minimum.X + ZeroAnchorOffsets.Left; ConvertedOffsets.Top = (-View.Y) * TargetAnchor.Minimum.Y + ZeroAnchorOffsets.Top; ConvertedOffsets.Right = TargetAnchor.Minimum.X != TargetAnchor.Maximum.X ? View.X * TargetAnchor.Maximum.X - (ZeroAnchorOffsets.Left + ZeroAnchorOffsets.Right) : ZeroAnchorOffsets.Right; ConvertedOffsets.Bottom = TargetAnchor.Minimum.Y != TargetAnchor.Maximum.Y ? View.Y * TargetAnchor.Maximum.Y - (ZeroAnchorOffsets.Top + ZeroAnchorOffsets.Bottom) : ZeroAnchorOffsets.Bottom; }
void UUserWidget::AddToViewport(int32 ZOrder) { if ( !FullScreenWidget.IsValid() ) { TSharedPtr<SWidget> OutUserSlateWidget; TSharedRef<SWidget> RootWidget = MakeViewportWidget(OutUserSlateWidget); FullScreenWidget = RootWidget; // If this is a game world add the widget to the current worlds viewport. UWorld* World = GetWorld(); if ( World && World->IsGameWorld() ) { if ( UGameViewportClient* ViewportClient = World->GetGameViewport() ) { ViewportClient->AddViewportWidgetContent(RootWidget, 10 + ZOrder); } } } }
void UUserWidget::RemoveFromParent() { if ( FullScreenWidget.IsValid() ) { TSharedPtr<SWidget> WidgetHost = FullScreenWidget.Pin(); // If this is a game world add the widget to the current worlds viewport. UWorld* World = GetWorld(); if ( World && World->IsGameWorld() ) { if ( UGameViewportClient* ViewportClient = World->GetGameViewport() ) { ViewportClient->RemoveViewportWidgetContent(WidgetHost.ToSharedRef()); } } } else { Super::RemoveFromParent(); } }
void USlateBlueprintLibrary::AbsoluteToViewport(UObject* WorldContextObject, const FGeometry& Geometry, FVector2D AbsoluteCoordinate, FVector2D& ScreenPosition, FVector2D& ViewportPosition) { UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject); if ( World && World->IsGameWorld() ) { if ( UGameViewportClient* ViewportClient = World->GetGameViewport() ) { if ( FViewport* Viewport = ViewportClient->Viewport ) { FVector2D ViewportSize; ViewportClient->GetViewportSize(ViewportSize); FVector2D PixelLocation = Viewport->VirtualDesktopPixelToViewport(FIntPoint((int32)AbsoluteCoordinate.X, (int32)AbsoluteCoordinate.Y)) * ViewportSize; float CurrentViewportScale = GetDefault<UUserInterfaceSettings>()->GetDPIScaleBasedOnSize(FIntPoint(ViewportSize.X, ViewportSize.Y)); // If the user has configured a resolution quality we need to multiply // the pixels by the resolution quality to arrive at the true position in // the viewport, as the rendered image will be stretched to fill whatever // size the viewport is at. Scalability::FQualityLevels ScalabilityQuality = Scalability::GetQualityLevels(); const float QualityScale = ( ScalabilityQuality.ResolutionQuality / 100.0f ); // Remove the resolution quality scale. ScreenPosition = PixelLocation * QualityScale; // Remove DPI Scaling. ViewportPosition = PixelLocation / CurrentViewportScale; return; } } } ScreenPosition = FVector2D(0, 0); ViewportPosition = FVector2D(0, 0); }