Files
ChildHood/Source/ChildHood/Collection/CollectionNotifWidget.cpp
T

81 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ChildHood Project - 收集通知 UI 实现
// 三段时序:淡入 → 停留 → 淡出 → 广播 OnFinished + 移除
#include "Collection/CollectionNotifWidget.h"
#include "Components/TextBlock.h"
#include "Engine/World.h"
#include "TimerManager.h"
void UCollectionNotifWidget::NativeOnInitialized()
{
Super::NativeOnInitialized();
}
void UCollectionNotifWidget::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 UCollectionNotifWidget::InitializeNotif(const FString& Text, FLinearColor TextColor,
float InFadeInDuration, float InStayDuration, float InFadeOutDuration)
{
CachedFadeOutDuration = InFadeOutDuration;
// 设置文字 + 颜色
if (NotifTextBlock)
{
NotifTextBlock->SetText(FText::FromString(Text));
NotifTextBlock->SetColorAndOpacity(TextColor);
}
// 淡入动画
if (FadeInAnim)
{
PlayAnimation(FadeInAnim);
}
// 停留时长后触发淡出
if (UWorld* World = GetWorld())
{
World->GetTimerManager().SetTimer(StayTimerHandle, [this]()
{
StartFadeOut();
}, InStayDuration, false);
}
}
void UCollectionNotifWidget::StartFadeOut()
{
// 淡出动画
if (FadeOutAnim)
{
PlayAnimation(FadeOutAnim);
}
// 淡出时长后移除(用计时器而非动画完成委托,避免委托绑定复杂度)
if (UWorld* World = GetWorld())
{
World->GetTimerManager().SetTimer(FadeOutTimerHandle, [this]()
{
OnFadeOutComplete();
}, CachedFadeOutDuration, false);
}
}
void UCollectionNotifWidget::OnFadeOutComplete()
{
// 通知 HUDComponent 从 ActiveNotifications 移除本 Widget
OnFinished.ExecuteIfBound(this);
// 从视口移除自己
RemoveFromParent();
}