SWaapiPicker.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. /*------------------------------------------------------------------------------------
  16. SWaapiPicker.h
  17. ------------------------------------------------------------------------------------*/
  18. #pragma once
  19. /*------------------------------------------------------------------------------------
  20. SWaapiPicker
  21. ------------------------------------------------------------------------------------*/
  22. #include "WwiseTreeItem.h"
  23. #include "Misc/TextFilter.h"
  24. #include "Widgets/Views/STableRow.h"
  25. #include "AkWaapiClient.h"
  26. #include "Dom/JsonObject.h"
  27. #include "Widgets/Input/SSearchBox.h"
  28. #include "Widgets/Views/STreeView.h"
  29. #include "Framework/Commands/UICommandList.h"
  30. DECLARE_DELEGATE_OneParam(FOnImportWwiseAssetsClicked, const FString&);
  31. typedef TTextFilter< const FString& > StringFilter;
  32. struct TransformStringField
  33. {
  34. const FString keyArg;
  35. const TArray<FString> valueStringArgs;
  36. const TArray<int32> valueNumberArgs;
  37. };
  38. class AKAUDIO_API SWaapiPicker : public SCompoundWidget
  39. {
  40. public:
  41. typedef TSlateDelegates< TSharedPtr< FWwiseTreeItem > >::FOnSelectionChanged FOnSelectionChanged;
  42. DECLARE_DELEGATE(FOnGenerateSoundBankClicked);
  43. DECLARE_DELEGATE(FOnRefreshClicked);
  44. public:
  45. SLATE_BEGIN_ARGS( SWaapiPicker )
  46. : _FocusSearchBoxWhenOpened(false)
  47. , _ShowTreeTitle(true)
  48. , _ShowSearchBar(true)
  49. , _ShowSeparator(true)
  50. , _AllowContextMenu(true)
  51. , _RestrictContextMenu(false)
  52. , _ShowGenerateSoundBanksButton(false)
  53. , _SelectionMode( ESelectionMode::Multi )
  54. {}
  55. /** Content displayed to the left of the search bar */
  56. SLATE_NAMED_SLOT( FArguments, SearchContent )
  57. /** If true, the search box will be focus the frame after construction */
  58. SLATE_ARGUMENT( bool, FocusSearchBoxWhenOpened )
  59. /** If true, The tree title will be displayed */
  60. SLATE_ARGUMENT( bool, ShowTreeTitle )
  61. /** If true, The tree search bar will be displayed */
  62. SLATE_ARGUMENT( bool, ShowSearchBar )
  63. /** If true, The tree search bar separator be displayed */
  64. SLATE_ARGUMENT( bool, ShowSeparator )
  65. /** If false, the context menu will be suppressed */
  66. SLATE_ARGUMENT( bool, AllowContextMenu )
  67. /** If true, editor options (like explore section) will be restricted from the context menu */
  68. SLATE_ARGUMENT(bool, RestrictContextMenu)
  69. /** If true, it will show the Generate SoundBanks button */
  70. SLATE_ARGUMENT(bool, ShowGenerateSoundBanksButton)
  71. /** The selection mode for the tree view */
  72. SLATE_ARGUMENT( ESelectionMode::Type, SelectionMode )
  73. /** Handles the drag and drop operations */
  74. SLATE_EVENT(FOnDragDetected, OnDragDetected)
  75. /** Handles the selection operation */
  76. SLATE_EVENT(FOnSelectionChanged, OnSelectionChanged)
  77. /** Handles the Generate SoundBanks click operation */
  78. SLATE_EVENT(FOnGenerateSoundBankClicked, OnGenerateSoundBanksClicked)
  79. /** Handles the Refresh click operation */
  80. SLATE_EVENT(FOnRefreshClicked, OnRefreshClicked)
  81. /** Handles the Import asset operation */
  82. SLATE_EVENT(FOnImportWwiseAssetsClicked, OnImportWwiseAssetsClicked)
  83. SLATE_END_ARGS( )
  84. void Construct(const FArguments& InArgs);
  85. SWaapiPicker(void);
  86. ~SWaapiPicker();
  87. static const FName WaapiPickerTabName;
  88. static const FText ModalWarning;
  89. virtual void Tick( const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime ) override;
  90. /**
  91. * Call WAAPI to get information about an object form the path or the id of the object (inFrom).
  92. *
  93. * @param inFromField The path or the id from which the data will be get.
  94. * @param outJsonResult A JSON object that contains useful information about the call process, gets the object infos or gets an error infos in case the call failed.
  95. * @return A boolean to ensure that the call was successfully done.
  96. */
  97. static bool CallWaapiGetInfoFrom(const FString& inFromField, const FString& inFromString, TSharedPtr<FJsonObject>& outJsonResult, const TArray<TransformStringField>& TransformFields);
  98. /**
  99. * Check if an item exists in the ItemTree, using the item path.
  100. *
  101. * @param RootItem The root of the item we are looking for.
  102. * @param CurrentItemPath The path of the item we are looking for.
  103. * @return The item that we are looking for if it exists, otherwise an invalid one.
  104. */
  105. TSharedPtr<FWwiseTreeItem> FindItemFromPath(const TSharedPtr<FWwiseTreeItem>& RootItem, const FString& CurrentItemPath);
  106. /**
  107. * Construct the tree items within the CurrentItem path.
  108. *
  109. * @param CurrentItem the current item already created and need to create his parents from his path.
  110. */
  111. void FindAndCreateItems(TSharedPtr<FWwiseTreeItem> CurrentItem);
  112. /**
  113. * Allows to get the root of an item from the path specified.
  114. *
  115. * @param InFullPath A path used to search for the right root item from the root list.
  116. * @return The root item correspondent to the full path.
  117. */
  118. inline TSharedPtr<FWwiseTreeItem> GetRootItem(const FString& InFullPath);
  119. /**
  120. * Allows to get information from a FJsonValue object and use it to create an FWwiseTreeItem.
  121. *
  122. * @param inJsonItem An FJsonValue from which we get useful data to construct the FWwiseTreeItem object.
  123. * @return An FWwiseTreeItem that will be added to the root items.
  124. */
  125. TSharedPtr<FWwiseTreeItem> ConstructWwiseTreeItem(const TSharedPtr<FJsonValue>& InJsonItem);
  126. TSharedPtr<FWwiseTreeItem> ConstructWwiseTreeItem(const TSharedPtr<FJsonObject>& ItemInfoObj);
  127. virtual FReply OnKeyDown(const FGeometry& MyGeometry, const FKeyEvent& InKeyboardEvent) override;
  128. /** Returns all the items currently selected in the Waapi Picker view */
  129. const TArray<TSharedPtr<FWwiseTreeItem>> GetSelectedItems() const;
  130. const FString GetSearchText() const;
  131. const void SetSearchText(const FString& newText);
  132. private:
  133. /** The tree view widget */
  134. TSharedPtr< STreeView< TSharedPtr<FWwiseTreeItem>> > TreeViewPtr;
  135. /** The asset tree search box */
  136. TSharedPtr< SSearchBox > SearchBoxPtr;
  137. /** Filter for the search box */
  138. TSharedPtr<StringFilter> SearchBoxFilter;
  139. /** Root items, one for each type of Wwise object */
  140. FCriticalSection RootItemsLock;
  141. TArray< TSharedPtr<FWwiseTreeItem> > RootItems;
  142. FGraphEventRef ConstructTreeTask;
  143. /** Bool to prevent the selection changed callback from running */
  144. bool AllowTreeViewDelegates;
  145. /** Remember the selected items. Useful when filtering to preserve selection status. */
  146. TSet< FGuid > LastSelectedItems;
  147. /** Remember the expanded items. Useful when filtering to preserve expansion status. */
  148. TSet< FGuid > LastExpandedItems;
  149. struct TransportInfo
  150. {
  151. int32 TransportID;
  152. uint64 SubscriptionID;
  153. TransportInfo(int32 transID, uint64 subsID) : TransportID(transID), SubscriptionID(subsID) {}
  154. };
  155. /** Remember the played items. Useful to play/stop and event. */
  156. TMap<FGuid, TransportInfo> ItemToTransport;
  157. /** Commands handled by this widget */
  158. TSharedRef<FUICommandList> CommandList;
  159. /** Delegate to invoke when drag drop detected. */
  160. FOnDragDetected OnDragDetected;
  161. /** Delegate to invoke when an item is selected. */
  162. FOnSelectionChanged OnSelectionChanged;
  163. FOnGenerateSoundBankClicked OnGenerateSoundBanksClicked;
  164. FOnRefreshClicked OnRefreshClicked;
  165. /** Delegate to invoke when assets are imported. */
  166. FOnImportWwiseAssetsClicked OnImportWwiseAssetsClicked;
  167. /** Whether to disable the context menu and keyboard controls of the explore section*/
  168. bool bRestrictContextMenu;
  169. /* Callback handles. */
  170. FDelegateHandle ProjectLoadedHandle;
  171. FDelegateHandle ConnectionLostHandle;
  172. FDelegateHandle ClientBeginDestroyHandle;
  173. //Waapi Client callbacks
  174. void RemoveClientCallbacks();
  175. void OnProjectLoadedCallback();
  176. void OnConnectionLostCallback();
  177. /* Used to show/hide the Picker/Warning */
  178. EVisibility isPickerAllowed() const;
  179. EVisibility isWarningVisible() const;
  180. FText GetWarningText() const;
  181. bool isPickerVisible;
  182. bool isModalActiveInWwise = false;
  183. /** One-off active timer to focus the widget post-construct */
  184. EActiveTimerReturnType SetFocusPostConstruct(double InCurrentTime, float InDeltaTime);
  185. /** Ran when the Refresh button is clicked. Populates the window. */
  186. FReply OnRefreshButtonClicked();
  187. FReply OnGenerateSoundBanksButtonClicked();
  188. /** Populates the picker window only (does not parse the Wwise project) */
  189. void ConstructTree();
  190. /** Generate a row in the tree view */
  191. TSharedRef<ITableRow> GenerateRow( TSharedPtr<FWwiseTreeItem> TreeItem, const TSharedRef<STableViewBase>& OwnerTable );
  192. /** Get the children of a specific tree element */
  193. void GetChildrenForTree( TSharedPtr< FWwiseTreeItem > TreeItem, TArray< TSharedPtr<FWwiseTreeItem> >& OutChildren );
  194. /** Handle Drag & Drop */
  195. FReply HandleOnDragDetected(const FGeometry& Geometry, const FPointerEvent& MouseEvent);
  196. void ExpandFirstLevel();
  197. void ExpandParents(TSharedPtr<FWwiseTreeItem> Item);
  198. FText GetProjectName() const;
  199. /** Used by the search filter */
  200. void PopulateSearchStrings( const FString& FolderName, OUT TArray< FString >& OutSearchStrings ) const;
  201. void OnSearchBoxChanged( const FText& InSearchText );
  202. FText GetHighlightText() const;
  203. void FilterUpdated();
  204. void SetItemVisibility(TSharedPtr<FWwiseTreeItem> Item, bool IsVisible);
  205. void ApplyFilter();
  206. void RestoreTreeExpansion(const TArray< TSharedPtr<FWwiseTreeItem> >& Items);
  207. /** Handler for tree view selection changes */
  208. void TreeSelectionChanged( TSharedPtr< FWwiseTreeItem > TreeItem, ESelectInfo::Type SelectInfo );
  209. /** Handler for tree view expansion changes */
  210. void TreeExpansionChanged( TSharedPtr< FWwiseTreeItem > TreeItem, bool bIsExpanded );
  211. FString ProjectFolder;
  212. FString ProjectName;
  213. /** True if the specified item is selected in the asset tree */
  214. bool IsTreeItemSelected(TSharedPtr<FWwiseTreeItem> TreeItem) const;
  215. /** Builds the command list for the context menu on Waapi Picker items. */
  216. void CreateWaapiPickerCommands();
  217. /** Callback for creating a context menu for the Wwise items list. */
  218. TSharedPtr<SWidget> MakeWaapiPickerContextMenu();
  219. /** Helper functions for playback */
  220. int32 CreateTransport(const FGuid& ItemID);
  221. void DestroyTransport(const FGuid& ItemID);
  222. void TogglePlayStop(int32 TransportID);
  223. void StopTransport(int32 TransportID);
  224. uint64 SubscribeToTransportStateChanged(int32 TransportID);
  225. void HandleStateChanged(TSharedPtr<FJsonObject> UEJsonObject);
  226. /** Callback returns true if the rename command can be executed. */
  227. bool HandleRenameWwiseItemCommandCanExecute() const;
  228. /** Callback to execute the rename command from the context menu. */
  229. void HandleRenameWwiseItemCommandExecute() const;
  230. /** Callback returns true if the play command can be executed. */
  231. bool HandlePlayWwiseItemCommandCanExecute() const;
  232. /** Callback to execute the play command from the context menu. */
  233. void HandlePlayWwiseItemCommandExecute();
  234. /** Callback to execute the stop all command from the context menu. */
  235. void StopAndDestroyAllTransports();
  236. /** Callback returns true if the delete command can be executed. */
  237. bool HandleDeleteWwiseItemCommandCanExecute() const;
  238. /** Callback to execute the delete command from the context menu. */
  239. void HandleDeleteWwiseItemCommandExecute();
  240. /** Callback to execute the explore item command from the context menu. */
  241. void HandleExploreWwiseItemCommandExecute() const;
  242. /** Callback returns true if the command can be executed. */
  243. bool HandleWwiseCommandCanExecute() const;
  244. /** Callback to execute the find item in project explorer command from the context menu. */
  245. void HandleFindWwiseItemInProjectExplorerCommandExecute() const;
  246. /** Callback to execute the refresh command from the context menu. */
  247. void HandleRefreshWaapiPickerCommandExecute();
  248. /** Callback to execute the undo command */
  249. void HandleUndoWaapiPickerCommandExecute() const;
  250. /** Callback to execute the redo command */
  251. void HandleRedoWaapiPickerCommandExecute() const;
  252. /** Callback to import a Wwise item into the project's Contents*/
  253. void HandleImportWwiseItemCommandExecute() const;
  254. void SubscribeWaapiCallbacks();
  255. void UnsubscribeWaapiCallbacks();
  256. void OnWaapiRenamed(uint64_t Id, TSharedPtr<FJsonObject> Response);
  257. void OnWaapiChildAdded(uint64_t Id, TSharedPtr<FJsonObject> Response);
  258. void OnWaapiChildRemoved(uint64_t Id, TSharedPtr<FJsonObject> Response);
  259. void OnWwiseSelectionChanged(uint64_t Id, TSharedPtr<FJsonObject> Response);
  260. void CreateTreeItemWaapi(const TSharedPtr<FWwiseTreeItem>& parentTreeItem, const TSharedPtr<FJsonObject>& childJson);
  261. template<typename ActionFunctor>
  262. void HandleOnWaapiChildResponse(TSharedPtr<FJsonObject> Response, const ActionFunctor& Action);
  263. TSharedPtr<FWwiseTreeItem> FindTreeItemFromJsonObject(const TSharedPtr<FJsonObject>& Object, const FString& OverrideLastPart = FString());
  264. TSharedPtr<FWwiseTreeItem> FindOrConstructTreeItemFromJsonObject(const TSharedPtr<FJsonObject>& Object);
  265. private:
  266. struct FWaapiSubscriptionIds
  267. {
  268. uint64 Renamed = 0;
  269. uint64 ChildAdded = 0;
  270. uint64 ChildRemoved = 0;
  271. uint64 SelectionChanged = 0;
  272. } WaapiSubscriptionIds;
  273. TMap<FGuid, TSharedPtr<FWwiseTreeItem>> pendingTreeItems;
  274. };