72 lines
1.7 KiB
C++
72 lines
1.7 KiB
C++
// ChildHood Project - 收集完成提示 UI 实现
|
||
// 三段时序:淡入 → 停留 → 淡出 → 移除(自管理,HUDComponent 按需创建后无需追踪)
|
||
|
||
#include "Collection/CollectionCompleteWidget.h"
|
||
#include "Components/TextBlock.h"
|
||
#include "Engine/World.h"
|
||
#include "TimerManager.h"
|
||
|
||
void UCollectionCompleteWidget::NativeOnInitialized()
|
||
{
|
||
Super::NativeOnInitialized();
|
||
}
|
||
|
||
void UCollectionCompleteWidget::NativeDestruct()
|
||
{
|
||
// 清理计时器:StayTimer/FadeOutTimer 的 lambda 捕获裸 this,Widget 销毁后 fire 会野指针
|
||
// (与 CollectionHUDComponent::EndPlay 同类卫生写法,防关卡切换/退 PIE 时的悬挂 timer)
|
||
if (UWorld* World = GetWorld())
|
||
{
|
||
World->GetTimerManager().ClearTimer(StayTimerHandle);
|
||
World->GetTimerManager().ClearTimer(FadeOutTimerHandle);
|
||
}
|
||
|
||
Super::NativeDestruct();
|
||
}
|
||
|
||
void UCollectionCompleteWidget::ShowComplete(const FString& Text, float DisplayDuration, float InFadeOutDuration)
|
||
{
|
||
CachedFadeOutDuration = InFadeOutDuration;
|
||
|
||
if (CompleteTextBlock)
|
||
{
|
||
CompleteTextBlock->SetText(FText::FromString(Text));
|
||
}
|
||
|
||
// 淡入
|
||
if (FadeInAnim)
|
||
{
|
||
PlayAnimation(FadeInAnim);
|
||
}
|
||
|
||
// 停留时长后触发淡出
|
||
if (UWorld* World = GetWorld())
|
||
{
|
||
World->GetTimerManager().SetTimer(StayTimerHandle, [this]()
|
||
{
|
||
StartFadeOut();
|
||
}, DisplayDuration, false);
|
||
}
|
||
}
|
||
|
||
void UCollectionCompleteWidget::StartFadeOut()
|
||
{
|
||
if (FadeOutAnim)
|
||
{
|
||
PlayAnimation(FadeOutAnim);
|
||
}
|
||
|
||
if (UWorld* World = GetWorld())
|
||
{
|
||
World->GetTimerManager().SetTimer(FadeOutTimerHandle, [this]()
|
||
{
|
||
OnFadeOutComplete();
|
||
}, CachedFadeOutDuration, false);
|
||
}
|
||
}
|
||
|
||
void UCollectionCompleteWidget::OnFadeOutComplete()
|
||
{
|
||
RemoveFromParent();
|
||
}
|