Helpers for building vehicle definitions

This commit is contained in:
nsubiron 2018-07-26 22:31:37 +02:00
parent d543c3812e
commit 4ee7c80448
2 changed files with 98 additions and 0 deletions

View File

@ -149,6 +149,20 @@ private:
FScopedStack<FString> Stack;
};
template <typename ... ARGS>
static FString JoinStrings(const FString &Separator, ARGS && ... Args) {
return FString::Join(TArray<FString>{std::forward<ARGS>(Args)...}, *Separator);
}
static FString ColorToFString(const FColor &Color)
{
return JoinStrings(
TEXT(","),
FString::FromInt(Color.R),
FString::FromInt(Color.G),
FString::FromInt(Color.B));
}
bool UActorBlueprintFunctionLibrary::CheckActorDefinition(const FActorDefinition &ActorDefinition)
{
FActorDefinitionValidator Validator;
@ -160,3 +174,54 @@ bool UActorBlueprintFunctionLibrary::CheckActorDefinitions(const TArray<FActorDe
FActorDefinitionValidator Validator;
return Validator.AreValid(ActorDefinitions);
}
void UActorBlueprintFunctionLibrary::MakeVehicleDefinition(
const FVehicleParameters &Parameters,
bool &Success,
FActorDefinition &Definition)
{
/// @todo We need to validate here the params.
Definition.Id = JoinStrings(TEXT("."), Parameters.Make, Parameters.Model).ToLower();
Definition.Class = Parameters.Class;
Definition.Tags = JoinStrings(TEXT(","), TEXT("vehicle"), Parameters.Make, Parameters.Model).ToLower();
FActorVariation Colors;
Colors.Id = TEXT("color");
Colors.Type = EActorAttributeType::RGBColor;
Colors.bRestrictToRecommended = false;
for (auto &Color : Parameters.RecommendedColors)
{
Colors.RecommendedValues.Emplace(ColorToFString(Color));
}
Definition.Variations.Emplace(Colors);
Definition.Attributes.Emplace(FActorAttribute{
TEXT("number_of_wheels"),
EActorAttributeType::Int,
FString::FromInt(Parameters.NumberOfWheels)
});
Success = CheckActorDefinition(Definition);
}
template <typename T, typename Functor>
static void FillActorDefinitionArray(
const TArray<T> &ParameterArray,
TArray<FActorDefinition> &Definitions,
Functor Maker)
{
for (auto &Item : ParameterArray)
{
FActorDefinition Definition;
bool Success = false;
Maker(Item, Success, Definition);
if (Success)
{
Definitions.Emplace(std::move(Definition));
}
}
}
void UActorBlueprintFunctionLibrary::MakeVehicleDefinitions(
const TArray<FVehicleParameters> &ParameterArray,
TArray<FActorDefinition> &Definitions)
{
FillActorDefinitionArray(ParameterArray, Definitions, &MakeVehicleDefinition);
}

View File

@ -7,11 +7,33 @@
#pragma once
#include "Carla/Actor/ActorDefinition.h"
#include "Carla/Vehicle/CarlaWheeledVehicle.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "ActorBlueprintFunctionLibrary.generated.h"
USTRUCT(BlueprintType)
struct CARLA_API FVehicleParameters
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString Make;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString Model;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf<ACarlaWheeledVehicle> Class;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 NumberOfWheels = 4;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TArray<FColor> RecommendedColors;
};
UCLASS()
class UActorBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
@ -27,5 +49,16 @@ public:
/// errors found.
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static bool CheckActorDefinitions(const TArray<FActorDefinition> &ActorDefinitions);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static void MakeVehicleDefinition(
const FVehicleParameters &Parameters,
bool &Success,
FActorDefinition &Definition);
UFUNCTION(Category = "Carla Actor", BlueprintCallable)
static void MakeVehicleDefinitions(
const TArray<FVehicleParameters> &ParameterArray,
TArray<FActorDefinition> &Definitions);
};