// Oleg Petruny proprietary.

#include "ResolutionResponsiveUserWidget.h"

#include "Components/PanelSlot.h"
#include "UnrealClient.h"

bool UResolutionResponsiveUserWidget::Initialize()
{
    _viewportResizedEventHandle = FViewport::ViewportResizedEvent.AddUObject(this, &UResolutionResponsiveUserWidget::Rescale);

    return UUserWidget::Initialize();
}

void UResolutionResponsiveUserWidget::BeginDestroy()
{
    if(_viewportResizedEventHandle.IsValid())
    {
        FViewport::ViewportResizedEvent.Remove(_viewportResizedEventHandle);
    }

    UUserWidget::BeginDestroy();
}

void UResolutionResponsiveUserWidget::Rescale(FViewport* ViewPort, uint32 val)
{
    _scale = (float)defaultResolution / ViewPort->GetSizeXY().GetMin();
    Rescale();
}

void UResolutionResponsiveUserWidget::Rescale_Implementation()
{
    SetRenderScale(FVector2D{ _scale });
}

float UResolutionResponsiveUserWidget::GetScale() const
{
    return _scale;
}