1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#include "file_browser.h"
5
6#include <base/dbg.h>
7#include <base/str.h>
8#include <base/time.h>
9
10#include <engine/font_icons.h>
11#include <engine/keys.h>
12#include <engine/sound.h>
13#include <engine/storage.h>
14
15#include <game/client/ui.h>
16#include <game/editor/editor.h>
17
18static constexpr const char *FILETYPE_EXTENSIONS[] = {
19 ".map",
20 ".png",
21 ".opus"};
22
23void CFileBrowser::ShowFileDialog(
24 int StorageType, EFileType FileType,
25 const char *pTitle, const char *pButtonText,
26 const char *pInitialPath, const char *pInitialFilename,
27 FFileDialogOpenCallback pfnOpenCallback, void *pOpenCallbackUser)
28{
29 m_StorageType = StorageType;
30 m_FileType = FileType;
31 if(m_StorageType == IStorage::TYPE_ALL)
32 {
33 int NumStoragesWithFolder = 0;
34 for(int CheckStorageType = IStorage::TYPE_SAVE; CheckStorageType < Storage()->NumPaths(); ++CheckStorageType)
35 {
36 if(Storage()->FolderExists(pFilename: pInitialPath, Type: CheckStorageType))
37 {
38 NumStoragesWithFolder++;
39 }
40 }
41 m_MultipleStorages = NumStoragesWithFolder > 1;
42 }
43 else
44 {
45 m_MultipleStorages = false;
46 }
47 m_SaveAction = m_StorageType == IStorage::TYPE_SAVE;
48
49 Ui()->ClosePopupMenus();
50 str_copy(dst&: m_aTitle, src: pTitle);
51 str_copy(dst&: m_aButtonText, src: pButtonText);
52 m_pfnOpenCallback = pfnOpenCallback;
53 m_pOpenCallbackUser = pOpenCallbackUser;
54 m_ShowingRoot = false;
55 str_copy(dst&: m_aInitialFolder, src: pInitialPath);
56 str_copy(dst&: m_aCurrentFolder, src: pInitialPath);
57 m_aCurrentLink[0] = '\0';
58 m_pCurrentPath = m_aCurrentFolder;
59 m_FilenameInput.Set(pInitialFilename);
60 m_FilterInput.Clear();
61 dbg_assert(m_PreviewState == EPreviewState::UNLOADED, "Preview was not unloaded before showing file dialog");
62 m_ListBox.Reset();
63
64 FilelistPopulate(StorageType: m_StorageType, KeepSelection: false);
65
66 if(m_SaveAction)
67 {
68 Ui()->SetActiveItem(&m_FilenameInput);
69 if(!m_FilenameInput.IsEmpty())
70 {
71 UpdateSelectedIndex(pDisplayName: m_FilenameInput.GetString());
72 }
73 }
74 else
75 {
76 Ui()->SetActiveItem(&m_FilterInput);
77 UpdateFilenameInput();
78 }
79
80 Editor()->m_Dialog = DIALOG_FILE;
81}
82
83void CFileBrowser::Render()
84{
85 if(Editor()->m_Dialog != DIALOG_FILE)
86 {
87 return;
88 }
89
90 Ui()->MapScreen();
91 CUIRect View = *Ui()->Screen();
92 CUIRect Preview = {.x: 0.0f, .y: 0.0f, .w: 0.0f, .h: 0.0f};
93 const float OriginalWidth = View.w;
94 const float OriginalHeight = View.h;
95
96 // Prevent UI elements below the file browser from being activated.
97 Ui()->SetHotItem(this);
98
99 View.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.25f), Corners: IGraphics::CORNER_NONE, Rounding: 0.0f);
100 View.VMargin(Cut: 150.0f, pOtherRect: &View);
101 View.HMargin(Cut: 50.0f, pOtherRect: &View);
102 View.Draw(Color: ColorRGBA(0.0f, 0.0f, 0.0f, 0.75f), Corners: IGraphics::CORNER_ALL, Rounding: 5.0f);
103 View.Margin(Cut: 10.0f, pOtherRect: &View);
104
105 CUIRect Title, FileBox, FileBoxLabel, ButtonBar, PathBox;
106 View.HSplitTop(Cut: 20.0f, pTop: &Title, pBottom: &View);
107 View.HSplitTop(Cut: 5.0f, pTop: nullptr, pBottom: &View);
108 View.HSplitBottom(Cut: 20.0f, pTop: &View, pBottom: &ButtonBar);
109 View.HSplitBottom(Cut: 5.0f, pTop: &View, pBottom: nullptr);
110 View.HSplitBottom(Cut: 15.0f, pTop: &View, pBottom: &PathBox);
111 View.HSplitBottom(Cut: 5.0f, pTop: &View, pBottom: nullptr);
112 View.HSplitBottom(Cut: 15.0f, pTop: &View, pBottom: &FileBox);
113 FileBox.VSplitLeft(Cut: 55.0f, pLeft: &FileBoxLabel, pRight: &FileBox);
114 View.HSplitBottom(Cut: 5.0f, pTop: &View, pBottom: nullptr);
115 if(CanPreviewFile())
116 {
117 View.VSplitMid(pLeft: &View, pRight: &Preview);
118 }
119
120 // Title bar sort buttons
121 if(!m_ShowingRoot)
122 {
123 CUIRect ButtonTimeModified, ButtonFilename;
124 Title.VSplitRight(Cut: m_ListBox.ScrollbarWidthMax(), pLeft: &Title, pRight: nullptr);
125 Title.VSplitRight(Cut: 90.0f, pLeft: &Title, pRight: &ButtonTimeModified);
126 Title.VSplitRight(Cut: 5.0f, pLeft: &Title, pRight: nullptr);
127 Title.VSplitRight(Cut: 90.0f, pLeft: &Title, pRight: &ButtonFilename);
128 Title.VSplitRight(Cut: 5.0f, pLeft: &Title, pRight: nullptr);
129
130 static constexpr const char *SORT_INDICATORS[] = {"", "▲", "▼"};
131
132 char aLabelButtonSortTimeModified[64];
133 str_format(buffer: aLabelButtonSortTimeModified, buffer_size: sizeof(aLabelButtonSortTimeModified), format: "Time modified %s", SORT_INDICATORS[(int)m_SortByTimeModified]);
134 if(Editor()->DoButton_Editor(pId: &m_ButtonSortTimeModifiedId, pText: aLabelButtonSortTimeModified, Checked: 0, pRect: &ButtonTimeModified, Flags: BUTTONFLAG_LEFT, pToolTip: "Sort by time modified."))
135 {
136 if(m_SortByTimeModified == ESortDirection::ASCENDING)
137 {
138 m_SortByTimeModified = ESortDirection::DESCENDING;
139 }
140 else if(m_SortByTimeModified == ESortDirection::DESCENDING)
141 {
142 m_SortByTimeModified = ESortDirection::NEUTRAL;
143 }
144 else
145 {
146 m_SortByTimeModified = ESortDirection::ASCENDING;
147 }
148
149 RefreshFilteredFileList();
150 }
151
152 char aLabelButtonSortFilename[64];
153 str_format(buffer: aLabelButtonSortFilename, buffer_size: sizeof(aLabelButtonSortFilename), format: "Filename %s", SORT_INDICATORS[(int)m_SortByFilename]);
154 if(Editor()->DoButton_Editor(pId: &m_ButtonSortFilenameId, pText: aLabelButtonSortFilename, Checked: 0, pRect: &ButtonFilename, Flags: BUTTONFLAG_LEFT, pToolTip: "Sort by filename."))
155 {
156 if(m_SortByFilename == ESortDirection::DESCENDING)
157 {
158 m_SortByFilename = ESortDirection::ASCENDING;
159 m_SortByTimeModified = ESortDirection::NEUTRAL;
160 }
161 else
162 {
163 m_SortByFilename = ESortDirection::DESCENDING;
164 m_SortByTimeModified = ESortDirection::NEUTRAL;
165 }
166
167 RefreshFilteredFileList();
168 }
169 }
170
171 // Title
172 Title.Draw(Color: ColorRGBA(1.0f, 1.0f, 1.0f, 0.25f), Corners: IGraphics::CORNER_ALL, Rounding: 5.0f);
173 Title.VMargin(Cut: 10.0f, pOtherRect: &Title);
174 Ui()->DoLabel(pRect: &Title, pText: m_aTitle, Size: 12.0f, Align: TEXTALIGN_ML);
175
176 // Current path
177 if(m_SelectedFileIndex >= 0 && m_vpFilteredFileList[m_SelectedFileIndex]->m_StorageType >= IStorage::TYPE_SAVE)
178 {
179 char aPath[IO_MAX_PATH_LENGTH];
180 Storage()->GetCompletePath(Type: m_vpFilteredFileList[m_SelectedFileIndex]->m_StorageType, pDir: m_pCurrentPath, pBuffer: aPath, BufferSize: sizeof(aPath));
181 char aPathLabel[128 + IO_MAX_PATH_LENGTH];
182 str_format(buffer: aPathLabel, buffer_size: sizeof(aPathLabel), format: "Current path: %s", aPath);
183 Ui()->DoLabel(pRect: &PathBox, pText: aPathLabel, Size: 10.0f, Align: TEXTALIGN_ML, LabelProps: {.m_MaxWidth = PathBox.w, .m_EllipsisAtEnd = true});
184 }
185
186 m_ListBox.SetActive(!Ui()->IsPopupOpen());
187
188 // Filename/filter input
189 if(m_SaveAction)
190 {
191 // Filename input when saving
192 Ui()->DoLabel(pRect: &FileBoxLabel, pText: "Filename:", Size: 10.0f, Align: TEXTALIGN_ML);
193 if(Ui()->DoEditBox(pLineInput: &m_FilenameInput, pRect: &FileBox, FontSize: 10.0f))
194 {
195 UpdateSelectedIndex(pDisplayName: m_FilenameInput.GetString());
196 }
197 }
198 else
199 {
200 // Filter input when loading
201 Ui()->DoLabel(pRect: &FileBoxLabel, pText: "Search:", Size: 10.0f, Align: TEXTALIGN_ML);
202 if(Input()->KeyPress(Key: KEY_F) && Input()->ModifierIsPressed())
203 {
204 Ui()->SetActiveItem(&m_FilterInput);
205 m_FilterInput.SelectAll();
206 }
207 if(Ui()->DoClearableEditBox(pLineInput: &m_FilterInput, pRect: &FileBox, FontSize: 10.0f))
208 {
209 RefreshFilteredFileList();
210 if(m_vpFilteredFileList.empty())
211 {
212 m_SelectedFileIndex = -1;
213 }
214 else if(m_SelectedFileIndex == -1 ||
215 (!m_FilterInput.IsEmpty() && !str_find_nocase(haystack: m_vpFilteredFileList[m_SelectedFileIndex]->m_aDisplayName, needle: m_FilterInput.GetString())))
216 {
217 m_SelectedFileIndex = -1;
218 for(size_t i = 0; i < m_vpFilteredFileList.size(); i++)
219 {
220 if(str_find_nocase(haystack: m_vpFilteredFileList[i]->m_aDisplayName, needle: m_FilterInput.GetString()))
221 {
222 m_SelectedFileIndex = i;
223 break;
224 }
225 }
226 if(m_SelectedFileIndex == -1)
227 {
228 m_SelectedFileIndex = 0;
229 }
230 }
231 str_copy(dst&: m_aSelectedFileDisplayName, src: m_SelectedFileIndex >= 0 ? m_vpFilteredFileList[m_SelectedFileIndex]->m_aDisplayName : "");
232 UpdateFilenameInput();
233 m_ListBox.ScrollToSelected();
234 m_PreviewState = EPreviewState::UNLOADED;
235 }
236 }
237
238 // File preview
239 if(m_SelectedFileIndex >= 0 && CanPreviewFile())
240 {
241 UpdateFilePreview();
242 Preview.Margin(Cut: 10.0f, pOtherRect: &Preview);
243 RenderFilePreview(Preview);
244 }
245
246 // File list
247 m_ListBox.DoStart(RowHeight: 15.0f, NumItems: m_vpFilteredFileList.size(), ItemsPerRow: 1, RowsPerScroll: 5, SelectedIndex: m_SelectedFileIndex, pRect: &View, Background: false, BackgroundCorners: IGraphics::CORNER_ALL, ForceShowScrollbar: true);
248
249 for(size_t i = 0; i < m_vpFilteredFileList.size(); i++)
250 {
251 const CListboxItem Item = m_ListBox.DoNextItem(pId: m_vpFilteredFileList[i], Selected: m_SelectedFileIndex >= 0 && (size_t)m_SelectedFileIndex == i);
252 if(!Item.m_Visible)
253 {
254 continue;
255 }
256
257 CUIRect Button, FileIcon, TimeModified;
258 Item.m_Rect.VMargin(Cut: 2.0f, pOtherRect: &Button);
259 Button.VSplitLeft(Cut: Button.h, pLeft: &FileIcon, pRight: &Button);
260 Button.VSplitLeft(Cut: 5.0f, pLeft: nullptr, pRight: &Button);
261 Button.VSplitRight(Cut: 100.0f, pLeft: &Button, pRight: &TimeModified);
262 Button.VSplitRight(Cut: 5.0f, pLeft: &Button, pRight: nullptr);
263
264 TextRender()->SetFontPreset(EFontPreset::ICON_FONT);
265 TextRender()->SetRenderFlags(ETextRenderFlags::TEXT_RENDER_FLAG_ONLY_ADVANCE_WIDTH | ETextRenderFlags::TEXT_RENDER_FLAG_NO_X_BEARING | ETextRenderFlags::TEXT_RENDER_FLAG_NO_Y_BEARING);
266 Ui()->DoLabel(pRect: &FileIcon, pText: DetermineFileFontIcon(pItem: m_vpFilteredFileList[i]), Size: 12.0f, Align: TEXTALIGN_ML);
267 TextRender()->SetRenderFlags(0);
268 TextRender()->SetFontPreset(EFontPreset::DEFAULT_FONT);
269
270 Ui()->DoLabel(pRect: &Button, pText: m_vpFilteredFileList[i]->m_aDisplayName, Size: 10.0f, Align: TEXTALIGN_ML, LabelProps: {.m_MaxWidth = Button.w, .m_EllipsisAtEnd = true});
271
272 if(!m_vpFilteredFileList[i]->m_IsLink && str_comp(a: m_vpFilteredFileList[i]->m_aFilename, b: "..") != 0)
273 {
274 char aLabelTimeModified[64];
275 str_timestamp_ex(time: m_vpFilteredFileList[i]->m_TimeModified, buffer: aLabelTimeModified, buffer_size: sizeof(aLabelTimeModified), format: "%d.%m.%Y %H:%M");
276 Ui()->DoLabel(pRect: &TimeModified, pText: aLabelTimeModified, Size: 10.0f, Align: TEXTALIGN_MR);
277 }
278 }
279
280 const int NewSelection = m_ListBox.DoEnd();
281 if(NewSelection != m_SelectedFileIndex)
282 {
283 m_SelectedFileIndex = NewSelection;
284 str_copy(dst&: m_aSelectedFileDisplayName, src: m_SelectedFileIndex >= 0 ? m_vpFilteredFileList[m_SelectedFileIndex]->m_aDisplayName : "");
285 const bool WasChanged = m_FilenameInput.WasChanged();
286 UpdateFilenameInput();
287 if(!WasChanged) // ensure that changed flag is not set if it wasn't previously set, as this would reset the selection after DoEditBox is called
288 {
289 m_FilenameInput.WasChanged(); // this clears the changed flag
290 }
291 m_PreviewState = EPreviewState::UNLOADED;
292 }
293
294 // Buttons
295 const float ButtonSpacing = ButtonBar.w > 600.0f ? 40.0f : 10.0f;
296
297 CUIRect Button;
298 ButtonBar.VSplitRight(Cut: 50.0f, pLeft: &ButtonBar, pRight: &Button);
299 const bool IsDir = m_SelectedFileIndex >= 0 && m_vpFilteredFileList[m_SelectedFileIndex]->m_IsDir;
300 const char *pOpenTooltip = IsDir ? "Open the selected folder." : (m_SaveAction ? "Save file with the specified name." : "Open the selected file.");
301 if(Editor()->DoButton_Editor(pId: &m_ButtonOkId, pText: IsDir ? "Open" : m_aButtonText, Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: pOpenTooltip) ||
302 m_ListBox.WasItemActivated() ||
303 (m_ListBox.Active() && Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ENTER)))
304 {
305 if(IsDir)
306 {
307 m_FilterInput.Clear();
308 Ui()->SetActiveItem(&m_FilterInput);
309 const bool ParentFolder = str_comp(a: m_vpFilteredFileList[m_SelectedFileIndex]->m_aFilename, b: "..") == 0;
310 if(ParentFolder)
311 {
312 str_copy(dst&: m_aSelectedFileDisplayName, src: fs_filename(path: m_pCurrentPath));
313 str_append(dst&: m_aSelectedFileDisplayName, src: "/");
314 if(fs_parent_dir(path: m_pCurrentPath))
315 {
316 if(str_comp(a: m_pCurrentPath, b: m_aCurrentFolder) == 0)
317 {
318 m_ShowingRoot = true;
319 if(m_StorageType == IStorage::TYPE_ALL)
320 {
321 m_aSelectedFileDisplayName[0] = '\0'; // will select first list item
322 }
323 else
324 {
325 Storage()->GetCompletePath(Type: m_StorageType, pDir: m_pCurrentPath, pBuffer: m_aSelectedFileDisplayName, BufferSize: sizeof(m_aSelectedFileDisplayName));
326 str_append(dst&: m_aSelectedFileDisplayName, src: "/");
327 }
328 }
329 else
330 {
331 m_pCurrentPath = m_aCurrentFolder; // leave the link
332 str_copy(dst&: m_aSelectedFileDisplayName, src: m_aCurrentLink);
333 str_append(dst&: m_aSelectedFileDisplayName, src: "/");
334 }
335 }
336 }
337 else // sub folder
338 {
339 if(m_vpFilteredFileList[m_SelectedFileIndex]->m_IsLink)
340 {
341 m_pCurrentPath = m_aCurrentLink; // follow the link
342 str_copy(dst&: m_aCurrentLink, src: m_vpFilteredFileList[m_SelectedFileIndex]->m_aFilename);
343 }
344 else
345 {
346 str_append(dst: m_pCurrentPath, src: "/", dst_size: IO_MAX_PATH_LENGTH);
347 str_append(dst: m_pCurrentPath, src: m_vpFilteredFileList[m_SelectedFileIndex]->m_aFilename, dst_size: IO_MAX_PATH_LENGTH);
348 }
349 if(m_ShowingRoot)
350 {
351 m_StorageType = m_vpFilteredFileList[m_SelectedFileIndex]->m_StorageType;
352 }
353 m_ShowingRoot = false;
354 }
355 FilelistPopulate(StorageType: m_StorageType, KeepSelection: ParentFolder);
356 UpdateFilenameInput();
357 }
358 else // file
359 {
360 const int StorageType = m_SelectedFileIndex >= 0 ? m_vpFilteredFileList[m_SelectedFileIndex]->m_StorageType : m_StorageType;
361
362 char aFilename[IO_MAX_PATH_LENGTH];
363 str_copy(dst&: aFilename, src: m_FilenameInput.GetString());
364 if(!str_endswith(str: aFilename, suffix: FILETYPE_EXTENSIONS[(int)m_FileType]))
365 {
366 str_append(dst&: aFilename, src: FILETYPE_EXTENSIONS[(int)m_FileType]);
367 }
368
369 char aSaveFilePath[IO_MAX_PATH_LENGTH];
370 str_format(buffer: aSaveFilePath, buffer_size: sizeof(aSaveFilePath), format: "%s/%s", m_pCurrentPath, aFilename);
371
372 if(m_SaveAction && !str_valid_filename(str: aFilename))
373 {
374 Editor()->ShowFileDialogError(pFormat: "This name cannot be used for files and folders.");
375 }
376 else if(m_SaveAction && Storage()->FileExists(pFilename: aSaveFilePath, Type: StorageType))
377 {
378 m_PopupConfirmOverwrite.m_pFileBrowser = this;
379 str_copy(dst&: m_PopupConfirmOverwrite.m_aOverwritePath, src: aSaveFilePath);
380 constexpr float PopupWidth = 400.0f;
381 constexpr float PopupHeight = 150.0f;
382 Ui()->DoPopupMenu(pId: &m_PopupConfirmOverwrite,
383 X: OriginalWidth / 2.0f - PopupWidth / 2.0f, Y: OriginalHeight / 2.0f - PopupHeight / 2.0f, Width: PopupWidth, Height: PopupHeight,
384 pContext: &m_PopupConfirmOverwrite, pfnFunc: CPopupConfirmOverwrite::Render);
385 }
386 else if(m_pfnOpenCallback && (m_SaveAction || m_SelectedFileIndex >= 0))
387 {
388 m_pfnOpenCallback(aSaveFilePath, StorageType, m_pOpenCallbackUser);
389 }
390 }
391 }
392
393 ButtonBar.VSplitRight(Cut: ButtonSpacing, pLeft: &ButtonBar, pRight: nullptr);
394 ButtonBar.VSplitRight(Cut: 50.0f, pLeft: &ButtonBar, pRight: &Button);
395 if(Editor()->DoButton_Editor(pId: &m_ButtonCancelId, pText: "Cancel", Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Close this dialog.") ||
396 (m_ListBox.Active() && Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ESCAPE)))
397 {
398 Editor()->OnDialogClose();
399 }
400
401 ButtonBar.VSplitRight(Cut: ButtonSpacing, pLeft: &ButtonBar, pRight: nullptr);
402 ButtonBar.VSplitRight(Cut: 50.0f, pLeft: &ButtonBar, pRight: &Button);
403 if(Editor()->DoButton_Editor(pId: &m_ButtonRefreshId, pText: "Refresh", Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Refresh the list of files.") ||
404 (m_ListBox.Active() && (Input()->KeyIsPressed(Key: KEY_F5) || (Input()->ModifierIsPressed() && Input()->KeyIsPressed(Key: KEY_R)))))
405 {
406 FilelistPopulate(StorageType: m_StorageType, KeepSelection: true);
407 }
408
409 if(m_SelectedFileIndex >= 0 && m_vpFilteredFileList[m_SelectedFileIndex]->m_StorageType != IStorage::TYPE_ALL)
410 {
411 ButtonBar.VSplitRight(Cut: ButtonSpacing, pLeft: &ButtonBar, pRight: nullptr);
412 ButtonBar.VSplitRight(Cut: 90.0f, pLeft: &ButtonBar, pRight: &Button);
413 if(Editor()->DoButton_Editor(pId: &m_ButtonShowDirectoryId, pText: "Show directory", Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Open the current directory in the file browser."))
414 {
415 char aOpenPath[IO_MAX_PATH_LENGTH];
416 Storage()->GetCompletePath(Type: m_vpFilteredFileList[m_SelectedFileIndex]->m_StorageType, pDir: m_pCurrentPath, pBuffer: aOpenPath, BufferSize: sizeof(aOpenPath));
417 if(!Client()->ViewFile(pFilename: aOpenPath))
418 {
419 Editor()->ShowFileDialogError(pFormat: "Failed to open the directory '%s'.", aOpenPath);
420 }
421 }
422 }
423
424 ButtonBar.VSplitRight(Cut: ButtonSpacing, pLeft: &ButtonBar, pRight: nullptr);
425 ButtonBar.VSplitRight(Cut: 50.0f, pLeft: &ButtonBar, pRight: &Button);
426 if(m_SelectedFileIndex >= 0 &&
427 m_vpFilteredFileList[m_SelectedFileIndex]->m_StorageType == IStorage::TYPE_SAVE &&
428 !m_vpFilteredFileList[m_SelectedFileIndex]->m_IsLink &&
429 str_comp(a: m_vpFilteredFileList[m_SelectedFileIndex]->m_aFilename, b: "..") != 0)
430 {
431 if(Editor()->DoButton_Editor(pId: &m_ButtonDeleteId, pText: "Delete", Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: IsDir ? "Delete the selected folder." : "Delete the selected file.") ||
432 (m_ListBox.Active() && Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_DELETE)))
433 {
434 m_PopupConfirmDelete.m_pFileBrowser = this;
435 m_PopupConfirmDelete.m_IsDirectory = IsDir;
436 str_format(buffer: m_PopupConfirmDelete.m_aDeletePath, buffer_size: sizeof(m_PopupConfirmDelete.m_aDeletePath), format: "%s/%s", m_pCurrentPath, m_vpFilteredFileList[m_SelectedFileIndex]->m_aFilename);
437 constexpr float PopupWidth = 400.0f;
438 constexpr float PopupHeight = 150.0f;
439 Ui()->DoPopupMenu(pId: &m_PopupConfirmDelete,
440 X: OriginalWidth / 2.0f - PopupWidth / 2.0f, Y: OriginalHeight / 2.0f - PopupHeight / 2.0f, Width: PopupWidth, Height: PopupHeight,
441 pContext: &m_PopupConfirmDelete, pfnFunc: CPopupConfirmDelete::Render);
442 }
443 }
444
445 if(!m_ShowingRoot && m_StorageType == IStorage::TYPE_SAVE)
446 {
447 ButtonBar.VSplitLeft(Cut: 70.0f, pLeft: &Button, pRight: &ButtonBar);
448 if(Editor()->DoButton_Editor(pId: &m_ButtonNewFolderId, pText: "New folder", Checked: 0, pRect: &Button, Flags: BUTTONFLAG_LEFT, pToolTip: "Create a new folder."))
449 {
450 m_PopupNewFolder.m_pFileBrowser = this;
451 m_PopupNewFolder.m_NewFolderNameInput.Clear();
452 constexpr float PopupWidth = 400.0f;
453 constexpr float PopupHeight = 110.0f;
454 Ui()->DoPopupMenu(pId: &m_PopupNewFolder,
455 X: OriginalWidth / 2.0f - PopupWidth / 2.0f, Y: OriginalHeight / 2.0f - PopupHeight / 2.0f, Width: PopupWidth, Height: PopupHeight,
456 pContext: &m_PopupNewFolder, pfnFunc: CPopupNewFolder::Render);
457 Ui()->SetActiveItem(&m_PopupNewFolder.m_NewFolderNameInput);
458 }
459 }
460}
461
462bool CFileBrowser::IsValidSaveFilename() const
463{
464 return m_pCurrentPath == m_aCurrentFolder ||
465 (m_pCurrentPath == m_aCurrentLink && str_startswith(str: m_aCurrentLink, prefix: "themes"));
466}
467
468void CFileBrowser::OnEditorClose()
469{
470 if(m_PreviewSound >= 0 && Sound()->IsPlaying(SampleId: m_PreviewSound))
471 {
472 Sound()->Pause(SampleId: m_PreviewSound);
473 }
474}
475
476void CFileBrowser::OnDialogClose()
477{
478 m_PreviewState = EPreviewState::UNLOADED;
479 Graphics()->UnloadTexture(pIndex: &m_PreviewImage);
480 m_PreviewImageWidth = -1;
481 m_PreviewImageHeight = -1;
482 Sound()->UnloadSample(SampleId: m_PreviewSound);
483 m_PreviewSound = -1;
484}
485
486bool CFileBrowser::CanPreviewFile() const
487{
488 return m_FileType == CFileBrowser::EFileType::IMAGE ||
489 m_FileType == CFileBrowser::EFileType::SOUND;
490}
491
492void CFileBrowser::UpdateFilePreview()
493{
494 if(m_PreviewState != EPreviewState::UNLOADED ||
495 !str_endswith(str: m_vpFilteredFileList[m_SelectedFileIndex]->m_aFilename, suffix: FILETYPE_EXTENSIONS[(int)m_FileType]))
496 {
497 return;
498 }
499
500 if(m_FileType == CFileBrowser::EFileType::IMAGE)
501 {
502 char aImagePath[IO_MAX_PATH_LENGTH];
503 str_format(buffer: aImagePath, buffer_size: sizeof(aImagePath), format: "%s/%s", m_pCurrentPath, m_vpFilteredFileList[m_SelectedFileIndex]->m_aFilename);
504 CImageInfo PreviewImageInfo;
505 if(Graphics()->LoadPng(Image&: PreviewImageInfo, pFilename: aImagePath, StorageType: m_vpFilteredFileList[m_SelectedFileIndex]->m_StorageType))
506 {
507 Graphics()->UnloadTexture(pIndex: &m_PreviewImage);
508 m_PreviewImageWidth = PreviewImageInfo.m_Width;
509 m_PreviewImageHeight = PreviewImageInfo.m_Height;
510 m_PreviewImage = Graphics()->LoadTextureRawMove(Image&: PreviewImageInfo, Flags: 0, pTexName: aImagePath);
511 m_PreviewState = EPreviewState::LOADED;
512 }
513 else
514 {
515 m_PreviewState = EPreviewState::ERROR;
516 }
517 }
518 else if(m_FileType == CFileBrowser::EFileType::SOUND)
519 {
520 char aSoundPath[IO_MAX_PATH_LENGTH];
521 str_format(buffer: aSoundPath, buffer_size: sizeof(aSoundPath), format: "%s/%s", m_pCurrentPath, m_vpFilteredFileList[m_SelectedFileIndex]->m_aFilename);
522 Sound()->UnloadSample(SampleId: m_PreviewSound);
523 m_PreviewSound = Sound()->LoadOpus(pFilename: aSoundPath, StorageType: m_vpFilteredFileList[m_SelectedFileIndex]->m_StorageType);
524 m_PreviewState = m_PreviewSound == -1 ? EPreviewState::ERROR : EPreviewState::LOADED;
525 }
526}
527
528void CFileBrowser::RenderFilePreview(CUIRect Preview)
529{
530 if(m_FileType == CFileBrowser::EFileType::IMAGE)
531 {
532 if(m_PreviewState == EPreviewState::LOADED)
533 {
534 CUIRect PreviewLabel, PreviewImage;
535 Preview.HSplitTop(Cut: 20.0f, pTop: &PreviewLabel, pBottom: &PreviewImage);
536
537 char aSizeLabel[64];
538 str_format(buffer: aSizeLabel, buffer_size: sizeof(aSizeLabel), format: "Size: %d × %d", m_PreviewImageWidth, m_PreviewImageHeight);
539 Ui()->DoLabel(pRect: &PreviewLabel, pText: aSizeLabel, Size: 12.0f, Align: TEXTALIGN_ML);
540
541 int Width = m_PreviewImageWidth;
542 int Height = m_PreviewImageHeight;
543 if(m_PreviewImageWidth > PreviewImage.w)
544 {
545 Height = m_PreviewImageHeight * PreviewImage.w / m_PreviewImageWidth;
546 Width = PreviewImage.w;
547 }
548 if(Height > PreviewImage.h)
549 {
550 Width = Width * PreviewImage.h / Height;
551 Height = PreviewImage.h;
552 }
553
554 Graphics()->TextureSet(Texture: m_PreviewImage);
555 Graphics()->QuadsBegin();
556 IGraphics::CQuadItem QuadItem(PreviewImage.x, PreviewImage.y, Width, Height);
557 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
558 Graphics()->QuadsEnd();
559 }
560 else if(m_PreviewState == EPreviewState::ERROR)
561 {
562 Ui()->DoLabel(pRect: &Preview, pText: "Failed to load the image (check the local console for details).", Size: 12.0f, Align: TEXTALIGN_TL, LabelProps: {.m_MaxWidth = Preview.w});
563 }
564 }
565 else if(m_FileType == CFileBrowser::EFileType::SOUND)
566 {
567 if(m_PreviewState == EPreviewState::LOADED)
568 {
569 Preview.HSplitTop(Cut: 20.0f, pTop: &Preview, pBottom: nullptr);
570 Preview.VSplitLeft(Cut: Preview.h / 4.0f, pLeft: nullptr, pRight: &Preview);
571 Editor()->DoAudioPreview(View: Preview, pPlayPauseButtonId: &m_ButtonPlayPauseId, pStopButtonId: &m_ButtonStopId, pSeekBarId: &m_SeekBarId, SampleId: m_PreviewSound);
572 }
573 else if(m_PreviewState == EPreviewState::ERROR)
574 {
575 Ui()->DoLabel(pRect: &Preview, pText: "Failed to load the sound (check the local console for details). Make sure you enabled sounds in the settings.", Size: 12.0f, Align: TEXTALIGN_TL, LabelProps: {.m_MaxWidth = Preview.w});
576 }
577 }
578}
579
580const char *CFileBrowser::DetermineFileFontIcon(const CFilelistItem *pItem) const
581{
582 if(!pItem->m_IsDir)
583 {
584 switch(m_FileType)
585 {
586 case EFileType::MAP:
587 return FontIcon::MAP;
588 case EFileType::IMAGE:
589 return FontIcon::IMAGE;
590 case EFileType::SOUND:
591 return FontIcon::MUSIC;
592 default:
593 dbg_assert_failed("m_FileType invalid: %d", (int)m_FileType);
594 }
595 }
596 else if(pItem->m_IsLink || str_comp(a: pItem->m_aFilename, b: "..") == 0)
597 {
598 return FontIcon::FOLDER_TREE;
599 }
600 else
601 {
602 return FontIcon::FOLDER;
603 }
604}
605
606void CFileBrowser::UpdateFilenameInput()
607{
608 if(m_SelectedFileIndex >= 0 && !m_vpFilteredFileList[m_SelectedFileIndex]->m_IsDir)
609 {
610 char aNameWithoutExt[IO_MAX_PATH_LENGTH];
611 fs_split_file_extension(filename: m_vpFilteredFileList[m_SelectedFileIndex]->m_aFilename, name: aNameWithoutExt, name_size: sizeof(aNameWithoutExt));
612 m_FilenameInput.Set(aNameWithoutExt);
613 }
614 else
615 {
616 m_FilenameInput.Clear();
617 }
618}
619
620void CFileBrowser::UpdateSelectedIndex(const char *pDisplayName)
621{
622 m_SelectedFileIndex = -1;
623 m_aSelectedFileDisplayName[0] = '\0';
624 for(size_t i = 0; i < m_vpFilteredFileList.size(); i++)
625 {
626 if(str_comp_nocase(a: m_vpFilteredFileList[i]->m_aDisplayName, b: pDisplayName) == 0)
627 {
628 m_SelectedFileIndex = i;
629 str_copy(dst&: m_aSelectedFileDisplayName, src: m_vpFilteredFileList[i]->m_aDisplayName);
630 break;
631 }
632 }
633 if(m_SelectedFileIndex >= 0)
634 {
635 m_ListBox.ScrollToSelected();
636 }
637}
638
639void CFileBrowser::SortFilteredFileList()
640{
641 if(m_SortByFilename == ESortDirection::ASCENDING)
642 {
643 std::sort(first: m_vpFilteredFileList.begin(), last: m_vpFilteredFileList.end(), comp: CFileBrowser::CompareFilenameAscending);
644 }
645 else
646 {
647 std::sort(first: m_vpFilteredFileList.begin(), last: m_vpFilteredFileList.end(), comp: CFileBrowser::CompareFilenameDescending);
648 }
649
650 if(m_SortByTimeModified == ESortDirection::ASCENDING)
651 {
652 std::stable_sort(first: m_vpFilteredFileList.begin(), last: m_vpFilteredFileList.end(), comp: CFileBrowser::CompareTimeModifiedAscending);
653 }
654 else if(m_SortByTimeModified == ESortDirection::DESCENDING)
655 {
656 std::stable_sort(first: m_vpFilteredFileList.begin(), last: m_vpFilteredFileList.end(), comp: CFileBrowser::CompareTimeModifiedDescending);
657 }
658}
659
660void CFileBrowser::RefreshFilteredFileList()
661{
662 m_vpFilteredFileList.clear();
663 for(const CFilelistItem &Item : m_vCompleteFileList)
664 {
665 if(m_FilterInput.IsEmpty() || str_find_nocase(haystack: Item.m_aDisplayName, needle: m_FilterInput.GetString()))
666 {
667 m_vpFilteredFileList.push_back(x: &Item);
668 }
669 }
670 if(!m_ShowingRoot)
671 {
672 SortFilteredFileList();
673 }
674 if(!m_vpFilteredFileList.empty())
675 {
676 if(m_aSelectedFileDisplayName[0] != '\0')
677 {
678 for(size_t i = 0; i < m_vpFilteredFileList.size(); i++)
679 {
680 if(str_comp(a: m_vpFilteredFileList[i]->m_aDisplayName, b: m_aSelectedFileDisplayName) == 0)
681 {
682 m_SelectedFileIndex = i;
683 break;
684 }
685 }
686 }
687 m_SelectedFileIndex = std::clamp<int>(val: m_SelectedFileIndex, lo: 0, hi: m_vpFilteredFileList.size() - 1);
688 str_copy(dst&: m_aSelectedFileDisplayName, src: m_vpFilteredFileList[m_SelectedFileIndex]->m_aDisplayName);
689 }
690 else
691 {
692 m_SelectedFileIndex = -1;
693 m_aSelectedFileDisplayName[0] = '\0';
694 }
695}
696
697void CFileBrowser::FilelistPopulate(int StorageType, bool KeepSelection)
698{
699 m_vCompleteFileList.clear();
700 if(m_ShowingRoot)
701 {
702 {
703 CFilelistItem Item;
704 str_copy(dst&: Item.m_aFilename, src: m_pCurrentPath);
705 str_copy(dst&: Item.m_aDisplayName, src: "All combined");
706 Item.m_IsDir = true;
707 Item.m_IsLink = true;
708 Item.m_StorageType = IStorage::TYPE_ALL;
709 Item.m_TimeModified = 0;
710 m_vCompleteFileList.push_back(x: Item);
711 }
712
713 for(int CheckStorageType = IStorage::TYPE_SAVE; CheckStorageType < Storage()->NumPaths(); ++CheckStorageType)
714 {
715 if(Storage()->FolderExists(pFilename: m_pCurrentPath, Type: CheckStorageType))
716 {
717 CFilelistItem Item;
718 str_copy(dst&: Item.m_aFilename, src: m_pCurrentPath);
719 Storage()->GetCompletePath(Type: CheckStorageType, pDir: m_pCurrentPath, pBuffer: Item.m_aDisplayName, BufferSize: sizeof(Item.m_aDisplayName));
720 str_append(dst&: Item.m_aDisplayName, src: "/");
721 Item.m_IsDir = true;
722 Item.m_IsLink = true;
723 Item.m_StorageType = CheckStorageType;
724 Item.m_TimeModified = 0;
725 m_vCompleteFileList.push_back(x: Item);
726 }
727 }
728 }
729 else
730 {
731 // Add links for downloadedmaps and themes
732 if(!str_comp(a: m_pCurrentPath, b: "maps"))
733 {
734 if(!m_SaveAction && Storage()->FolderExists(pFilename: "downloadedmaps", Type: StorageType))
735 {
736 CFilelistItem Item;
737 str_copy(dst&: Item.m_aFilename, src: "downloadedmaps");
738 str_copy(dst&: Item.m_aDisplayName, src: "downloadedmaps/");
739 Item.m_IsDir = true;
740 Item.m_IsLink = true;
741 Item.m_StorageType = StorageType;
742 Item.m_TimeModified = 0;
743 m_vCompleteFileList.push_back(x: Item);
744 }
745
746 if(Storage()->FolderExists(pFilename: "themes", Type: StorageType))
747 {
748 CFilelistItem Item;
749 str_copy(dst&: Item.m_aFilename, src: "themes");
750 str_copy(dst&: Item.m_aDisplayName, src: "themes/");
751 Item.m_IsDir = true;
752 Item.m_IsLink = true;
753 Item.m_StorageType = StorageType;
754 Item.m_TimeModified = 0;
755 m_vCompleteFileList.push_back(x: Item);
756 }
757 }
758 Storage()->ListDirectoryInfo(Type: StorageType, pPath: m_pCurrentPath, pfnCallback: DirectoryListingCallback, pUser: this);
759 }
760 RefreshFilteredFileList();
761 if(!KeepSelection)
762 {
763 m_SelectedFileIndex = m_vpFilteredFileList.empty() ? -1 : 0;
764 str_copy(dst&: m_aSelectedFileDisplayName, src: m_SelectedFileIndex >= 0 ? m_vpFilteredFileList[m_SelectedFileIndex]->m_aDisplayName : "");
765 }
766 m_PreviewState = EPreviewState::UNLOADED;
767}
768
769int CFileBrowser::DirectoryListingCallback(const CFsFileInfo *pInfo, int IsDir, int StorageType, void *pUser)
770{
771 CFileBrowser *pFileBrowser = static_cast<CFileBrowser *>(pUser);
772 if(IsDir)
773 {
774 if(str_comp(a: pInfo->m_pName, b: ".") == 0)
775 {
776 return 0;
777 }
778 if(str_comp(a: pInfo->m_pName, b: "..") == 0 &&
779 !pFileBrowser->m_MultipleStorages &&
780 str_comp(a: pFileBrowser->m_aInitialFolder, b: pFileBrowser->m_pCurrentPath) == 0)
781 {
782 return 0;
783 }
784 }
785 else
786 {
787 if(!str_endswith(str: pInfo->m_pName, suffix: FILETYPE_EXTENSIONS[(int)pFileBrowser->m_FileType]))
788 {
789 return 0;
790 }
791 }
792
793 CFilelistItem Item;
794 str_copy(dst&: Item.m_aFilename, src: pInfo->m_pName);
795 if(IsDir)
796 {
797 str_format(buffer: Item.m_aDisplayName, buffer_size: sizeof(Item.m_aDisplayName), format: "%s/", pInfo->m_pName);
798 }
799 else
800 {
801 fs_split_file_extension(filename: pInfo->m_pName, name: Item.m_aDisplayName, name_size: sizeof(Item.m_aDisplayName));
802 }
803 Item.m_IsDir = IsDir != 0;
804 Item.m_IsLink = false;
805 Item.m_StorageType = StorageType;
806 Item.m_TimeModified = pInfo->m_TimeModified;
807 pFileBrowser->m_vCompleteFileList.push_back(x: Item);
808 return 0;
809}
810
811std::optional<bool> CFileBrowser::CompareCommon(const CFilelistItem *pLhs, const CFilelistItem *pRhs)
812{
813 if(str_comp(a: pRhs->m_aFilename, b: "..") == 0)
814 {
815 return false;
816 }
817 if(str_comp(a: pLhs->m_aFilename, b: "..") == 0)
818 {
819 return true;
820 }
821 if(pLhs->m_IsLink != pRhs->m_IsLink)
822 {
823 return pLhs->m_IsLink;
824 }
825 if(pLhs->m_IsDir != pRhs->m_IsDir)
826 {
827 return pLhs->m_IsDir;
828 }
829 return std::nullopt;
830}
831
832bool CFileBrowser::CompareFilenameAscending(const CFilelistItem *pLhs, const CFilelistItem *pRhs)
833{
834 return CompareCommon(pLhs, pRhs).value_or(u: str_comp_filenames(a: pLhs->m_aDisplayName, b: pRhs->m_aDisplayName) < 0);
835}
836
837bool CFileBrowser::CompareFilenameDescending(const CFilelistItem *pLhs, const CFilelistItem *pRhs)
838{
839 return CompareCommon(pLhs, pRhs).value_or(u: str_comp_filenames(a: pLhs->m_aDisplayName, b: pRhs->m_aDisplayName) > 0);
840}
841
842bool CFileBrowser::CompareTimeModifiedAscending(const CFilelistItem *pLhs, const CFilelistItem *pRhs)
843{
844 return CompareCommon(pLhs, pRhs).value_or(u: pLhs->m_TimeModified < pRhs->m_TimeModified);
845}
846
847bool CFileBrowser::CompareTimeModifiedDescending(const CFilelistItem *pLhs, const CFilelistItem *pRhs)
848{
849 return CompareCommon(pLhs, pRhs).value_or(u: pLhs->m_TimeModified > pRhs->m_TimeModified);
850}
851
852CUi::EPopupMenuFunctionResult CFileBrowser::CPopupNewFolder::Render(void *pContext, CUIRect View, bool Active)
853{
854 CPopupNewFolder *pNewFolderContext = static_cast<CPopupNewFolder *>(pContext);
855 CFileBrowser *pFileBrowser = pNewFolderContext->m_pFileBrowser;
856 CEditor *pEditor = pFileBrowser->Editor();
857
858 CUIRect Label, ButtonBar, FolderName, ButtonCancel, ButtonCreate;
859
860 View.Margin(Cut: 10.0f, pOtherRect: &View);
861 View.HSplitBottom(Cut: 20.0f, pTop: &View, pBottom: &ButtonBar);
862 ButtonBar.VSplitLeft(Cut: 110.0f, pLeft: &ButtonCancel, pRight: &ButtonBar);
863 ButtonBar.VSplitRight(Cut: 110.0f, pLeft: &ButtonBar, pRight: &ButtonCreate);
864
865 View.HSplitTop(Cut: 20.0f, pTop: &Label, pBottom: &View);
866 pFileBrowser->Ui()->DoLabel(pRect: &Label, pText: "Create new folder", Size: 20.0f, Align: TEXTALIGN_MC);
867 View.HSplitTop(Cut: 10.0f, pTop: nullptr, pBottom: &View);
868
869 View.HSplitTop(Cut: 20.0f, pTop: &Label, pBottom: &View);
870 pFileBrowser->Ui()->DoLabel(pRect: &Label, pText: "Name:", Size: 10.0f, Align: TEXTALIGN_ML);
871 Label.VSplitLeft(Cut: 50.0f, pLeft: nullptr, pRight: &FolderName);
872 FolderName.HMargin(Cut: 2.0f, pOtherRect: &FolderName);
873 pEditor->DoEditBox(pLineInput: &pNewFolderContext->m_NewFolderNameInput, pRect: &FolderName, FontSize: 12.0f);
874
875 if(pEditor->DoButton_Editor(pId: &pNewFolderContext->m_ButtonCancelId, pText: "Cancel", Checked: 0, pRect: &ButtonCancel, Flags: BUTTONFLAG_LEFT, pToolTip: nullptr))
876 {
877 return CUi::POPUP_CLOSE_CURRENT;
878 }
879
880 if(pEditor->DoButton_Editor(pId: &pNewFolderContext->m_ButtonCreateId, pText: "Create", Checked: 0, pRect: &ButtonCreate, Flags: BUTTONFLAG_LEFT, pToolTip: nullptr) ||
881 (Active && pFileBrowser->Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ENTER)))
882 {
883 char aFolderPath[IO_MAX_PATH_LENGTH];
884 str_format(buffer: aFolderPath, buffer_size: sizeof(aFolderPath), format: "%s/%s", pFileBrowser->m_pCurrentPath, pNewFolderContext->m_NewFolderNameInput.GetString());
885 if(!str_valid_filename(str: pNewFolderContext->m_NewFolderNameInput.GetString()))
886 {
887 pEditor->ShowFileDialogError(pFormat: "This name cannot be used for files and folders.");
888 }
889 else if(!pFileBrowser->Storage()->CreateFolder(pFoldername: aFolderPath, Type: pFileBrowser->m_StorageType))
890 {
891 pEditor->ShowFileDialogError(pFormat: "Failed to create the folder '%s'.", aFolderPath);
892 }
893 else
894 {
895 char aFolderDisplayName[IO_MAX_PATH_LENGTH];
896 str_format(buffer: aFolderDisplayName, buffer_size: sizeof(aFolderDisplayName), format: "%s/", pNewFolderContext->m_NewFolderNameInput.GetString());
897 pFileBrowser->FilelistPopulate(StorageType: pFileBrowser->m_StorageType, KeepSelection: false);
898 pFileBrowser->UpdateSelectedIndex(pDisplayName: aFolderDisplayName);
899 return CUi::POPUP_CLOSE_CURRENT;
900 }
901 }
902
903 return CUi::POPUP_KEEP_OPEN;
904}
905
906CUi::EPopupMenuFunctionResult CFileBrowser::CPopupConfirmDelete::Render(void *pContext, CUIRect View, bool Active)
907{
908 CPopupConfirmDelete *pConfirmDeleteContext = static_cast<CPopupConfirmDelete *>(pContext);
909 CFileBrowser *pFileBrowser = pConfirmDeleteContext->m_pFileBrowser;
910 CEditor *pEditor = pFileBrowser->Editor();
911
912 CUIRect Label, ButtonBar, ButtonCancel, ButtonDelete;
913 View.Margin(Cut: 10.0f, pOtherRect: &View);
914 View.HSplitBottom(Cut: 20.0f, pTop: &View, pBottom: &ButtonBar);
915 View.HSplitTop(Cut: 20.0f, pTop: &Label, pBottom: &View);
916 ButtonBar.VSplitLeft(Cut: 110.0f, pLeft: &ButtonCancel, pRight: &ButtonBar);
917 ButtonBar.VSplitRight(Cut: 110.0f, pLeft: &ButtonBar, pRight: &ButtonDelete);
918
919 pFileBrowser->Ui()->DoLabel(pRect: &Label, pText: "Confirm delete", Size: 20.0f, Align: TEXTALIGN_MC);
920
921 char aMessage[IO_MAX_PATH_LENGTH + 128];
922 str_format(buffer: aMessage, buffer_size: sizeof(aMessage), format: "Are you sure that you want to delete the %s '%s'?",
923 pConfirmDeleteContext->m_IsDirectory ? "folder" : "file", pConfirmDeleteContext->m_aDeletePath);
924 pFileBrowser->Ui()->DoLabel(pRect: &View, pText: aMessage, Size: 10.0f, Align: TEXTALIGN_ML, LabelProps: {.m_MaxWidth = View.w});
925
926 if(pEditor->DoButton_Editor(pId: &pConfirmDeleteContext->m_ButtonCancelId, pText: "Cancel", Checked: 0, pRect: &ButtonCancel, Flags: BUTTONFLAG_LEFT, pToolTip: nullptr))
927 {
928 return CUi::POPUP_CLOSE_CURRENT;
929 }
930
931 if(pEditor->DoButton_Editor(pId: &pConfirmDeleteContext->m_ButtonDeleteId, pText: "Delete", Checked: EditorButtonChecked::DANGEROUS_ACTION, pRect: &ButtonDelete, Flags: BUTTONFLAG_LEFT, pToolTip: nullptr) ||
932 (Active && pFileBrowser->Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ENTER)))
933 {
934 if(pConfirmDeleteContext->m_IsDirectory)
935 {
936 if(pFileBrowser->Storage()->RemoveFolder(pFilename: pConfirmDeleteContext->m_aDeletePath, Type: IStorage::TYPE_SAVE))
937 {
938 pFileBrowser->FilelistPopulate(StorageType: pFileBrowser->m_StorageType, KeepSelection: true);
939 }
940 else
941 {
942 pEditor->ShowFileDialogError(pFormat: "Failed to delete folder '%s'. Make sure it's empty first. Check the local console for details.", pConfirmDeleteContext->m_aDeletePath);
943 }
944 }
945 else
946 {
947 if(pFileBrowser->Storage()->RemoveFile(pFilename: pConfirmDeleteContext->m_aDeletePath, Type: IStorage::TYPE_SAVE))
948 {
949 pFileBrowser->FilelistPopulate(StorageType: pFileBrowser->m_StorageType, KeepSelection: true);
950 }
951 else
952 {
953 pEditor->ShowFileDialogError(pFormat: "Failed to delete file '%s'. Check the local console for details.", pConfirmDeleteContext->m_aDeletePath);
954 }
955 }
956 pFileBrowser->UpdateFilenameInput();
957 return CUi::POPUP_CLOSE_CURRENT;
958 }
959
960 return CUi::POPUP_KEEP_OPEN;
961}
962
963CUi::EPopupMenuFunctionResult CFileBrowser::CPopupConfirmOverwrite::Render(void *pContext, CUIRect View, bool Active)
964{
965 CPopupConfirmOverwrite *pConfirmOverwriteContext = static_cast<CPopupConfirmOverwrite *>(pContext);
966 CFileBrowser *pFileBrowser = pConfirmOverwriteContext->m_pFileBrowser;
967 CEditor *pEditor = pFileBrowser->Editor();
968
969 CUIRect Label, ButtonBar, ButtonCancel, ButtonOverride;
970 View.Margin(Cut: 10.0f, pOtherRect: &View);
971 View.HSplitBottom(Cut: 20.0f, pTop: &View, pBottom: &ButtonBar);
972 View.HSplitTop(Cut: 20.0f, pTop: &Label, pBottom: &View);
973 ButtonBar.VSplitLeft(Cut: 110.0f, pLeft: &ButtonCancel, pRight: &ButtonBar);
974 ButtonBar.VSplitRight(Cut: 110.0f, pLeft: &ButtonBar, pRight: &ButtonOverride);
975
976 pFileBrowser->Ui()->DoLabel(pRect: &Label, pText: "Confirm overwrite", Size: 20.0f, Align: TEXTALIGN_MC);
977
978 char aMessage[IO_MAX_PATH_LENGTH + 128];
979 str_format(buffer: aMessage, buffer_size: sizeof(aMessage), format: "The file '%s' already exists.\n\nAre you sure that you want to overwrite it?",
980 pConfirmOverwriteContext->m_aOverwritePath);
981 pFileBrowser->Ui()->DoLabel(pRect: &View, pText: aMessage, Size: 10.0f, Align: TEXTALIGN_ML, LabelProps: {.m_MaxWidth = View.w});
982
983 if(pEditor->DoButton_Editor(pId: &pConfirmOverwriteContext->m_ButtonCancelId, pText: "Cancel", Checked: 0, pRect: &ButtonCancel, Flags: BUTTONFLAG_LEFT, pToolTip: nullptr))
984 {
985 return CUi::POPUP_CLOSE_CURRENT;
986 }
987
988 if(pEditor->DoButton_Editor(pId: &pConfirmOverwriteContext->m_ButtonOverwriteId, pText: "Overwrite", Checked: EditorButtonChecked::DANGEROUS_ACTION, pRect: &ButtonOverride, Flags: BUTTONFLAG_LEFT, pToolTip: nullptr) ||
989 (Active && pFileBrowser->Ui()->ConsumeHotkey(Hotkey: CUi::HOTKEY_ENTER)))
990 {
991 pFileBrowser->m_pfnOpenCallback(pConfirmOverwriteContext->m_aOverwritePath, IStorage::TYPE_SAVE, pFileBrowser->m_pOpenCallbackUser);
992 return CUi::POPUP_CLOSE_CURRENT;
993 }
994
995 return CUi::POPUP_KEEP_OPEN;
996}
997