WwiseAssetDragDropOp.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. #include "WwiseBrowser/WwiseAssetDragDropOp.h"
  16. #include "AkAudioType.h"
  17. #include "AkSettings.h"
  18. #include "WwiseUEFeatures.h"
  19. #include "WwiseUnrealHelper.h"
  20. #include "AssetToolsModule.h"
  21. #include "AssetManagement/AkAssetDatabase.h"
  22. #include "ContentBrowserModule.h"
  23. #include "FileHelpers.h"
  24. #include "Misc/Paths.h"
  25. #include "ObjectTools.h"
  26. #define LOCTEXT_NAMESPACE "AkAudio"
  27. TSharedRef<FAssetDragDropOp> FWwiseAssetDragDropOp::New(TArray<WwiseBrowserHelpers::WwiseBrowserAssetPayload> InAssetData, UActorFactory* ActorFactory)
  28. {
  29. return New(MoveTemp(InAssetData), TArray<FString>(), ActorFactory);
  30. }
  31. TSharedRef<FAssetDragDropOp> FWwiseAssetDragDropOp::New(TArray<WwiseBrowserHelpers::WwiseBrowserAssetPayload> InAssetData, TArray<FString> InAssetPaths, UActorFactory* ActorFactory)
  32. {
  33. TArray<FAssetData> NewAssets;
  34. for (WwiseBrowserHelpers::WwiseBrowserAssetPayload AssetResult : InAssetData)
  35. {
  36. if (AssetResult.ExistingAssets.Num() > 0)
  37. {
  38. NewAssets.Add(AssetResult.ExistingAssets[0]);
  39. }
  40. else
  41. {
  42. NewAssets.Add(AssetResult.CreatedAsset);
  43. }
  44. }
  45. TSharedRef<FAssetDragDropOp> ParentOperation = FAssetDragDropOp::New(NewAssets, InAssetPaths, ActorFactory);
  46. FWwiseAssetDragDropOp* RawPointer = new FWwiseAssetDragDropOp();
  47. TSharedRef<FWwiseAssetDragDropOp> Operation = MakeShareable(RawPointer);
  48. // ugly hack since FAssetDragDropOp data is private
  49. static_cast<FAssetDragDropOp*>(RawPointer)->operator=(ParentOperation.Get());
  50. FAssetViewDragAndDropExtender::FOnDropDelegate DropDelegate = FAssetViewDragAndDropExtender::FOnDropDelegate::CreateRaw(RawPointer, &FWwiseAssetDragDropOp::OnAssetViewDrop);
  51. FAssetViewDragAndDropExtender::FOnDragOverDelegate DragOverDelegate = FAssetViewDragAndDropExtender::FOnDragOverDelegate::CreateRaw(RawPointer, &FWwiseAssetDragDropOp::OnAssetViewDragOver);
  52. Operation->Extender = new FAssetViewDragAndDropExtender(DropDelegate, DragOverDelegate);
  53. FContentBrowserModule& ContentBrowserModule = FModuleManager::GetModuleChecked<FContentBrowserModule>("ContentBrowser");
  54. TArray<FAssetViewDragAndDropExtender>& AssetViewDragAndDropExtenders = ContentBrowserModule.GetAssetViewDragAndDropExtenders();
  55. AssetViewDragAndDropExtenders.Add(*(Operation->Extender));
  56. Operation->WwiseAssetsToDrop = InAssetData;
  57. return Operation;
  58. }
  59. FWwiseAssetDragDropOp::~FWwiseAssetDragDropOp()
  60. {
  61. FContentBrowserModule& ContentBrowserModule = FModuleManager::GetModuleChecked<FContentBrowserModule>("ContentBrowser");
  62. TArray<FAssetViewDragAndDropExtender>& AssetViewDragAndDropExtenders = ContentBrowserModule.GetAssetViewDragAndDropExtenders();
  63. for (int32 i = 0; i < AssetViewDragAndDropExtenders.Num(); i++)
  64. {
  65. if (AssetViewDragAndDropExtenders[i].OnDropDelegate.IsBoundToObject(this))
  66. {
  67. AssetViewDragAndDropExtenders.RemoveAt(i);
  68. }
  69. }
  70. delete Extender;
  71. }
  72. bool FWwiseAssetDragDropOp::OnAssetViewDrop(const FAssetViewDragAndDropExtender::FPayload& Payload)
  73. {
  74. if (!Payload.DragDropOp->IsOfType<FWwiseAssetDragDropOp>())
  75. {
  76. SetCanDrop(false);
  77. return false;
  78. }
  79. if (CanDrop)
  80. {
  81. bDroppedOnContentBrowser =true;
  82. if (Payload.PackagePaths.Num() <= 0)
  83. {
  84. return false;
  85. }
  86. const auto AssetDragDrop = static_cast<FWwiseAssetDragDropOp*>(Payload.DragDropOp.Get());
  87. auto PackagePath = Payload.PackagePaths[0].ToString();
  88. // UE5 adds "/All" to all game content folder paths, but CreateAsset doesn't like it
  89. PackagePath.RemoveFromStart(TEXT("/All"));
  90. // UE5 adds "/All/Plugins" to all plugin content folder paths, but CreateAsset doesn't like it
  91. PackagePath.RemoveFromStart(TEXT("/Plugins"));
  92. AssetViewDropTargetPackagePath = PackagePath;
  93. }
  94. return CanDrop;
  95. }
  96. bool FWwiseAssetDragDropOp::OnAssetViewDragOver(const FAssetViewDragAndDropExtender::FPayload& Payload)
  97. {
  98. if (!Payload.DragDropOp->IsOfType<FWwiseAssetDragDropOp>())
  99. {
  100. SetCanDrop(false);
  101. return false;
  102. }
  103. SetCanDrop(true);
  104. return true;
  105. }
  106. void FWwiseAssetDragDropOp::SaveAssets()
  107. {
  108. FString DefaultPath = FPaths::ProjectContentDir();
  109. auto AkSettings = GetDefault<UAkSettings>();
  110. if (LIKELY(AkSettings))
  111. {
  112. DefaultPath = AkSettings->DefaultAssetCreationPath;
  113. }
  114. FString TargetRootPackagePath = DefaultPath;
  115. WwiseBrowserHelpers::EAssetDuplicationMode DuplicationMode = WwiseBrowserHelpers::EAssetDuplicationMode::NoDuplication;
  116. if (bDroppedOnContentBrowser)
  117. {
  118. TargetRootPackagePath = AssetViewDropTargetPackagePath;
  119. DuplicationMode = WwiseBrowserHelpers::EAssetDuplicationMode::DoDuplication;
  120. }
  121. WwiseBrowserHelpers::SaveSelectedAssets(WwiseAssetsToDrop, TargetRootPackagePath, WwiseBrowserHelpers::EAssetCreationMode::Transient, DuplicationMode);
  122. }
  123. void FWwiseAssetDragDropOp::DeleteAssets()
  124. {
  125. TArray<FAssetData> AssetsToDelete;
  126. for (WwiseBrowserHelpers::WwiseBrowserAssetPayload& AssetResult : WwiseAssetsToDrop)
  127. {
  128. if (AssetResult.CreatedAsset.IsValid())
  129. {
  130. AssetsToDelete.Add(AssetResult.CreatedAsset);
  131. }
  132. }
  133. if (AssetsToDelete.Num() > 0)
  134. {
  135. ObjectTools::DeleteAssets(AssetsToDelete, false);
  136. }
  137. }
  138. void FWwiseAssetDragDropOp::OnDrop(bool bDropWasHandled, const FPointerEvent& MouseEvent)
  139. {
  140. if (!bDropWasHandled)
  141. {
  142. DeleteAssets();
  143. return;
  144. }
  145. SaveAssets();
  146. }
  147. void FWwiseAssetDragDropOp::SetCanDrop(const bool InCanDrop)
  148. {
  149. CanDrop = InCanDrop;
  150. SetTooltipText();
  151. if (InCanDrop)
  152. {
  153. MouseCursor = EMouseCursor::GrabHandClosed;
  154. SetToolTip(GetTooltipText(), FAkAppStyle::Get().GetBrush(TEXT("Graph.ConnectorFeedback.Ok")));
  155. }
  156. else
  157. {
  158. MouseCursor = EMouseCursor::SlashedCircle;
  159. SetToolTip(GetTooltipText(), FAkAppStyle::Get().GetBrush(TEXT("Graph.ConnectorFeedback.Error")));
  160. }
  161. }
  162. void FWwiseAssetDragDropOp::SetTooltipText()
  163. {
  164. if (CanDrop)
  165. {
  166. auto& assets = GetAssets();
  167. FString Text = assets.Num() ? assets[0].AssetName.ToString() : TEXT("");
  168. if (assets.Num() > 1)
  169. {
  170. Text += TEXT(" ");
  171. Text += FString::Printf(TEXT("and %d other(s)"), assets.Num() - 1);
  172. }
  173. CurrentHoverText = FText::FromString(Text);
  174. }
  175. else
  176. {
  177. CurrentHoverText = LOCTEXT("OnDragAkEventsOverFolder_CannotDrop", "Wwise assets can only be dropped in the right folder");
  178. }
  179. }
  180. FText FWwiseAssetDragDropOp::GetTooltipText() const
  181. {
  182. return CurrentHoverText;
  183. }
  184. #undef LOCTEXT_NAMESPACE