Quick widgets refactor

This commit is contained in:
baz 2025-07-28 23:42:39 +01:00
parent 8d8da3ddc1
commit ec818cfc55
22 changed files with 153 additions and 180 deletions

View File

@ -9,41 +9,41 @@ void UHUDWidget::Init()
{ {
} }
void UHUDWidget::UpdateEXPBar(float currentLevelPercent) void UHUDWidget::UpdateEXPBar(float CurrentLevelPercent)
{ {
EXPbar->SetPercent(currentLevelPercent); EXPbar->SetPercent(CurrentLevelPercent);
} }
void UHUDWidget::UpdateLevelBlock(int level) void UHUDWidget::UpdateLevelBlock(int Level)
{ {
LevelBlock->SetText(FText::FromString("LV" + FString::FromInt(level))); LevelBlock->SetText(FText::FromString("LV" + FString::FromInt(Level)));
} }
void UHUDWidget::UpdateTimerBlock(float deltaTime) void UHUDWidget::UpdateTimerBlock(float DeltaTime)
{ {
int timeSinceStart = FMath::FloorToInt(deltaTime); int TimeSinceStart = FMath::FloorToInt(DeltaTime);
FString mins = FString::FromInt(timeSinceStart / 60); FString Mins = FString::FromInt(TimeSinceStart / 60);
if (timeSinceStart / 60 < 10) if (TimeSinceStart / 60 < 10)
{ {
mins = "0" + mins; Mins = "0" + Mins;
}
FString secs = FString::FromInt(timeSinceStart % 60);
if (timeSinceStart % 60 < 10)
{
secs = "0" + secs;
} }
TimerBLock->SetText(FText::FromString(mins + ":" + secs)); FString Secs = FString::FromInt(TimeSinceStart % 60);
if (TimeSinceStart % 60 < 10)
{
Secs = "0" + Secs;
}
TimerBLock->SetText(FText::FromString(Mins + ":" + Secs));
} }
void UHUDWidget::UpdateKillBlock(int killCount) void UHUDWidget::UpdateKillBlock(int KillCount)
{ {
KillBLock->SetText(FText::FromString("Kills: " + FString::FromInt(killCount))); KillBLock->SetText(FText::FromString("Kills: " + FString::FromInt(KillCount)));
} }
void UHUDWidget::UpdateGoldBlock(int goldCount) void UHUDWidget::UpdateGoldBlock(int GoldCount)
{ {
GoldBLock->SetText(FText::FromString("Gold: " + FString::FromInt(goldCount))); GoldBLock->SetText(FText::FromString("Gold: " + FString::FromInt(GoldCount)));
} }

View File

@ -15,18 +15,17 @@ UCLASS()
class VAMPIRES_API UHUDWidget : public UUserWidget class VAMPIRES_API UHUDWidget : public UUserWidget
{ {
GENERATED_BODY() GENERATED_BODY()
public: protected:
UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UProgressBar* EXPbar; UProgressBar* EXPbar;
UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UTextBlock* LevelBlock; UTextBlock* LevelBlock;
UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UTextBlock* TimerBLock; UTextBlock* TimerBLock;
UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UTextBlock* KillBLock; UTextBlock* KillBLock;
@ -34,20 +33,20 @@ public:
UTextBlock* GoldBLock; UTextBlock* GoldBLock;
void Init(); void Init();
public:
UFUNCTION() UFUNCTION()
void UpdateEXPBar(float currentLevelPercent); void UpdateEXPBar(float CurrentLevelPercent);
UFUNCTION() UFUNCTION()
void UpdateLevelBlock(int level); void UpdateLevelBlock(int Level);
UFUNCTION() UFUNCTION()
void UpdateTimerBlock(float deltaTime); void UpdateTimerBlock(float DeltaTime);
UFUNCTION() UFUNCTION()
void UpdateKillBlock(int killCount); void UpdateKillBlock(int KillCount);
UFUNCTION() UFUNCTION()
void UpdateGoldBlock(int goldCount); void UpdateGoldBlock(int GoldCount);
}; };

View File

@ -12,23 +12,23 @@ void UHealthbarWidget::NativeConstruct()
{ {
Super::NativeConstruct(); Super::NativeConstruct();
if (ACharacter* character = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)) if (ACharacter* Character = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0))
{ {
if (UHealthComponent* healthComponent = character->FindComponentByClass<UHealthComponent>()) if (UHealthComponent* HealthComponent = Character->FindComponentByClass<UHealthComponent>())
{ {
healthComponent->OnDamaged.AddDynamic(this, &UHealthbarWidget::UpdateHealthBar); HealthComponent->OnDamaged.AddDynamic(this, &UHealthbarWidget::UpdateHealthBar);
UpdateHealthBar({}); UpdateHealthBar({});
} }
} }
} }
void UHealthbarWidget::UpdateHealthBar(FDamageInfo damageInfo) void UHealthbarWidget::UpdateHealthBar(FDamageInfo DamageInfo)
{ {
if (ACharacter* character = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)) if (ACharacter* Character = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0))
{ {
if (UHealthComponent* healthComponent = character->FindComponentByClass<UHealthComponent>()) if (UHealthComponent* HealthComponent = Character->FindComponentByClass<UHealthComponent>())
{ {
float percent = healthComponent->GetCurrentHealth() / healthComponent->GetMaxHealth(); float percent = HealthComponent->GetCurrentHealth() / HealthComponent->GetMaxHealth();
HealthBar->SetPercent(percent); HealthBar->SetPercent(percent);
} }
} }

View File

@ -6,6 +6,7 @@
#include "Blueprint/UserWidget.h" #include "Blueprint/UserWidget.h"
#include "HealthbarWidget.generated.h" #include "HealthbarWidget.generated.h"
struct FDamageInfo;
class UProgressBar; class UProgressBar;
/** /**
* *
@ -15,13 +16,13 @@ class VAMPIRES_API UHealthbarWidget : public UUserWidget
{ {
GENERATED_BODY() GENERATED_BODY()
public: protected:
UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UProgressBar* HealthBar; TObjectPtr<UProgressBar> HealthBar;
virtual void NativeConstruct() override; virtual void NativeConstruct() override;
private: private:
UFUNCTION() UFUNCTION()
void UpdateHealthBar(FDamageInfo damageInfo); void UpdateHealthBar(FDamageInfo DamageInfo);
}; };

View File

@ -40,50 +40,50 @@ void ULevelUpWidget::NativeConstruct()
TArray<AWeapon*> Inventory = InventoryComponent->GetInventory(); TArray<AWeapon*> Inventory = InventoryComponent->GetInventory();
// Get list of weapons that the player owns that can be upgraded // Get list of weapons that the player owns that can be upgraded
TArray<UUpgradeButtonDataObject*> upgradeItems; TArray<UUpgradeButtonDataObject*> UpgradeItems;
for (AWeapon* weapon : Inventory) for (AWeapon* Weapon : Inventory)
{ {
if (weapon->GetWeaponLevel() < weapon->GetUpgradeDescriptions().Num()) if (Weapon->GetWeaponLevel() < Weapon->GetUpgradeDescriptions().Num())
{ {
UUpgradeButtonDataObject* Temp = NewObject<UUpgradeButtonDataObject>(this); UUpgradeButtonDataObject* Temp = NewObject<UUpgradeButtonDataObject>(this);
Temp->SetData(weapon, this); Temp->SetData(Weapon, this);
upgradeItems.Add(Temp); UpgradeItems.Add(Temp);
} }
} }
// Get list of weapons that the player can still obtain // Get list of weapons that the player can still obtain
TArray<TSubclassOf<AWeapon>> ObtainableWeapons = InventoryComponent->obtainableWeapons; TArray<TSubclassOf<AWeapon>> ObtainableWeapons = InventoryComponent->obtainableWeapons;
for (TSubclassOf<AWeapon> weapon : ObtainableWeapons) for (TSubclassOf<AWeapon> Weapon : ObtainableWeapons)
{ {
UUpgradeButtonDataObject* Temp = NewObject<UUpgradeButtonDataObject>(this); UUpgradeButtonDataObject* Temp = NewObject<UUpgradeButtonDataObject>(this);
Temp->SetData(weapon, this); Temp->SetData(Weapon, this);
upgradeItems.Add(Temp); UpgradeItems.Add(Temp);
} }
// If no valid options exist, populate list with default options // If no valid options exist, populate list with default options
if (upgradeItems.Num() == 0) if (UpgradeItems.Num() == 0)
{ {
UUpgradeButtonDataObject* tempHealth = NewObject<UUpgradeButtonDataObject>(this); UUpgradeButtonDataObject* TempHealth = NewObject<UUpgradeButtonDataObject>(this);
tempHealth->SetData(FText::FromString("Health"), TempHealth->SetData(FText::FromString("Health"),
FText::FromString("Recover 10% of your health"), FText::FromString("Recover 10% of your health"),
nullptr, nullptr,
this); this);
upgradeItems.Add(tempHealth); UpgradeItems.Add(TempHealth);
UUpgradeButtonDataObject* tempGold = NewObject<UUpgradeButtonDataObject>(this); UUpgradeButtonDataObject* TempGold = NewObject<UUpgradeButtonDataObject>(this);
tempGold->SetData(FText::FromString("Gold"), TempGold->SetData(FText::FromString("Gold"),
FText::FromString("Gain 10 gold"), FText::FromString("Gain 10 gold"),
nullptr, nullptr,
this); this);
upgradeItems.Add(tempGold); UpgradeItems.Add(TempGold);
} }
// Select up to three random options from the list of options // Select up to three random options from the list of options
for (int i = 0; i < 3 && upgradeItems.Num() > 0; i++) for (int i = 0; i < 3 && UpgradeItems.Num() > 0; i++)
{ {
int rand = FMath::RandRange(0, upgradeItems.Num() - 1); int Rand = FMath::RandRange(0, UpgradeItems.Num() - 1);
UpgradesListView->AddItem(upgradeItems[rand]); UpgradesListView->AddItem(UpgradeItems[Rand]);
upgradeItems.RemoveAt(rand); UpgradeItems.RemoveAt(Rand);
} }
} }
SetIsFocusable(true); SetIsFocusable(true);
@ -93,11 +93,11 @@ void ULevelUpWidget::ResumeButtonClicked()
{ {
RemoveFromParent(); RemoveFromParent();
if (APlayerController* playerController = UGameplayStatics::GetPlayerController(GetWorld(), 0)) if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{ {
UWidgetBlueprintLibrary::SetInputMode_GameOnly(playerController); UWidgetBlueprintLibrary::SetInputMode_GameOnly(PlayerController);
playerController->bShowMouseCursor = false; PlayerController->bShowMouseCursor = false;
playerController->SetPause(false); PlayerController->SetPause(false);
} }
SetIsFocusable(false); SetIsFocusable(false);

View File

@ -17,18 +17,16 @@ class VAMPIRES_API ULevelUpWidget : public UUserWidget
{ {
GENERATED_BODY() GENERATED_BODY()
public: protected:
UPROPERTY(BlueprintReadWrite, meta=(BindWidget)) UPROPERTY(BlueprintReadWrite, meta=(BindWidget))
UButton* ResumeButton; UButton* ResumeButton;
UPROPERTY(BlueprintReadWrite, meta=(BindWidget)) UPROPERTY(BlueprintReadWrite, meta=(BindWidget))
UListView* UpgradesListView; UListView* UpgradesListView;
virtual void NativeConstruct() override; virtual void NativeConstruct() override;
private: private:
UFUNCTION() UFUNCTION()
void ResumeButtonClicked(); void ResumeButtonClicked();
}; };

View File

@ -51,27 +51,14 @@ void UMainMenuWidget::NewGameButtonOnClicked()
{ {
RemoveFromParent(); RemoveFromParent();
UUserWidget* selectWeaponWidget = CreateWidget<UUserWidget, APlayerController*>( UUserWidget* SelectWeaponWidget = CreateWidget<UUserWidget, APlayerController*>(
UGameplayStatics::GetPlayerController(GetWorld(), 0), NewGameMenuWidget); UGameplayStatics::GetPlayerController(GetWorld(), 0), NewGameMenuWidget);
if (selectWeaponWidget) if (SelectWeaponWidget)
{ {
selectWeaponWidget->AddToViewport(); SelectWeaponWidget->AddToViewport();
} }
} }
// if (!NewGameLevel.IsNull())
// {
// UGameplayStatics::OpenLevelBySoftObjectPtr(GetWorld(), NewGameLevel);
// }
//
// if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
// {
// PlayerController->bShowMouseCursor = false;
// UWidgetBlueprintLibrary::SetInputMode_GameOnly(PlayerController);
// }
//
// SetIsFocusable(false);
} }
void UMainMenuWidget::QuitButtonOnClicked() void UMainMenuWidget::QuitButtonOnClicked()

View File

@ -17,7 +17,7 @@ class VAMPIRES_API UMainMenuWidget : public UVampireInteractiveWidget
// TODO: Add options menu // TODO: Add options menu
public: protected:
UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
TObjectPtr<UButton> NewGameButton; TObjectPtr<UButton> NewGameButton;
@ -38,7 +38,7 @@ public:
private: private:
UPROPERTY() UPROPERTY()
TObjectPtr<UUserWidget> currentNewGameWidget; TObjectPtr<UUserWidget> CurrentNewGameWidget;
public: public:
virtual void NativeConstruct() override; virtual void NativeConstruct() override;

View File

@ -27,11 +27,11 @@ void UPauseWidget::ResumeButtonClicked()
{ {
RemoveFromParent(); RemoveFromParent();
if (APlayerController* playerController = UGameplayStatics::GetPlayerController(GetWorld(), 0)) if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{ {
UWidgetBlueprintLibrary::SetInputMode_GameOnly(playerController); UWidgetBlueprintLibrary::SetInputMode_GameOnly(PlayerController);
playerController->bShowMouseCursor = false; PlayerController->bShowMouseCursor = false;
playerController->SetPause(false); PlayerController->SetPause(false);
} }
SetIsFocusable(false); SetIsFocusable(false);

View File

@ -16,7 +16,6 @@ class VAMPIRES_API UPauseWidget : public UUserWidget
GENERATED_BODY() GENERATED_BODY()
public: public:
UPROPERTY(BlueprintReadWrite, meta=(BindWidget)) UPROPERTY(BlueprintReadWrite, meta=(BindWidget))
UButton* ResumeButton; UButton* ResumeButton;
@ -25,7 +24,6 @@ public:
virtual void NativeConstruct() override; virtual void NativeConstruct() override;
private: private:
UFUNCTION() UFUNCTION()
void ResumeButtonClicked(); void ResumeButtonClicked();
}; };

View File

@ -29,10 +29,10 @@ void USelectWeaponWidget::NativeConstruct()
if (UpgradesListView) if (UpgradesListView)
{ {
// Get a list of weapons that the player owns that can be upgraded // Get a list of weapons that the player owns that can be upgraded
for (TSubclassOf<AWeapon> weapon : starterWeapons) for (TSubclassOf<AWeapon> Weapon : StarterWeapons)
{ {
UStarterWeaponButtonDataObject* Temp = NewObject<UStarterWeaponButtonDataObject>(this); UStarterWeaponButtonDataObject* Temp = NewObject<UStarterWeaponButtonDataObject>(this);
Temp->SetData(weapon, this); Temp->SetData(Weapon, this);
UpgradesListView->AddItem(Temp); UpgradesListView->AddItem(Temp);
} }
} }
@ -44,12 +44,12 @@ void USelectWeaponWidget::BackButtonClicked()
{ {
RemoveFromParent(); RemoveFromParent();
UUserWidget* selectWeaponWidget = CreateWidget<UUserWidget, APlayerController*>( UUserWidget* SelectWeaponWidget = CreateWidget<UUserWidget, APlayerController*>(
UGameplayStatics::GetPlayerController(GetWorld(), 0), PreviousWidget); UGameplayStatics::GetPlayerController(GetWorld(), 0), PreviousWidget);
if (selectWeaponWidget) if (SelectWeaponWidget)
{ {
selectWeaponWidget->AddToViewport(); SelectWeaponWidget->AddToViewport();
} }
} }
} }

View File

@ -17,7 +17,7 @@ UCLASS()
class VAMPIRES_API USelectWeaponWidget : public UVampireInteractiveWidget class VAMPIRES_API USelectWeaponWidget : public UVampireInteractiveWidget
{ {
GENERATED_BODY() GENERATED_BODY()
public: protected:
UPROPERTY(BlueprintReadWrite, meta=(BindWidget)) UPROPERTY(BlueprintReadWrite, meta=(BindWidget))
TObjectPtr<UButton> BackButton; TObjectPtr<UButton> BackButton;
@ -29,7 +29,7 @@ public:
TObjectPtr<UListView> UpgradesListView; TObjectPtr<UListView> UpgradesListView;
UPROPERTY(EditDefaultsOnly) UPROPERTY(EditDefaultsOnly)
TArray<TSubclassOf<AWeapon>> starterWeapons; TArray<TSubclassOf<AWeapon>> StarterWeapons;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSubclassOf<class UUserWidget> PreviousWidget; TSubclassOf<class UUserWidget> PreviousWidget;

View File

@ -5,29 +5,29 @@
#include "vampires/Weapon.h" #include "vampires/Weapon.h"
void UStarterWeaponButtonDataObject::SetData(AWeapon* Weapon, UUserWidget* parent) void UStarterWeaponButtonDataObject::SetData(AWeapon* Weapon, UUserWidget* ParentWidget)
{ {
WeaponName = Weapon->GetWeaponName(); WeaponName = Weapon->GetWeaponName();
Description = Weapon->GetDescription(); WeaponDescription = Weapon->GetDescription();
WeaponIcon = Weapon->GetIcon(); WeaponIcon = Weapon->GetIcon();
WeaponInstance = Weapon; WeaponInstance = Weapon;
Parent = parent; Parent = ParentWidget;
} }
void UStarterWeaponButtonDataObject::SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* parent) void UStarterWeaponButtonDataObject::SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* ParentWidget)
{ {
if (AWeapon* tempWeapon = NewObject<AWeapon>(this, Weapon)) if (AWeapon* tempWeapon = NewObject<AWeapon>(this, Weapon))
{ {
SetData(tempWeapon, parent); SetData(tempWeapon, ParentWidget);
WeaponTemplate = Weapon; WeaponTemplate = Weapon;
} }
} }
void UStarterWeaponButtonDataObject::SetData(FText weaponName, FText description, TObjectPtr<UTexture2D> weaponIcon, void UStarterWeaponButtonDataObject::SetData(FText NewWeaponName, FText NewWeaponDescription, TObjectPtr<UTexture2D> NewWeaponIcon,
UUserWidget* parent) UUserWidget* ParentWidget)
{ {
WeaponName = weaponName; WeaponName = NewWeaponName;
Description = description; WeaponDescription = NewWeaponDescription;
WeaponIcon = weaponIcon; WeaponIcon = NewWeaponIcon;
Parent = parent; Parent = ParentWidget;
} }

View File

@ -19,7 +19,7 @@ public:
FText WeaponName; FText WeaponName;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
FText Description; FText WeaponDescription;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UTexture2D> WeaponIcon; TObjectPtr<UTexture2D> WeaponIcon;
@ -33,7 +33,7 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UUserWidget> Parent; TObjectPtr<UUserWidget> Parent;
void SetData(AWeapon* Weapon, UUserWidget* parent); void SetData(AWeapon* Weapon, UUserWidget* ParentWidget);
void SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* parent); void SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* ParentWidget);
void SetData(FText weaponName, FText description, TObjectPtr<UTexture2D> weaponIcon, UUserWidget* parent); void SetData(FText NewWeaponName, FText NewWeaponDescription, TObjectPtr<UTexture2D> NewWeaponIcon, UUserWidget* ParentWidget);
}; };

View File

@ -18,12 +18,10 @@ void UStarterWeaponButtonWidget::NativeConstruct()
void UStarterWeaponButtonWidget::NativeOnListItemObjectSet(UObject* ListItemObject) void UStarterWeaponButtonWidget::NativeOnListItemObjectSet(UObject* ListItemObject)
{ {
UStarterWeaponButtonDataObject* Item = Cast<UStarterWeaponButtonDataObject>(ListItemObject); if (UStarterWeaponButtonDataObject* Item = Cast<UStarterWeaponButtonDataObject>(ListItemObject))
if (Item)
{ {
WeaponNameTextBlock->SetText(Item->WeaponName); WeaponNameTextBlock->SetText(Item->WeaponName);
DescriptionTextBlock->SetText(Item->Description); DescriptionTextBlock->SetText(Item->WeaponDescription);
WeaponIcon->SetBrushFromTexture(Item->WeaponIcon); WeaponIcon->SetBrushFromTexture(Item->WeaponIcon);
Parent = Item->Parent; Parent = Item->Parent;
WeaponTemplate = Item->WeaponTemplate; WeaponTemplate = Item->WeaponTemplate;
@ -43,13 +41,13 @@ void UStarterWeaponButtonWidget::NativeOnListItemObjectSet(UObject* ListItemObje
void UStarterWeaponButtonWidget::OnClicked() void UStarterWeaponButtonWidget::OnClicked()
{ {
if (UVampireGameInstance* gameInstance = Cast<UVampireGameInstance>(GetGameInstance())) if (UVampireGameInstance* GameInstance = Cast<UVampireGameInstance>(GetGameInstance()))
{ {
gameInstance->StarterWeapon = WeaponTemplate; GameInstance->StarterWeapon = WeaponTemplate;
if (!gameInstance->GameWorld.IsNull()) if (!GameInstance->GameWorld.IsNull())
{ {
UGameplayStatics::OpenLevelBySoftObjectPtr(GetWorld(), gameInstance->GameWorld); UGameplayStatics::OpenLevelBySoftObjectPtr(GetWorld(), GameInstance->GameWorld);
if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0)) if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0))
{ {

View File

@ -20,7 +20,7 @@ class VAMPIRES_API UStarterWeaponButtonWidget : public UVampireInteractiveWidget
{ {
GENERATED_BODY() GENERATED_BODY()
public: protected:
UPROPERTY(EditDefaultsOnly, meta=(BindWidget)) UPROPERTY(EditDefaultsOnly, meta=(BindWidget))
TObjectPtr<UButton> Body; TObjectPtr<UButton> Body;
@ -39,7 +39,6 @@ public:
UPROPERTY() UPROPERTY()
TObjectPtr<UUserWidget> Parent; TObjectPtr<UUserWidget> Parent;
protected:
virtual void NativeConstruct() override; virtual void NativeConstruct() override;
virtual void NativeOnListItemObjectSet(UObject* ListItemObject) override; virtual void NativeOnListItemObjectSet(UObject* ListItemObject) override;

View File

@ -5,32 +5,32 @@
#include "vampires/Weapon.h" #include "vampires/Weapon.h"
void UUpgradeButtonDataObject::SetData(AWeapon* Weapon, UUserWidget* parent) void UUpgradeButtonDataObject::SetData(AWeapon* Weapon, UUserWidget* ParentWidget)
{ {
WeaponName = Weapon->GetWeaponName(); WeaponName = Weapon->GetWeaponName();
WeaponIcon = Weapon->GetIcon(); WeaponIcon = Weapon->GetIcon();
WeaponInstance = Weapon; WeaponInstance = Weapon;
Parent = parent; Parent = ParentWidget;
if (Weapon->GetUpgradeDescriptions().Num() > Weapon->GetWeaponLevel()) if (Weapon->GetUpgradeDescriptions().Num() > Weapon->GetWeaponLevel())
{ {
Description = Weapon->GetUpgradeDescriptions()[Weapon->GetWeaponLevel()]; WeaponDescription = Weapon->GetUpgradeDescriptions()[Weapon->GetWeaponLevel()];
} }
} }
void UUpgradeButtonDataObject::SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* parent) void UUpgradeButtonDataObject::SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* ParentWidget)
{ {
if (AWeapon* tempWeapon = NewObject<AWeapon>(this, Weapon)) if (AWeapon* tempWeapon = NewObject<AWeapon>(this, Weapon))
{ {
SetData(tempWeapon, parent); SetData(tempWeapon, ParentWidget);
} }
} }
void UUpgradeButtonDataObject::SetData(FText weaponName, FText description, TObjectPtr<UTexture2D> weaponIcon, void UUpgradeButtonDataObject::SetData(FText NewWeaponName, FText NewDescription, TObjectPtr<UTexture2D> NewWeaponIcon,
UUserWidget* parent) UUserWidget* ParentWidget)
{ {
WeaponName = weaponName; WeaponName = NewWeaponName;
Description = description; WeaponDescription = NewDescription;
WeaponIcon = weaponIcon; WeaponIcon = NewWeaponIcon;
Parent = parent; Parent = ParentWidget;
} }

View File

@ -20,7 +20,7 @@ public:
FText WeaponName; FText WeaponName;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
FText Description; FText WeaponDescription;
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UTexture2D> WeaponIcon; TObjectPtr<UTexture2D> WeaponIcon;
@ -34,7 +34,7 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
TObjectPtr<UUserWidget> Parent; TObjectPtr<UUserWidget> Parent;
void SetData(AWeapon* Weapon, UUserWidget* parent); void SetData(AWeapon* Weapon, UUserWidget* ParentWidget);
void SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* parent); void SetData(TSubclassOf<AWeapon> Weapon, UUserWidget* ParentWidget);
void SetData(FText weaponName, FText description, TObjectPtr<UTexture2D> weaponIcon, UUserWidget* parent); void SetData(FText NewWeaponName, FText NewDescription, TObjectPtr<UTexture2D> NewWeaponIcon, UUserWidget* ParentWidget);
}; };

View File

@ -22,12 +22,10 @@ void UUpgradeButtonWidget::NativeConstruct()
void UUpgradeButtonWidget::NativeOnListItemObjectSet(UObject* ListItemObject) void UUpgradeButtonWidget::NativeOnListItemObjectSet(UObject* ListItemObject)
{ {
UUpgradeButtonDataObject* Item = Cast<UUpgradeButtonDataObject>(ListItemObject); if (UUpgradeButtonDataObject* Item = Cast<UUpgradeButtonDataObject>(ListItemObject))
if (Item)
{ {
WeaponNameTextBlock->SetText(Item->WeaponName); WeaponNameTextBlock->SetText(Item->WeaponName);
DescriptionTextBlock->SetText(Item->Description); DescriptionTextBlock->SetText(Item->WeaponDescription);
WeaponIcon->SetBrushFromTexture(Item->WeaponIcon); WeaponIcon->SetBrushFromTexture(Item->WeaponIcon);
Parent = Item->Parent; Parent = Item->Parent;
@ -83,7 +81,7 @@ void UUpgradeButtonWidget::NativeOnListItemObjectSet(UObject* ListItemObject)
void UUpgradeButtonWidget::OnClicked() void UUpgradeButtonWidget::OnClicked()
{ {
APlayerController* playerController = UGameplayStatics::GetPlayerController(GetWorld(), 0); APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
switch (UpgradeType) switch (UpgradeType)
{ {
@ -92,8 +90,7 @@ void UUpgradeButtonWidget::OnClicked()
break; break;
case NewWeapon: case NewWeapon:
if (UWeaponInventoryComponent* Inventory = playerController->GetPawn()->GetComponentByClass< if (UWeaponInventoryComponent* Inventory = PlayerController->GetPawn()->GetComponentByClass<UWeaponInventoryComponent>())
UWeaponInventoryComponent>())
{ {
Inventory->AddWeaponToInventory(WeaponTemplate); Inventory->AddWeaponToInventory(WeaponTemplate);
Inventory->obtainableWeapons.Remove(WeaponTemplate); Inventory->obtainableWeapons.Remove(WeaponTemplate);
@ -101,22 +98,21 @@ void UUpgradeButtonWidget::OnClicked()
break; break;
case Health: case Health:
if (playerController) if (PlayerController)
{ {
if (UHealthComponent* healthComponent = playerController->GetPawn()->GetComponentByClass< if (UHealthComponent* HealthComponent = PlayerController->GetPawn()->GetComponentByClass<UHealthComponent>())
UHealthComponent>())
{ {
healthComponent->RecoverHealth(healthComponent->GetMaxHealth() / 10.0f); HealthComponent->RecoverHealth(HealthComponent->GetMaxHealth() / 10.0f);
} }
} }
break; break;
case Gold: case Gold:
if (playerController) if (PlayerController)
{ {
if (UGoldComponent* goldComponent = playerController->GetPawn()->GetComponentByClass<UGoldComponent>()) if (UGoldComponent* GoldComponent = PlayerController->GetPawn()->GetComponentByClass<UGoldComponent>())
{ {
goldComponent->IncrementGold(10); GoldComponent->IncrementGold(10);
} }
} }
break; break;
@ -129,11 +125,11 @@ void UUpgradeButtonWidget::OnClicked()
{ {
Parent->RemoveFromParent(); Parent->RemoveFromParent();
if (playerController) if (PlayerController)
{ {
UWidgetBlueprintLibrary::SetInputMode_GameOnly(playerController); UWidgetBlueprintLibrary::SetInputMode_GameOnly(PlayerController);
playerController->bShowMouseCursor = false; PlayerController->bShowMouseCursor = false;
playerController->SetPause(false); PlayerController->SetPause(false);
} }
Parent->SetIsFocusable(false); Parent->SetIsFocusable(false);

View File

@ -31,7 +31,7 @@ class VAMPIRES_API UUpgradeButtonWidget : public UVampireInteractiveWidget, publ
{ {
GENERATED_BODY() GENERATED_BODY()
public: protected:
UPROPERTY(EditDefaultsOnly, meta=(BindWidget)) UPROPERTY(EditDefaultsOnly, meta=(BindWidget))
TObjectPtr<UButton> Body; TObjectPtr<UButton> Body;
@ -80,11 +80,10 @@ protected:
private: private:
UFUNCTION() UFUNCTION()
virtual void OnClicked(); virtual void OnClicked();
UFUNCTION() UFUNCTION()
void OnHoveredDelegate() { SetTextBlockHovered(WeaponNameTextBlock); SetTextBlockHovered(DescriptionTextBlock); } void OnHoveredDelegate() { SetTextBlockHovered(WeaponNameTextBlock); SetTextBlockHovered(DescriptionTextBlock); }
UFUNCTION() UFUNCTION()
void OnUnhoveredDelegate() { SetTextBlockUnhovered(WeaponNameTextBlock); SetTextBlockUnhovered(DescriptionTextBlock); } void OnUnhoveredDelegate() { SetTextBlockUnhovered(WeaponNameTextBlock); SetTextBlockUnhovered(DescriptionTextBlock); }
}; };

View File

@ -7,11 +7,11 @@
#include "GameFramework/GameUserSettings.h" #include "GameFramework/GameUserSettings.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
void UVampireInteractiveWidget::SetReturnScreen(UUserWidget* userWidget) void UVampireInteractiveWidget::SetReturnScreen(UUserWidget* UserWidget)
{ {
if (userWidget) if (UserWidget)
{ {
PreviousScreen = userWidget; PreviousScreen = UserWidget;
} }
} }

View File

@ -14,12 +14,11 @@ UCLASS()
class VAMPIRES_API UVampireInteractiveWidget : public UUserWidget class VAMPIRES_API UVampireInteractiveWidget : public UUserWidget
{ {
GENERATED_BODY() GENERATED_BODY()
public:
protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TObjectPtr<USoundBase> ButtonHoveredSound; TObjectPtr<USoundBase> ButtonHoveredSound;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
FLinearColor ButtonHoveredTextColor = {0, 1, 0, 1}; FLinearColor ButtonHoveredTextColor = {0, 1, 0, 1};
@ -37,9 +36,8 @@ protected:
TObjectPtr<UUserWidget> PreviousScreen; TObjectPtr<UUserWidget> PreviousScreen;
public: public:
UFUNCTION() UFUNCTION()
void SetReturnScreen(UUserWidget* userWidget); void SetReturnScreen(UUserWidget* UserWidget);
protected: protected:
UFUNCTION() UFUNCTION()