#include "ZMMOHudHpSpWidget.h" #include "Components/ProgressBar.h" #include "Components/TextBlock.h" #include "Internationalization/Text.h" #include "ZMMOAttributeComponent.h" void UZMMOHudHpSpWidget::BindToAttributeComponent(UZMMOAttributeComponent* InComponent) { if (UZMMOAttributeComponent* Old = BoundComponent.Get()) { Old->OnAttributesChanged.RemoveDynamic(this, &UZMMOHudHpSpWidget::HandleAttributesChanged); Old->OnHpSpChanged.RemoveDynamic(this, &UZMMOHudHpSpWidget::HandleHpSpChanged); } BoundComponent = InComponent; if (InComponent) { InComponent->OnAttributesChanged.AddDynamic(this, &UZMMOHudHpSpWidget::HandleAttributesChanged); InComponent->OnHpSpChanged.AddDynamic(this, &UZMMOHudHpSpWidget::HandleHpSpChanged); // Refresh imediato — caso o primeiro snapshot tenha chegado antes // do widget existir. HandleAttributesChanged(InComponent->GetSnapshot()); } } void UZMMOHudHpSpWidget::NativeDestruct() { if (UZMMOAttributeComponent* Old = BoundComponent.Get()) { Old->OnAttributesChanged.RemoveDynamic(this, &UZMMOHudHpSpWidget::HandleAttributesChanged); Old->OnHpSpChanged.RemoveDynamic(this, &UZMMOHudHpSpWidget::HandleHpSpChanged); } BoundComponent.Reset(); Super::NativeDestruct(); } void UZMMOHudHpSpWidget::HandleAttributesChanged(const FZMMOAttributesSnapshot& Snapshot) { OnSnapshotApplied(Snapshot); } void UZMMOHudHpSpWidget::HandleHpSpChanged(int32 Hp, int32 Sp) { OnHpSpDelta(Hp, Sp); } void UZMMOHudHpSpWidget::OnSnapshotApplied_Implementation(const FZMMOAttributesSnapshot& Snapshot) { if (HpBar) { HpBar->SetPercent(Snapshot.MaxHp > 0 ? static_cast(Snapshot.Hp) / static_cast(Snapshot.MaxHp) : 0.f); } if (SpBar) { SpBar->SetPercent(Snapshot.MaxSp > 0 ? static_cast(Snapshot.Sp) / static_cast(Snapshot.MaxSp) : 0.f); } if (HpText) { HpText->SetText(FText::FromString(FString::Printf(TEXT("%d / %d"), Snapshot.Hp, Snapshot.MaxHp))); } if (SpText) { SpText->SetText(FText::FromString(FString::Printf(TEXT("%d / %d"), Snapshot.Sp, Snapshot.MaxSp))); } if (LevelText) { LevelText->SetText(FText::FromString(FString::Printf(TEXT("Lv %d"), Snapshot.BaseLevel))); } } void UZMMOHudHpSpWidget::OnHpSpDelta_Implementation(int32 Hp, int32 Sp) { // Atualiza apenas os campos vitais (evita refresh redundante de // stats/level que nao mudaram). if (UZMMOAttributeComponent* Comp = BoundComponent.Get()) { const FZMMOAttributesSnapshot& Snap = Comp->GetSnapshot(); if (HpBar) { HpBar->SetPercent(Snap.MaxHp > 0 ? static_cast(Hp) / static_cast(Snap.MaxHp) : 0.f); } if (SpBar) { SpBar->SetPercent(Snap.MaxSp > 0 ? static_cast(Sp) / static_cast(Snap.MaxSp) : 0.f); } if (HpText) { HpText->SetText(FText::FromString(FString::Printf(TEXT("%d / %d"), Hp, Snap.MaxHp))); } if (SpText) { SpText->SetText(FText::FromString(FString::Printf(TEXT("%d / %d"), Sp, Snap.MaxSp))); } } }