blaster/Source/Blaster/HUD/BlasterHUD.cpp

157 lines
4.3 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterHUD.h"
#include "Announcement.h"
#include "CharacterOverlay.h"
#include "DebugWidget.h"
#include "ElimAnnouncement.h"
#include "Blueprint/UserWidget.h"
#include "Blueprint/WidgetLayoutLibrary.h"
#include "Components/CanvasPanelSlot.h"
#include "Components/HorizontalBox.h"
void ABlasterHUD::BeginPlay()
{
Super::BeginPlay();
}
void ABlasterHUD::AddElimAnnouncement(FString Attacker, FString Victim)
{
OwningPlayer = OwningPlayer == nullptr ? GetOwningPlayerController() : OwningPlayer;
if (OwningPlayer && ElimAnnouncementClass)
{
UElimAnnouncement* ElimAnnouncementWidget = CreateWidget<UElimAnnouncement>(OwningPlayer, ElimAnnouncementClass);
if (ElimAnnouncementWidget)
{
ElimAnnouncementWidget->SetElimAnnouncementText(Attacker, Victim);
ElimAnnouncementWidget->AddToViewport();
for (UElimAnnouncement* Msg : ElimMessages)
{
if (Msg && Msg->AnnouncementBox)
{
UCanvasPanelSlot* CanvasSlot = UWidgetLayoutLibrary::SlotAsCanvasSlot(Msg->AnnouncementBox);
FVector2D Position = CanvasSlot->GetPosition();
FVector2D NewPosition(
CanvasSlot->GetPosition().X,
Position.Y - CanvasSlot->GetSize().Y
);
CanvasSlot->SetPosition(NewPosition);
}
}
ElimMessages.Add(ElimAnnouncementWidget);
FTimerHandle ElimMsgTimer;
FTimerDelegate ElimMsgDelegate;
ElimMsgDelegate.BindUFunction(this, FName("ElimAnnouncementTimerFinished"), ElimAnnouncementWidget);
GetWorldTimerManager().SetTimer(
ElimMsgTimer,
ElimMsgDelegate,
ElimAnnouncementTime,
false
);
}
}
}
void ABlasterHUD::ElimAnnouncementTimerFinished(UElimAnnouncement* MsgToRemove)
{
if (MsgToRemove)
{
MsgToRemove->RemoveFromParent();
}
}
void ABlasterHUD::AddDebugWidget()
{
APlayerController* PlayerController = GetOwningPlayerController();
if (PlayerController && DebugWidgetClass)
{
DebugWidget = CreateWidget<UDebugWidget>(PlayerController, DebugWidgetClass);
DebugWidget->AddToViewport();
}
}
void ABlasterHUD::AddCharacterOverlay()
{
APlayerController* PlayerController = GetOwningPlayerController();
if (PlayerController && CharacterOverlayClass)
{
CharacterOverlay = CreateWidget<UCharacterOverlay>(PlayerController, CharacterOverlayClass);
CharacterOverlay->AddToViewport();
}
}
void ABlasterHUD::AddAnnouncementOverlay()
{
APlayerController* PlayerController = GetOwningPlayerController();
if (PlayerController && AnnouncementClass)
{
Announcement = CreateWidget<UAnnouncement>(PlayerController, AnnouncementClass);
Announcement->AddToViewport();
}
}
void ABlasterHUD::DrawHUD()
{
Super::DrawHUD();
if (GEngine)
{
FVector2D ViewportSize;
GEngine->GameViewport->GetViewportSize(ViewportSize);
const FVector2D ViewportCenter(ViewportSize.X / 2.f, ViewportSize.Y / 2.f);
const float SpreadScaled = CrosshairSpreadMax * HUDPackage.CrosshairSpread;
if (HUDPackage.CrosshairsCenter)
{
const FVector2D Spread(0.f, 0.f);
DrawCrosshair(HUDPackage.CrosshairsCenter, ViewportCenter, Spread, HUDPackage.CrosshairsColor);
}
if (HUDPackage.CrosshairsLeft)
{
const FVector2D Spread(-SpreadScaled, 0.f);
DrawCrosshair(HUDPackage.CrosshairsLeft, ViewportCenter, Spread, HUDPackage.CrosshairsColor);
}
if (HUDPackage.CrosshairsRight)
{
const FVector2D Spread(SpreadScaled, 0.f);
DrawCrosshair(HUDPackage.CrosshairsRight, ViewportCenter, Spread, HUDPackage.CrosshairsColor);
}
if (HUDPackage.CrosshairsTop)
{
const FVector2D Spread(0.f, -SpreadScaled);
DrawCrosshair(HUDPackage.CrosshairsTop, ViewportCenter, Spread, HUDPackage.CrosshairsColor);
}
if (HUDPackage.CrosshairsBottom)
{
const FVector2D Spread(0.f, SpreadScaled);
DrawCrosshair(HUDPackage.CrosshairsBottom, ViewportCenter, Spread, HUDPackage.CrosshairsColor);
}
}
}
void ABlasterHUD::DrawCrosshair(UTexture2D* Texture, FVector2D ViewportCenter, FVector2D Spread, FLinearColor CrosshairColor)
{
const float TextureWidth = Texture->GetSizeX();
const float TextureHeight = Texture->GetSizeY();
const FVector2D TextureDrawPoint(
ViewportCenter.X - (TextureWidth / 2.f) + Spread.X,
ViewportCenter.Y - (TextureHeight / 2.f) + Spread.Y
);
DrawTexture(
Texture,
TextureDrawPoint.X,
TextureDrawPoint.Y,
TextureWidth,
TextureHeight,
0.f,
0.f,
1.f,
1.f,
CrosshairColor
);
}