mirror of https://github.com/mgba-emu/mgba.git
GUI: Improve file browsing with proper filters and current directory listing
This commit is contained in:
parent
278b17e56f
commit
d67844e95f
|
@ -108,7 +108,8 @@ int main() {
|
|||
|
||||
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();
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
* 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 @@ int main() {
|
|||
|
||||
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)) {
|
||||
|
|
|
@ -185,6 +185,7 @@ struct VFile* _vdsceOpenFile(struct VDir* vd, const char* path, int mode) {
|
|||
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);
|
||||
|
|
|
@ -167,6 +167,7 @@ int main() {
|
|||
|
||||
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 @@ int main() {
|
|||
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);
|
||||
|
|
|
@ -37,7 +37,7 @@ static void _upDirectory(char* currentPath) {
|
|||
// 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 @@ static bool _refreshDirectory(const char* currentPath, struct FileList* currentF
|
|||
}
|
||||
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 @@ bool selectFile(const struct GUIParams* params, const char* basePath, char* outP
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
#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
|
||||
|
|
Loading…
Reference in New Issue