OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
imguifilesystem.h
Go to the documentation of this file.
1 // This software is provided 'as-is', without any express or implied
2 // warranty. In no event will the authors be held liable for any damages
3 // arising from the use of this software.
4 // Permission is granted to anyone to use this software for any purpose,
5 // including commercial applications, and to alter it and redistribute it
6 // freely, subject to the following restrictions:
7 // 1. The origin of this software must not be misrepresented; you must not
8 // claim that you wrote the original software. If you use this software
9 // in a product, an acknowledgment in the product documentation would be
10 // appreciated but is not required.
11 // 2. Altered source versions must be plainly marked as such, and must not be
12 // misrepresented as being the original software.
13 // 3. This notice may not be removed or altered from any source distribution.
14 
15 #ifndef IMGUI_FILESYSTEM_H_
16 #define IMGUI_FILESYSTEM_H_
17 
18 // USAGE EXAMPLE:
19 /*
20 #include "imguifilesystem.h" // imguifilesystem.cpp must be compiled
21 
22 // Inside a ImGui window:
23 const bool browseButtonPressed = ImGui::Button("..."); // we need a trigger boolean variable
24 static ImGuiFs::Dialog dlg; // one per dialog (and must be static)
25 dlg.chooseFileDialog(browseButtonPressed); // see other dialog types and the full list of arguments for advanced usage
26 if (strlen(dlg.getChosenPath())>0) {
27  ImGui::Text("Chosen file: \"%s\"",dlg.getChosenPath());
28 }
29 
30 // If you want to copy the (valid) returned path somewhere, you can use something like:
31 static char myPath[ImGuiFs::MAX_PATH_BYTES];
32 if (strlen(dlg.getChosenPath())>0) {
33  strcpy(myPath,dlg.getChosenPath());
34 }
35 */
36 
37 // MISSING FEATURES:
38 /*
39 -> [File and Folder] Links are NOT supported (they don't show up).
40 -> Multiselections in chooseFileDialogs are NOT supported.
41 -> Hidden and temporary files don't show up on nix systems (by design). Nothing has been done for Windows about it yet.
42 */
43 
44 // COMPILING AND TESTING:
45 /*
46 -> Compiled and tested using "ImGui library v1.17 wip"
47 
48 -> Successfully compiled using gcc, clang and mingw32 compilers.
49 x> Never compiled on any other compiler (Visual C++'s cl.exe included).
50 
51 -> Tested on Ubuntu 64bit and Wine 32bit.
52 x> Never tested on a real Windows OS and on MacOS.
53 */
54 
55 //#define DIRENT_USES_UTF8_CHARS // Optional. Affects Windows users only. Needs recompilation of imguifilesystem.cpp. Enables long UTF8 paths instead of short ASCII paths.
56  // Unfortunately it's NOT 100% functional (in my tests some folders can't be browsed). Patches are welcome. See "dirent_portable.h" for further info.
57  // When experiencing problems on Windows, trying commenting this definition out is a good start.
58  // On a second thought, I think we should leave this definition commented out (Windows users can always define it at the project level, if needed).
59 
60 namespace ImGuiFs {
61 
62 
63 extern const int MAX_FILENAME_BYTES;
64 extern const int MAX_PATH_BYTES;
65 
66 
67 struct Dialog {
68  public:
69 
70  // default arguments are usually what most users expect (better not touch them in most cases)
71  Dialog(bool noKnownDirectoriesSection=false,bool noCreateDirectorySection=false,bool noFilteringSection=false,bool detectKnownDirectoriesAtEachOpening=false,bool addDisplayByOption=false,bool dontFilterSaveFilePathsEnteredByTheUser=false);
72  ~Dialog();
73 
74  // "dialogTriggerButton" is usually a bool variable connected to a ImGui::Button(...).
75  // returns the chosen path (internally stored). Users can check when the returned path has strlen()>0.
76  // "fileFilterExtensionString" can be something like ".png;.jpg;.jpeg;.bmp;.tga;.gif;.tiff;.txt". It's case insensitive.
77  // "directory" and "startingFileNameEntry" (only available in saveFileDialog(...)) are quite flexible and can be set to almost everything: the method will use the most resonable choice.
78  const char* chooseFileDialog(bool dialogTriggerButton,const char* directory=NULL,const char* fileFilterExtensionString=NULL,const char* windowTitle=NULL,const ImVec2& windowSize=ImVec2(-1,-1),const ImVec2& windowPos=ImVec2(-1,-1),const float windowAlpha=0.875f);
79  const char* chooseFolderDialog(bool dialogTriggerButton,const char* directory=NULL,const char* windowTitle=NULL,const ImVec2& windowSize=ImVec2(-1,-1),const ImVec2& windowPos=ImVec2(-1,-1),const float windowAlpha=0.875f);
80  const char* saveFileDialog(bool dialogTriggerButton,const char* directory=NULL,const char* startingFileNameEntry=NULL,const char* fileFilterExtensionString=NULL,const char* windowTitle=NULL,const ImVec2& windowSize=ImVec2(-1,-1),const ImVec2& windowPos=ImVec2(-1,-1),const float windowAlpha=0.875f);
81 
82  // gets the chosen path (internally stored). It's valid (its strlen()>0) only when the user performs a valid selection.
83  const char* getChosenPath() const;
84  // returns the last directory browsed by the user using this class (internally stored). Can be passed as "directory" parameter in the methods above to reuse last used directory.
85  const char* getLastDirectory() const;
86 
87  private:
88  struct Internal* internal;
89  friend const char* ChooseFileMainMethod(Dialog& ist,const char* directory,const bool _isFolderChooserDialog,const bool _isSaveFileDialog,const char* _saveFileName,const char* fileFilterExtensionString,const char* windowTitle,const ImVec2& windowSize,const ImVec2& windowPos,const float windowAlpha);
90 };
91 
92 
93 } // namespace ImGuiFs
94 
95 #endif //IMGUI_FILESYSTEM_H_
const char * chooseFileDialog(bool dialogTriggerButton, const char *directory=NULL, const char *fileFilterExtensionString=NULL, const char *windowTitle=NULL, const ImVec2 &windowSize=ImVec2(-1,-1), const ImVec2 &windowPos=ImVec2(-1,-1), const float windowAlpha=0.875f)
Dialog(bool noKnownDirectoriesSection=false, bool noCreateDirectorySection=false, bool noFilteringSection=false, bool detectKnownDirectoriesAtEachOpening=false, bool addDisplayByOption=false, bool dontFilterSaveFilePathsEnteredByTheUser=false)
const int MAX_FILENAME_BYTES
friend const char * ChooseFileMainMethod(Dialog &ist, const char *directory, const bool _isFolderChooserDialog, const bool _isSaveFileDialog, const char *_saveFileName, const char *fileFilterExtensionString, const char *windowTitle, const ImVec2 &windowSize, const ImVec2 &windowPos, const float windowAlpha)
const char * getLastDirectory() const
const char * getChosenPath() const
Definition: imgui.h:50
const char * saveFileDialog(bool dialogTriggerButton, const char *directory=NULL, const char *startingFileNameEntry=NULL, const char *fileFilterExtensionString=NULL, const char *windowTitle=NULL, const ImVec2 &windowSize=ImVec2(-1,-1), const ImVec2 &windowPos=ImVec2(-1,-1), const float windowAlpha=0.875f)
const char * chooseFolderDialog(bool dialogTriggerButton, const char *directory=NULL, const char *windowTitle=NULL, const ImVec2 &windowSize=ImVec2(-1,-1), const ImVec2 &windowPos=ImVec2(-1,-1), const float windowAlpha=0.875f)
const int MAX_PATH_BYTES