ExoWest/Source/Exo/Private/Widget/WBP_PlayerUI.cpp

172 lines
4.8 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Widget/WBP_PlayerUI.h"
#include "Widget/UWBP_Buf.h"
#include "Blueprint/WidgetTree.h"
#include "Components/CanvasPanel.h"
#include "Components/CanvasPanelSlot.h"
#include "Components/Image.h"
#include "Components/TextBlock.h"
#include "Widget/UWBP_RoundProgressBar.h"
void UWBP_PlayerUI::SetHp(float hp, float hpMax)
{
if (!HPBar)
return;
HPBar->SetPercent(hp/hpMax);
}
void UWBP_PlayerUI::SetAmmoType(EAmmoType ammoType)
{
if (!AmmoImage)
return;
FSlateBrush NewBrush;
if (!AmmoIconMap.Contains(ammoType))
return;
UTexture2D* IconTexture = AmmoIconMap[ammoType];
if (!IconTexture)
return;
NewBrush.SetResourceObject(IconTexture);
NewBrush.ImageSize = FVector2D(64, 64);
AmmoImage->SetBrush(NewBrush);
}
void UWBP_PlayerUI::SetAmmoNumber(int ammo)
{
if (AmmoText)
AmmoText->SetText(FText::AsNumber(ammo));
}
void UWBP_PlayerUI::SetDialogueText(FText text)
{
timerDialogue = 0.0f;
textDialogue = text;
}
UUWBP_Buf* UWBP_PlayerUI::AddBuf(UTexture2D* texture, float procent)
{
if (!BufClass)
return NULL;
if (!BufPanel)
return NULL;
UUWBP_Buf* NewBuf = WidgetTree->ConstructWidget<UUWBP_Buf>(BufClass, FName(FString::Printf(TEXT("Buff %d"), bufsSlots.Num())));
if (!NewBuf)
return NULL;
float angle=bufsSlots.Num()*BufAngleDiff;
float radians= FMath::DegreesToRadians(angle);
NewBuf->SetTexture(texture);
NewBuf->SetPrecent(procent);
UCanvasPanelSlot* CanvasSlot = Cast<UCanvasPanelSlot>(BufPanel->AddChild(NewBuf));
if (!CanvasSlot)
return NULL;
bufsSlots.Add(CanvasSlot);
CanvasSlot->SetAnchors(FAnchors(0.5f, 0.5f));
CanvasSlot->SetAlignment(FVector2D(0.5f, 0.5f));
CanvasSlot->SetPosition(FVector2D(sin(radians)*BufRange,-cos(radians)*BufRange));
CanvasSlot->SetAutoSize(true);
updateBufPosition();
return NewBuf;
}
void UWBP_PlayerUI::SetVisibilityHP(bool visibility)
{
showHp=visibility;
}
void UWBP_PlayerUI::SetVisibilityBuf(bool visibility)
{
showBuf=visibility;
}
void UWBP_PlayerUI::SetVisibilityAmmo(bool visibility)
{
showAmmo=visibility;
}
void UWBP_PlayerUI::SetVisibilityViewFinder(bool visibility)
{
showViewFinder=visibility;
}
void UWBP_PlayerUI::SetShootingViewFinder(bool Shooting)
{
shootingViewFinder=Shooting;
}
void UWBP_PlayerUI::SetVisibilityDialogue(bool visibility)
{
showDialogue=visibility;
}
void UWBP_PlayerUI::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
Super::NativeTick(MyGeometry, InDeltaTime);
float showSpeed=DisplaySpeed*InDeltaTime;
opacityHp = FMath::Clamp(opacityHp + showSpeed * (showHp?1:-1), 0.0f, 1.0f);
opacityViewFinder = FMath::Clamp(opacityViewFinder + showSpeed * (showViewFinder?1:-1), 0.0f, 1.0f);
opacityBuf = FMath::Clamp(opacityBuf + showSpeed * (showBuf?1:-1), 0.0f, 1.0f);
opacityAmmo = FMath::Clamp(opacityAmmo + showSpeed * (showAmmo?1:-1), 0.0f, 1.0f);
opacityDialogue = FMath::Clamp(opacityDialogue + showSpeed * (showDialogue?1:-1), 0.0f, 1.0f);
shootingViewFinderPercent=FMath::Clamp(shootingViewFinderPercent + showSpeed * (shootingViewFinder?1:-1), 0.0f, 1.0f);
timerDialogue += DialogueSpeed * InDeltaTime;
FString OriginalString = textDialogue.ToString();
int textDialogueSize = OriginalString.Len() * FMath::Clamp(timerDialogue / DialogueSpeed, 0.0f, 1.0f);
FString Substring = OriginalString.Left(textDialogueSize);
FText text = FText::FromString(Substring);
if (DialogueText)
DialogueText->SetText(text);
if (HPBar)
HPBar->SetRenderOpacity(opacityHp);
if (ViewFinder)
ViewFinder->SetRenderOpacity(opacityViewFinder);
if (BufPanel)
BufPanel->SetRenderOpacity(opacityBuf);
if (AmmoImage)
AmmoPanel->SetRenderOpacity(opacityAmmo);
if (DialoguePanel)
DialoguePanel->SetRenderOpacity(opacityDialogue);
if (!DynamicViewFinderMaterial)
{
if (!ViewFinder)
return;
FSlateBrush brush= ViewFinder->GetBrush();
UMaterialInterface* material=Cast<UMaterialInterface>(brush.GetResourceObject());
if (!material)
return;
DynamicViewFinderMaterial = UMaterialInstanceDynamic::Create(material, this);
if (DynamicViewFinderMaterial)
{
FSlateBrush newBrush= brush;
newBrush.SetResourceObject(DynamicViewFinderMaterial);
ViewFinder->SetBrush(newBrush);
}
}
if (DynamicViewFinderMaterial)
{
float changeViewFinderProcent=FMath::Clamp( FMath::Pow(shootingViewFinderPercent,3),0,1.0f);
DynamicViewFinderMaterial->SetScalarParameterValue("Percent",changeViewFinderProcent);
ViewFinder->SetRenderScale(FVector2D(1.0f, 1.0f)*((1.0f-changeViewFinderProcent)*3+1));
}
}
void UWBP_PlayerUI::updateBufPosition()
{
float startAngle =- (bufsSlots.Num() - 1) / 2.0f * BufAngleDiff;
for (int i=0;i<bufsSlots.Num();i++)
{
float radians = FMath::DegreesToRadians(startAngle+i*BufAngleDiff);
bufsSlots[i]->SetPosition(FVector2D(sin(radians)*BufRange,-cos(radians)*BufRange));
}
}