Initial Commit - Lesson 31 (Commit #1)
This commit is contained in:
63
Source/FPSTemplate/Private/UI/Elims/ScoreWidget.cpp
Normal file
63
Source/FPSTemplate/Private/UI/Elims/ScoreWidget.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
// Fill out your copyright notice in the Description page of Project Settings.
|
||||
|
||||
|
||||
#include "UI/Elims/ScoreWidget.h"
|
||||
#include "Components/TextBlock.h"
|
||||
#include "Player/MatchPlayerState.h"
|
||||
#include "Player/ShooterPlayerController.h"
|
||||
|
||||
void UScoreWidget::NativeConstruct()
|
||||
{
|
||||
Super::NativeConstruct();
|
||||
|
||||
AMatchPlayerState* PlayerState = GetPlayerState();
|
||||
if (IsValid(PlayerState))
|
||||
{
|
||||
PlayerState->OnScoreChanged.AddDynamic(this, &UScoreWidget::OnScoreChanged);
|
||||
OnScoreChanged(PlayerState->GetScoredElims());
|
||||
}
|
||||
else
|
||||
{
|
||||
AShooterPlayerController* ShooterPlayerController = Cast<AShooterPlayerController>(GetOwningPlayer());
|
||||
if (IsValid(ShooterPlayerController))
|
||||
{
|
||||
ShooterPlayerController->OnPlayerStateReplicated.AddUniqueDynamic(this, &UScoreWidget::OnPlayerStateInitialized);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UScoreWidget::OnScoreChanged(int32 NewScore)
|
||||
{
|
||||
if (IsValid(ScoreText))
|
||||
{
|
||||
ScoreText->SetText(FText::AsNumber(NewScore));
|
||||
}
|
||||
}
|
||||
|
||||
void UScoreWidget::OnPlayerStateInitialized()
|
||||
{
|
||||
// Get the PlayerState and bind to the score changed delegate
|
||||
AMatchPlayerState* PlayerState = GetPlayerState();
|
||||
if (IsValid(PlayerState))
|
||||
{
|
||||
PlayerState->OnScoreChanged.AddDynamic(this, &UScoreWidget::OnScoreChanged);
|
||||
OnScoreChanged(PlayerState->GetScoredElims());
|
||||
}
|
||||
|
||||
// Unsubscribe from the OnPlayerStateChanged delegate
|
||||
AShooterPlayerController* ShooterPlayerController = Cast<AShooterPlayerController>(GetOwningPlayer());
|
||||
if (IsValid(ShooterPlayerController))
|
||||
{
|
||||
ShooterPlayerController->OnPlayerStateReplicated.RemoveDynamic(this, &UScoreWidget::OnPlayerStateInitialized);
|
||||
}
|
||||
}
|
||||
|
||||
AMatchPlayerState* UScoreWidget::GetPlayerState() const
|
||||
{
|
||||
APlayerController* PlayerController = GetOwningPlayer();
|
||||
if (IsValid(PlayerController))
|
||||
{
|
||||
return PlayerController->GetPlayerState<AMatchPlayerState>();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
35
Source/FPSTemplate/Private/UI/Elims/SpecialElimWidget.cpp
Normal file
35
Source/FPSTemplate/Private/UI/Elims/SpecialElimWidget.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
// Fill out your copyright notice in the Description page of Project Settings.
|
||||
|
||||
|
||||
#include "UI/Elims/SpecialElimWidget.h"
|
||||
|
||||
#include "Blueprint/WidgetLayoutLibrary.h"
|
||||
#include "Components/TextBlock.h"
|
||||
#include "Components/Image.h"
|
||||
|
||||
void USpecialElimWidget::InitializeWidget(const FString& InElimMessage, UTexture2D* InElimTexture)
|
||||
{
|
||||
if (IsValid(ElimText))
|
||||
{
|
||||
ElimText->SetText(FText::FromString(InElimMessage));
|
||||
}
|
||||
|
||||
if (IsValid(ElimImage) && InElimTexture)
|
||||
{
|
||||
ElimImage->SetBrushFromTexture(InElimTexture);
|
||||
}
|
||||
}
|
||||
|
||||
void USpecialElimWidget::CenterWidget(UUserWidget* Widget, float VerticalRatio)
|
||||
{
|
||||
if (!IsValid(Widget))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FVector2D ViewportSize = UWidgetLayoutLibrary::GetViewportSize(Widget);
|
||||
const float VerticalFraction = VerticalRatio == 0.f ? 1.f : VerticalRatio * 2.f;
|
||||
FVector2D CenterPosition(ViewportSize.X / 2.0f, VerticalFraction * ViewportSize.Y / 2.0f);
|
||||
Widget->SetAlignmentInViewport(FVector2D(0.5f, 0.5f)); // Align widget center to the center of the viewport
|
||||
Widget->SetPositionInViewport(CenterPosition, true);
|
||||
}
|
||||
Reference in New Issue
Block a user