2
0

ImGuiSaveFilePath.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #pragma once
  9. #include <AzCore/RTTI/ReflectContext.h>
  10. #include <AzCore/IO/SystemFile.h>
  11. #include <AzCore/std/string/string.h>
  12. namespace AtomSampleViewer
  13. {
  14. //! Provides a common utility for either auto-generating or manually selecting a file path for saving.
  15. class ImGuiSaveFilePath
  16. {
  17. public:
  18. struct WidgetSettings
  19. {
  20. struct Labels
  21. {
  22. const char* m_filePath = "File Path";
  23. } m_labels;
  24. };
  25. static void Reflect(AZ::ReflectContext* context);
  26. void Activate();
  27. void Deactivate();
  28. //! @param configFilePath - Path to a local file for maintaining state between runs. Should start with "@user@/"
  29. explicit ImGuiSaveFilePath(AZStd::string_view configFilePath);
  30. void SetDefaultFolder(const AZStd::string& folderPath);
  31. void SetDefaultFileName(const AZStd::string& fileNameNoExt);
  32. //! Sets a list of available extensions that can be used in Auto mode. The first one will be the default.
  33. void SetAvailableExtensions(const AZStd::vector<AZStd::string>& extensions);
  34. //! Draw the ImGui
  35. //! @return true if the asset selection changed
  36. void Tick(const WidgetSettings& widgetSettings);
  37. //! Returns the save file path chosen by the user, either manually entered or auto-generated.
  38. AZStd::string GetSaveFilePath() const;
  39. //! Returns the path to a new file that doesn't exist, in the form:
  40. //! "[default folder]/[default file name]_[counter].[current extension]"
  41. //! Note, the function you probably want is GetSaveFilePath(). GetNextAutoSaveFilePath() is only for special cases.
  42. AZStd::string GetNextAutoSaveFilePath();
  43. private:
  44. struct Config final
  45. {
  46. AZ_TYPE_INFO(Config, "{FE014766-4D51-4FBB-B610-21A877C6F34D}");
  47. AZ_CLASS_ALLOCATOR(Config, AZ::SystemAllocator, 0);
  48. static void Reflect(AZ::ReflectContext* context);
  49. bool m_autoMode = true;
  50. int m_currentExtension = 0;
  51. AZStd::string m_filePath;
  52. };
  53. static bool GetExtension(void* data, int index, const char** out);
  54. bool LoadConfigFile();
  55. void SaveConfigFile();
  56. AZStd::string m_configFilePath;
  57. Config m_config;
  58. AZStd::string m_defaultFolder;
  59. AZStd::string m_defaultFileName;
  60. AZStd::vector<AZStd::string> m_availableExtensions;
  61. uint32_t m_autoFileIndex = 0;
  62. char m_filePath[AZ_MAX_PATH_LEN] = "";
  63. };
  64. } // namespace AtomSampleViewer