Agrarsense
EnumUtilities.h
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#pragma once
7
8#include "CoreMinimal.h"
9#include "UObject/Class.h"
10#include "Kismet/BlueprintFunctionLibrary.h"
11
19
20#include "EnumUtilities.generated.h"
21
25UCLASS()
26class AGRARSENSE_API UEnumUtilities : public UBlueprintFunctionLibrary
27{
28
29 GENERATED_BODY()
30
31public:
32 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
33 static EFoliageTypes ConvertStringToFoliageType(const FString& String);
34
35 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
36 static FString ConvertFoliageTypeToString(EFoliageTypes FoliageType);
37
38 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
39 static EPropTypes ConvertStringToPropType(const FString& String);
40
41 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
42 static FString ConvertPropTypeToString(EPropTypes PropType);
43
44 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
45 static ESensorTypes ConvertStringToSensorType(const FString& String);
46
47 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
48 static FString ConvertSensorTypeToString(ESensorTypes Sensortype);
49
50 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
51 static EVehicleTypes ConvertStringToVehicleType(const FString& String);
52
53 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
54 static FString ConvertVehicleTypeToString(EVehicleTypes VehicleType);
55
56 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
57 static EWalkerType ConvertStringToWalkerType(const FString& String);
58
59 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
60 static FString ConvertWalkerTypeToString(EWalkerType WalkerType);
61
62 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
63 static EWalkerAction ConvertStringToWalkerActionType(const FString& String);
64
65 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
66 static FString ConvertWalkerActionTypeToString(EWalkerAction WalkerAction);
67
68 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
69 static EWalkerEndAction ConvertStringToWalkerEndActionType(const FString& String);
70
71 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
72 static FString ConvertWalkerEndActionTypeToString(EWalkerEndAction WalkerEndAction);
73
74 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
75 static EQualityLevel ConvertStringToQualityLevel(const FString& String);
76
77 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
78 static FString ConvertQualityLevelToString(EQualityLevel QualityLevel);
79
80 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
81 static EAntiAliasing ConvertStringToAntiAliasingMethod(const FString& String);
82
83 UFUNCTION(BlueprintCallable, Category = "Agrarsense Conversion Utilities")
84 static FString ConvertAntiAliasingMethodToString(EAntiAliasing AntiAliasingMethod);
85
89 template <typename TEnum>
90 static TMap<TEnum, FString> CreateEnumStringMap(const FString& EnumTypeName)
91 {
92 TMap<TEnum, FString> EnumMap;
93
94 // EnumTypeName must be in the following format: "/Script/ProjectName.EnumClassName"
95 UEnum* Enum = FindObject<UEnum>(nullptr, *EnumTypeName, true);
96 if (Enum)
97 {
98 for (int32 EnumIndex = 0; EnumIndex < Enum->NumEnums(); EnumIndex++)
99 {
100 TEnum EnumValue = static_cast<TEnum>(Enum->GetValueByIndex(EnumIndex));
101 FString EnumName = Enum->GetNameStringByIndex(EnumIndex);
102
103 EnumMap.Add(EnumValue, EnumName);
104 }
105 }
106
107 return EnumMap;
108 }
109
110 template <typename TEnum>
111 static TMap<FString, TEnum> CreateStringEnumMap(const FString& EnumTypeName)
112 {
113 TMap<FString, TEnum> EnumMap;
114
115 // EnumTypeName must be in the following format: "/Script/ProjectName.EnumClassName"
116 UEnum* Enum = FindObject<UEnum>(nullptr, *EnumTypeName, true);
117 if (Enum)
118 {
119 for (int32 EnumIndex = 0; EnumIndex < Enum->NumEnums(); EnumIndex++)
120 {
121 TEnum EnumValue = static_cast<TEnum>(Enum->GetValueByIndex(EnumIndex));
122 FString EnumName = Enum->GetNameStringByIndex(EnumIndex);
123
124 EnumMap.Add(EnumName, EnumValue);
125 }
126 }
127
128 return EnumMap;
129 }
130
131private:
140 template <typename TEnum>
141 static TEnum ConvertStringToEnum(const FString& EnumString, const FString& EnumTypeName)
142 {
143 UEnum* Enum = FindObject<UEnum>(nullptr, *EnumTypeName, true);
144 if (Enum)
145 {
146 int32 EnumIndex = Enum->GetIndexByName(FName(*EnumString));
147 if (EnumIndex != INDEX_NONE)
148 {
149 return static_cast<TEnum>(Enum->GetValueByIndex(EnumIndex));
150 }
151 }
152
153 return TEnum(0); // Default value if not found
154 }
155};
EAntiAliasing
Definition: AntiAliasing.h:15
EFoliageTypes
Definition: FoliageTypes.h:15
EPropTypes
Definition: PropTypes.h:15
EQualityLevel
Definition: QualityLevel.h:16
ESensorTypes
Definition: SensorTypes.h:15
EVehicleTypes
Definition: VehicleTypes.h:15
EWalkerAction
Definition: WalkerAction.h:15
EWalkerEndAction
EWalkerType
Definition: WalkerType.h:16
static TMap< TEnum, FString > CreateEnumStringMap(const FString &EnumTypeName)
Definition: EnumUtilities.h:90
static TMap< FString, TEnum > CreateStringEnumMap(const FString &EnumTypeName)
static TEnum ConvertStringToEnum(const FString &EnumString, const FString &EnumTypeName)