blaster/Source/Blaster/HUD/BlasterHUD.cpp

157 lines
4.3 KiB
C++
Raw Normal View History

2022-05-05 15:57:57 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "BlasterHUD.h"
2022-05-10 11:31:39 +00:00
#include "Announcement.h"
2022-05-07 09:04:11 +00:00
#include "CharacterOverlay.h"
2022-05-12 15:05:30 +00:00
#include "DebugWidget.h"
2022-05-29 10:42:42 +00:00
#include "ElimAnnouncement.h"
2022-05-07 09:04:11 +00:00
#include "Blueprint/UserWidget.h"
2022-05-29 11:02:43 +00:00
#include "Blueprint/WidgetLayoutLibrary.h"
#include "Components/CanvasPanelSlot.h"
#include "Components/HorizontalBox.h"
2022-05-07 09:04:11 +00:00
void ABlasterHUD::BeginPlay()
{
Super::BeginPlay();
}
2022-05-29 11:02:43 +00:00
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();
}
}
2022-05-12 15:05:30 +00:00
void ABlasterHUD::AddDebugWidget()
{
APlayerController* PlayerController = GetOwningPlayerController();
if (PlayerController && DebugWidgetClass)
{
DebugWidget = CreateWidget<UDebugWidget>(PlayerController, DebugWidgetClass);
DebugWidget->AddToViewport();
}
}
2022-05-07 09:04:11 +00:00
void ABlasterHUD::AddCharacterOverlay()
{
APlayerController* PlayerController = GetOwningPlayerController();
if (PlayerController && CharacterOverlayClass)
{
CharacterOverlay = CreateWidget<UCharacterOverlay>(PlayerController, CharacterOverlayClass);
CharacterOverlay->AddToViewport();
}
}
2022-05-10 11:31:39 +00:00
void ABlasterHUD::AddAnnouncementOverlay()
{
APlayerController* PlayerController = GetOwningPlayerController();
if (PlayerController && AnnouncementClass)
{
Announcement = CreateWidget<UAnnouncement>(PlayerController, AnnouncementClass);
Announcement->AddToViewport();
}
}
2022-05-05 15:57:57 +00:00
void ABlasterHUD::DrawHUD()
{
Super::DrawHUD();
2022-05-05 16:16:59 +00:00
if (GEngine)
{
2022-05-05 16:48:17 +00:00
FVector2D ViewportSize;
2022-05-05 16:16:59 +00:00
GEngine->GameViewport->GetViewportSize(ViewportSize);
const FVector2D ViewportCenter(ViewportSize.X / 2.f, ViewportSize.Y / 2.f);
2022-05-05 16:48:17 +00:00
const float SpreadScaled = CrosshairSpreadMax * HUDPackage.CrosshairSpread;
2022-05-05 16:16:59 +00:00
if (HUDPackage.CrosshairsCenter)
{
2022-05-05 16:48:17 +00:00
const FVector2D Spread(0.f, 0.f);
2022-05-12 13:37:49 +00:00
DrawCrosshair(HUDPackage.CrosshairsCenter, ViewportCenter, Spread, HUDPackage.CrosshairsColor);
2022-05-05 16:16:59 +00:00
}
if (HUDPackage.CrosshairsLeft)
{
2022-05-05 16:48:17 +00:00
const FVector2D Spread(-SpreadScaled, 0.f);
2022-05-12 13:37:49 +00:00
DrawCrosshair(HUDPackage.CrosshairsLeft, ViewportCenter, Spread, HUDPackage.CrosshairsColor);
2022-05-05 16:16:59 +00:00
}
if (HUDPackage.CrosshairsRight)
{
2022-05-05 16:48:17 +00:00
const FVector2D Spread(SpreadScaled, 0.f);
2022-05-12 13:37:49 +00:00
DrawCrosshair(HUDPackage.CrosshairsRight, ViewportCenter, Spread, HUDPackage.CrosshairsColor);
2022-05-05 16:16:59 +00:00
}
if (HUDPackage.CrosshairsTop)
{
2022-05-05 16:48:17 +00:00
const FVector2D Spread(0.f, -SpreadScaled);
2022-05-12 13:37:49 +00:00
DrawCrosshair(HUDPackage.CrosshairsTop, ViewportCenter, Spread, HUDPackage.CrosshairsColor);
2022-05-05 16:16:59 +00:00
}
if (HUDPackage.CrosshairsBottom)
{
2022-05-05 16:48:17 +00:00
const FVector2D Spread(0.f, SpreadScaled);
2022-05-12 13:37:49 +00:00
DrawCrosshair(HUDPackage.CrosshairsBottom, ViewportCenter, Spread, HUDPackage.CrosshairsColor);
2022-05-05 16:16:59 +00:00
}
}
}
2022-05-05 20:46:35 +00:00
void ABlasterHUD::DrawCrosshair(UTexture2D* Texture, FVector2D ViewportCenter, FVector2D Spread, FLinearColor CrosshairColor)
2022-05-05 16:16:59 +00:00
{
const float TextureWidth = Texture->GetSizeX();
const float TextureHeight = Texture->GetSizeY();
const FVector2D TextureDrawPoint(
2022-05-05 16:48:17 +00:00
ViewportCenter.X - (TextureWidth / 2.f) + Spread.X,
ViewportCenter.Y - (TextureHeight / 2.f) + Spread.Y
2022-05-05 16:16:59 +00:00
);
DrawTexture(
Texture,
TextureDrawPoint.X,
TextureDrawPoint.Y,
TextureWidth,
TextureHeight,
0.f,
0.f,
1.f,
1.f,
2022-05-05 20:46:35 +00:00
CrosshairColor
2022-05-05 16:16:59 +00:00
);
2022-05-05 15:57:57 +00:00
}