ImGuiAssetBrowser.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Utils/ImGuiAssetBrowser.h>
  9. #include <AzCore/IO/SystemFile.h>
  10. #include <AzCore/std/sort.h>
  11. #include <AzCore/Serialization/Utils.h>
  12. #include <Automation/ScriptableImGui.h>
  13. namespace AtomSampleViewer
  14. {
  15. void ImGuiAssetBrowser::Reflect(AZ::ReflectContext* context)
  16. {
  17. ImGuiAssetBrowser::ConfigFile::Reflect(context);
  18. }
  19. void ImGuiAssetBrowser::ConfigFile::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<ConfigFile>()
  24. ->Version(0)
  25. ->Field("PinnedAssetPaths", &ConfigFile::m_pinnedAssetPaths)
  26. ->Field("ExpandRoot", &ConfigFile::m_expandRoot)
  27. ->Field("ExpandAvailableList", &ConfigFile::m_expandAvailableList)
  28. ->Field("ExpandPinnedList", &ConfigFile::m_expandPinnedList)
  29. ;
  30. }
  31. }
  32. ImGuiAssetBrowser::ImGuiAssetBrowser(AZStd::string_view configFilePath)
  33. {
  34. m_configFilePath = configFilePath;
  35. }
  36. void ImGuiAssetBrowser::Activate()
  37. {
  38. // The original m_configFilePath passed to the constructor likely starts with "@user@" which needs to be replaced with the real path.
  39. // The constructor is too early to resolve the path so we do it here on activation.
  40. char configFileFullPath[AZ_MAX_PATH_LEN] = {0};
  41. AZ::IO::FileIOBase::GetInstance()->ResolvePath(m_configFilePath.c_str(), configFileFullPath, AZ_MAX_PATH_LEN);
  42. m_configFilePath = configFileFullPath;
  43. AzFramework::AssetCatalogEventBus::Handler::BusConnect();
  44. }
  45. void ImGuiAssetBrowser::Deactivate()
  46. {
  47. if (!m_configFilePath.empty())
  48. {
  49. // We only report this message in Deactivate(), not inside SaveConfigFile(), to avoid spamming
  50. // this message when SaveConfigFile() is called in OnTick()
  51. AZ_TracePrintf("ImGuiAssetBrowser", "Saved settings to '%s'\n", m_configFilePath.c_str());
  52. SaveConfigFile();
  53. }
  54. AzFramework::AssetCatalogEventBus::Handler::BusDisconnect();
  55. }
  56. void ImGuiAssetBrowser::OnCatalogAssetAdded(const AZ::Data::AssetId& assetId)
  57. {
  58. OnCatalogChanged(assetId);
  59. }
  60. void ImGuiAssetBrowser::OnCatalogAssetRemoved(const AZ::Data::AssetId& assetId, const AZ::Data::AssetInfo&)
  61. {
  62. OnCatalogChanged(assetId);
  63. }
  64. void ImGuiAssetBrowser::OnCatalogChanged(const AZ::Data::AssetId& assetId)
  65. {
  66. AZ::Data::AssetInfo assetInfo;
  67. AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetInfoById, assetId);
  68. if (m_includedAssetFilter && m_includedAssetFilter(assetInfo))
  69. {
  70. m_needsRefresh = true;
  71. }
  72. }
  73. void ImGuiAssetBrowser::SetFilter(AssetFilterCallback shouldInclude)
  74. {
  75. m_includedAssetFilter = shouldInclude;
  76. }
  77. void ImGuiAssetBrowser::PopulateAssets(AZStd::function<bool(const AZ::Data::AssetInfo& assetInfo)> shouldInclude)
  78. {
  79. m_assets.clear();
  80. m_pinnedAssets.clear();
  81. m_configFile.m_pinnedAssetPaths.clear();
  82. m_prevSelectedAssetIndex = -1;
  83. m_selectedAssetIndex = -1;
  84. m_selectedPinnedAssetIndex = -1;
  85. auto startCB = []() {};
  86. auto enumerateCB = [this,shouldInclude](const AZ::Data::AssetId id, const AZ::Data::AssetInfo& assetInfo)
  87. {
  88. if (shouldInclude(assetInfo))
  89. {
  90. Utils::AssetEntry entry;
  91. entry.m_path = assetInfo.m_relativePath;
  92. entry.m_assetId = id;
  93. entry.m_name = assetInfo.m_relativePath;
  94. m_assets.push_back(entry);
  95. }
  96. };
  97. auto endCB = []() {};
  98. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequestBus::Events::EnumerateAssets, startCB, enumerateCB, endCB);
  99. // Sort the assets that we've found alphabetically
  100. AZStd::sort(m_assets.begin(), m_assets.end(), [](const Utils::AssetEntry& lhs, const Utils::AssetEntry& rhs) {
  101. return lhs.m_path < rhs.m_path;
  102. });
  103. }
  104. const ImGuiAssetBrowser::AssetList& ImGuiAssetBrowser::GetAssets() const
  105. {
  106. return m_assets;
  107. }
  108. const ImGuiAssetBrowser::AssetList& ImGuiAssetBrowser::GetPinnedAssets() const
  109. {
  110. return m_pinnedAssets;
  111. }
  112. void ImGuiAssetBrowser::SelectAsset(int32_t assetIndex)
  113. {
  114. m_prevSelectedAssetIndex = m_selectedAssetIndex;
  115. m_selectedAssetIndex = assetIndex;
  116. m_selectedPinnedAssetIndex = -1;
  117. }
  118. int32_t ImGuiAssetBrowser::GetSelectedAssetIndex() const
  119. {
  120. return m_selectedAssetIndex;
  121. }
  122. AZ::Data::AssetId ImGuiAssetBrowser::GetSelectedAssetId() const
  123. {
  124. AZ::Data::AssetId id;
  125. if (m_selectedAssetIndex >= 0)
  126. {
  127. id = m_assets[m_selectedAssetIndex].m_assetId;
  128. }
  129. return id;
  130. }
  131. AZStd::string ImGuiAssetBrowser::GetSelectedAssetPath() const
  132. {
  133. AZStd::string path;
  134. if (m_selectedAssetIndex >= 0)
  135. {
  136. path = m_assets[m_selectedAssetIndex].m_path;
  137. }
  138. return path;
  139. }
  140. int32_t ImGuiAssetBrowser::GetPrevSelectedAssetIndex() const
  141. {
  142. return m_prevSelectedAssetIndex;
  143. }
  144. AZ::Data::AssetId ImGuiAssetBrowser::GetPrevSelectedAssetId() const
  145. {
  146. AZ::Data::AssetId id;
  147. if (m_prevSelectedAssetIndex >= 0)
  148. {
  149. id = m_assets[m_prevSelectedAssetIndex].m_assetId;
  150. }
  151. return id;
  152. }
  153. void ImGuiAssetBrowser::SetDefaultPinnedAssets(const AZStd::vector<AZStd::string>& assetPaths, bool applyNow)
  154. {
  155. m_defaultPinnedAssetPaths = assetPaths;
  156. if (applyNow)
  157. {
  158. ResetPinnedAssetsToDefault();
  159. }
  160. }
  161. void ImGuiAssetBrowser::ResetPinnedAssetsToDefault()
  162. {
  163. SetPinnedAssets(m_defaultPinnedAssetPaths);
  164. }
  165. void ImGuiAssetBrowser::SetPinnedAssets(const AZStd::vector<AZStd::string>& assetPaths)
  166. {
  167. m_configFile.m_pinnedAssetPaths = assetPaths;
  168. // Look up asset ids from asset paths
  169. m_pinnedAssets.clear();
  170. m_pinnedAssets.reserve(m_configFile.m_pinnedAssetPaths.size());
  171. for (const AZStd::string& assetPath : m_configFile.m_pinnedAssetPaths)
  172. {
  173. AZ::Data::AssetId assetId;
  174. AZ::Data::AssetCatalogRequestBus::BroadcastResult(
  175. assetId, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetIdByPath,
  176. assetPath.c_str(), AZ::Data::AssetType(), false);
  177. AZ_Warning("ImGuiAssetBrowser", assetId.IsValid(), "Failed to get asset id for '%s'", assetPath.c_str());
  178. Utils::AssetEntry entry;
  179. entry.m_path = assetPath;
  180. entry.m_assetId = assetId;
  181. entry.m_name = assetId.IsValid() ? assetPath : "<Missing> " + assetPath;
  182. m_pinnedAssets.push_back(entry);
  183. }
  184. }
  185. bool ImGuiAssetBrowser::LoadConfigFile()
  186. {
  187. AZStd::unique_ptr<ConfigFile> configFile(AZ::Utils::LoadObjectFromFile<ConfigFile>(m_configFilePath));
  188. if (configFile)
  189. {
  190. m_isConfigFileLoaded = true;
  191. m_configFile = *configFile;
  192. SetPinnedAssets(configFile->m_pinnedAssetPaths);
  193. return true;
  194. }
  195. else
  196. {
  197. return false;
  198. }
  199. }
  200. bool ImGuiAssetBrowser::IsConfigFileLoaded() const
  201. {
  202. return m_isConfigFileLoaded;
  203. }
  204. void ImGuiAssetBrowser::UpdateConfigFilePins()
  205. {
  206. m_configFile.m_pinnedAssetPaths.clear();
  207. m_configFile.m_pinnedAssetPaths.reserve(m_pinnedAssets.size());
  208. for (const Utils::AssetEntry& entry : m_pinnedAssets)
  209. {
  210. m_configFile.m_pinnedAssetPaths.push_back(entry.m_path);
  211. }
  212. }
  213. void ImGuiAssetBrowser::SaveConfigFile()
  214. {
  215. if (m_configFilePath.empty())
  216. {
  217. AZ_Warning("ImGuiAssetBrowser", false, "m_configFilePath is not set. GUI state not saved.");
  218. }
  219. else if (!AZ::Utils::SaveObjectToFile(m_configFilePath, AZ::DataStream::ST_XML, &m_configFile))
  220. {
  221. AZ_Error("ImGuiAssetBrowser", false, "Failed to save '%s'", m_configFilePath.c_str());
  222. }
  223. }
  224. bool ImGuiAssetBrowser::Tick(const WidgetSettings& widgetSettings)
  225. {
  226. ScriptableImGui::ScopedNameContext nameContext(widgetSettings.m_labels.m_root);
  227. if (m_needsRefresh)
  228. {
  229. // Save currently pinned assets so they can be restored if the config file fails to load.
  230. auto savedPinnedAssets = m_configFile.m_pinnedAssetPaths;
  231. PopulateAssets(m_includedAssetFilter);
  232. if (!LoadConfigFile())
  233. {
  234. AZ_Warning("ImGuiAssetBrowser", false, "Failed to load config '%s'.", m_configFilePath.data());
  235. SetPinnedAssets(savedPinnedAssets);
  236. }
  237. m_needsRefresh = false;
  238. }
  239. bool selectionChanged = false;
  240. bool pinListChanged = false;
  241. bool rootExpansionChanged = false;
  242. bool availableListExpansionChanged = false;
  243. bool pinnedListExpansionChanged = false;
  244. bool isRootNodeOpen = false;
  245. bool isAvailableListOpen = false;
  246. bool isPinnedListOpen = false;
  247. auto treeNodeFlag = [](bool shouldExpand)
  248. {
  249. return shouldExpand ? ImGuiTreeNodeFlags_DefaultOpen : 0;
  250. };
  251. ImGui::PushID(this);
  252. isRootNodeOpen = ImGui::TreeNodeEx(widgetSettings.m_labels.m_root, treeNodeFlag(m_configFile.m_expandRoot));
  253. if (isRootNodeOpen)
  254. {
  255. isAvailableListOpen = ImGui::TreeNodeEx(widgetSettings.m_labels.m_assetList, treeNodeFlag(m_configFile.m_expandAvailableList));
  256. if (isAvailableListOpen)
  257. {
  258. availableListExpansionChanged = !m_configFile.m_expandAvailableList;
  259. // [GFX TODO] When these list boxes are controlled from a script it would be nice to support auto-scrolling
  260. // to the selected position; that would require using ListBoxHeader/ListBoxFooter and Selectable instead of ListBox.
  261. ImGui::PushItemWidth(-1.0f);
  262. if (ScriptableImGui::ListBox("##Available", &m_selectedAssetIndex, &Utils::AssetEntryNameGetter, m_assets.data(), static_cast<int32_t>(m_assets.size()), 16))
  263. {
  264. selectionChanged = true;
  265. }
  266. ImGui::PopItemWidth();
  267. ImGui::Spacing();
  268. if (ScriptableImGui::Button(widgetSettings.m_labels.m_pinButton))
  269. {
  270. if (m_selectedAssetIndex >= 0)
  271. {
  272. const Utils::AssetEntry& selectedAsset = m_assets[m_selectedAssetIndex];
  273. bool alreadyExists = false;
  274. for (const Utils::AssetEntry& entry : m_pinnedAssets)
  275. {
  276. if (entry.m_assetId == selectedAsset.m_assetId)
  277. {
  278. alreadyExists = true;
  279. break;
  280. }
  281. }
  282. if (!alreadyExists)
  283. {
  284. m_pinnedAssets.push_back(selectedAsset);
  285. pinListChanged = true;
  286. }
  287. }
  288. }
  289. ImGui::TreePop();
  290. }
  291. ImGui::Spacing();
  292. isPinnedListOpen = ImGui::TreeNodeEx(widgetSettings.m_labels.m_pinnedAssetList, treeNodeFlag(m_configFile.m_expandPinnedList));
  293. if (isPinnedListOpen)
  294. {
  295. pinnedListExpansionChanged = !m_configFile.m_expandPinnedList;
  296. ImGui::PushItemWidth(-1.0f);
  297. if (ScriptableImGui::ListBox("##Pinned", &m_selectedPinnedAssetIndex, &Utils::AssetEntryNameGetter, m_pinnedAssets.data(), static_cast<int32_t>(m_pinnedAssets.size()), 6))
  298. {
  299. selectionChanged = true;
  300. // Since GetSelectedAssetIndex() returns m_selectedAssetIndex, we have to keep that updated
  301. // based on changes to m_selectedPinnedAssetIndex as well.
  302. m_prevSelectedAssetIndex = m_selectedAssetIndex;
  303. m_selectedAssetIndex = -1;
  304. if (m_selectedPinnedAssetIndex >= 0)
  305. {
  306. AZ::Data::AssetId selectedAssetId = m_pinnedAssets[m_selectedPinnedAssetIndex].m_assetId;
  307. for (int32_t i = 0; i < m_assets.size(); ++i)
  308. {
  309. if (m_assets[i].m_assetId == selectedAssetId)
  310. {
  311. m_selectedAssetIndex = i;
  312. break;
  313. }
  314. }
  315. }
  316. }
  317. ImGui::PopItemWidth();
  318. ImGui::Spacing();
  319. if (ScriptableImGui::Button(widgetSettings.m_labels.m_unpinButton))
  320. {
  321. if (m_selectedPinnedAssetIndex >= 0 && m_selectedPinnedAssetIndex < m_pinnedAssets.size())
  322. {
  323. m_pinnedAssets.erase(m_pinnedAssets.begin() + m_selectedPinnedAssetIndex);
  324. pinListChanged = true;
  325. if (m_pinnedAssets.size() == 0)
  326. {
  327. // If there are no more pinned assets, explicitely set the m_selectedPinnedAssetIndex to -1. This seems like this should be imGui's responsibility, but it doesn't work that way.
  328. m_selectedPinnedAssetIndex = -1;
  329. }
  330. }
  331. }
  332. if (ScriptableImGui::Button(m_defaultPinnedAssetPaths.empty() ? "Clear" : "Reset to Default"))
  333. {
  334. m_confirmClearPinList.OpenPopupConfirmation(
  335. m_defaultPinnedAssetPaths.empty() ? "Confirm Clear" : "Confirm Reset to Default",
  336. AZStd::string::format("Reset %s to default?", widgetSettings.m_labels.m_pinnedAssetList),
  337. [this,&pinListChanged]() {
  338. ResetPinnedAssetsToDefault();
  339. pinListChanged = true;
  340. });
  341. }
  342. ImGui::TreePop();
  343. }
  344. ImGui::TreePop();
  345. // These only get set inside "if(isRootNodeOpen)" because otherwise we don't have correct values for isAvailableListOpen and isPinnedListOpen
  346. availableListExpansionChanged = isAvailableListOpen != m_configFile.m_expandAvailableList;
  347. pinnedListExpansionChanged = isPinnedListOpen != m_configFile.m_expandPinnedList;
  348. }
  349. m_confirmClearPinList.TickPopup();
  350. rootExpansionChanged = isRootNodeOpen != m_configFile.m_expandRoot;
  351. ImGui::PopID();
  352. if (pinListChanged ||
  353. rootExpansionChanged ||
  354. availableListExpansionChanged ||
  355. pinnedListExpansionChanged)
  356. {
  357. if (rootExpansionChanged)
  358. {
  359. m_configFile.m_expandRoot = isRootNodeOpen;
  360. }
  361. if (availableListExpansionChanged)
  362. {
  363. m_configFile.m_expandAvailableList = isAvailableListOpen;
  364. }
  365. if (pinnedListExpansionChanged)
  366. {
  367. m_configFile.m_expandPinnedList = isPinnedListOpen;
  368. }
  369. UpdateConfigFilePins();
  370. SaveConfigFile();
  371. }
  372. return selectionChanged;
  373. }
  374. } // namespace AtomSampleViewer