GUI: Improve file browsing with proper filters and current directory listing

This commit is contained in:
Jeffrey Pfau 2015-08-25 22:42:19 -07:00
parent 278b17e56f
commit d67844e95f
6 changed files with 37 additions and 17 deletions

View File

@ -108,7 +108,8 @@ int main() {
while (aptMainLoop()) { while (aptMainLoop()) {
char path[256]; char path[256];
if (!selectFile(&params, "/", path, sizeof(path), "gba")) { char currentPath[256] = "";
if (!selectFile(&params, "/", path, currentPath, sizeof(path), GBAIsROM)) {
break; break;
} }
_drawStart(); _drawStart();

View File

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "psp2-context.h" #include "psp2-context.h"
#include "gba/gba.h"
#include "util/gui.h" #include "util/gui.h"
#include "util/gui/font.h" #include "util/gui/font.h"
#include "util/gui/file-select.h" #include "util/gui/file-select.h"
@ -69,7 +70,8 @@ int main() {
while (true) { while (true) {
char path[256]; char path[256];
if (!selectFile(&params, "cache0:", path, sizeof(path), "gba")) { char currentPath[256] = "";
if (!selectFile(&params, "cache0:", path, currentPath, sizeof(path), GBAIsROM)) {
break; break;
} }
if (!GBAPSP2LoadROM(path)) { if (!GBAPSP2LoadROM(path)) {

View File

@ -185,6 +185,7 @@ struct VFile* _vdsceOpenFile(struct VDir* vd, const char* path, int mode) {
const char* dir = vdsce->path; const char* dir = vdsce->path;
char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + strlen(PATH_SEP) + 1)); char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + strlen(PATH_SEP) + 1));
sprintf(combined, "%s%s%s", dir, PATH_SEP, path); sprintf(combined, "%s%s%s", dir, PATH_SEP, path);
printf("Opening %s\n", combined);
struct VFile* file = VFileOpen(combined, mode); struct VFile* file = VFileOpen(combined, mode);
free(combined); free(combined);

View File

@ -167,6 +167,7 @@ int main() {
while (true) { while (true) {
char path[256]; char path[256];
char currentPath[256] = "";
guOrtho(proj, -20, 240, 0, 352, 0, 300); guOrtho(proj, -20, 240, 0, 352, 0, 300);
GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC); GX_LoadProjectionMtx(proj, GX_ORTHOGRAPHIC);
@ -174,7 +175,7 @@ int main() {
352, 230, 352, 230,
font, _drawStart, _drawEnd, _pollInput font, _drawStart, _drawEnd, _pollInput
}; };
if (!selectFile(&params, "/", path, sizeof(path), "gba") || !GBAWiiLoadGame(path)) { if (!selectFile(&params, "/", path, currentPath, sizeof(path), GBAIsROM) || !GBAWiiLoadGame(path)) {
break; break;
} }
GBAContextStart(&context); GBAContextStart(&context);

View File

@ -37,7 +37,7 @@ static void _upDirectory(char* currentPath) {
// TODO: What if there was a trailing slash? // 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); _cleanFiles(currentFiles);
struct VDir* dir = VDirOpen(currentPath); struct VDir* dir = VDirOpen(currentPath);
@ -46,24 +46,37 @@ static bool _refreshDirectory(const char* currentPath, struct FileList* currentF
} }
struct VDirEntry* de; struct VDirEntry* de;
while ((de = dir->listNext(dir))) { while ((de = dir->listNext(dir))) {
if (de->name(de)[0] == '.') { const char* name = de->name(de);
if (name[0] == '.') {
continue; 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); dir->close(dir);
return true; return true;
} }
bool selectFile(const struct GUIParams* params, const char* basePath, char* outPath, size_t outLen, const char* suffix) { bool selectFile(const struct GUIParams* params, const char* basePath, char* outPath, char* currentPath, size_t outLen, bool (*filter)(struct VFile*)) {
char currentPath[256]; if (!currentPath[0]) {
strncpy(currentPath, basePath, sizeof(currentPath)); strncpy(currentPath, basePath, outLen);
}
size_t fileIndex = 0; size_t fileIndex = 0;
size_t start = 0; size_t start = 0;
struct FileList currentFiles; struct FileList currentFiles;
FileListInit(&currentFiles, 0); FileListInit(&currentFiles, 0);
_refreshDirectory(currentPath, &currentFiles); _refreshDirectory(currentPath, &currentFiles, filter);
int inputHistory[GUI_INPUT_MAX] = { 0 }; int inputHistory[GUI_INPUT_MAX] = { 0 };
@ -98,27 +111,27 @@ bool selectFile(const struct GUIParams* params, const char* basePath, char* outP
FileListDeinit(&currentFiles); FileListDeinit(&currentFiles);
return false; return false;
} }
if (newInput & (1 << GUI_INPUT_SELECT)) { if (newInput & (1 << GUI_INPUT_SELECT) && FileListSize(&currentFiles)) {
size_t len = strlen(currentPath); size_t len = strlen(currentPath);
const char* sep = PATH_SEP; const char* sep = PATH_SEP;
if (currentPath[len - 1] == *sep) { if (currentPath[len - 1] == *sep) {
sep = ""; sep = "";
} }
snprintf(currentPath, sizeof(currentPath), "%s%s%s", currentPath, sep, *FileListGetPointer(&currentFiles, fileIndex)); snprintf(outPath, outLen, "%s%s%s", currentPath, sep, *FileListGetPointer(&currentFiles, fileIndex));
if (!_refreshDirectory(currentPath, &currentFiles)) { if (!_refreshDirectory(outPath, &currentFiles, filter)) {
strncpy(outPath, currentPath, outLen);
return true; return true;
} }
strncpy(currentPath, outPath, outLen);
fileIndex = 0; fileIndex = 0;
} }
if (newInput & (1 << GUI_INPUT_BACK)) { if (newInput & (1 << GUI_INPUT_BACK)) {
if (strncmp(currentPath, basePath, sizeof(currentPath)) == 0) { if (strncmp(currentPath, basePath, outLen) == 0) {
_cleanFiles(&currentFiles); _cleanFiles(&currentFiles);
FileListDeinit(&currentFiles); FileListDeinit(&currentFiles);
return false; return false;
} }
_upDirectory(currentPath); _upDirectory(currentPath);
_refreshDirectory(currentPath, &currentFiles); _refreshDirectory(currentPath, &currentFiles, filter);
fileIndex = 0; fileIndex = 0;
} }

View File

@ -8,6 +8,8 @@
#include "util/gui.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 #endif