1+ /**
2+ * Original Author: prime31
3+ * Source: https://gist.github.com/prime31/91d1582624eb2635395417393018016e
4+ */
5+ using ImGuiNET ;
6+ using System ;
7+ using System . Collections . Generic ;
8+ using System . IO ;
9+ using Num = System . Numerics ;
10+
11+ namespace FreeFrame . Lib . FilePicker
12+ {
13+ public class FilePicker
14+ {
15+ static readonly Dictionary < object , FilePicker > _filePickers = new Dictionary < object , FilePicker > ( ) ;
16+
17+ public string RootFolder ;
18+ public string CurrentFolder ;
19+ public string SelectedFile ;
20+ public List < string > AllowedExtensions ;
21+ public bool OnlyAllowFolders ;
22+
23+ public static FilePicker GetFolderPicker ( object o , string startingPath )
24+ => GetFilePicker ( o , startingPath , null , true ) ;
25+
26+ public static FilePicker GetFilePicker ( object o , string startingPath , string searchFilter = null , bool onlyAllowFolders = false )
27+ {
28+ if ( File . Exists ( startingPath ) )
29+ {
30+ startingPath = new FileInfo ( startingPath ) . DirectoryName ;
31+ }
32+ else if ( string . IsNullOrEmpty ( startingPath ) || ! Directory . Exists ( startingPath ) )
33+ {
34+ startingPath = Environment . CurrentDirectory ;
35+ if ( string . IsNullOrEmpty ( startingPath ) )
36+ startingPath = AppContext . BaseDirectory ;
37+ }
38+
39+ if ( ! _filePickers . TryGetValue ( o , out FilePicker fp ) )
40+ {
41+ fp = new FilePicker
42+ {
43+ RootFolder = startingPath ,
44+ CurrentFolder = startingPath ,
45+ OnlyAllowFolders = onlyAllowFolders
46+ } ;
47+
48+ if ( searchFilter != null )
49+ {
50+ if ( fp . AllowedExtensions != null )
51+ fp . AllowedExtensions . Clear ( ) ;
52+ else
53+ fp . AllowedExtensions = new List < string > ( ) ;
54+
55+ fp . AllowedExtensions . AddRange ( searchFilter . Split ( new char [ ] { '|' } , StringSplitOptions . RemoveEmptyEntries ) ) ;
56+ }
57+
58+ _filePickers . Add ( o , fp ) ;
59+ }
60+
61+ return fp ;
62+ }
63+
64+ public static void RemoveFilePicker ( object o ) => _filePickers . Remove ( o ) ;
65+
66+ public bool Draw ( )
67+ {
68+ if ( CurrentFolder . Length >= 50 )
69+ ImGui . Text ( "Current Folder: " + CurrentFolder . Replace ( CurrentFolder [ 10 ..( CurrentFolder . Length - 40 ) ] , "..." ) ) ;
70+ else
71+ ImGui . Text ( "Current Folder: " + CurrentFolder ) ;
72+ bool result = false ;
73+
74+ if ( ImGui . BeginChildFrame ( 1 , new Num . Vector2 ( 400 , 400 ) ) )
75+ {
76+ var di = new DirectoryInfo ( CurrentFolder ) ;
77+ if ( di . Exists )
78+ {
79+ if ( di . Parent != null ) // && CurrentFolder != RootFolder
80+ if ( ImGui . Selectable ( "../" , false , ImGuiSelectableFlags . DontClosePopups ) )
81+ CurrentFolder = di . Parent . FullName ;
82+
83+ var fileSystemEntries = GetFileSystemEntries ( di . FullName ) ;
84+ foreach ( var fse in fileSystemEntries )
85+ {
86+ if ( Directory . Exists ( fse ) )
87+ {
88+ var name = Path . GetFileName ( fse ) ;
89+ ImGui . PushStyleColor ( ImGuiCol . Text , new Num . Vector4 ( 0 , 125 , 255 , 255 ) ) ;
90+ if ( ImGui . Selectable ( $ "[D] { name } /", false , ImGuiSelectableFlags . DontClosePopups ) )
91+ CurrentFolder = fse ;
92+ ImGui . PopStyleColor ( ) ;
93+ }
94+ else
95+ {
96+ var name = Path . GetFileName ( fse ) ;
97+ bool isSelected = SelectedFile == fse ;
98+ if ( ImGui . Selectable ( $ "[F] { name } ", isSelected , ImGuiSelectableFlags . DontClosePopups ) )
99+ SelectedFile = fse ;
100+
101+ if ( ImGui . IsMouseDoubleClicked ( 0 ) )
102+ {
103+ result = true ;
104+ ImGui . CloseCurrentPopup ( ) ;
105+ }
106+ }
107+ }
108+ }
109+ }
110+ ImGui . EndChildFrame ( ) ;
111+
112+
113+ if ( ImGui . Button ( "Cancel" ) )
114+ {
115+ result = false ;
116+ ImGui . CloseCurrentPopup ( ) ;
117+ }
118+
119+ if ( OnlyAllowFolders )
120+ {
121+ ImGui . SameLine ( ) ;
122+ if ( ImGui . Button ( "Open" ) )
123+ {
124+ result = true ;
125+ SelectedFile = CurrentFolder ;
126+ ImGui . CloseCurrentPopup ( ) ;
127+ }
128+ }
129+ else if ( SelectedFile != null )
130+ {
131+ ImGui . SameLine ( ) ;
132+ if ( ImGui . Button ( "Open" ) )
133+ {
134+ result = true ;
135+ ImGui . CloseCurrentPopup ( ) ;
136+ }
137+ }
138+
139+ return result ;
140+ }
141+ List < string > GetFileSystemEntries ( string fullName )
142+ {
143+ var files = new List < string > ( ) ;
144+ var dirs = new List < string > ( ) ;
145+
146+ foreach ( var fse in Directory . GetFileSystemEntries ( fullName , "" ) )
147+ {
148+ if ( Directory . Exists ( fse ) )
149+ {
150+ dirs . Add ( fse ) ;
151+ }
152+ else if ( ! OnlyAllowFolders )
153+ {
154+ if ( AllowedExtensions != null )
155+ {
156+ if ( AllowedExtensions . Contains ( Path . GetExtension ( fse ) ) )
157+ files . Add ( fse ) ;
158+ }
159+ else
160+ files . Add ( fse ) ;
161+ }
162+ }
163+
164+ var ret = new List < string > ( dirs ) ;
165+ ret . AddRange ( files ) ;
166+
167+ return ret ;
168+ }
169+
170+ }
171+ }
0 commit comments