-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimgui_filebrowser.cpp
More file actions
502 lines (439 loc) · 13.3 KB
/
Copy pathimgui_filebrowser.cpp
File metadata and controls
502 lines (439 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "imgui_filebrowser.h"
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include <filesystem>
#include "ImGui/imgui.h"
#include "ImGui/imgui_custom.h"
#include "ImGui/imgui_stdlib.h"
#include "Utility/File/File.h"
inline std::string ImGuiStdStringReplace(const std::string& InStr, const std::string& InSearch, const std::string& InRep)
{
if(InSearch.empty())
return InStr;
std::string result = InStr;
size_t start_pos = 0;
while((start_pos = result.find(InSearch, start_pos)) != std::string::npos) {
result.replace(start_pos, InSearch.length(), InRep);
start_pos += InRep.length();
}
return result;
}
inline std::string ImGuiStdStringRemove(const std::string& InStr, const std::string& InSearch)
{
return ImGuiStdStringReplace(InStr, InSearch, "");
}
std::string ImGui::FileBrowser::GetLabel() const
{
return "File Browser##" + OriginalPath;
}
std::string ImGui::FileBrowser::GetRelative(const std::string& InPath)
{
return "..\\content\\" + InPath;
}
std::string ImGui::FileBrowser::GetLocal(const std::string& InPath)
{
const auto relativePath = std::filesystem::relative(InPath);
return ImGuiStdStringRemove(relativePath.string(), "..\\content");
}
std::string ImGui::FileBrowser::GetEditedPath(const std::string& InPath)
{
if (InPath.starts_with("..\\"))
return InPath.substr(3);
return InPath;
}
bool ImGui::OpenFileBrowser(const std::string& InPath, FileBrowserOption InOption, const std::set<std::string>& InExt)
{
// Create instance if it does not exist
if (!FileBrowser::Instance)
FileBrowser::Instance = new FileBrowser();
return FileBrowser::Instance->OpenInternal(InPath, InOption, InExt);
}
bool ImGui::FetchFileBrowserResult(const std::string& InPath, std::string& OutSelectedPath)
{
if (FileBrowser::Instance)
{
if (FileBrowser::Instance->FetchInternal(InPath, OutSelectedPath))
{
// Delete instance when finished
delete FileBrowser::Instance;
FileBrowser::Instance = nullptr;
return true;
}
}
return false;
}
bool ImGui::FileBrowser::OpenInternal(const std::string& InPath, FileBrowserOption InOption, const std::set<std::string>& InExt)
{
if (IsOpen)
return OriginalPath == InPath;
std::string directory = InPath;
if (!std::filesystem::is_directory(GetRelative(directory)))
{
const auto dirPath = std::filesystem::path(directory);
if (dirPath.has_parent_path())
directory = dirPath.parent_path().string();
}
// Setup
IsOpen = true;
OriginalPath = InPath;
Option = InOption;
Ext = InExt;
TryApplyPath(directory);
OpenPopup(GetLabel().c_str());
return true;
}
bool ImGui::FileBrowser::FetchInternal(const std::string& InPath, std::string& OutSelectedPath)
{
if (!IsOpen)
return false;
if (OriginalPath != InPath)
return false;
bool result = false;
ImGuiIO& io = GetIO();
SetNextWindowSize({ Width, Height });
SetNextWindowPos(io.DisplaySize * 0.5f, ImGuiCond_Appearing, ImVec2(0.5f,0.5f));
SetNextWindowFocus();
if (BeginPopupModal(GetLabel().c_str(), nullptr, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse))
{
EditNavigation();
Spacing();
Separator();
Spacing();
EditContent();
Spacing();
// Extension text in the bottom right
std::string ext;
switch (Option)
{
case FileBrowserOption::DIRECTORY:
ext = "Directory";
break;
case FileBrowserOption::FILE:
for (auto e : Ext)
{
if (!ext.empty())
ext += ", ";
ext += e;
}
break;
}
if (!ext.empty())
{
Text((" Ext: " + ext).c_str());
SameLine();
}
// Select / Cancel buttons
const ImGuiStyle style = GetStyle();
constexpr ImVec2 buttonSize(100.f, 0.f);
const float widthNeeded = buttonSize.x + style.ItemSpacing.x + buttonSize.x;
SetCursorPosX(GetCursorPosX() + GetContentRegionAvail().x - widthNeeded);
if (Button("Cancel", buttonSize))
{
OutSelectedPath = OriginalPath;
result = true;
}
SameLine();
if (Button("Select", buttonSize))
{
switch (Option)
{
case FileBrowserOption::DIRECTORY:
OutSelectedPath = Path;
break;
case FileBrowserOption::FILE:
OutSelectedPath = Selected.empty() ?
OriginalPath : Path + "\\" + Selected;
break;
}
while (OutSelectedPath.starts_with('\\'))
OutSelectedPath = OutSelectedPath.substr(1);
result = true;
}
EndPopup();
}
return result;
}
void ImGui::FileBrowser::Refresh()
{
// 1. Find current directory
const std::filesystem::path path(GetRelative(Path));
if (!std::filesystem::exists(path))
{
TryPopPath();
return;
}
// 2. Store directory content
Directories.clear();
Files.clear();
for (auto& entry : std::filesystem::directory_iterator(path))
{
if (entry.is_directory())
{
Directories.push_back(entry.path().filename().string());
}
else if (Option != FileBrowserOption::DIRECTORY)
{
// Filter extensions
if (!Ext.empty())
{
if (!entry.path().has_extension())
continue;
const std::string ext = entry.path().extension().string();
if (!Ext.contains(ext))
continue;
}
Files.push_back(entry.path().filename().string());
}
}
Selected = "";
RenameResult = "";
newEntry = false;
NavigationGuess = "";
// 3. Refresh hint
RefreshGuess();
}
void ImGui::FileBrowser::RefreshGuess()
{
std::string editedPath = GetEditedPath(EditedPath);
const std::filesystem::path path(GetRelative(editedPath));
if (!path.has_parent_path())
return;
if (!exists(path.parent_path()))
return;
int matchC = 0;
std::string matchPath;
for (auto& entry : std::filesystem::directory_iterator(path.parent_path()))
{
// Find content of parent path
if (!entry.is_directory())
continue;
// Compare to EditedPath
// Find the one with greatest match
std::string entryPath = GetLocal(entry.path().string());
int i = 0;
for (i = 0; i < MIN(entryPath.length(), editedPath.length()); i++)
if (entryPath[i] != editedPath[i])
break;
if (i > matchC)
{
matchPath = entryPath;
matchC = i;
}
}
if (matchC > 0)
NavigationGuess = matchPath;
}
bool ImGui::FileBrowser::TryPopPath()
{
// Pop until exists or at root
std::filesystem::path path(GetRelative(Path));
while (!std::filesystem::exists(path) && path.has_parent_path())
path = path.parent_path();
return TryApplyPath(GetLocal(path.parent_path().string()));
}
bool ImGui::FileBrowser::TryApplyPath(const std::string& InString)
{
const std::filesystem::path path(GetRelative(InString));
if (!std::filesystem::exists(path))
return false;
Path = GetLocal(path.string());
EditedPath = "..\\" + Path;
Refresh();
return true;
}
void ImGui::FileBrowser::EditNavigation()
{
constexpr ImGuiInputTextFlags flags =
ImGuiInputTextFlags_CharsNoBlank |
ImGuiInputTextFlags_AutoSelectAll |
ImGuiInputTextFlags_EnterReturnsTrue |
ImGuiInputTextFlags_CallbackCompletion |
ImGuiInputTextFlags_CallbackHistory;
if (InputText("##Path", &EditedPath, flags))
{
EditedPath == ".." ?
TryPopPath() :
TryApplyPath(GetEditedPath(EditedPath));
}
if (IsItemEdited())
RefreshGuess();
if (IsItemActive())
{
if (IsKeyDown(ImGuiKey_Tab))
{
EditedPath == ".." ?
TryPopPath() : TryApplyPath(NavigationGuess);
ClearActiveID();
}
}
SameLine();
if (Button("^"))
TryPopPath();
SameLine();
if (Button("Refresh"))
Refresh();
}
void ImGui::FileBrowser::EditContent()
{
constexpr ImVec2 buttonSize(100.f, 0.f);
if (Button("New", buttonSize))
{
Refresh();
// We're creating a new entry and
// using the rename feature to select a name
std::string newFileName = "untitled";
newEntry = true;
Selected = newFileName;
RenameResult = newFileName;
}
bool disabled = Selected.empty();
if (disabled)
BeginDisabled();
SameLine();
if (Button("Duplicate", buttonSize))
{
// Duplicate current file / directory
std::string duplicate = TryDuplicate(Selected);
if (!duplicate.empty())
{
std::string prevSelected = Selected;
Refresh();
Selected = prevSelected;
}
}
SameLine();
if (Button("Rename", buttonSize))
{
// Rename current entry
RenameResult = Selected;
}
SameLine();
if (Button("Delete", buttonSize))
{
// Delete current file / directory
if (TryDelete(Selected))
Refresh();
}
if (disabled)
EndDisabled();
const ImGuiStyle style = GetStyle();
const ImVec2 size = {
Width - style.WindowPadding.x * 2.0f,
Height - style.WindowPadding.y * 2.0f - 115.0f
};
if (BeginListBox("##FileBrowserContent", size))
{
Spacing();
Spacing();
if (!Path.empty())
if (Selectable(" ..##FileListBack"))
TryPopPath();
bool renaming = !RenameResult.empty();
std::string newDir;
for (auto& dir : Directories)
{
if (ContentEntry(dir, true))
{
if (dir == Selected)
{
newDir = dir;
}
else
{
Selected = dir;
RenameResult = "";
}
}
}
for (auto& file : Files)
{
if (ContentEntry(file, false))
{
Selected = file;
RenameResult = "";
}
}
if (newEntry)
ContentEntry(Selected, false);
if (!newDir.empty())
TryApplyPath(Path + "\\" + newDir);
if (renaming && RenameResult.empty())
Refresh();
Spacing();
Spacing();
EndListBox();
}
}
bool ImGui::FileBrowser::ContentEntry(const std::string& InEntry, bool InIsDir)
{
const bool selected = Selected == InEntry;
if (selected && !RenameResult.empty())
{
constexpr ImGuiInputTextFlags flags =
ImGuiInputTextFlags_EnterReturnsTrue |
ImGuiInputTextFlags_EscapeClearsAll |
ImGuiInputTextFlags_CharsNoBlank;
if (InputText("##ListEntryRename", &RenameResult, flags))
{
TryApplyRename(Selected, RenameResult);
RenameResult = "";
}
return false;
}
return Selectable(((InIsDir ? "- " : " ") + InEntry + "##ListEntry").c_str(), selected);
}
bool ImGui::FileBrowser::TryApplyRename(const std::string& InPreviousName, const std::string& InNewName) const
{
const std::filesystem::path newPath(GetRelative(Path + "\\" + InNewName));
const std::filesystem::path oldPath(GetRelative(Path + "\\" + InPreviousName));
if (std::filesystem::exists(newPath))
return false;
if (!std::filesystem::exists(oldPath))
{
// Do not rename, instead create file / directory
if (newPath.has_extension())
{
// Create file
Utility::WriteFile(Path + "\\" + InNewName, "");
}
else
{
// Create dir
std::filesystem::create_directory(newPath);
}
}
else
{
std::filesystem::rename(oldPath, newPath);
}
return std::filesystem::exists(newPath);
}
bool ImGui::FileBrowser::TryDelete(const std::string& InName) const
{
const std::filesystem::path path(GetRelative(Path + "\\" + InName));
if (!std::filesystem::exists(path))
return false;
return std::filesystem::remove_all(path);
}
std::string ImGui::FileBrowser::TryDuplicate(const std::string& InName) const
{
const std::filesystem::path path(GetRelative(Path + "\\" + InName));
if (!std::filesystem::exists(path))
return {};
int copyC = 0;
while (copyC < 100)
{
copyC++;
const std::string newName = InName + "_" + std::to_string(copyC);
const std::filesystem::path newPath(GetRelative(Path + "\\" + newName));
if (!std::filesystem::exists(newPath))
{
std::filesystem::copy(path, newPath);
if (std::filesystem::exists(newPath))
return newName;
return {};
}
}
return {};
}