Skip to content

Instantly share code, notes, and snippets.

@KaosSpectrum
Last active October 2, 2023 23:04
Show Gist options
  • Save KaosSpectrum/a7c8b578646d98203400bdcfa96abd4e to your computer and use it in GitHub Desktop.
Save KaosSpectrum/a7c8b578646d98203400bdcfa96abd4e to your computer and use it in GitHub Desktop.
// Fill out your copyright notice in the Description page of Project Settings.
#include "AresBuffDisplayContainer.h"
#include "AbilitySystemComponent.h"
#include "AbilitySystemGlobals.h"
#include "AresBuffDisplaySlot.h"
#include "Components/HorizontalBox.h"
#include "CoreFramework/AresGameplayTags.h"
void UAresBuffDisplayContainer::HandleGameplayEffectAppliedToSelf(UAbilitySystemComponent* AbilitySystemComponent, const FGameplayEffectSpec& GameplayEffectSpec, FActiveGameplayEffectHandle ActiveGameplayEffectHandle)
{
if (SlotToActiveMapping.Contains(ActiveGameplayEffectHandle))
{
return;
}
FGameplayTagContainer AssetTags;
GameplayEffectSpec.GetAllAssetTags(AssetTags);
if (AssetTags.HasTag(AresGameplayTags::Gameplay::Effect::Buff_Visualize))
{
UAresBuffDisplaySlot* BuffDisplaySlot = CreateWidget<UAresBuffDisplaySlot>(GetOwningPlayer(), BuffDisplaySlotWidget);
BuffDisplaySlot->InitWidget(AbilitySystemComponent, GameplayEffectSpec, ActiveGameplayEffectHandle);
BuffSlotBox->AddChildToHorizontalBox(BuffDisplaySlot);
AbilitySystemComponent->OnGameplayEffectRemoved_InfoDelegate(ActiveGameplayEffectHandle)->AddUObject(this, &ThisClass::HandleGameplayEffectRemoved);
SlotToActiveMapping.Add(ActiveGameplayEffectHandle, BuffDisplaySlot);
}
}
void UAresBuffDisplayContainer::NativeConstruct()
{
Super::NativeConstruct();
if (UAbilitySystemComponent* ASC = UAbilitySystemGlobals::GetAbilitySystemComponentFromActor(GetOwningPlayerPawn()))
{
ASC->OnGameplayEffectAppliedDelegateToSelf.AddUObject(this, &UAresBuffDisplayContainer::HandleGameplayEffectAppliedToSelf);
}
}
void UAresBuffDisplayContainer::HandleGameplayEffectRemoved(const FGameplayEffectRemovalInfo& GameplayEffectRemovalInfo)
{
if (UAresBuffDisplaySlot* Slot = SlotToActiveMapping.FindAndRemoveChecked(GameplayEffectRemovalInfo.ActiveEffect->Handle))
{
Slot->HandleGameplayEffectRemoved(GameplayEffectRemovalInfo);
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "CommonActivatableWidget.h"
#include "GameplayEffect.h"
#include "UObject/Object.h"
#include "AresBuffDisplayContainer.generated.h"
class UAresBuffDisplaySlot;
class UHorizontalBox;
/**
*
*/
UCLASS()
class ARESGAME_API UAresBuffDisplayContainer : public UCommonActivatableWidget
{
GENERATED_BODY()
public:
void HandleGameplayEffectAppliedToSelf(UAbilitySystemComponent* AbilitySystemComponent, const FGameplayEffectSpec& GameplayEffectSpec, FActiveGameplayEffectHandle ActiveGameplayEffectHandle);
virtual void NativeConstruct() override;
UPROPERTY(EditAnywhere, BlueprintReadOnly)
TSubclassOf<UUserWidget> BuffDisplaySlotWidget;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (BindWidget))
TObjectPtr<UHorizontalBox> BuffSlotBox;
protected:
void HandleGameplayEffectRemoved(const FGameplayEffectRemovalInfo& GameplayEffectRemovalInfo);
UPROPERTY()
TMap<FActiveGameplayEffectHandle, UAresBuffDisplaySlot*> SlotToActiveMapping;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "AresBuffDisplaySlot.h"
#include "AbilitySystemComponent.h"
void UAresBuffDisplaySlot::HandleGameplayEffectRemoved(const FGameplayEffectRemovalInfo& GameplayEffectRemovalInfo)
{
K2_HandleBeingRemoved(GameplayEffectRemovalInfo);
}
void UAresBuffDisplaySlot::HandleGameplayEffectStacksChanged(FActiveGameplayEffectHandle ActiveGameplayEffectHandle, int32 NewStackCount, int32 PreviousStackCount)
{
K2_HandleStackChange(NewStackCount, PreviousStackCount);
}
void UAresBuffDisplaySlot::HandleGameplayEffectTimeChanged(FActiveGameplayEffectHandle ActiveGameplayEffectHandle, float NewStartTime, float NewDuration)
{
K2_HandleTimesChanged(NewStartTime, NewDuration);
}
void UAresBuffDisplaySlot::InitWidget(UAbilitySystemComponent* AbilitySystemComponent, const FGameplayEffectSpec& GameplayEffectSpec, FActiveGameplayEffectHandle ActiveGameplayEffectHandle)
{
CachedActiveEffectHandle = ActiveGameplayEffectHandle;
AbilitySystemComponent->OnGameplayEffectStackChangeDelegate(ActiveGameplayEffectHandle)->AddUObject(this, &ThisClass::HandleGameplayEffectStacksChanged);
AbilitySystemComponent->OnGameplayEffectTimeChangeDelegate(ActiveGameplayEffectHandle)->AddUObject(this, &ThisClass::HandleGameplayEffectTimeChanged);
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Abilities/Async/AbilityAsync.h"
#include "UI/Foundation/KaosCoreActivatableWidget.h"
#include "UObject/Object.h"
#include "AresBuffDisplaySlot.generated.h"
/**
*
*/
UCLASS()
class ARESGAME_API UAresBuffDisplaySlot : public UCommonActivatableWidget
{
GENERATED_BODY()
public:
void InitWidget(UAbilitySystemComponent* AbilitySystemComponent, const FGameplayEffectSpec& GameplayEffectSpec, FActiveGameplayEffectHandle ActiveGameplayEffectHandle);
void HandleGameplayEffectRemoved(const FGameplayEffectRemovalInfo& GameplayEffectRemovalInfo);
protected:
void HandleGameplayEffectStacksChanged(FActiveGameplayEffectHandle ActiveGameplayEffectHandle, int32 NewStackCount, int32 PreviousStackCount);
void HandleGameplayEffectTimeChanged(FActiveGameplayEffectHandle ActiveGameplayEffectHandle, float NewStartTime, float NewDuration);
UFUNCTION(BlueprintImplementableEvent)
void K2_HandleBeingRemoved(const FGameplayEffectRemovalInfo& GameplayEffectRemovalInfo);
UFUNCTION(BlueprintImplementableEvent)
void K2_HandleTimesChanged(float NewStartTime, float NewDuration);
UFUNCTION(BlueprintImplementableEvent)
void K2_HandleStackChange(int32 NewStackCount, int32 PreviousStackCount);
//Allow access so widget can use if it wants.
UPROPERTY(BlueprintReadOnly)
FActiveGameplayEffectHandle CachedActiveEffectHandle;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment