AronaSlate/Source/Arona/UI/Widget/SUpdatableImage.cpp
2024-01-25 11:21:15 +08:00

81 lines
2.3 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "SUpdatableImage.h"
#include "SlateApplication.h"
#include "SlateOptMacros.h"
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
SUpdatableImage::SUpdatableImage()
{
LastSize = FIntPoint::ZeroValue;
SetVolatilePrepass(false);
}
void SUpdatableImage::Construct(const FArguments& InArgs)
{
FixedWidth = InArgs._FixedWidth;
FixedHeight = InArgs._FixedHeight;
MaxWidth = InArgs._MaxWidth;
MaxHeight = InArgs._MaxHeight;
Texture = MakeShareable(new FUpdatableTexture(FIntPoint(16, 16)));
Texture->RedrawImage = InArgs._OnPostResize;
}
void SUpdatableImage::NeedRedraw()
{
Invalidate(EInvalidateWidgetReason::Paint);
ForceRedraw = true;
}
void SUpdatableImage::Resize(FIntPoint NewSize, bool Immediate)
{
if (Immediate)
{
if (FixedHeight.IsSet())
NewSize.Y = FixedHeight.GetValue();
if (FixedWidth.IsSet())
NewSize.X = FixedWidth.GetValue();
if (MaxHeight.IsSet())
NewSize.Y = FMath::Min(NewSize.Y, MaxHeight.GetValue());
if (MaxWidth.IsSet())
NewSize.X = FMath::Min(NewSize.X, MaxWidth.GetValue());
Texture->Resize(NewSize);
}
}
void SUpdatableImage::AsyncUpdate() const
{
Texture->Resize(FIntPoint(LastSize.X, LastSize.Y));
Texture->RequestUpdate(true);
}
int32 SUpdatableImage::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
{
const auto& CurrentSize = AllottedGeometry.GetAbsoluteSize();
FIntPoint CurrentSizeInt(CurrentSize.X, CurrentSize.Y);
if (FixedHeight.IsSet())
CurrentSizeInt.Y = FixedHeight.GetValue();
if (FixedWidth.IsSet())
CurrentSizeInt.X = FixedWidth.GetValue();
if (MaxHeight.IsSet())
CurrentSizeInt.Y = FMath::Min(CurrentSizeInt.Y, MaxHeight.GetValue());
if (MaxWidth.IsSet())
CurrentSizeInt.X = FMath::Min(CurrentSizeInt.X, MaxWidth.GetValue());
if (ForceRedraw || LastSize != CurrentSizeInt)
{
LastSize = CurrentSizeInt;
ForceRedraw = false;
AsyncUpdate();
}
FSlateDrawElement::MakeViewport(OutDrawElements, LayerId, AllottedGeometry.ToPaintGeometry(), Texture, ESlateDrawEffect::None, FLinearColor::White);
return ++LayerId;
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION