GASATHON/Project/Source/Gasa/UI/GlobeProgressBar.cpp

199 lines
5.6 KiB
C++
Raw Normal View History

#include "GlobeProgressBar.h"
2024-04-16 14:18:06 -07:00
#include "GasaImage.h"
#include "GasaOverlay.h"
2024-04-16 14:18:06 -07:00
#include "GasaProgressBar.h"
#include "GasaSizeBox.h"
2024-04-16 14:18:06 -07:00
#include "Components/OverlaySlot.h"
#include "Blueprint/WidgetBlueprintGeneratedClass.h"
#include "Blueprint/WidgetTree.h"
2024-10-19 18:33:20 -07:00
#include "TimerManager.h"
#include "Extensions/WidgetBlueprintGeneratedClassExtension.h"
#if WITH_EDITOR
#include "WidgetBlueprint.h"
#include "Kismet2/BlueprintEditorUtils.h"
#endif
using namespace Gasa;
2024-04-16 14:18:06 -07:00
// UGlobeProgressBar::UGlobeProgressBar(FObjectInitializer const& ObjectInitializer)
// {
// }
void UGlobeProgressBar::GenerateDesignerWidgetTemplate()
{
#if WITH_EDITOR
UWidgetBlueprintGeneratedClass* WBG_Class = Cast<UWidgetBlueprintGeneratedClass>(GetClass());
UPackage* Package = WBG_Class->GetPackage();
UWidgetBlueprint* AssetBP = Cast<UWidgetBlueprint>(Package->FindAssetInPackage());
UWidgetTree* WT = AssetBP->WidgetTree;
UWidget* AssetRoot = AssetBP->WidgetTree->RootWidget;
UGasaSizeBox* Asset_SB = WT->FindWidget<UGasaSizeBox>("Root");
UGasaOverlay* Asset_Overlay = WT->FindWidget<UGasaOverlay>("Overlay");
UGasaImage* Asset_Bezel = WT->FindWidget<UGasaImage>("Bezel");
UGasaImage* Asset_Glass = WT->FindWidget<UGasaImage>("Glass");
UGasaProgressBar* Asset_Bar = WT->FindWidget<UGasaProgressBar>("Bar");
if (Root_SB == nullptr)
Asset_SB = WT->ConstructWidget<UGasaSizeBox>(UGasaSizeBox::StaticClass(), FName("Root_SB"));
if (Overlay == nullptr)
Asset_Overlay = WT->ConstructWidget<UGasaOverlay>(UGasaOverlay::StaticClass(), FName("Overlay"));
if (Bezel == nullptr)
Asset_Bezel = WT->ConstructWidget<UGasaImage>(UGasaImage::StaticClass(), FName("Bezel"));
if (Glass == nullptr)
Asset_Glass = WT->ConstructWidget<UGasaImage>(UGasaImage::StaticClass(), FName("Glass"));
if (Bar == nullptr)
Asset_Bar = WT->ConstructWidget<UGasaProgressBar>(UGasaProgressBar::StaticClass(), FName("Bar"));
WT->RootWidget = Asset_SB;
Asset_SB->ClearChildren();
Asset_Overlay->ClearChildren();
Asset_SB->AddChild(Asset_Overlay);
Asset_Overlay->AddChild(Asset_Bezel);
Asset_Overlay->AddChild(Asset_Glass);
Asset_Overlay->AddChild(Asset_Bar);
AssetBP->Modify();
FBlueprintEditorUtils::MarkBlueprintAsStructurallyModified(AssetBP);
#endif
}
2024-10-19 18:33:20 -07:00
void UGlobeProgressBar::GhostPercentUpdateViaTimer()
{
GhostTargetPercent = Bar->GetPercent();
}
#pragma region Bindings
void UGlobeProgressBar::SetBezelStyle(FSlateBrush brush)
2024-04-16 14:18:06 -07:00
{
Bezel->SetBrush( brush );
2024-04-16 14:18:06 -07:00
}
void UGlobeProgressBar::SetBarPadding(FMargin margin )
{
UOverlaySlot* BarSlot = CastChecked<UOverlaySlot>(Bar->Slot);
BarSlot->SetPadding( margin );
}
void UGlobeProgressBar::SetBarStyle(FProgressBarStyle style)
{
Bar->SetWidgetStyle( style );
}
2024-10-19 18:33:20 -07:00
void UGlobeProgressBar::SetGhostBarStyle(FProgressBarStyle style)
{
GhostBar->SetWidgetStyle( style );
}
2024-04-16 14:18:06 -07:00
void UGlobeProgressBar::SetGlassPadding(FMargin margin)
{
UOverlaySlot* GlassSlot = CastChecked<UOverlaySlot>(Glass->Slot);
GlassSlot->SetPadding(margin);
}
void UGlobeProgressBar::SetGlassStyle(FSlateBrush brush)
{
Glass->SetBrush(brush);
}
2024-04-21 21:30:29 -07:00
void UGlobeProgressBar::SetPercentage(float CurrentValue, float MaxValue)
{
2024-10-19 18:33:20 -07:00
float PreviousValue = Bar->GetPercent();
float CurrentValueClamped = MaxValue > 0.f ? CurrentValue / MaxValue : 0.f;
Bar->SetPercent( CurrentValueClamped );
UWorld* World = GetWorld();
FTimerManager& TM = World->GetTimerManager();
2024-10-19 19:34:51 -07:00
if ( CurrentValueClamped <= PreviousValue )
2024-10-19 18:33:20 -07:00
{
// Timer will auto-clear previous set delay
TM.SetTimer( GhostPercentChangeTimer, this, & UGlobeProgressBar::GhostPercentUpdateViaTimer, GhostPercentChangeDelay );
}
2024-10-19 19:34:51 -07:00
else if ( Bar->GetPercent() >= GhostBar->GetPercent() )
2024-10-19 18:33:20 -07:00
{
if ( TM.TimerExists( GhostPercentChangeTimer ))
TM.ClearTimer( GhostPercentChangeTimer );
GhostBar->SetPercent( CurrentValueClamped );
GhostTargetPercent = CurrentValueClamped;
}
2024-04-21 21:30:29 -07:00
}
2024-04-16 14:18:06 -07:00
void UGlobeProgressBar::SetSize(float width, float height)
{
Root_SB->SetWidthOverride( width );
Root_SB->SetHeightOverride( height );
2024-04-16 14:18:06 -07:00
}
#pragma endregion Bindings
2024-04-16 14:18:06 -07:00
#pragma region Widget
void UGlobeProgressBar::SynchronizeProperties()
2024-04-16 14:18:06 -07:00
{
Super::SynchronizeProperties();
2024-04-16 14:18:06 -07:00
}
void UGlobeProgressBar::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
2024-04-16 14:18:06 -07:00
{
Super::PostEditChangeProperty(PropertyChangedEvent);
2024-04-16 14:18:06 -07:00
}
#pragma endregion Widget
2024-04-16 14:18:06 -07:00
#pragma region UserWidget
void UGlobeProgressBar::NativePreConstruct()
{
// Super::NativePreConstruct(); - Inlined
LLM_SCOPE_BYTAG(UI_UMG);
const bool bIsDesignTime = IsDesignTime();
UWidgetBlueprintGeneratedClass* WBG_Class = Cast<UWidgetBlueprintGeneratedClass>(GetClass());
if (WBG_Class)
{
WBG_Class->ForEachExtension([this, bIsDesignTime](UWidgetBlueprintGeneratedClassExtension* Extension)
{
Extension->PreConstruct(this, bIsDesignTime);
});
}
DesiredFocusWidget.Resolve(WidgetTree);
2024-10-19 18:33:20 -07:00
// Basic initialization
{
GhostTargetPercent = Bar->GetPercent();
GhostBar->SetPercent( GhostTargetPercent );
}
// Blueprint Callback
PreConstruct(bIsDesignTime);
}
2024-10-19 18:33:20 -07:00
void UGlobeProgressBar::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
Super::NativeTick(MyGeometry, InDeltaTime);
UWorld* World = GetWorld();
FTimerManager& TM = World->GetTimerManager();
// Ghost Percent Interpolation
2024-10-19 19:34:51 -07:00
if ( ! TM.TimerExists( GhostPercentChangeTimer ) )
2024-10-19 18:33:20 -07:00
{
float NextPercent = FMath::FInterpTo( GhostBar->GetPercent(), GhostTargetPercent, InDeltaTime, GhostPercentInterpolationSpeed );
GhostBar->SetPercent( NextPercent );
}
}
void UGlobeProgressBar::Serialize(FArchive& Ar)
{
Super::Serialize(Ar);
}
void UGlobeProgressBar::Serialize(FStructuredArchive::FRecord Record)
{
Super::Serialize(Record);
2024-04-16 14:18:06 -07:00
}
#pragma endregion UserWidget