GUI: Improve file browsing with proper filters and current directory listing
Jeffrey Pfau jeffrey@endrift.com
Tue, 25 Aug 2015 22:42:19 -0700
6 files changed,
37 insertions(+),
17 deletions(-)
M
src/platform/3ds/main.c
→
src/platform/3ds/main.c
@@ -108,7 +108,8 @@ };
while (aptMainLoop()) { char path[256]; - if (!selectFile(¶ms, "/", path, sizeof(path), "gba")) { + char currentPath[256] = ""; + if (!selectFile(¶ms, "/", path, currentPath, sizeof(path), GBAIsROM)) { break; } _drawStart();
M
src/platform/psp2/main.c
→
src/platform/psp2/main.c
@@ -5,6 +5,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "psp2-context.h" +#include "gba/gba.h" #include "util/gui.h" #include "util/gui/font.h" #include "util/gui/file-select.h"@@ -69,7 +70,8 @@ };
while (true) { char path[256]; - if (!selectFile(¶ms, "cache0:", path, sizeof(path), "gba")) { + char currentPath[256] = ""; + if (!selectFile(¶ms, "cache0:", path, currentPath, sizeof(path), GBAIsROM)) { break; } if (!GBAPSP2LoadROM(path)) {
M
src/platform/psp2/sce-vfs.c
→
src/platform/psp2/sce-vfs.c
@@ -185,6 +185,7 @@ }
const char* dir = vdsce->path; char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + strlen(PATH_SEP) + 1)); sprintf(combined, "%s%s%s", dir, PATH_SEP, path); + printf("Opening %s\n", combined); struct VFile* file = VFileOpen(combined, mode); free(combined);
M
src/platform/wii/main.c
→
src/platform/wii/main.c
@@ -167,6 +167,7 @@ #endif
while (true) { char path[256]; + char currentPath[256] = ""; guOrtho(proj, -20, 240, 0, 352, 0, 300); GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);@@ -174,7 +175,7 @@ struct GUIParams params = {
352, 230, font, _drawStart, _drawEnd, _pollInput }; - if (!selectFile(¶ms, "/", path, sizeof(path), "gba") || !GBAWiiLoadGame(path)) { + if (!selectFile(¶ms, "/", path, currentPath, sizeof(path), GBAIsROM) || !GBAWiiLoadGame(path)) { break; } GBAContextStart(&context);
M
src/util/gui/file-select.c
→
src/util/gui/file-select.c
@@ -37,7 +37,7 @@ }
// TODO: What if there was a trailing slash? } -static bool _refreshDirectory(const char* currentPath, struct FileList* currentFiles) { +static bool _refreshDirectory(const char* currentPath, struct FileList* currentFiles, bool (*filter)(struct VFile*)) { _cleanFiles(currentFiles); struct VDir* dir = VDirOpen(currentPath);@@ -46,24 +46,37 @@ return false;
} struct VDirEntry* de; while ((de = dir->listNext(dir))) { - if (de->name(de)[0] == '.') { + const char* name = de->name(de); + if (name[0] == '.') { continue; } - *FileListAppend(currentFiles) = strdup(de->name(de)); + if (de->type(de) == VFS_FILE) { + struct VFile* vf = dir->openFile(dir, name, O_RDONLY); + if (!vf) { + continue; + } + if (!filter || filter(vf)) { + *FileListAppend(currentFiles) = strdup(name); + } + vf->close(vf); + } else { + *FileListAppend(currentFiles) = strdup(name); + } } dir->close(dir); return true; } -bool selectFile(const struct GUIParams* params, const char* basePath, char* outPath, size_t outLen, const char* suffix) { - char currentPath[256]; - strncpy(currentPath, basePath, sizeof(currentPath)); +bool selectFile(const struct GUIParams* params, const char* basePath, char* outPath, char* currentPath, size_t outLen, bool (*filter)(struct VFile*)) { + if (!currentPath[0]) { + strncpy(currentPath, basePath, outLen); + } size_t fileIndex = 0; size_t start = 0; struct FileList currentFiles; FileListInit(¤tFiles, 0); - _refreshDirectory(currentPath, ¤tFiles); + _refreshDirectory(currentPath, ¤tFiles, filter); int inputHistory[GUI_INPUT_MAX] = { 0 };@@ -98,27 +111,27 @@ _cleanFiles(¤tFiles);
FileListDeinit(¤tFiles); return false; } - if (newInput & (1 << GUI_INPUT_SELECT)) { + if (newInput & (1 << GUI_INPUT_SELECT) && FileListSize(¤tFiles)) { size_t len = strlen(currentPath); const char* sep = PATH_SEP; if (currentPath[len - 1] == *sep) { sep = ""; } - snprintf(currentPath, sizeof(currentPath), "%s%s%s", currentPath, sep, *FileListGetPointer(¤tFiles, fileIndex)); - if (!_refreshDirectory(currentPath, ¤tFiles)) { - strncpy(outPath, currentPath, outLen); + snprintf(outPath, outLen, "%s%s%s", currentPath, sep, *FileListGetPointer(¤tFiles, fileIndex)); + if (!_refreshDirectory(outPath, ¤tFiles, filter)) { return true; } + strncpy(currentPath, outPath, outLen); fileIndex = 0; } if (newInput & (1 << GUI_INPUT_BACK)) { - if (strncmp(currentPath, basePath, sizeof(currentPath)) == 0) { + if (strncmp(currentPath, basePath, outLen) == 0) { _cleanFiles(¤tFiles); FileListDeinit(¤tFiles); return false; } _upDirectory(currentPath); - _refreshDirectory(currentPath, ¤tFiles); + _refreshDirectory(currentPath, ¤tFiles, filter); fileIndex = 0; }
M
src/util/gui/file-select.h
→
src/util/gui/file-select.h
@@ -8,6 +8,8 @@ #define GUI_FILE_CHOOSER_H
#include "util/gui.h" -bool selectFile(const struct GUIParams*, const char* basePath, char* outPath, size_t outLen, const char* suffix); +struct VFile; + +bool selectFile(const struct GUIParams*, const char* basePath, char* outPath, char* currentPath, size_t outLen, bool (*filter)(struct VFile*)); #endif