|
| 1 | +// Copyright 2015-2020 Piperift. All Rights Reserved. |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include <CoreMinimal.h> |
| 6 | +#include <Kismet/BlueprintFunctionLibrary.h> |
| 7 | + |
| 8 | +#include "DoubleAttr.h" |
| 9 | + |
| 10 | +#include "DoubleAttributesLibrary.generated.h" |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * |
| 15 | + */ |
| 16 | +UCLASS() |
| 17 | +class ATTRIBUTES_API UDoubleAttributesLibrary : public UBlueprintFunctionLibrary |
| 18 | +{ |
| 19 | + GENERATED_BODY() |
| 20 | + |
| 21 | +public: |
| 22 | + |
| 23 | + /** @return true when two Attributes are the same */ |
| 24 | + UFUNCTION(BlueprintPure, Category = Attributes, meta = (CompactNodeTitle = "==")) |
| 25 | + static FORCEINLINE bool Is(const FDoubleAttr& A, const FDoubleAttr& B) { return A == B; } |
| 26 | + |
| 27 | + /** @return true when two Attributes are not the same */ |
| 28 | + UFUNCTION(BlueprintPure, Category = Attributes, meta = (CompactNodeTitle = "!=")) |
| 29 | + static FORCEINLINE bool IsNot(const FDoubleAttr& A, const FDoubleAttr& B) { return A != B; } |
| 30 | + |
| 31 | + /** @return true if two attributes have the same base value */ |
| 32 | + UFUNCTION(BlueprintPure, Category = Attributes) |
| 33 | + static FORCEINLINE bool Equals(const FDoubleAttr& A, const FDoubleAttr& B) |
| 34 | + { |
| 35 | + return A.GetBaseValue() == B.GetBaseValue(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Get the final value of an attribute |
| 40 | + * @param Attribute to get value from |
| 41 | + * @return the final value |
| 42 | + */ |
| 43 | + UFUNCTION(BlueprintPure, Category = Attributes, meta = (Keywords = "get value double total final")) |
| 44 | + static FORCEINLINE double GetValue(const FDoubleAttr& Attribute) { return Attribute.GetValue(); } |
| 45 | + |
| 46 | + // Get final value |
| 47 | + UFUNCTION(BlueprintPure, Category = Attributes, meta = (DisplayName = "ToFloat (DoubleAttr)", CompactNodeTitle = "->", Keywords = "get value double", BlueprintAutocast)) |
| 48 | + static FORCEINLINE double Conv_AttributeToFloat(const FDoubleAttr& Attribute) { return GetValue(Attribute); } |
| 49 | + |
| 50 | + // Get final value as String |
| 51 | + UFUNCTION(BlueprintPure, Category = Attributes, meta = (DisplayName = "ToString (DoubleAttr)", CompactNodeTitle = "->", BlueprintAutocast)) |
| 52 | + static FORCEINLINE FString Conv_AttributeToString(const FDoubleAttr& Attribute) { |
| 53 | + return FString::SanitizeFloat(GetValue(Attribute)); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get the base value of an attribute |
| 58 | + * @param Attribute to get base value from |
| 59 | + * @return the base value |
| 60 | + */ |
| 61 | + UFUNCTION(BlueprintPure, Category = Attributes) |
| 62 | + static FORCEINLINE double GetBase(const FDoubleAttr& Attribute) { return Attribute.GetBaseValue(); } |
| 63 | + |
| 64 | + /** |
| 65 | + * Set the base value of an attribute |
| 66 | + * @param Attribute to set base value at |
| 67 | + * @param Value to set as the base value |
| 68 | + */ |
| 69 | + UFUNCTION(BlueprintCallable, Category = Attributes) |
| 70 | + static void SetBase(UPARAM(ref) FDoubleAttr& Attribute, double Value) { Attribute.SetBaseValue(Value); } |
| 71 | + |
| 72 | + /** |
| 73 | + * Adds a modifier to an attribute |
| 74 | + * @param Attribute to be modified |
| 75 | + * @param Modifier to apply to the attribute |
| 76 | + * @param Category of the modifier (Optional) |
| 77 | + */ |
| 78 | + UFUNCTION(BlueprintCallable, Category = Attributes, meta = (AdvancedDisplay = "Category")) |
| 79 | + static void AddModifier(UPARAM(ref) FDoubleAttr& Attribute, const FAttrModifier& Modifier, const FAttrCategory Category) |
| 80 | + { |
| 81 | + Attribute.AddModifier(Modifier, Category); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Removes a modifier from an attribute |
| 86 | + * @param Attribute to be modified |
| 87 | + * @param Modifier to remove from the attribute |
| 88 | + * @param Category of the modifier (Optional) |
| 89 | + * @return true if any modifier was removed |
| 90 | + */ |
| 91 | + UFUNCTION(BlueprintCallable, Category = Attributes, meta=(AdvancedDisplay="Category,bRemoveFromAllCategories")) |
| 92 | + static FORCEINLINE bool RemoveModifier(UPARAM(ref) FDoubleAttr& Attribute, const FAttrModifier& Modifier, const FAttrCategory Category, bool bRemoveFromAllCategories = false) |
| 93 | + { |
| 94 | + return Attribute.RemoveModifier(Modifier, Category, bRemoveFromAllCategories); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get all modifiers of a category, base mods will be returned if category is None |
| 99 | + * @param Attribute to get modifiers from |
| 100 | + * @return Modifiers of a category as an Array |
| 101 | + */ |
| 102 | + UFUNCTION(BlueprintPure, Category = Attributes, meta = (AdvancedDisplay = "Category")) |
| 103 | + static void GetModifiers(const FDoubleAttr& Attribute, const FAttrCategory Category, TArray<FAttrModifier>& Modifiers) |
| 104 | + { |
| 105 | + Modifiers = Attribute.GetModifiers(Category); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Get all categories where the attribute has any modifiers |
| 110 | + * @param Attribute to get categories from |
| 111 | + * @return Categories of an attribute as an Array |
| 112 | + */ |
| 113 | + UFUNCTION(BlueprintPure, Category = Attributes) |
| 114 | + static void GetModifiedCategories(const FDoubleAttr& Attribute, TArray<FAttrCategory>& Categories) |
| 115 | + { |
| 116 | + Attribute.GetModifiedCategories(Categories); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Remove all modifiers of an attribute |
| 121 | + * @param Attribute to clean |
| 122 | + */ |
| 123 | + UFUNCTION(BlueprintCallable, Category = Attributes) |
| 124 | + static void CleanModifiers(UPARAM(ref) FDoubleAttr& Attribute) |
| 125 | + { |
| 126 | + return Attribute.CleanModifiers(); |
| 127 | + } |
| 128 | + |
| 129 | + UFUNCTION(BlueprintCallable, Category = Attributes, meta = (AdvancedDisplay = "Category")) |
| 130 | + static void CleanCategoryModifiers(UPARAM(ref) FDoubleAttr& Attribute, const FAttrCategory Category) |
| 131 | + { |
| 132 | + Attribute.CleanCategoryModifiers(Category); |
| 133 | + } |
| 134 | + |
| 135 | + UFUNCTION(BlueprintCallable, Category = Attributes, meta = (AdvancedDisplay = "Category")) |
| 136 | + static void BindOnModified(UPARAM(ref) FDoubleAttr& Attribute, const FFloatModifiedDelegate& Event) |
| 137 | + { |
| 138 | + Attribute.GetOnModified().AddUnique(Event); |
| 139 | + } |
| 140 | + |
| 141 | + UFUNCTION(BlueprintCallable, Category = Attributes, meta = (AdvancedDisplay = "Category")) |
| 142 | + static void UnbindOnModified(UPARAM(ref) FDoubleAttr& Attribute, const FFloatModifiedDelegate& Event) |
| 143 | + { |
| 144 | + Attribute.GetOnModified().Remove(Event); |
| 145 | + } |
| 146 | + |
| 147 | +protected: |
| 148 | + |
| 149 | + UFUNCTION(BlueprintPure, Category = Attributes) |
| 150 | + static void Make(double BaseValue, FDoubleAttr& DoubleAttr) { DoubleAttr = { BaseValue }; } |
| 151 | + |
| 152 | + UFUNCTION(BlueprintPure, Category = Attributes) |
| 153 | + static void Break(const FDoubleAttr& DoubleAttr, double& BaseValue, double& Value) { |
| 154 | + BaseValue = DoubleAttr.GetBaseValue(); |
| 155 | + Value = DoubleAttr.GetValue(); |
| 156 | + } |
| 157 | +}; |
0 commit comments