/** adds new enumerator (with default unique name) for user defined enum */ void FEnumEditorUtils::AddNewEnumeratorForUserDefinedEnum(UUserDefinedEnum* Enum) { if (!Enum) { return; } PrepareForChange(Enum); TArray<TPair<FName, int8>> OldNames, Names; CopyEnumeratorsWithoutMax(Enum, OldNames); Names = OldNames; FString EnumNameString = Enum->GenerateNewEnumeratorName(); const FString FullNameStr = Enum->GenerateFullEnumName(*EnumNameString); Names.Add(TPairInitializer<FName, int8>(FName(*FullNameStr), Enum->GetMaxEnumValue() + 1)); const UEnum::ECppForm EnumType = Enum->GetCppForm(); Enum->SetEnums(Names, EnumType); EnsureAllDisplayNamesExist(Enum); BroadcastChanges(Enum, OldNames); Enum->MarkPackageDirty(); }
/** Reorder enumerators in enum. Swap an enumerator with given name, with previous or next (based on bDirectionUp parameter) */ void FEnumEditorUtils::MoveEnumeratorInUserDefinedEnum(UUserDefinedEnum* Enum, int32 EnumeratorIndex, bool bDirectionUp) { if (!(Enum && (Enum->GetNameByIndex(EnumeratorIndex) != NAME_None))) { return; } PrepareForChange(Enum); TArray<TPair<FName, int8>> OldNames, Names; CopyEnumeratorsWithoutMax(Enum, OldNames); Names = OldNames; const bool bIsLast = ((Names.Num() - 1) == EnumeratorIndex); const bool bIsFirst = (0 == EnumeratorIndex); if (bDirectionUp && !bIsFirst) { Names.Swap(EnumeratorIndex, EnumeratorIndex - 1); } else if (!bDirectionUp && !bIsLast) { Names.Swap(EnumeratorIndex, EnumeratorIndex + 1); } const UEnum::ECppForm EnumType = Enum->GetCppForm(); Enum->SetEnums(Names, EnumType); EnsureAllDisplayNamesExist(Enum); BroadcastChanges(Enum, OldNames); Enum->MarkPackageDirty(); }
void FEnumEditorUtils::RemoveEnumeratorFromUserDefinedEnum(UUserDefinedEnum* Enum, int32 EnumeratorIndex) { if (!(Enum && (Enum->GetNameByIndex(EnumeratorIndex) != NAME_None))) { return; } PrepareForChange(Enum); TArray<TPair<FName, uint8>> OldNames, Names; CopyEnumeratorsWithoutMax(Enum, OldNames); Names = OldNames; Names.RemoveAt(EnumeratorIndex); Enum->RemoveMetaData(FEnumEditorUtilsHelper::DisplayName(), EnumeratorIndex); // Clean up enum values. for (int32 i = 0; i < Names.Num(); ++i) { Names[i].Value = i; } const UEnum::ECppForm EnumType = Enum->GetCppForm(); Enum->SetEnums(Names, EnumType); EnsureAllDisplayNamesExist(Enum); BroadcastChanges(Enum, OldNames); Enum->MarkPackageDirty(); }
bool FEnumEditorUtils::SetEnumeratorDisplayName(UUserDefinedEnum* Enum, int32 EnumeratorIndex, FString NewDisplayName) { if (Enum && (EnumeratorIndex >= 0) && (EnumeratorIndex < Enum->NumEnums())) { if (IsEnumeratorDisplayNameValid(Enum, NewDisplayName)) { PrepareForChange(Enum); Enum->SetMetaData(FEnumEditorUtilsHelper::DisplayName(), *NewDisplayName, EnumeratorIndex); EnsureAllDisplayNamesExist(Enum); BroadcastChanges(Enum, TArray<TPair<FName, int8>>(), false); return true; } } return false; }