Helpers to convert actor attribute values

This commit is contained in:
nsubiron 2018-07-27 14:51:30 +02:00
parent 2e71467ff5
commit 81ba9782cb
4 changed files with 203 additions and 3 deletions

View File

@ -33,7 +33,7 @@ namespace rpc {
id(FromFString(Description.Id)) {
attributes.reserve(Description.Variations.Num());
for (const auto &Item : Description.Variations) {
attributes.push_back(Item);
attributes.emplace_back(Item.Value);
}
}
@ -43,7 +43,7 @@ namespace rpc {
Description.Id = ToFString(id);
Description.Variations.Reserve(attributes.size());
for (const auto &item : attributes) {
Description.Variations.Add(item);
Description.Variations.Emplace(ToFString(item.id), item);
}
return Description;
}

View File

@ -10,6 +10,7 @@
#include "Carla/Util/ScopedStack.h"
#include <algorithm>
#include <limits>
#include <stack>
/// Checks validity of FActorDefinition.
@ -225,3 +226,135 @@ void UActorBlueprintFunctionLibrary::MakeVehicleDefinitions(
{
FillActorDefinitionArray(ParameterArray, Definitions, &MakeVehicleDefinition);
}
bool UActorBlueprintFunctionLibrary::ActorAttributeToBool(
const FActorAttribute &ActorAttribute,
bool Default)
{
if (ActorAttribute.Type != EActorAttributeType::Bool)
{
UE_LOG(LogCarla, Error, TEXT("ActorAttribute '%s' is not a bool"), *ActorAttribute.Id);
return Default;
}
return ActorAttribute.Value.ToBool();
}
int32 UActorBlueprintFunctionLibrary::ActorAttributeToInt(
const FActorAttribute &ActorAttribute,
int32 Default)
{
if (ActorAttribute.Type != EActorAttributeType::Int)
{
UE_LOG(LogCarla, Error, TEXT("ActorAttribute '%s' is not an int"), *ActorAttribute.Id);
return Default;
}
return FCString::Atoi(*ActorAttribute.Value);
}
float UActorBlueprintFunctionLibrary::ActorAttributeToFloat(
const FActorAttribute &ActorAttribute,
float Default)
{
if (ActorAttribute.Type != EActorAttributeType::Float)
{
UE_LOG(LogCarla, Error, TEXT("ActorAttribute '%s' is not a float"), *ActorAttribute.Id);
return Default;
}
return FCString::Atof(*ActorAttribute.Value);
}
FString UActorBlueprintFunctionLibrary::ActorAttributeToString(
const FActorAttribute &ActorAttribute,
const FString &Default)
{
if (ActorAttribute.Type != EActorAttributeType::String)
{
UE_LOG(LogCarla, Error, TEXT("ActorAttribute '%s' is not a string"), *ActorAttribute.Id);
return Default;
}
return ActorAttribute.Value;
}
FColor UActorBlueprintFunctionLibrary::ActorAttributeToColor(
const FActorAttribute &ActorAttribute,
const FColor &Default)
{
if (ActorAttribute.Type != EActorAttributeType::RGBColor)
{
UE_LOG(LogCarla, Error, TEXT("ActorAttribute '%s' is not a color"), *ActorAttribute.Id);
return Default;
}
TArray<FString> Channels;
ActorAttribute.Value.ParseIntoArray(Channels, TEXT(","), false);
if (Channels.Num() != 3)
{
UE_LOG(LogCarla, Error, TEXT("ActorAttribute '%s': invalid color '%s'"), *ActorAttribute.Id, *ActorAttribute.Value);
return Default;
}
TArray<uint8> Colors;
for (auto &Str : Channels)
{
auto Val = FCString::Atoi(*Str);
if ((Val < 0) || (Val > std::numeric_limits<uint8>::max()))
{
UE_LOG(LogCarla, Error, TEXT("ActorAttribute '%s': invalid color '%s'"), *ActorAttribute.Id, *ActorAttribute.Value);
return Default;
}
Colors.Add(Val);
}
FColor Color;
Color.R = Colors[0u];
Color.G = Colors[1u];
Color.B = Colors[2u];
return Color;
}
bool UActorBlueprintFunctionLibrary::RetrieveActorAttributeToBool(
const FString &Id,
const TMap<FString, FActorAttribute> &Attributes,
bool Default)
{
return Attributes.Contains(Id) ?
ActorAttributeToBool(Attributes[Id], Default) :
Default;
}
int32 UActorBlueprintFunctionLibrary::RetrieveActorAttributeToInt(
const FString &Id,
const TMap<FString, FActorAttribute> &Attributes,
int32 Default)
{
return Attributes.Contains(Id) ?
ActorAttributeToInt(Attributes[Id], Default) :
Default;
}
float UActorBlueprintFunctionLibrary::RetrieveActorAttributeToFloat(
const FString &Id,
const TMap<FString, FActorAttribute> &Attributes,
float Default)
{
return Attributes.Contains(Id) ?
ActorAttributeToFloat(Attributes[Id], Default) :
Default;
}
FString UActorBlueprintFunctionLibrary::RetrieveActorAttributeToString(
const FString &Id,
const TMap<FString, FActorAttribute> &Attributes,
const FString &Default)
{
return Attributes.Contains(Id) ?
ActorAttributeToString(Attributes[Id], Default) :
Default;
}
FColor UActorBlueprintFunctionLibrary::RetrieveActorAttributeToColor(
const FString &Id,
const TMap<FString, FActorAttribute> &Attributes,
const FColor &Default)
{
return Attributes.Contains(Id) ?
ActorAttributeToColor(Attributes[Id], Default) :
Default;
}

View File

@ -41,6 +41,11 @@ class UActorBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
public:
/// ==========================================================================
/// @name Actor definition validators
/// ==========================================================================
/// @{
/// Return whether the actor definition is valid. Prints all the errors found.
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static bool CheckActorDefinition(const FActorDefinition &ActorDefinitions);
@ -50,6 +55,12 @@ public:
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static bool CheckActorDefinitions(const TArray<FActorDefinition> &ActorDefinitions);
/// @}
/// ==========================================================================
/// @name Helpers to create actor definitions
/// ==========================================================================
/// @{
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static void MakeVehicleDefinition(
const FVehicleParameters &Parameters,
@ -60,5 +71,58 @@ public:
static void MakeVehicleDefinitions(
const TArray<FVehicleParameters> &ParameterArray,
TArray<FActorDefinition> &Definitions);
/// @}
/// ==========================================================================
/// @name Helpers to retrieve attribute values
/// ==========================================================================
/// @{
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static bool ActorAttributeToBool(const FActorAttribute &ActorAttribute, bool Default);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static int32 ActorAttributeToInt(const FActorAttribute &ActorAttribute, int32 Default);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static float ActorAttributeToFloat(const FActorAttribute &ActorAttribute, float Default);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static FString ActorAttributeToString(const FActorAttribute &ActorAttribute, const FString &Default);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static FColor ActorAttributeToColor(const FActorAttribute &ActorAttribute, const FColor &Default);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static bool RetrieveActorAttributeToBool(
const FString &Id,
const TMap<FString, FActorAttribute> &Attributes,
bool Default);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static int32 RetrieveActorAttributeToInt(
const FString &Id,
const TMap<FString, FActorAttribute> &Attributes,
int32 Default);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static float RetrieveActorAttributeToFloat(
const FString &Id,
const TMap<FString, FActorAttribute> &Attributes,
float Default);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static FString RetrieveActorAttributeToString(
const FString &Id,
const TMap<FString, FActorAttribute> &Attributes,
const FString &Default);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static FColor RetrieveActorAttributeToColor(
const FString &Id,
const TMap<FString, FActorAttribute> &Attributes,
const FColor &Default);
/// @}
};

View File

@ -29,6 +29,9 @@ struct FActorDescription
/// User selected variations of the actor. Note that at this point are
/// represented by non-modifiable attributes.
///
/// Key: Id of the attribute.
/// Value: The attribute.
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TArray<FActorAttribute> Variations;
TMap<FString, FActorAttribute> Variations;
};