blaster/Source/Blaster/HUD/BlasterHUD.cpp

108 lines
2.9 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-07 09:04:11 +00:00
#include "Blueprint/UserWidget.h"
2022-05-12 15:05:30 +00:00
#include "Components/TextBlock.h"
2022-05-07 09:04:11 +00:00
void ABlasterHUD::BeginPlay()
{
Super::BeginPlay();
}
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
}