111 lines
3.4 KiB
C++
111 lines
3.4 KiB
C++
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "UI/PHYAttributeResourceBarWidget.h"
|
|
|
|
#include UE_INLINE_GENERATED_CPP_BY_NAME(PHYAttributeResourceBarWidget)
|
|
|
|
#include "Blueprint/WidgetTree.h"
|
|
#include "Components/HorizontalBox.h"
|
|
#include "Components/HorizontalBoxSlot.h"
|
|
#include "Components/ProgressBar.h"
|
|
#include "Components/TextBlock.h"
|
|
#include "Components/VerticalBox.h"
|
|
#include "Components/VerticalBoxSlot.h"
|
|
#include "Fonts/SlateFontInfo.h"
|
|
#include "Styling/CoreStyle.h"
|
|
#include "Widgets/SWidget.h"
|
|
|
|
namespace
|
|
{
|
|
float MakePercent(const float CurrentValue, const float MaxValue)
|
|
{
|
|
return MaxValue > UE_SMALL_NUMBER ? FMath::Clamp(CurrentValue / MaxValue, 0.0f, 1.0f) : 0.0f;
|
|
}
|
|
}
|
|
|
|
UPHYAttributeResourceBarWidget::UPHYAttributeResourceBarWidget(const FObjectInitializer& ObjectInitializer)
|
|
: Super(ObjectInitializer)
|
|
{
|
|
}
|
|
|
|
TSharedRef<SWidget> UPHYAttributeResourceBarWidget::RebuildWidget()
|
|
{
|
|
BuildNativeWidgetTree();
|
|
return Super::RebuildWidget();
|
|
}
|
|
|
|
void UPHYAttributeResourceBarWidget::SynchronizeProperties()
|
|
{
|
|
Super::SynchronizeProperties();
|
|
UpdateResourceWidgets();
|
|
}
|
|
|
|
void UPHYAttributeResourceBarWidget::SetResource(FText InLabel, const float InCurrentValue, const float InMaxValue, const FLinearColor InBarTint)
|
|
{
|
|
Label = MoveTemp(InLabel);
|
|
CurrentValue = InCurrentValue;
|
|
MaxValue = FMath::Max(1.0f, InMaxValue);
|
|
BarTint = InBarTint;
|
|
UpdateResourceWidgets();
|
|
}
|
|
|
|
void UPHYAttributeResourceBarWidget::BuildNativeWidgetTree()
|
|
{
|
|
if (!WidgetTree)
|
|
{
|
|
WidgetTree = NewObject<UWidgetTree>(this, TEXT("WidgetTree"), RF_Transient);
|
|
}
|
|
|
|
if (WidgetTree->RootWidget && LabelText && ValueText && ResourceBar)
|
|
{
|
|
return;
|
|
}
|
|
|
|
UVerticalBox* RootBox = WidgetTree->ConstructWidget<UVerticalBox>(UVerticalBox::StaticClass(), TEXT("RootBox"));
|
|
WidgetTree->RootWidget = RootBox;
|
|
|
|
UHorizontalBox* HeaderBox = WidgetTree->ConstructWidget<UHorizontalBox>(UHorizontalBox::StaticClass(), TEXT("HeaderBox"));
|
|
if (UVerticalBoxSlot* HeaderSlot = RootBox->AddChildToVerticalBox(HeaderBox))
|
|
{
|
|
HeaderSlot->SetPadding(FMargin(0.0f, 2.0f, 0.0f, 2.0f));
|
|
}
|
|
|
|
LabelText = WidgetTree->ConstructWidget<UTextBlock>(UTextBlock::StaticClass(), TEXT("LabelText"));
|
|
LabelText->SetFont(FSlateFontInfo(FCoreStyle::GetDefaultFont(), 14));
|
|
LabelText->SetColorAndOpacity(FSlateColor(FLinearColor(0.30f, 0.26f, 0.20f, 1.0f)));
|
|
if (UHorizontalBoxSlot* LabelSlot = HeaderBox->AddChildToHorizontalBox(LabelText))
|
|
{
|
|
LabelSlot->SetSize(FSlateChildSize(ESlateSizeRule::Fill));
|
|
}
|
|
|
|
ValueText = WidgetTree->ConstructWidget<UTextBlock>(UTextBlock::StaticClass(), TEXT("ValueText"));
|
|
ValueText->SetFont(FSlateFontInfo(FCoreStyle::GetDefaultFont(), 14));
|
|
ValueText->SetColorAndOpacity(FSlateColor(FLinearColor(0.10f, 0.26f, 0.23f, 1.0f)));
|
|
HeaderBox->AddChildToHorizontalBox(ValueText);
|
|
|
|
ResourceBar = WidgetTree->ConstructWidget<UProgressBar>(UProgressBar::StaticClass(), TEXT("ResourceBar"));
|
|
if (UVerticalBoxSlot* BarSlot = RootBox->AddChildToVerticalBox(ResourceBar))
|
|
{
|
|
BarSlot->SetPadding(FMargin(0.0f, 0.0f, 0.0f, 7.0f));
|
|
}
|
|
|
|
UpdateResourceWidgets();
|
|
}
|
|
|
|
void UPHYAttributeResourceBarWidget::UpdateResourceWidgets()
|
|
{
|
|
if (LabelText)
|
|
{
|
|
LabelText->SetText(Label);
|
|
}
|
|
if (ValueText)
|
|
{
|
|
ValueText->SetText(FText::FromString(FString::Printf(TEXT("%.0f / %.0f"), FMath::Max(0.0f, CurrentValue), FMath::Max(1.0f, MaxValue))));
|
|
}
|
|
if (ResourceBar)
|
|
{
|
|
ResourceBar->SetPercent(MakePercent(CurrentValue, MaxValue));
|
|
ResourceBar->SetFillColorAndOpacity(BarTint);
|
|
}
|
|
}
|