68 lines
2.0 KiB
C++
68 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include "CoreMinimal.h"
|
|
#include "WingServer.h"
|
|
#include "WingHandler.h"
|
|
#include "WingUtils.h"
|
|
#include "Engine/UserDefinedEnum.h"
|
|
#include "Kismet2/EnumEditorUtils.h"
|
|
#include "Factories/EnumFactory.h"
|
|
#include "WingPackageMaker.h"
|
|
#include "Enum_Create.generated.h"
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------
|
|
|
|
UCLASS()
|
|
class UWing_Enum_Create : public UObject, public IWingHandler
|
|
{
|
|
GENERATED_BODY()
|
|
|
|
public:
|
|
UPROPERTY(meta=(Description="Full package path for the new enum (e.g. '/Game/DataTypes/E_MyEnum')"))
|
|
FString AssetPath;
|
|
|
|
UPROPERTY(meta=(Description="Array of enum value names"))
|
|
FWingJsonArray Values;
|
|
|
|
virtual FString GetDescription() const override
|
|
{
|
|
return TEXT("Create a new UserDefinedEnum asset with the specified values.");
|
|
}
|
|
|
|
virtual void Handle() override
|
|
{
|
|
WingPackageMaker Maker(AssetPath);
|
|
if (!Maker.Ok()) return;
|
|
|
|
TArray<FString> EnumValues;
|
|
for (const TSharedPtr<FJsonValue>& Val : Values.Array)
|
|
{
|
|
FString Str = Val->AsString();
|
|
if (!Str.IsEmpty()) EnumValues.Add(Str);
|
|
}
|
|
if (EnumValues.Num() == 0)
|
|
{
|
|
UWingServer::Print(TEXT("ERROR: Values must be a non-empty array of strings\n"));
|
|
return;
|
|
}
|
|
|
|
// Create the enum using AssetTools.
|
|
UUserDefinedEnum* NewEnum = Maker.CreateAsset<UUserDefinedEnum, UEnumFactory>();
|
|
if (!NewEnum) return;
|
|
|
|
// Add enum values — UUserDefinedEnum starts with a MAX value.
|
|
// We need to add entries before MAX.
|
|
for (int32 i = 0; i < EnumValues.Num(); ++i)
|
|
{
|
|
FEnumEditorUtils::AddNewEnumeratorForUserDefinedEnum(NewEnum);
|
|
int32 NewIndex = NewEnum->NumEnums() - 2;
|
|
FEnumEditorUtils::SetEnumeratorDisplayName(NewEnum, NewIndex, FText::FromString(EnumValues[i]));
|
|
}
|
|
|
|
UWingServer::Printf(TEXT("Created %s with %d values\n"), *NewEnum->GetPathName(), EnumValues.Num());
|
|
}
|
|
};
|