// Copyright Epic Games, Inc. All Rights Reserved. #include "SoundWaveAssetActionExtender.h" #include "AssetToolsModule.h" #include "ToolMenus.h" #include "IAssetTools.h" #include "IContentBrowserSingleton.h" #include "ContentBrowserModule.h" #include "ContentBrowserMenuContexts.h" #include "Misc/PackageName.h" #include "Sound/SoundWaveProcedural.h" #include "SoundSimple.h" #include "SoundSimpleFactory.h" #include "Algo/AnyOf.h" #include "ToolMenu.h" #include "ToolMenuSection.h" #define LOCTEXT_NAMESPACE "AssetTypeActions" void FSoundWaveAssetActionExtender::RegisterMenus() { if (!UToolMenus::IsToolMenuUIEnabled()) { return; } FToolMenuOwnerScoped MenuOwner("SoundUtilities"); UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("ContentBrowser.AssetContextMenu.SoundWave"); FToolMenuSection& Section = Menu->FindOrAddSection("GetAssetActions"); Section.AddDynamicEntry(NAME_None, FNewToolMenuSectionDelegate::CreateLambda([](FToolMenuSection& InSection) { if (UContentBrowserAssetContextMenuContext* Context = InSection.FindContext()) { if (Algo::AnyOf(Context->SelectedAssets, [](const FAssetData& AssetData){ return AssetData.IsInstanceOf(); })) { return; } const TAttribute Label = LOCTEXT("SoundWave_CreateSimpleSound", "Create Simple Sound"); const TAttribute ToolTip = LOCTEXT("SoundWave_CreateSimpleSoundTooltip", "Creates a simple sound asset using the selected sound waves."); const FSlateIcon Icon = FSlateIcon(FAppStyle::GetAppStyleSetName(), "ClassIcon.SoundSimple"); const FToolMenuExecuteAction UIAction = FToolMenuExecuteAction::CreateStatic(&FSoundWaveAssetActionExtender::ExecuteCreateSimpleSound); InSection.AddMenuEntry("SoundWave_CreateSimpleSound", Label, ToolTip, Icon, UIAction); } })); } void FSoundWaveAssetActionExtender::ExecuteCreateSimpleSound(const FToolMenuContext& MenuContext) { if (const UContentBrowserAssetContextMenuContext* Context = UContentBrowserAssetContextMenuContext::FindContextWithAssets(MenuContext)) { const FString DefaultSuffix = TEXT("_SimpleSound"); TArray SoundWaves = Context->LoadSelectedObjectsIf([](const FAssetData& AssetData) { return !AssetData.IsInstanceOf(); }); if (!SoundWaves.IsEmpty()) { // Determine an appropriate name FString Name; FString PackagePath; FAssetToolsModule& AssetToolsModule = FModuleManager::Get().LoadModuleChecked("AssetTools"); AssetToolsModule.Get().CreateUniqueAssetName(SoundWaves[0]->GetOutermost()->GetName(), DefaultSuffix, PackagePath, Name); // Create the factory used to generate the asset USoundSimpleFactory* Factory = NewObject(); Factory->SoundWaves = SoundWaves; FContentBrowserModule& ContentBrowserModule = FModuleManager::LoadModuleChecked("ContentBrowser"); ContentBrowserModule.Get().CreateNewAsset(Name, FPackageName::GetLongPackagePath(PackagePath), USoundSimple::StaticClass(), Factory); } } } #undef LOCTEXT_NAMESPACE