Files
UnrealEngineUWP/Engine/Source/Runtime/UMG/Private/WidgetLayoutLibrary.cpp
Matthew Griffin bb70b349ce Merging CL 2804086 from //UE4/Release-4.11 to Dev-Main (//UE4/Dev-Main) to isolate copyright update
#lockdown Nick.Penwarden

[CL 2819020 by Matthew Griffin in Main branch]
2016-01-07 08:17:16 -05:00

189 lines
4.7 KiB
C++

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#include "UMGPrivatePCH.h"
#include "Slate/SlateBrushAsset.h"
#include "WidgetLayoutLibrary.h"
#include "Runtime/Engine/Classes/Engine/UserInterfaceSettings.h"
#include "Runtime/Engine/Classes/Engine/RendererSettings.h"
#define LOCTEXT_NAMESPACE "UMG"
/////////////////////////////////////////////////////
// UWidgetLayoutLibrary
UWidgetLayoutLibrary::UWidgetLayoutLibrary(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
bool UWidgetLayoutLibrary::ProjectWorldLocationToWidgetPosition(APlayerController* PlayerController, FVector WorldLocation, FVector2D& ScreenPosition)
{
FVector ScreenPosition3D;
const bool bSuccess = ProjectWorldLocationToWidgetPositionWithDistance(PlayerController, WorldLocation, ScreenPosition3D);
ScreenPosition = FVector2D(ScreenPosition3D.X, ScreenPosition3D.Y);
return bSuccess;
}
bool UWidgetLayoutLibrary::ProjectWorldLocationToWidgetPositionWithDistance(APlayerController* PlayerController, FVector WorldLocation, FVector& ScreenPosition)
{
if ( PlayerController )
{
FVector PixelLocation;
const bool bProjected = PlayerController->ProjectWorldLocationToScreenWithDistance(WorldLocation, PixelLocation);
if ( bProjected )
{
ScreenPosition = PixelLocation;
// Round the pixel projected value to reduce jittering due to layout rounding,
// I do this before I remove scaling, because scaling is going to be applied later
// in the opposite direction, so as long as we round, before inverse scale, scale should
// result in more or less the same value, especially after slate does layout rounding.
ScreenPosition.X = FMath::RoundToInt(ScreenPosition.X);
ScreenPosition.Y = FMath::RoundToInt(ScreenPosition.Y);
ScreenPosition.Z = PixelLocation.Z;
// Get the application / DPI scale
const float Scale = UWidgetLayoutLibrary::GetViewportScale(PlayerController);
// Apply inverse DPI scale so that the widget ends up in the expected position
ScreenPosition = ScreenPosition / Scale;
return true;
}
}
ScreenPosition = FVector(0, 0, 0);
return false;
}
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;
}
bool UWidgetLayoutLibrary::GetMousePositionScaledByDPI(APlayerController* Player, float& LocationX, float& LocationY)
{
if ( Player && Player->GetMousePosition(LocationX, LocationY) )
{
float Scale = UWidgetLayoutLibrary::GetViewportScale(Player);
LocationX = LocationX / Scale;
LocationY = LocationY / Scale;
return true;
}
return false;
}
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);
}
UBorderSlot* UWidgetLayoutLibrary::SlotAsBorderSlot(UWidget* Widget)
{
if (Widget)
{
return Cast<UBorderSlot>(Widget->Slot);
}
return nullptr;
}
UCanvasPanelSlot* UWidgetLayoutLibrary::SlotAsCanvasSlot(UWidget* Widget)
{
if (Widget)
{
return Cast<UCanvasPanelSlot>(Widget->Slot);
}
return nullptr;
}
UGridSlot* UWidgetLayoutLibrary::SlotAsGridSlot(UWidget* Widget)
{
if (Widget)
{
return Cast<UGridSlot>(Widget->Slot);
}
return nullptr;
}
UHorizontalBoxSlot* UWidgetLayoutLibrary::SlotAsHorizontalBoxSlot(UWidget* Widget)
{
if (Widget)
{
return Cast<UHorizontalBoxSlot>(Widget->Slot);
}
return nullptr;
}
UOverlaySlot* UWidgetLayoutLibrary::SlotAsOverlaySlot(UWidget* Widget)
{
if (Widget)
{
return Cast<UOverlaySlot>(Widget->Slot);
}
return nullptr;
}
UUniformGridSlot* UWidgetLayoutLibrary::SlotAsUniformGridSlot(UWidget* Widget)
{
if (Widget)
{
return Cast<UUniformGridSlot>(Widget->Slot);
}
return nullptr;
}
UVerticalBoxSlot* UWidgetLayoutLibrary::SlotAsVerticalBoxSlot(UWidget* Widget)
{
if (Widget)
{
return Cast<UVerticalBoxSlot>(Widget->Slot);
}
return nullptr;
}
void UWidgetLayoutLibrary::RemoveAllWidgets(UObject* WorldContextObject)
{
UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject);
if ( World && World->IsGameWorld() )
{
if ( UGameViewportClient* ViewportClient = World->GetGameViewport() )
{
ViewportClient->RemoveAllViewportWidgets();
}
}
}
#undef LOCTEXT_NAMESPACE