ImGuiSaveFilePath.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/ImGuiSaveFilePath.h>
  9. #include <Automation/ScriptableImGui.h>
  10. #include <AzFramework/IO/LocalFileIO.h>
  11. #include <AzFramework/StringFunc/StringFunc.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzCore/Serialization/Utils.h>
  14. namespace AtomSampleViewer
  15. {
  16. void ImGuiSaveFilePath::Reflect(AZ::ReflectContext* context)
  17. {
  18. ImGuiSaveFilePath::Config::Reflect(context);
  19. }
  20. void ImGuiSaveFilePath::Config::Reflect(AZ::ReflectContext* context)
  21. {
  22. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  23. {
  24. serializeContext->Class<Config>()
  25. ->Version(0)
  26. ->Field("autoMode", &Config::m_autoMode)
  27. ->Field("currentExtension", &Config::m_currentExtension)
  28. ->Field("filePath", &Config::m_filePath)
  29. ;
  30. }
  31. }
  32. ImGuiSaveFilePath::ImGuiSaveFilePath(AZStd::string_view configFilePath)
  33. {
  34. m_configFilePath = configFilePath;
  35. }
  36. void ImGuiSaveFilePath::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. LoadConfigFile();
  44. }
  45. void ImGuiSaveFilePath::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. }
  55. void ImGuiSaveFilePath::SetDefaultFolder(const AZStd::string& folderPath)
  56. {
  57. m_defaultFolder = folderPath;
  58. }
  59. void ImGuiSaveFilePath::SetDefaultFileName(const AZStd::string& fileNameNoExt)
  60. {
  61. m_defaultFileName = fileNameNoExt;
  62. }
  63. void ImGuiSaveFilePath::SetAvailableExtensions(const AZStd::vector<AZStd::string>& extensions)
  64. {
  65. AZ_Assert(!extensions.empty(), "At least one extension is required");
  66. m_availableExtensions = extensions;
  67. }
  68. bool ImGuiSaveFilePath::LoadConfigFile()
  69. {
  70. AZStd::unique_ptr<Config> configFile(AZ::Utils::LoadObjectFromFile<Config>(m_configFilePath));
  71. if (configFile)
  72. {
  73. m_config = *configFile;
  74. azstrncpy(m_filePath, AZ_ARRAY_SIZE(m_filePath), m_config.m_filePath.c_str(), m_config.m_filePath.size());
  75. if (m_config.m_currentExtension >= m_availableExtensions.size() || m_config.m_currentExtension < 0)
  76. {
  77. m_config.m_currentExtension = 0;
  78. }
  79. return true;
  80. }
  81. else
  82. {
  83. return false;
  84. }
  85. }
  86. void ImGuiSaveFilePath::SaveConfigFile()
  87. {
  88. m_config.m_filePath = m_filePath;
  89. if (m_configFilePath.empty())
  90. {
  91. AZ_Warning("ImGuiSaveFilePath", false, "m_configFilePath is not set. GUI state not saved.");
  92. }
  93. else if (!AZ::Utils::SaveObjectToFile(m_configFilePath, AZ::DataStream::ST_XML, &m_config))
  94. {
  95. AZ_Error("ImGuiSaveFilePath", false, "Failed to save '%s'", m_configFilePath.c_str());
  96. }
  97. }
  98. AZStd::string ImGuiSaveFilePath::GetNextAutoSaveFilePath()
  99. {
  100. auto makeDefaultFilePath = [this](uint32_t fileIndex)
  101. {
  102. AZStd::string defaultFilePath;
  103. AZStd::string defaultFileName = AZStd::string::format("%s_%i.%s", m_defaultFileName.c_str(), fileIndex, m_availableExtensions[m_config.m_currentExtension].c_str());
  104. AzFramework::StringFunc::Path::Join(m_defaultFolder.c_str(), defaultFileName.c_str(), defaultFilePath, true, false);
  105. return defaultFilePath;
  106. };
  107. AZStd::string defaultFilePath = makeDefaultFilePath(m_autoFileIndex);
  108. while (AZ::IO::LocalFileIO().Exists(defaultFilePath.c_str()))
  109. {
  110. defaultFilePath = makeDefaultFilePath(++m_autoFileIndex);
  111. }
  112. return defaultFilePath;
  113. }
  114. bool ImGuiSaveFilePath::GetExtension(void* data, int index, const char** out)
  115. {
  116. ImGuiSaveFilePath* thisPtr = reinterpret_cast<ImGuiSaveFilePath*>(data);
  117. if (index < thisPtr->m_availableExtensions.size())
  118. {
  119. *out = thisPtr->m_availableExtensions[index].c_str();
  120. return true;
  121. }
  122. else
  123. {
  124. return false;
  125. }
  126. }
  127. void ImGuiSaveFilePath::Tick(const WidgetSettings& widgetSettings)
  128. {
  129. bool configChanged = false;
  130. if (ImGui::TreeNodeEx(widgetSettings.m_labels.m_filePath, ImGuiTreeNodeFlags_DefaultOpen))
  131. {
  132. if (ImGui::Checkbox("Auto", &m_config.m_autoMode))
  133. {
  134. configChanged = true;
  135. }
  136. if (m_config.m_autoMode)
  137. {
  138. if (m_availableExtensions.size() > 1)
  139. {
  140. if (ImGui::Combo("File Type", &m_config.m_currentExtension, &GetExtension, this, aznumeric_cast<int>(m_availableExtensions.size())))
  141. {
  142. // Search for a new "first available" filename
  143. m_autoFileIndex = 0;
  144. configChanged = true;
  145. }
  146. }
  147. AZStd::string defaultFilePath = GetNextAutoSaveFilePath();
  148. azstrncpy(m_filePath, AZ_ARRAY_SIZE(m_filePath), defaultFilePath.c_str(), defaultFilePath.size());
  149. ImGui::Text("%s", defaultFilePath.c_str());
  150. }
  151. else
  152. {
  153. if (ImGui::InputText("##FilePath", m_filePath, AZ_MAX_PATH_LEN))
  154. {
  155. configChanged = true;
  156. }
  157. }
  158. ImGui::TreePop();
  159. }
  160. if (configChanged)
  161. {
  162. SaveConfigFile();
  163. }
  164. }
  165. AZStd::string ImGuiSaveFilePath::GetSaveFilePath() const
  166. {
  167. return m_filePath;
  168. }
  169. } // namespace AtomSampleViewer